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.
27 #include "platform/graphics/Color.h"
29 #include "platform/Decimal.h"
30 #include "wtf/Assertions.h"
31 #include "wtf/HexNumber.h"
32 #include "wtf/MathExtras.h"
34 #include "wtf/text/StringBuilder.h"
41 // FIXME: Use C++11 strong enums to avoid static data member with initializer definition problems.
42 const RGBA32 Color::black;
43 const RGBA32 Color::white;
44 const RGBA32 Color::darkGray;
45 const RGBA32 Color::gray;
46 const RGBA32 Color::lightGray;
47 const RGBA32 Color::transparent;
50 static const RGBA32 lightenedBlack = 0xFF545454;
51 static const RGBA32 darkenedWhite = 0xFFABABAB;
53 RGBA32 makeRGB(int r, int g, int b)
55 return 0xFF000000 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | max(0, min(b, 255));
58 RGBA32 makeRGBA(int r, int g, int b, int a)
60 return max(0, min(a, 255)) << 24 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | max(0, min(b, 255));
63 static int colorFloatToRGBAByte(float f)
65 // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's rounding
66 return max(0, min(static_cast<int>(lroundf(255.0f * f)), 255));
69 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a)
71 return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | colorFloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b);
74 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha)
76 RGBA32 rgbOnly = color & 0x00FFFFFF;
77 RGBA32 rgba = rgbOnly | colorFloatToRGBAByte(overrideAlpha) << 24;
81 static double calcHue(double temp1, double temp2, double hueVal)
85 else if (hueVal > 1.0)
87 if (hueVal * 6.0 < 1.0)
88 return temp1 + (temp2 - temp1) * hueVal * 6.0;
89 if (hueVal * 2.0 < 1.0)
91 if (hueVal * 3.0 < 2.0)
92 return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0;
96 // Explanation of this algorithm can be found in the CSS3 Color Module
97 // specification at http://www.w3.org/TR/css3-color/#hsl-color with further
98 // explanation available at http://en.wikipedia.org/wiki/HSL_color_space
100 // all values are in the range of 0 to 1.0
101 RGBA32 makeRGBAFromHSLA(double hue, double saturation, double lightness, double alpha)
103 const double scaleFactor = nextafter(256.0, 0.0);
106 int greyValue = static_cast<int>(lightness * scaleFactor);
107 return makeRGBA(greyValue, greyValue, greyValue, static_cast<int>(alpha * scaleFactor));
110 double temp2 = lightness < 0.5 ? lightness * (1.0 + saturation) : lightness + saturation - lightness * saturation;
111 double temp1 = 2.0 * lightness - temp2;
113 return makeRGBA(static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * scaleFactor),
114 static_cast<int>(calcHue(temp1, temp2, hue) * scaleFactor),
115 static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * scaleFactor),
116 static_cast<int>(alpha * scaleFactor));
119 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a)
121 double colors = 1 - k;
122 int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c)));
123 int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m)));
124 int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y)));
125 return makeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a));
128 // originally moved here from the CSS parser
129 template <typename CharacterType>
130 static inline bool parseHexColorInternal(const CharacterType* name, unsigned length, RGBA32& rgb)
132 if (length != 3 && length != 6)
135 for (unsigned i = 0; i < length; ++i) {
136 if (!isASCIIHexDigit(name[i]))
139 value |= toASCIIHexValue(name[i]);
142 rgb = 0xFF000000 | value;
145 // #abc converts to #aabbcc
147 | (value & 0xF00) << 12 | (value & 0xF00) << 8
148 | (value & 0xF0) << 8 | (value & 0xF0) << 4
149 | (value & 0xF) << 4 | (value & 0xF);
153 bool Color::parseHexColor(const LChar* name, unsigned length, RGBA32& rgb)
155 return parseHexColorInternal(name, length, rgb);
158 bool Color::parseHexColor(const UChar* name, unsigned length, RGBA32& rgb)
160 return parseHexColorInternal(name, length, rgb);
163 bool Color::parseHexColor(const String& name, RGBA32& rgb)
165 unsigned length = name.length();
170 return parseHexColor(name.characters8(), name.length(), rgb);
171 return parseHexColor(name.characters16(), name.length(), rgb);
174 int differenceSquared(const Color& c1, const Color& c2)
176 int dR = c1.red() - c2.red();
177 int dG = c1.green() - c2.green();
178 int dB = c1.blue() - c2.blue();
179 return dR * dR + dG * dG + dB * dB;
182 bool Color::setFromString(const String& name)
185 return setNamedColor(name);
187 return parseHexColor(name.characters8() + 1, name.length() - 1, m_color);
188 return parseHexColor(name.characters16() + 1, name.length() - 1, m_color);
191 String Color::serializedAsCSSComponentValue() const
193 StringBuilder result;
194 result.reserveCapacity(32);
195 bool colorHasAlpha = hasAlpha();
197 result.append("rgba(", 5);
199 result.append("rgb(", 4);
201 result.appendNumber(static_cast<unsigned char>(red()));
202 result.append(", ", 2);
204 result.appendNumber(static_cast<unsigned char>(green()));
205 result.append(", ", 2);
207 result.appendNumber(static_cast<unsigned char>(blue()));
209 result.append(", ", 2);
211 NumberToStringBuffer buffer;
212 const char* alphaString = numberToFixedPrecisionString(alpha() / 255.0f, 6, buffer, true);
213 result.append(alphaString, strlen(alphaString));
217 return result.toString();
220 String Color::serialized() const
223 StringBuilder builder;
224 builder.reserveCapacity(7);
226 appendByteAsHex(red(), builder, Lowercase);
227 appendByteAsHex(green(), builder, Lowercase);
228 appendByteAsHex(blue(), builder, Lowercase);
229 return builder.toString();
232 StringBuilder result;
233 result.reserveCapacity(28);
234 const char commaSpace[] = ", ";
235 const char rgbaParen[] = "rgba(";
237 result.append(rgbaParen, 5);
238 result.appendNumber(red());
239 result.append(commaSpace, 2);
240 result.appendNumber(green());
241 result.append(commaSpace, 2);
242 result.appendNumber(blue());
243 result.append(commaSpace, 2);
248 result.append(Decimal::fromDouble(alpha() / 255.0).toString());
252 return result.toString();
255 String Color::nameForRenderTreeAsText() const
258 return String::format("#%02X%02X%02X%02X", red(), green(), blue(), alpha());
259 return String::format("#%02X%02X%02X", red(), green(), blue());
262 static inline const NamedColor* findNamedColor(const String& name)
264 char buffer[64]; // easily big enough for the longest color name
265 unsigned length = name.length();
266 if (length > sizeof(buffer) - 1)
268 for (unsigned i = 0; i < length; ++i) {
272 buffer[i] = toASCIILower(static_cast<char>(c));
274 buffer[length] = '\0';
275 return findColor(buffer, length);
278 bool Color::setNamedColor(const String& name)
280 const NamedColor* foundColor = findNamedColor(name);
281 m_color = foundColor ? foundColor->ARGBValue : 0;
285 Color Color::light() const
287 // Hardcode this common case for speed.
288 if (m_color == black)
289 return lightenedBlack;
291 const float scaleFactor = nextafterf(256.0f, 0.0f);
296 float v = max(r, max(g, b));
299 // Lightened black with alpha.
300 return Color(0x54, 0x54, 0x54, alpha());
302 float multiplier = min(1.0f, v + 0.33f) / v;
304 return Color(static_cast<int>(multiplier * r * scaleFactor),
305 static_cast<int>(multiplier * g * scaleFactor),
306 static_cast<int>(multiplier * b * scaleFactor),
310 Color Color::dark() const
312 // Hardcode this common case for speed.
313 if (m_color == white)
314 return darkenedWhite;
316 const float scaleFactor = nextafterf(256.0f, 0.0f);
321 float v = max(r, max(g, b));
322 float multiplier = max(0.0f, (v - 0.33f) / v);
324 return Color(static_cast<int>(multiplier * r * scaleFactor),
325 static_cast<int>(multiplier * g * scaleFactor),
326 static_cast<int>(multiplier * b * scaleFactor),
330 Color Color::combineWithAlpha(float otherAlpha) const
332 return colorWithOverrideAlpha(rgb(), (alpha() / 255.f) * otherAlpha);
335 static int blendComponent(int c, int a)
338 float alpha = a / 255.0f;
339 int whiteBlend = 255 - a;
341 return static_cast<int>(c / alpha);
344 const int cStartAlpha = 153; // 60%
345 const int cEndAlpha = 204; // 80%;
346 const int cAlphaIncrement = 17; // Increments in between.
348 Color Color::blend(const Color& source) const
350 if (!alpha() || !source.hasAlpha())
356 int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
358 int r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
359 int g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
360 int b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
361 return Color(r, g, b, a);
364 Color Color::blendWithWhite() const
366 // If the color contains alpha already, we leave it alone.
371 for (int alpha = cStartAlpha; alpha <= cEndAlpha; alpha += cAlphaIncrement) {
372 // We have a solid color. Convert to an equivalent color that looks the same when blended with white
373 // at the current alpha. Try using less transparency if the numbers end up being negative.
374 int r = blendComponent(red(), alpha);
375 int g = blendComponent(green(), alpha);
376 int b = blendComponent(blue(), alpha);
378 newColor = Color(r, g, b, alpha);
380 if (r >= 0 && g >= 0 && b >= 0)
386 void Color::getRGBA(float& r, float& g, float& b, float& a) const
389 g = green() / 255.0f;
391 a = alpha() / 255.0f;
394 void Color::getRGBA(double& r, double& g, double& b, double& a) const
402 void Color::getHSL(double& hue, double& saturation, double& lightness) const
404 // http://en.wikipedia.org/wiki/HSL_color_space. This is a direct copy of
405 // the algorithm therein, although it's 360^o based and we end up wanting
406 // [0...1) based. It's clearer if we stick to 360^o until the end.
407 double r = static_cast<double>(red()) / 255.0;
408 double g = static_cast<double>(green()) / 255.0;
409 double b = static_cast<double>(blue()) / 255.0;
410 double max = std::max(std::max(r, g), b);
411 double min = std::min(std::min(r, g), b);
416 hue = (60.0 * ((g - b) / (max - min))) + 360.0;
418 hue = (60.0 * ((b - r) / (max - min))) + 120.0;
420 hue = (60.0 * ((r - g) / (max - min))) + 240.0;
425 // makeRGBAFromHSLA assumes that hue is in [0...1).
428 lightness = 0.5 * (max + min);
431 else if (lightness <= 0.5)
432 saturation = ((max - min) / (max + min));
434 saturation = ((max - min) / (2.0 - (max + min)));
437 Color colorFromPremultipliedARGB(RGBA32 pixelColor)
439 int alpha = alphaChannel(pixelColor);
440 if (alpha && alpha < 255) {
441 return Color::createUnchecked(
442 redChannel(pixelColor) * 255 / alpha,
443 greenChannel(pixelColor) * 255 / alpha,
444 blueChannel(pixelColor) * 255 / alpha,
447 return Color(pixelColor);
450 RGBA32 premultipliedARGBFromColor(const Color& color)
454 unsigned alpha = color.alpha();
456 pixelColor = Color::createUnchecked(
457 (color.red() * alpha + 254) / 255,
458 (color.green() * alpha + 254) / 255,
459 (color.blue() * alpha + 254) / 255,
462 pixelColor = color.rgb();