svn-gvsig-desktop / tags / v1_0_2_Build_900 / libraries / libCq CMS for java.old / src / org / cresques / filter / ColorSpaceConversion.java @ 10562
History | View | Annotate | Download (6 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.filter; |
25 |
|
26 |
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
31 |
*/
|
32 |
|
33 |
public class ColorSpaceConversion { |
34 |
public static int H = 0, R = 0; |
35 |
public static int S = 1, G = 1; |
36 |
public static int L = 2, B = 2; |
37 |
|
38 |
|
39 |
/**
|
40 |
* Convierte HSL a RGB
|
41 |
* @param h hue
|
42 |
* @param s saturation
|
43 |
* @param l ligthness
|
44 |
* @return
|
45 |
*/
|
46 |
public static int[] HSLtoRGB(int h, int s, int i){ |
47 |
double red; /* the red band output */ |
48 |
double red255; /* the red band output */ |
49 |
double green; /* the green band output */ |
50 |
double green255; /* the green band output */ |
51 |
double blue; /* the blue band output */ |
52 |
double blue255; /* the blue band output */ |
53 |
double m1; /* value used for determining RGB */ |
54 |
double m2; /* value used for determining RGB */ |
55 |
double scalei; /* intensity value */ |
56 |
double scales; /* saturation value */ |
57 |
double hue; /* hue */ |
58 |
double savehue; /* save the hue for future processing */ |
59 |
int[] res = new int[3]; |
60 |
|
61 |
if(h == s && h == 0){ |
62 |
res[0] = res[1] = res[2] = i; |
63 |
return res;
|
64 |
} |
65 |
|
66 |
red = green = blue = 0.0;
|
67 |
scalei = (double) (i / 255.0); |
68 |
scales = (double) (s / 255.0); |
69 |
m2 = 0.0;
|
70 |
|
71 |
if (scalei <= 0.50) |
72 |
m2 = scalei*(1.0 + scales);
|
73 |
else if (scalei > 0.50) |
74 |
m2 = scalei+scales-(scalei*scales); |
75 |
m1 = 2.0 * scalei - m2;
|
76 |
|
77 |
hue = (double) 360.0 * h / 255.0; |
78 |
|
79 |
if (scales == 0.0){ |
80 |
if (hue == -1.0){ |
81 |
red = scalei; |
82 |
green = scalei; |
83 |
blue = scalei; |
84 |
} |
85 |
}else{
|
86 |
savehue = hue + 120.0;
|
87 |
if (savehue > 360.0) |
88 |
savehue -= 360.0;
|
89 |
if (savehue < 0.0) |
90 |
savehue += 360.0;
|
91 |
if (savehue < 60.0) |
92 |
red = m1 + (m2-m1) * savehue/60.0;
|
93 |
else if (savehue < 180.0) |
94 |
red = m2; |
95 |
else if (savehue < 240.0) |
96 |
red = m1 + (m2-m1) * (240.0-savehue)/60.0; |
97 |
else
|
98 |
red = m1; |
99 |
|
100 |
savehue = hue; |
101 |
if (savehue > 360.0) |
102 |
savehue -= 360.0;
|
103 |
if (savehue < 0.0) |
104 |
savehue += 360.0;
|
105 |
if (savehue < 60.0) |
106 |
green = m1 + (m2-m1) * savehue/60.0;
|
107 |
else if (savehue < 180.0) |
108 |
green = m2; |
109 |
else if (savehue < 240.0) |
110 |
green = m1 + (m2-m1) * (240.0-savehue)/60.0; |
111 |
else
|
112 |
green = m1; |
113 |
|
114 |
|
115 |
savehue = hue - 120.0;
|
116 |
if (savehue > 360.0) |
117 |
savehue -= 360.0;
|
118 |
if (savehue < 0.0) |
119 |
savehue += 360.0;
|
120 |
if (savehue < 60.0) |
121 |
blue = m1 + (m2-m1) * savehue/60.0;
|
122 |
else if (savehue < 180.0) |
123 |
blue = m2; |
124 |
else if (savehue < 240.0) |
125 |
blue = m1 + (m2-m1) * (240.0-savehue)/60.0; |
126 |
else
|
127 |
blue = m1; |
128 |
} |
129 |
|
130 |
red255 = red*255.0;
|
131 |
green255 = green*255.0;
|
132 |
blue255 = blue*255.0;
|
133 |
if (red255 > 255.0) |
134 |
red = 255.0;
|
135 |
else
|
136 |
red = red255; |
137 |
if (green255 > 255.0) |
138 |
green = 255.0;
|
139 |
else
|
140 |
green = green255; |
141 |
if (blue255 > 255.0) |
142 |
blue = 255.0;
|
143 |
else
|
144 |
blue = blue255; |
145 |
|
146 |
if (red > 254.5) red = 254.5; |
147 |
if (red < 0.0) red = 0.0; |
148 |
if (green > 254.5) green = 254.5; |
149 |
if (green < 0.0) green = 0.0; |
150 |
if (blue > 254.5) blue = 254.5; |
151 |
if (blue < 0.0) blue = 0.0; |
152 |
|
153 |
res[0] = (int) (red + 0.5); |
154 |
res[1] = (int) (green +0.5); |
155 |
res[2] = (int) (blue + 0.5); |
156 |
return res;
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Convierte RGB a HSL
|
161 |
* @param r red
|
162 |
* @param g green
|
163 |
* @param b blue
|
164 |
* @return
|
165 |
*/
|
166 |
public static double[] RGBtoHSL(int rojo, int verde, int azul){ |
167 |
double scaler, scaleg, scaleb;
|
168 |
double sat;
|
169 |
double red, green, blue;
|
170 |
double low, high, intens;
|
171 |
double hue=0.0; |
172 |
double[] res1 = new double[3]; |
173 |
|
174 |
scaler = (double) (rojo / 255.0); |
175 |
scaleg = (double) (verde / 255.0); |
176 |
scaleb = (double) (azul / 255.0); |
177 |
|
178 |
high = scaler; |
179 |
|
180 |
if (scaleg > high)
|
181 |
high = scaleg; |
182 |
if (scaleb > high)
|
183 |
high = scaleb; |
184 |
low = scaler; |
185 |
if (scaleg < low)
|
186 |
low = scaleg; |
187 |
if (scaleb < low)
|
188 |
low = scaleb; |
189 |
|
190 |
intens = ((high + low) / 2.0);
|
191 |
if (high == low){
|
192 |
sat = 0.0;
|
193 |
hue = 0.0;
|
194 |
res1[0] = (double) hue; |
195 |
res1[2] = (double) intens; |
196 |
res1[1] = (double) sat; |
197 |
}else if (high != low){ |
198 |
if (intens <= 0.5) |
199 |
sat = (high-low)/(high+low); |
200 |
else
|
201 |
sat = (high-low)/(2 - high - low);
|
202 |
|
203 |
red = (high-scaler)/(high-low); |
204 |
green = (high-scaleg)/(high-low); |
205 |
blue =(high-scaleb)/(high-low); |
206 |
|
207 |
if (scaler == high)
|
208 |
hue = blue - green; |
209 |
else
|
210 |
if (scaleg == high)
|
211 |
hue = 2 + red - blue;
|
212 |
else
|
213 |
if (scaleb == high)
|
214 |
hue = 4 + green - red;
|
215 |
|
216 |
hue *= 60.0;
|
217 |
|
218 |
if (hue < 0.0) |
219 |
hue += 360.0;
|
220 |
|
221 |
//res[0] = (byte)(255.0 * hue / 360.0 + 0.5);
|
222 |
//res[1] = (byte) (intens*255. + 0.5);
|
223 |
//res[2] = (byte) (sat*255. + 0.5);
|
224 |
res1[0] = (double)(hue); |
225 |
res1[2] = (double) (intens); |
226 |
res1[1] = (double) (sat); |
227 |
} |
228 |
return res1;
|
229 |
} |
230 |
} |