import static util.SVGUtil.*;

public class CharImage {
    private static int current = 0;
    private final static int charRange[][] = {
        { 0x2654, 0x266f },
        { 0x2600, 0x2606 }, 
    };
    private final static int nRanges = charRange.length;
    private static int c = -1;
	
	private final static int nextChar() {
	    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;
	}

	private static int calcColor1(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);
	}
	
	private static int calcColor2(double x, double y) { /* x, y は -1 〜 1 の範囲と仮定する */
	    double d = 4 * PI * sqrt(x*x+y*y);
	    double x1 = x * cos(d) - y * sin(d);
	    double y1 = x * sin(d) + y * cos(d);
	    double r = x1/1.41421356 * 0.5 + 0.5;
	    double b = 1-r;
	    double g = y1/1.41421356 * 0.5 + 0.5;
	    return rgb1(r, g, b);
	}

	public static void main(String[] args) {
	    int i, j;
	    int num = 16;         /* 文字数 -- 2 の倍数 */

	    double fontSize = 6;  /* フォントサイズ */

	    start();
	    rulers();
	    pushMatrix();
	    textFont("Segoe UI Symbol", fontSize);
	    translate(centerX()-60, centerY());
	    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(calcColor1(x/(num-1)*2, y/(num-1)*2));
	            text("&#x%x;", x*fontSize, y*fontSize+fontSize/2, nextChar());
	        }
	    }
	    popMatrix();
	    
	    pushMatrix();
	    translate(centerX()+60, centerY());
	    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(calcColor2(x/(num-1)*2, y/(num-1)*2));
	            text("&#x%x;", x*fontSize, y*fontSize+fontSize/2, nextChar());
	        }
	    }
	    popMatrix();
	    
	    finish();
	}
}
