1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2007 1&1 Internet AG, Germany, http://www.1and1.org
License:
LGPL: http://www.gnu.org/licenses/lgpl.html
EPL: http://www.eclipse.org/org/documents/epl-v10.php
See the LICENSE file in the project's top-level directory for details.
Authors:
* Sebastian Werner (wpbasti)
* Andreas Ecker (ecker)
************************************************************************ */
/* ************************************************************************
************************************************************************ */
/**
* Methods to convert colors between ddiffernt color spaces.
*/
qx.OO.defineClass("qx.util.ColorUtil");
/**
* Convert RGB colors to HSB
*
* @param vRed {Number} Red value. Range: 0..255
* @param vGreen {Number} Green value. Range: 0..255
* @param vBlue {Number} Blue value. Range: 0..255
*
* @return {Map} Map with the keys following keys:
* <ul>
* <li>'hue': range 0..360</li>
* <li>'saturation': range 0..100</li>
* <li>'brightness': range 0..100</li>
* </ul>
*/
qx.Class.rgb2hsb = function(vRed, vGreen, vBlue)
{
var vHue, vSaturation, vBrightness;
vRed = parseFloat(vRed);
vGreen = parseFloat(vGreen);
vBlue = parseFloat(vBlue);
var cmax = (vRed > vGreen) ? vRed : vGreen;
if (vBlue > cmax) {
cmax = vBlue;
}
var cmin = (vRed < vGreen) ? vRed : vGreen;
if (vBlue < cmin) {
cmin = vBlue;
}
vBrightness = cmax / 255.0;
if (cmax != 0)
{
vSaturation = (cmax - cmin) / cmax;
}
else
{
vSaturation = 0;
}
if (vSaturation == 0)
{
vHue = 0;
}
else
{
var redc = (cmax - vRed) / (cmax - cmin);
var greenc = (cmax - vGreen) / (cmax - cmin);
var bluec = (cmax - vBlue) / (cmax - cmin);
if (vRed == cmax)
{
vHue = bluec - greenc;
}
else if (vGreen == cmax)
{
vHue = 2.0 + redc - bluec;
}
else
{
vHue = 4.0 + greenc - redc;
}
vHue = vHue / 6.0;
if (vHue < 0) vHue = vHue + 1.0;
}
return {
hue : Math.round(vHue * 360),
saturation : Math.round(vSaturation * 100),
brightness : Math.round(vBrightness * 100)
}
}
/**
* Convert HSB colors to RGB
*
* @param vHue {Number} Hue value. Range 0..360
* @param vSaturation {Number} Saturation value. Range 0..100
* @param vBrightness {Number} Brightness value. Range 0..100
*
* @return {Map} Map the the following keys:
* <ul>
* <li>'red': range 0..255</li>
* <li>'green': range 0..255</li>
* <li>'blue': range 0..255</li>
* </ul>
*/
qx.Class.hsb2rgb = function(vHue, vSaturation, vBrightness)
{
var i, f, p, q, t, vReturn;
vHue = parseFloat(vHue/360);
vSaturation = parseFloat(vSaturation/100);
vBrightness = parseFloat(vBrightness/100);
if(vHue >= 1.0) vHue %= 1.0;
if(vSaturation > 1.0) vSaturation = 1.0;
if(vBrightness > 1.0) vBrightness = 1.0;
var tov = Math.floor(255 * vBrightness);
var vReturn = {};
if(vSaturation == 0.0)
{
vReturn.red = vReturn.green = vReturn.blue = tov;
}
else
{
vHue *= 6.0;
i = Math.floor(vHue);
f = vHue - i;
p = Math.floor(tov * (1.0 - vSaturation));
q = Math.floor(tov * (1.0 - (vSaturation * f)));
t = Math.floor(tov * (1.0 - (vSaturation * (1.0 - f))));
switch(i)
{
case 0:
vReturn.red = tov;
vReturn.green = t;
vReturn.blue = p;
break;
case 1:
vReturn.red = q;
vReturn.green = tov;
vReturn.blue = p;
break;
case 2:
vReturn.red = p;
vReturn.green = tov;
vReturn.blue = t;
break;
case 3:
vReturn.red = p;
vReturn.green = q;
vReturn.blue = tov;
break;
case 4:
vReturn.red = t;
vReturn.green = p;
vReturn.blue = tov;
break;
case 5:
vReturn.red = tov;
vReturn.green = p;
vReturn.blue = q;
break;
}
}
return vReturn;
}
|