KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > plaf > ColorBlind


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.plaf;
15
16 import java.awt.Color JavaDoc;
17
18 import javax.swing.plaf.ColorUIResource JavaDoc;
19
20 /**
21  * Color Blind Utilities.
22  * These utilities help to show how color blind/challenged see colors.
23  *
24  * Created by Thomas Wolfmaier.
25  * Copyright (C) 1999-2002 HCIRN.
26  * All rights reserved.
27  * @see <a HREF="http://www.hcirn.com">HCIRN</a>
28  *
29  * @author Thomas Wolfmaier
30  * @version $Id: ColorBlind.java,v 1.5 2003/09/27 11:08:52 jjanke Exp $
31  */

32 public class ColorBlind
33 {
34     /** Color Blindness Type - 0=none */
35     public static final String JavaDoc[] 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     /** Color Blind Type */
73     private static int s_colorType = NORMAL;
74
75     /**
76      * Set Color Type for Color Blind view
77      * @param colorType (0 = none)
78      */

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     } // setColorType
87

88     /**
89      * Get Color Type for Color Blind view
90      * @return colorType (0 = none)
91      */

92     public static int getColorType ()
93     {
94         return s_colorType;
95     } // getColorType
96

97     /*************************************************************************/
98
99     /**
100      * Convert "normal" color to Dichromat Color based on set color type
101      * @param color Java Color object containing values for RGB
102      * @return Dichromat Color
103      */

104     public static ColorUIResource JavaDoc getDichromatColorUIResource (ColorUIResource JavaDoc color)
105     {
106         if (s_colorType == NORMAL)
107             return color;
108         return new ColorUIResource JavaDoc(getDichromatColorUIResource (color, s_colorType));
109     } // convertToDichromatColorUIResource
110

111     /**
112      * Convert "normal" color to Dichromat Color
113      * @param color Java Color object containing values for RGB
114      * @param colorType PROTANOPIA = 1, DEUTERANOPIA = 2 or TRITANOPIA = 3 as declared above
115      * @return Dichromat Color
116      */

117     public static Color JavaDoc getDichromatColorUIResource (ColorUIResource JavaDoc color, int colorType)
118     {
119         if (s_colorType == NORMAL)
120             return color;
121         return new ColorUIResource JavaDoc(getDichromatColor (color, s_colorType));
122     } // convertToDichromatColorUIResource
123

124     /**
125      * Convert "normal" color to Dichromat Color based on set color type
126      * @param color Java Color object containing values for RGB
127      * @return Dichromat Color
128      */

129     public static Color JavaDoc getDichromatColor (Color JavaDoc color)
130     {
131         if (s_colorType == NORMAL)
132             return color;
133         return getDichromatColor (color, s_colorType);
134     } // convertToDichromatColor
135

136     /**
137      * Convert "normal" color to Dichromat Color
138      * @param color Java Color object containing values for RGB
139      * @param colorType PROTANOPIA = 1, DEUTERANOPIA = 2 or TRITANOPIA = 3 as declared above
140      * @return Dichromat Color
141      */

142     public static Color JavaDoc getDichromatColor (Color JavaDoc color, int colorType)
143     {
144         // check type & return if not valid
145
int type = 0;
146         if (colorType > 0 && colorType < 4)
147             type = colorType;
148         // No conversion or no color
149
if (type == 0 || color == null)
150             return color;
151         type--; // correct to zero based
152

153         // Return white & black - not converted
154
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     // System.out.println("Red: " + red + " Green: " + green + " Blue: " + blue);
162

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     // System.out.println("X: " + X + " Y: " + Y + " Z: " + Z);
174

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     // System.out.println("x: " + x + " y: " + y + " Y: " + Y);
185

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     // System.out.println("ap: " + ap + " bp: " + bp);
202

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     // System.out.println("a: " + a + " b: " + b);
212

213         x = (bp - b) / (a - ap);
214         y = a * x + b;
215
216     // System.out.println("x: " + x + " y: " + y);
217

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     // System.out.println("X: " + X + " Y: " + Y + " Z: " + Z);
227

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     // System.out.println("Red: " + red + " Green: " + green + " Blue: " + blue);
239

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     // System.out.println("cr: " + cr + " cg: " + cg + " cb: " + cb);
255

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     // System.out.println("c: " + c);
262

263         red = red + c * reddiff;
264         green = green + c * greendiff;
265         blue = blue + c * bluediff;
266
267     // System.out.println("Red: " + red + " Green: " + green + " Blue: " + blue);
268

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     // System.out.println("Red: " + red + " Green: " + green + " Blue: " + blue);
274
// System.out.println("Red: " + red * 255.0 + " Green: " + green * 255.0 + " Blue: " + blue * 255.0);
275

276         Color JavaDoc retValue = new Color JavaDoc((float)red, (float)green, (float)blue);
277     // System.out.println("Color " + color.getRed() + "-" + color.getGreen() + "-" + color.getBlue()
278
// + " -> " + retValue.getRed() + "-" + retValue.getGreen() + "-" + retValue.getBlue() + " <- " + COLORBLIND_TYPE[colorType]);
279

280         return retValue;
281     } // convertToDichromatColor
282

283 } // ColorBlind
284
Popular Tags