2 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "HashTools.h"
30 #include <wtf/Assertions.h>
31 #include <wtf/DecimalNumber.h>
32 #include <wtf/HexNumber.h>
33 #include <wtf/MathExtras.h>
34 #include <wtf/text/StringBuilder.h>
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;
49 static const RGBA32 lightenedBlack = 0xFF545454;
50 static const RGBA32 darkenedWhite = 0xFFABABAB;
52 RGBA32 makeRGB(int r, int g, int b)
54 return 0xFF000000 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | max(0, min(b, 255));
57 RGBA32 makeRGBA(int r, int g, int b, int a)
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));
62 static int colorFloatToRGBAByte(float f)
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));
68 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a)
70 return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | colorFloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b);
73 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha)
75 RGBA32 rgbOnly = color & 0x00FFFFFF;
76 RGBA32 rgba = rgbOnly | colorFloatToRGBAByte(overrideAlpha) << 24;
80 static double calcHue(double temp1, double temp2, double hueVal)
84 else if (hueVal > 1.0)
86 if (hueVal * 6.0 < 1.0)
87 return temp1 + (temp2 - temp1) * hueVal * 6.0;
88 if (hueVal * 2.0 < 1.0)
90 if (hueVal * 3.0 < 2.0)
91 return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0;
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
99 // all values are in the range of 0 to 1.0
100 RGBA32 makeRGBAFromHSLA(double hue, double saturation, double lightness, double alpha)
102 const double scaleFactor = nextafter(256.0, 0.0);
105 int greyValue = static_cast<int>(lightness * scaleFactor);
106 return makeRGBA(greyValue, greyValue, greyValue, static_cast<int>(alpha * scaleFactor));
109 double temp2 = lightness < 0.5 ? lightness * (1.0 + saturation) : lightness + saturation - lightness * saturation;
110 double temp1 = 2.0 * lightness - temp2;
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));
118 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a)
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));
127 // originally moved here from the CSS parser
128 bool Color::parseHexColor(const UChar* name, unsigned length, RGBA32& rgb)
130 if (length != 3 && length != 6)
133 for (unsigned i = 0; i < length; ++i) {
134 if (!isASCIIHexDigit(name[i]))
137 value |= toASCIIHexValue(name[i]);
140 rgb = 0xFF000000 | value;
143 // #abc converts to #aabbcc
145 | (value & 0xF00) << 12 | (value & 0xF00) << 8
146 | (value & 0xF0) << 8 | (value & 0xF0) << 4
147 | (value & 0xF) << 4 | (value & 0xF);
151 bool Color::parseHexColor(const String& name, RGBA32& rgb)
153 return parseHexColor(name.characters(), name.length(), rgb);
156 int differenceSquared(const Color& c1, const Color& c2)
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;
164 Color::Color(const String& name)
167 m_valid = parseHexColor(name.characters() + 1, name.length() - 1, m_color);
172 Color::Color(const char* name)
175 m_valid = parseHexColor(&name[1], m_color);
177 const NamedColor* foundColor = findColor(name, strlen(name));
178 m_color = foundColor ? foundColor->ARGBValue : 0;
179 m_valid = foundColor;
183 String Color::serialized() const
185 DEFINE_STATIC_LOCAL(const String, commaSpace, (", "));
186 DEFINE_STATIC_LOCAL(const String, rgbaParen, ("rgba("));
189 StringBuilder builder;
190 builder.reserveCapacity(7);
192 appendByteAsHex(red(), builder, Lowercase);
193 appendByteAsHex(green(), builder, Lowercase);
194 appendByteAsHex(blue(), builder, Lowercase);
195 return builder.toString();
198 Vector<UChar> result;
199 result.reserveInitialCapacity(28);
201 append(result, rgbaParen);
202 appendNumber(result, red());
203 append(result, commaSpace);
204 appendNumber(result, green());
205 append(result, commaSpace);
206 appendNumber(result, blue());
207 append(result, commaSpace);
212 NumberToUStringBuffer buffer;
213 unsigned length = DecimalNumber(alpha() / 255.0).toStringDecimal(buffer, WTF::NumberToStringBufferLength);
214 result.append(buffer, length);
218 return String::adopt(result);
221 String Color::nameForRenderTreeAsText() const
224 return String::format("#%02X%02X%02X%02X", red(), green(), blue(), alpha());
225 return String::format("#%02X%02X%02X", red(), green(), blue());
228 static inline const NamedColor* findNamedColor(const String& name)
230 char buffer[64]; // easily big enough for the longest color name
231 unsigned length = name.length();
232 if (length > sizeof(buffer) - 1)
234 for (unsigned i = 0; i < length; ++i) {
238 buffer[i] = toASCIILower(static_cast<char>(c));
240 buffer[length] = '\0';
241 return findColor(buffer, length);
244 void Color::setNamedColor(const String& name)
246 const NamedColor* foundColor = findNamedColor(name);
247 m_color = foundColor ? foundColor->ARGBValue : 0;
248 m_valid = foundColor;
251 Color Color::light() const
253 // Hardcode this common case for speed.
254 if (m_color == black)
255 return lightenedBlack;
257 const float scaleFactor = nextafterf(256.0f, 0.0f);
262 float v = max(r, max(g, b));
265 // Lightened black with alpha.
266 return Color(0x54, 0x54, 0x54, alpha());
268 float multiplier = min(1.0f, v + 0.33f) / v;
270 return Color(static_cast<int>(multiplier * r * scaleFactor),
271 static_cast<int>(multiplier * g * scaleFactor),
272 static_cast<int>(multiplier * b * scaleFactor),
276 Color Color::dark() const
278 // Hardcode this common case for speed.
279 if (m_color == white)
280 return darkenedWhite;
282 const float scaleFactor = nextafterf(256.0f, 0.0f);
287 float v = max(r, max(g, b));
288 float multiplier = max(0.0f, (v - 0.33f) / v);
290 return Color(static_cast<int>(multiplier * r * scaleFactor),
291 static_cast<int>(multiplier * g * scaleFactor),
292 static_cast<int>(multiplier * b * scaleFactor),
296 static int blendComponent(int c, int a)
299 float alpha = a / 255.0f;
300 int whiteBlend = 255 - a;
302 return static_cast<int>(c / alpha);
305 const int cStartAlpha = 153; // 60%
306 const int cEndAlpha = 204; // 80%;
307 const int cAlphaIncrement = 17; // Increments in between.
309 Color Color::blend(const Color& source) const
311 if (!alpha() || !source.hasAlpha())
317 int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
319 int r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
320 int g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
321 int b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
322 return Color(r, g, b, a);
325 Color Color::blendWithWhite() const
327 // If the color contains alpha already, we leave it alone.
332 for (int alpha = cStartAlpha; alpha <= cEndAlpha; alpha += cAlphaIncrement) {
333 // We have a solid color. Convert to an equivalent color that looks the same when blended with white
334 // at the current alpha. Try using less transparency if the numbers end up being negative.
335 int r = blendComponent(red(), alpha);
336 int g = blendComponent(green(), alpha);
337 int b = blendComponent(blue(), alpha);
339 newColor = Color(r, g, b, alpha);
341 if (r >= 0 && g >= 0 && b >= 0)
347 void Color::getRGBA(float& r, float& g, float& b, float& a) const
350 g = green() / 255.0f;
352 a = alpha() / 255.0f;
355 void Color::getRGBA(double& r, double& g, double& b, double& a) const
363 void Color::getHSL(double& hue, double& saturation, double& lightness) const
365 // http://en.wikipedia.org/wiki/HSL_color_space. This is a direct copy of
366 // the algorithm therein, although it's 360^o based and we end up wanting
367 // [0...1) based. It's clearer if we stick to 360^o until the end.
368 double r = static_cast<double>(red()) / 255.0;
369 double g = static_cast<double>(green()) / 255.0;
370 double b = static_cast<double>(blue()) / 255.0;
371 double max = std::max(std::max(r, g), b);
372 double min = std::min(std::min(r, g), b);
377 hue = (60.0 * ((g - b) / (max - min))) + 360.0;
379 hue = (60.0 * ((b - r) / (max - min))) + 120.0;
381 hue = (60.0 * ((r - g) / (max - min))) + 240.0;
386 // makeRGBAFromHSLA assumes that hue is in [0...1).
389 lightness = 0.5 * (max + min);
392 else if (lightness <= 0.5)
393 saturation = ((max - min) / (max + min));
395 saturation = ((max - min) / (2.0 - (max + min)));
398 Color colorFromPremultipliedARGB(unsigned pixelColor)
402 if (unsigned alpha = (pixelColor & 0xFF000000) >> 24) {
403 color = Color::createUnCheked(
404 ((pixelColor & 0x00FF0000) >> 16) * 255 / alpha,
405 ((pixelColor & 0x0000FF00) >> 8) * 255 / alpha,
406 (pixelColor & 0x000000FF) * 255 / alpha,
409 color = Color(pixelColor);
414 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING)
415 Color colorFromPremultipliedABGR(unsigned pixelColor)
419 if (unsigned alpha = (pixelColor & 0xFF000000) >> 24) {
420 rgba = makeRGBA((pixelColor & 0x000000FF) * 255 / alpha,
421 ((pixelColor & 0x0000FF00) >> 8) * 255 / alpha,
422 ((pixelColor & 0x00FF0000) >> 16) * 255 / alpha,
430 unsigned premultipliedABGRFromColor(const Color& color)
434 if (unsigned alpha = color.alpha()) {
435 pixelColor = alpha << 24
436 | ((color.blue() * alpha + 254) / 255) << 16
437 | ((color.green() * alpha + 254) / 255) << 8
438 | ((color.red() * alpha + 254) / 255);
440 pixelColor = color.rgb();
446 unsigned premultipliedARGBFromColor(const Color& color)
450 if (unsigned alpha = color.alpha()) {
451 pixelColor = alpha << 24 |
452 ((color.red() * alpha + 254) / 255) << 16 |
453 ((color.green() * alpha + 254) / 255) << 8 |
454 ((color.blue() * alpha + 254) / 255);
456 pixelColor = color.rgb();
461 } // namespace WebCore