#include <math.h>
#include "svg.h"

int nextChar(void) {
    static const int charRange[][2] = {
        { 0x1f638, 0x1f640 },
    };
    static int current = 0;
    static int nRanges = sizeof(charRange)/(sizeof(int)*2);
    static int c = -1;
    int nextC;

    if (c==-1) {
        c = charRange[current][0];
    }
    nextC = c++;
    if (c > charRange[current][1]) {
        current++;
        if (current >= nRanges) {
            current = 0;
        }
        c = charRange[current][0];
    }

    return nextC;
}

const int M = 6;

int calcColor(double x, double y) { /* x, y は -1 〜 1 の範囲と仮定する */
    double d = 2 * PI * sqrt(x*x+y*y);
    double x1 = x * cos(d) - y * sin(d);
    double y1 = x * sin(d) + y * cos(d);
    double b = x1/1.41421356 * 0.5 + 0.5;
    double g = 1-b;
    double r = y1/1.41421356 * 0.5 + 0.5;
    return rgb1(r, g, b);
}

int main(void) {
    int i, j;
    int num = 16;         /* 文字数 -- 2 の倍数 */
    double x0 = centerX(), y0 = centerY();
    double fontSize = 9;  /* フォントサイズ */

    start();
    rulers();
    textFont("Arial", fontSize);
    for (i=0; i<num; i++) {   /* 少し中止から離れたところから始める */
        for (j=0; j<num; j++) {
            double x = i-(num-1)/2.0, y = j-(num-1)/2.0;
            fill(calcColor(x/(num-1)*2, y/(num-1)*2));
            text("&#x%x;", x0+x*fontSize, y0-y*fontSize+fontSize/2, nextChar());
        }
    }
    finish();
    return 0;
}
