1 14 package org.compiere.plaf; 15 16 import java.awt.Color ; 17 18 import javax.swing.plaf.ColorUIResource ; 19 20 32 public class ColorBlind 33 { 34 35 public static final String [] COLORBLIND_TYPE = 36 {"", "Protanopia", "Deuteranopia", "Tritanopia"}; 37 38 public static final int NORMAL = 0; 39 public static final int PROTANOPIA = 1; 40 public static final int DEUTERANOPIA = 2; 41 public static final int TRITANOPIA = 3; 42 43 44 45 private static final double[][] confusionPoints = 46 { {0.735, 0.265}, 47 {1.140, -0.140}, 48 {0.171, -0.003} 49 }; 50 private static final double[][][] colorAxes = 51 { {{0.115807, 0.073581}, {0.471899, 0.527051}}, 52 {{0.102776, 0.102864}, {0.505845, 0.493211}}, 53 {{0.045391, 0.294976}, {0.665764, 0.334011}} 54 }; 55 private static final double[][] RGBtoXYZMatrix = 56 { {0.430574, 0.341550, 0.178325}, 57 {0.222015, 0.706655, 0.071330}, 58 {0.020183, 0.129553, 0.939180} 59 }; 60 private static final double[][] XYZtoRGBMatrix = 61 { { 3.063218, -1.393325, -0.475802}, 62 {-0.969243, 1.875966, 0.041555}, 63 { 0.067871, -0.228834, 1.069251} 64 }; 65 private static final double[] whitePoint = 66 { 0.312713, 0.329016, 0.358271 67 }; 68 private static final double gamma = 2.2; 69 70 71 72 73 private static int s_colorType = NORMAL; 74 75 79 public static void setColorType (int colorType) 80 { 81 if (colorType > 0 && colorType < 4) 82 s_colorType = colorType; 83 else 84 s_colorType = 0; 85 System.out.println("ColorBlind = " + COLORBLIND_TYPE[colorType]); 86 } 88 92 public static int getColorType () 93 { 94 return s_colorType; 95 } 97 98 99 104 public static ColorUIResource getDichromatColorUIResource (ColorUIResource color) 105 { 106 if (s_colorType == NORMAL) 107 return color; 108 return new ColorUIResource (getDichromatColorUIResource (color, s_colorType)); 109 } 111 117 public static Color getDichromatColorUIResource (ColorUIResource color, int colorType) 118 { 119 if (s_colorType == NORMAL) 120 return color; 121 return new ColorUIResource (getDichromatColor (color, s_colorType)); 122 } 124 129 public static Color getDichromatColor (Color color) 130 { 131 if (s_colorType == NORMAL) 132 return color; 133 return getDichromatColor (color, s_colorType); 134 } 136 142 public static Color getDichromatColor (Color color, int colorType) 143 { 144 int type = 0; 146 if (colorType > 0 && colorType < 4) 147 type = colorType; 148 if (type == 0 || color == null) 150 return color; 151 type--; 153 if (color.equals(Color.black) || color.equals(Color.white)) 155 return color; 156 157 double red = color.getRed(); 158 double green = color.getGreen(); 159 double blue = color.getBlue(); 160 161 163 double X = RGBtoXYZMatrix[0][0] * Math.pow(red / 255.0, gamma) + 164 RGBtoXYZMatrix[0][1] * Math.pow(green / 255.0, gamma) + 165 RGBtoXYZMatrix[0][2] * Math.pow(blue / 255.0, gamma); 166 double Y = RGBtoXYZMatrix[1][0] * Math.pow(red / 255.0, gamma) + 167 RGBtoXYZMatrix[1][1] * Math.pow(green / 255.0, gamma) + 168 RGBtoXYZMatrix[1][2] * Math.pow(blue / 255.0, gamma); 169 double Z = RGBtoXYZMatrix[2][0] * Math.pow(red / 255.0, gamma) + 170 RGBtoXYZMatrix[2][1] * Math.pow(green / 255.0, gamma) + 171 RGBtoXYZMatrix[2][2] * Math.pow(blue / 255.0, gamma); 172 173 175 double x = 0.0; 176 double y = 0.0; 177 178 if ((X + Y + Z) != 0.0) 179 { 180 x = X / (X + Y + Z); 181 y = Y / (X + Y + Z); 182 } 183 184 186 double Yn = Y; 187 double Xn = (whitePoint[0] * Yn) / whitePoint[1]; 188 double Zn = (whitePoint[2] * Yn) / whitePoint[1]; 189 190 double xc = confusionPoints[type][0]; 191 double yc = confusionPoints[type][1]; 192 193 double x1 = colorAxes[type][0][0]; 194 double y1 = colorAxes[type][0][1]; 195 double x2 = colorAxes[type][1][0]; 196 double y2 = colorAxes[type][1][1]; 197 198 double ap = (y2 - y1) / (x2 - x1); 199 double bp = y1 - (x1 * ap); 200 201 203 double a; 204 if (x < xc) 205 a = (yc - y) / (xc - x); 206 else 207 a = (y - yc) / (x - xc); 208 209 double b = y - (x * a); 210 211 213 x = (bp - b) / (a - ap); 214 y = a * x + b; 215 216 218 X = 0.0; 219 Z = 0.0; 220 if (y != 0) 221 { 222 X = x * (Y / y); 223 Z = (1 - x - y) * (Y / y); 224 } 225 226 228 red = XYZtoRGBMatrix[0][0] * X + 229 XYZtoRGBMatrix[0][1] * Y + 230 XYZtoRGBMatrix[0][2] * Z; 231 green = XYZtoRGBMatrix[1][0] * X + 232 XYZtoRGBMatrix[1][1] * Y + 233 XYZtoRGBMatrix[1][2] * Z; 234 blue = XYZtoRGBMatrix[2][0] * X + 235 XYZtoRGBMatrix[2][1] * Y + 236 XYZtoRGBMatrix[2][2] * Z; 237 238 240 double reddiff = XYZtoRGBMatrix[0][0] * (Xn - X) + 241 XYZtoRGBMatrix[0][1] * (Yn - Y) + 242 XYZtoRGBMatrix[0][2] * (Zn - Z); 243 double greendiff = XYZtoRGBMatrix[1][0] * (Xn - X) + 244 XYZtoRGBMatrix[1][1] * (Yn - Y) + 245 XYZtoRGBMatrix[1][2] * (Zn - Z); 246 double bluediff = XYZtoRGBMatrix[2][0] * (Xn - X) + 247 XYZtoRGBMatrix[2][1] * (Yn - Y) + 248 XYZtoRGBMatrix[2][2] * (Zn - Z); 249 250 double cr = ((red < 0.0 ? 0.0 : 1.0) - red) / reddiff; 251 double cg = ((green < 0.0 ? 0.0 : 1.0) - green) / greendiff; 252 double cb = ((blue < 0.0 ? 0.0 : 1.0) - blue) / bluediff; 253 254 256 double c1 = (cr < 0 || cr > 1) ? 0 : cr; 257 double c2 = (cg < 0 || cg > 1) ? 0 : cg; 258 double c3 = (cb < 0 || cb > 1) ? 0 : cb; 259 double c = Math.max(c1, Math.max(c2, c3)); 260 261 263 red = red + c * reddiff; 264 green = green + c * greendiff; 265 blue = blue + c * bluediff; 266 267 269 red = Math.pow(red, 1.0 / gamma); 270 green = Math.pow(green, 1.0 / gamma); 271 blue = Math.pow(blue, 1.0 / gamma); 272 273 276 Color retValue = new Color ((float)red, (float)green, (float)blue); 277 280 return retValue; 281 } 283 } | Popular Tags |