OSDN Git Service

Merge changes Ide388898,Ic49f367c,I1158a808,Iacb6ca5d,I2100dd3a,I5c1abe54,Ib0ef9902...
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / Color.cpp
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "Color.h"
28
29 #include "HashTools.h"
30 #include "PlatformString.h"
31 #include <math.h>
32 #include <wtf/Assertions.h>
33 #include <wtf/MathExtras.h>
34
35 using namespace std;
36 using namespace WTF;
37
38 namespace WebCore {
39
40 #if !COMPILER(MSVC)
41 const RGBA32 Color::black;
42 const RGBA32 Color::white;
43 const RGBA32 Color::darkGray;
44 const RGBA32 Color::gray;
45 const RGBA32 Color::lightGray;
46 const RGBA32 Color::transparent;
47 #endif
48
49 static const RGBA32 lightenedBlack = 0xFF545454;
50 static const RGBA32 darkenedWhite = 0xFFABABAB;
51
52 RGBA32 makeRGB(int r, int g, int b)
53 {
54     return 0xFF000000 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | max(0, min(b, 255));
55 }
56
57 RGBA32 makeRGBA(int r, int g, int b, int a)
58 {
59     return max(0, min(a, 255)) << 24 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | max(0, min(b, 255));
60 }
61
62 static int colorFloatToRGBAByte(float f)
63 {
64     // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's rounding
65     return max(0, min(static_cast<int>(lroundf(255.0f * f)), 255));
66 }
67
68 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a)
69 {
70     return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | colorFloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b);
71 }
72
73 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha)
74 {
75     RGBA32 rgbOnly = color & 0x00FFFFFF;
76     RGBA32 rgba = rgbOnly | colorFloatToRGBAByte(overrideAlpha) << 24;
77     return rgba;
78 }
79
80 static double calcHue(double temp1, double temp2, double hueVal)
81 {
82     if (hueVal < 0.0)
83         hueVal++;
84     else if (hueVal > 1.0)
85         hueVal--;
86     if (hueVal * 6.0 < 1.0)
87         return temp1 + (temp2 - temp1) * hueVal * 6.0;
88     if (hueVal * 2.0 < 1.0)
89         return temp2;
90     if (hueVal * 3.0 < 2.0)
91         return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0;
92     return temp1;
93 }
94
95 // Explanation of this algorithm can be found in the CSS3 Color Module
96 // specification at http://www.w3.org/TR/css3-color/#hsl-color with further
97 // explanation available at http://en.wikipedia.org/wiki/HSL_color_space 
98
99 // all values are in the range of 0 to 1.0
100 RGBA32 makeRGBAFromHSLA(double hue, double saturation, double lightness, double alpha)
101 {
102     const double scaleFactor = nextafter(256.0, 0.0);
103
104     if (!saturation) {
105         int greyValue = static_cast<int>(lightness * scaleFactor);
106         return makeRGBA(greyValue, greyValue, greyValue, static_cast<int>(alpha * scaleFactor));
107     }
108
109     double temp2 = lightness < 0.5 ? lightness * (1.0 + saturation) : lightness + saturation - lightness * saturation;
110     double temp1 = 2.0 * lightness - temp2;
111     
112     return makeRGBA(static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * scaleFactor), 
113                     static_cast<int>(calcHue(temp1, temp2, hue) * scaleFactor),
114                     static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * scaleFactor),
115                     static_cast<int>(alpha * scaleFactor));
116 }
117
118 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a)
119 {
120     double colors = 1 - k;
121     int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c)));
122     int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m)));
123     int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y)));
124     return makeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a));
125 }
126
127 // originally moved here from the CSS parser
128 bool Color::parseHexColor(const UChar* name, unsigned length, RGBA32& rgb)
129 {
130     if (length != 3 && length != 6)
131         return false;
132     unsigned value = 0;
133     for (unsigned i = 0; i < length; ++i) {
134         if (!isASCIIHexDigit(name[i]))
135             return false;
136         value <<= 4;
137         value |= toASCIIHexValue(name[i]);
138     }
139     if (length == 6) {
140         rgb = 0xFF000000 | value;
141         return true;
142     }
143     // #abc converts to #aabbcc
144     rgb = 0xFF000000
145         | (value & 0xF00) << 12 | (value & 0xF00) << 8
146         | (value & 0xF0) << 8 | (value & 0xF0) << 4
147         | (value & 0xF) << 4 | (value & 0xF);
148     return true;
149 }
150
151 bool Color::parseHexColor(const String& name, RGBA32& rgb)
152 {
153     return parseHexColor(name.characters(), name.length(), rgb);
154 }
155
156 int differenceSquared(const Color& c1, const Color& c2)
157 {
158     int dR = c1.red() - c2.red();
159     int dG = c1.green() - c2.green();
160     int dB = c1.blue() - c2.blue();
161     return dR * dR + dG * dG + dB * dB;
162 }
163
164 Color::Color(const String& name)
165 {
166     if (name[0] == '#')
167         m_valid = parseHexColor(name.characters() + 1, name.length() - 1, m_color);
168     else
169         setNamedColor(name);
170 }
171
172 Color::Color(const char* name)
173 {
174     if (name[0] == '#')
175         m_valid = parseHexColor(&name[1], m_color);
176     else {
177         const NamedColor* foundColor = findColor(name, strlen(name));
178         m_color = foundColor ? foundColor->ARGBValue : 0;
179         m_valid = foundColor;
180     }
181 }
182
183 String Color::serialized() const
184 {
185     if (alpha() == 0xFF)
186         return String::format("#%02x%02x%02x", red(), green(), blue());
187
188     // Match Gecko ("0.0" for zero, 5 decimals for anything else)
189     if (!alpha())
190         return String::format("rgba(%u, %u, %u, 0.0)", red(), green(), blue());
191
192     return String::format("rgba(%u, %u, %u, %.5f)", red(), green(), blue(), alpha() / 255.0f);
193 }
194
195 String Color::name() const
196 {
197     if (alpha() < 0xFF)
198         return String::format("#%02X%02X%02X%02X", red(), green(), blue(), alpha());
199     return String::format("#%02X%02X%02X", red(), green(), blue());
200 }
201
202 static inline const NamedColor* findNamedColor(const String& name)
203 {
204     char buffer[64]; // easily big enough for the longest color name
205     unsigned length = name.length();
206     if (length > sizeof(buffer) - 1)
207         return 0;
208     for (unsigned i = 0; i < length; ++i) {
209         UChar c = name[i];
210         if (!c || c > 0x7F)
211             return 0;
212         buffer[i] = toASCIILower(static_cast<char>(c));
213     }
214     buffer[length] = '\0';
215     return findColor(buffer, length);
216 }
217
218 void Color::setNamedColor(const String& name)
219 {
220     const NamedColor* foundColor = findNamedColor(name);
221     m_color = foundColor ? foundColor->ARGBValue : 0;
222     m_valid = foundColor;
223 }
224
225 Color Color::light() const
226 {
227     // Hardcode this common case for speed.
228     if (m_color == black)
229         return lightenedBlack;
230     
231     const float scaleFactor = nextafterf(256.0f, 0.0f);
232
233     float r, g, b, a;
234     getRGBA(r, g, b, a);
235
236     float v = max(r, max(g, b));
237
238     if (v == 0.0f)
239         // Lightened black with alpha.
240         return Color(0x54, 0x54, 0x54, alpha());
241
242     float multiplier = min(1.0f, v + 0.33f) / v;
243
244     return Color(static_cast<int>(multiplier * r * scaleFactor),
245                  static_cast<int>(multiplier * g * scaleFactor),
246                  static_cast<int>(multiplier * b * scaleFactor),
247                  alpha());
248 }
249
250 Color Color::dark() const
251 {
252     // Hardcode this common case for speed.
253     if (m_color == white)
254         return darkenedWhite;
255     
256     const float scaleFactor = nextafterf(256.0f, 0.0f);
257
258     float r, g, b, a;
259     getRGBA(r, g, b, a);
260
261     float v = max(r, max(g, b));
262     float multiplier = max(0.0f, (v - 0.33f) / v);
263
264     return Color(static_cast<int>(multiplier * r * scaleFactor),
265                  static_cast<int>(multiplier * g * scaleFactor),
266                  static_cast<int>(multiplier * b * scaleFactor),
267                  alpha());
268 }
269
270 static int blendComponent(int c, int a)
271 {
272     // We use white.
273     float alpha = a / 255.0f;
274     int whiteBlend = 255 - a;
275     c -= whiteBlend;
276     return static_cast<int>(c / alpha);
277 }
278
279 const int cStartAlpha = 153; // 60%
280 const int cEndAlpha = 204; // 80%;
281 const int cAlphaIncrement = 17; // Increments in between.
282
283 Color Color::blend(const Color& source) const
284 {
285     if (!alpha() || !source.hasAlpha())
286         return source;
287
288     if (!source.alpha())
289         return *this;
290
291     int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
292     int a = d / 255;
293     int r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
294     int g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
295     int b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
296     return Color(r, g, b, a);
297 }
298
299 Color Color::blendWithWhite() const
300 {
301     // If the color contains alpha already, we leave it alone.
302     if (hasAlpha())
303         return *this;
304
305     Color newColor;
306     for (int alpha = cStartAlpha; alpha <= cEndAlpha; alpha += cAlphaIncrement) {
307         // We have a solid color.  Convert to an equivalent color that looks the same when blended with white
308         // at the current alpha.  Try using less transparency if the numbers end up being negative.
309         int r = blendComponent(red(), alpha);
310         int g = blendComponent(green(), alpha);
311         int b = blendComponent(blue(), alpha);
312         
313         newColor = Color(r, g, b, alpha);
314
315         if (r >= 0 && g >= 0 && b >= 0)
316             break;
317     }
318     return newColor;
319 }
320
321 void Color::getRGBA(float& r, float& g, float& b, float& a) const
322 {
323     r = red() / 255.0f;
324     g = green() / 255.0f;
325     b = blue() / 255.0f;
326     a = alpha() / 255.0f;
327 }
328
329 void Color::getRGBA(double& r, double& g, double& b, double& a) const
330 {
331     r = red() / 255.0;
332     g = green() / 255.0;
333     b = blue() / 255.0;
334     a = alpha() / 255.0;
335 }
336
337 void Color::getHSL(double& hue, double& saturation, double& lightness) const
338 {
339     // http://en.wikipedia.org/wiki/HSL_color_space. This is a direct copy of
340     // the algorithm therein, although it's 360^o based and we end up wanting
341     // [0...1) based. It's clearer if we stick to 360^o until the end.
342     double r = static_cast<double>(red()) / 255.0;
343     double g = static_cast<double>(green()) / 255.0;
344     double b = static_cast<double>(blue()) / 255.0;
345     double max = std::max(std::max(r, g), b);
346     double min = std::min(std::min(r, g), b);
347
348     if (max == min)
349         hue = 0.0;
350     else if (max == r)
351         hue = (60.0 * ((g - b) / (max - min))) + 360.0;
352     else if (max == g)
353         hue = (60.0 * ((b - r) / (max - min))) + 120.0;
354     else
355         hue = (60.0 * ((r - g) / (max - min))) + 240.0;
356
357     if (hue >= 360.0)
358         hue -= 360.0;
359
360     // makeRGBAFromHSLA assumes that hue is in [0...1).
361     hue /= 360.0;
362
363     lightness = 0.5 * (max + min);
364     if (max == min)
365         saturation = 0.0;
366     else if (lightness <= 0.5)
367         saturation = ((max - min) / (max + min));
368     else
369         saturation = ((max - min) / (2.0 - (max + min)));
370 }
371
372 Color colorFromPremultipliedARGB(unsigned pixelColor)
373 {
374     RGBA32 rgba;
375
376     if (unsigned alpha = (pixelColor & 0xFF000000) >> 24) {
377         rgba = makeRGBA(((pixelColor & 0x00FF0000) >> 16) * 255 / alpha,
378                         ((pixelColor & 0x0000FF00) >> 8) * 255 / alpha,
379                          (pixelColor & 0x000000FF) * 255 / alpha,
380                           alpha);
381     } else
382         rgba = pixelColor;
383
384     return Color(rgba);
385 }
386
387 unsigned premultipliedARGBFromColor(const Color& color)
388 {
389     unsigned pixelColor;
390
391     if (unsigned alpha = color.alpha()) {
392         pixelColor = alpha << 24 |
393              ((color.red() * alpha  + 254) / 255) << 16 | 
394              ((color.green() * alpha  + 254) / 255) << 8 | 
395              ((color.blue() * alpha  + 254) / 255);
396     } else
397          pixelColor = color.rgb();
398
399     return pixelColor;
400 }
401
402 } // namespace WebCore