static Rgb hisToRgb(double h, double i, double s) { double m1, m2, i1; m1 = s * Math.sin(h * Math.PI / 180); m2 = s * Math.cos(h * Math.PI / 180); i1 = i / Math.sqrt(3); Rgb rgb = new Rgb(); rgb.r = m1 * 2 / Math.sqrt(6) + i1 / Math.sqrt(3); rgb.g = -m1 / Math.sqrt(6) + m2 / Math.sqrt(2) + i1 / Math.sqrt(3); rgb.b = -m1 / Math.sqrt(6) - m2 / Math.sqrt(2) + i1 / Math.sqrt(3); rgb.r = rgb.r / 2 + 0.5; rgb.g = rgb.g / 2 + 0.5; rgb.b = rgb.b / 2 + 0.5; if (rgb.r > 1) rgb.r = 1; if (rgb.r < 0) rgb.r = 0; if (rgb.g > 1) rgb.g = 1; if (rgb.g < 0) rgb.g = 0; if (rgb.b > 1) rgb.b = 1; if (rgb.b < 0) rgb.b = 0; return rgb; }
public static Color rgbColor(int idx) { double gray_i0 = 0.32; double gray_i1 = 0.95; double h1, i1, s1; double r, g, b; int i, j, k; Rgb rgb; Color color = null; if (idx == 300) // TODO, should be background color idx = 31; if (idx < 4) color = new Color( (int) (colorValues[3 * idx] * 255), (int) (colorValues[3 * idx + 1] * 255), (int) (colorValues[3 * idx + 2] * 255)); else if (idx < 20) { h1 = 360. * (idx - 4) / 16; s1 = 1.5; i1 = 1; rgb = hisToRgb(h1, i1, s1); color = new Color((int) (rgb.r * 255), (int) (rgb.g * 255), (int) (rgb.b * 255)); } else if (idx < 60) { int val; if (idx < 30 || (50 <= idx && idx < 60)) gray_i0 = 0.25F; val = (int) ((gray_i0 + (gray_i1 - gray_i0) * Math.pow((double) (9 - idx % 10) / 9, 0.9)) * 255); color = new Color(val, val, val); } else if (idx < 300) { double i_min, i_max, s, a, h, hk; i = (idx - 60) / 30; j = (idx - 60 - i * 30) / 10; k = 9 - (idx - 60 - i * 30 - j * 10); h = ctab[i * 14]; hk = ctab[i * 14 + 1]; s = ctab[i * 14 + 2 + j * 4]; i_min = ctab[i * 14 + 2 + j * 4 + 1]; i_max = ctab[i * 14 + 2 + j * 4 + 2]; a = ctab[i * 14 + 2 + j * 4 + 3]; s1 = s; i1 = i_min + (i_max - i_min) * Math.pow((double) k / 9, a); h1 = h + hk * k / 9; if ((i == 0 && j == 2) || (i == 2 && j == 2 && k > 5)) { // Formula doesn't work for yellow... rgb = new Rgb(); rgb.r = rgbTab[(i * 10 + k) * 3]; rgb.g = rgbTab[(i * 10 + k) * 3 + 1]; rgb.b = rgbTab[(i * 10 + k) * 3 + 2]; } else rgb = hisToRgb(h1, i1, s1); color = new Color((int) (rgb.r * 255), (int) (rgb.g * 255), (int) (rgb.b * 255)); } return color; }