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 "platform/graphics/Color.h" 28 29 #include "platform/Decimal.h" 30 #include "wtf/Assertions.h" 31 #include "wtf/HexNumber.h" 32 #include "wtf/MathExtras.h" 33 #include "wtf/dtoa.h" 34 #include "wtf/text/StringBuilder.h" 35 36 namespace blink { 37 38 #if !COMPILER(MSVC) 39 // FIXME: Use C++11 strong enums to avoid static data member with initializer definition problems. 40 const RGBA32 Color::black; 41 const RGBA32 Color::white; 42 const RGBA32 Color::darkGray; 43 const RGBA32 Color::gray; 44 const RGBA32 Color::lightGray; 45 const RGBA32 Color::transparent; 46 #endif 47 48 static const RGBA32 lightenedBlack = 0xFF545454; 49 static const RGBA32 darkenedWhite = 0xFFABABAB; 50 51 RGBA32 makeRGB(int r, int g, int b) 52 { 53 return 0xFF000000 | std::max(0, std::min(r, 255)) << 16 | std::max(0, std::min(g, 255)) << 8 | std::max(0, std::min(b, 255)); 54 } 55 56 RGBA32 makeRGBA(int r, int g, int b, int a) 57 { 58 return std::max(0, std::min(a, 255)) << 24 | std::max(0, std::min(r, 255)) << 16 | std::max(0, std::min(g, 255)) << 8 | std::max(0, std::min(b, 255)); 59 } 60 61 static int colorFloatToRGBAByte(float f) 62 { 63 // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's rounding 64 return std::max(0, std::min(static_cast<int>(lroundf(255.0f * f)), 255)); 65 } 66 67 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a) 68 { 69 return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | colorFloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b); 70 } 71 72 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha) 73 { 74 RGBA32 rgbOnly = color & 0x00FFFFFF; 75 RGBA32 rgba = rgbOnly | colorFloatToRGBAByte(overrideAlpha) << 24; 76 return rgba; 77 } 78 79 static double calcHue(double temp1, double temp2, double hueVal) 80 { 81 if (hueVal < 0.0) 82 hueVal++; 83 else if (hueVal > 1.0) 84 hueVal--; 85 if (hueVal * 6.0 < 1.0) 86 return temp1 + (temp2 - temp1) * hueVal * 6.0; 87 if (hueVal * 2.0 < 1.0) 88 return temp2; 89 if (hueVal * 3.0 < 2.0) 90 return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0; 91 return temp1; 92 } 93 94 // Explanation of this algorithm can be found in the CSS3 Color Module 95 // specification at http://www.w3.org/TR/css3-color/#hsl-color with further 96 // explanation available at http://en.wikipedia.org/wiki/HSL_color_space 97 98 // all values are in the range of 0 to 1.0 99 RGBA32 makeRGBAFromHSLA(double hue, double saturation, double lightness, double alpha) 100 { 101 const double scaleFactor = nextafter(256.0, 0.0); 102 103 if (!saturation) { 104 int greyValue = static_cast<int>(lightness * scaleFactor); 105 return makeRGBA(greyValue, greyValue, greyValue, static_cast<int>(alpha * scaleFactor)); 106 } 107 108 double temp2 = lightness < 0.5 ? lightness * (1.0 + saturation) : lightness + saturation - lightness * saturation; 109 double temp1 = 2.0 * lightness - temp2; 110 111 return makeRGBA(static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * scaleFactor), 112 static_cast<int>(calcHue(temp1, temp2, hue) * scaleFactor), 113 static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * scaleFactor), 114 static_cast<int>(alpha * scaleFactor)); 115 } 116 117 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a) 118 { 119 double colors = 1 - k; 120 int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c))); 121 int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m))); 122 int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y))); 123 return makeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a)); 124 } 125 126 // originally moved here from the CSS parser 127 template <typename CharacterType> 128 static inline bool parseHexColorInternal(const CharacterType* 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 LChar* name, unsigned length, RGBA32& rgb) 152 { 153 return parseHexColorInternal(name, length, rgb); 154 } 155 156 bool Color::parseHexColor(const UChar* name, unsigned length, RGBA32& rgb) 157 { 158 return parseHexColorInternal(name, length, rgb); 159 } 160 161 bool Color::parseHexColor(const String& name, RGBA32& rgb) 162 { 163 unsigned length = name.length(); 164 165 if (!length) 166 return false; 167 if (name.is8Bit()) 168 return parseHexColor(name.characters8(), name.length(), rgb); 169 return parseHexColor(name.characters16(), name.length(), rgb); 170 } 171 172 int differenceSquared(const Color& c1, const Color& c2) 173 { 174 int dR = c1.red() - c2.red(); 175 int dG = c1.green() - c2.green(); 176 int dB = c1.blue() - c2.blue(); 177 return dR * dR + dG * dG + dB * dB; 178 } 179 180 bool Color::setFromString(const String& name) 181 { 182 if (name[0] != '#') 183 return setNamedColor(name); 184 if (name.is8Bit()) 185 return parseHexColor(name.characters8() + 1, name.length() - 1, m_color); 186 return parseHexColor(name.characters16() + 1, name.length() - 1, m_color); 187 } 188 189 String Color::serializedAsCSSComponentValue() const 190 { 191 StringBuilder result; 192 result.reserveCapacity(32); 193 bool colorHasAlpha = hasAlpha(); 194 if (colorHasAlpha) 195 result.appendLiteral("rgba("); 196 else 197 result.appendLiteral("rgb("); 198 199 result.appendNumber(static_cast<unsigned char>(red())); 200 result.appendLiteral(", "); 201 202 result.appendNumber(static_cast<unsigned char>(green())); 203 result.appendLiteral(", "); 204 205 result.appendNumber(static_cast<unsigned char>(blue())); 206 if (colorHasAlpha) { 207 result.appendLiteral(", "); 208 209 NumberToStringBuffer buffer; 210 const char* alphaString = numberToFixedPrecisionString(alpha() / 255.0f, 6, buffer, true); 211 result.append(alphaString, strlen(alphaString)); 212 } 213 214 result.append(')'); 215 return result.toString(); 216 } 217 218 String Color::serialized() const 219 { 220 if (!hasAlpha()) { 221 StringBuilder builder; 222 builder.reserveCapacity(7); 223 builder.append('#'); 224 appendByteAsHex(red(), builder, Lowercase); 225 appendByteAsHex(green(), builder, Lowercase); 226 appendByteAsHex(blue(), builder, Lowercase); 227 return builder.toString(); 228 } 229 230 StringBuilder result; 231 result.reserveCapacity(28); 232 233 result.appendLiteral("rgba("); 234 result.appendNumber(red()); 235 result.appendLiteral(", "); 236 result.appendNumber(green()); 237 result.appendLiteral(", "); 238 result.appendNumber(blue()); 239 result.appendLiteral(", "); 240 241 if (!alpha()) 242 result.append('0'); 243 else { 244 result.append(Decimal::fromDouble(alpha() / 255.0).toString()); 245 } 246 247 result.append(')'); 248 return result.toString(); 249 } 250 251 String Color::nameForRenderTreeAsText() const 252 { 253 if (alpha() < 0xFF) 254 return String::format("#%02X%02X%02X%02X", red(), green(), blue(), alpha()); 255 return String::format("#%02X%02X%02X", red(), green(), blue()); 256 } 257 258 static inline const NamedColor* findNamedColor(const String& name) 259 { 260 char buffer[64]; // easily big enough for the longest color name 261 unsigned length = name.length(); 262 if (length > sizeof(buffer) - 1) 263 return 0; 264 for (unsigned i = 0; i < length; ++i) { 265 UChar c = name[i]; 266 if (!c || c > 0x7F) 267 return 0; 268 buffer[i] = toASCIILower(static_cast<char>(c)); 269 } 270 buffer[length] = '\0'; 271 return findColor(buffer, length); 272 } 273 274 bool Color::setNamedColor(const String& name) 275 { 276 const NamedColor* foundColor = findNamedColor(name); 277 m_color = foundColor ? foundColor->ARGBValue : 0; 278 return foundColor; 279 } 280 281 Color Color::light() const 282 { 283 // Hardcode this common case for speed. 284 if (m_color == black) 285 return lightenedBlack; 286 287 const float scaleFactor = nextafterf(256.0f, 0.0f); 288 289 float r, g, b, a; 290 getRGBA(r, g, b, a); 291 292 float v = std::max(r, std::max(g, b)); 293 294 if (v == 0.0f) 295 // Lightened black with alpha. 296 return Color(0x54, 0x54, 0x54, alpha()); 297 298 float multiplier = std::min(1.0f, v + 0.33f) / v; 299 300 return Color(static_cast<int>(multiplier * r * scaleFactor), 301 static_cast<int>(multiplier * g * scaleFactor), 302 static_cast<int>(multiplier * b * scaleFactor), 303 alpha()); 304 } 305 306 Color Color::dark() const 307 { 308 // Hardcode this common case for speed. 309 if (m_color == white) 310 return darkenedWhite; 311 312 const float scaleFactor = nextafterf(256.0f, 0.0f); 313 314 float r, g, b, a; 315 getRGBA(r, g, b, a); 316 317 float v = std::max(r, std::max(g, b)); 318 float multiplier = std::max(0.0f, (v - 0.33f) / v); 319 320 return Color(static_cast<int>(multiplier * r * scaleFactor), 321 static_cast<int>(multiplier * g * scaleFactor), 322 static_cast<int>(multiplier * b * scaleFactor), 323 alpha()); 324 } 325 326 Color Color::combineWithAlpha(float otherAlpha) const 327 { 328 return colorWithOverrideAlpha(rgb(), (alpha() / 255.f) * otherAlpha); 329 } 330 331 static int blendComponent(int c, int a) 332 { 333 // We use white. 334 float alpha = a / 255.0f; 335 int whiteBlend = 255 - a; 336 c -= whiteBlend; 337 return static_cast<int>(c / alpha); 338 } 339 340 const int cStartAlpha = 153; // 60% 341 const int cEndAlpha = 204; // 80%; 342 const int cAlphaIncrement = 17; // Increments in between. 343 344 Color Color::blend(const Color& source) const 345 { 346 if (!alpha() || !source.hasAlpha()) 347 return source; 348 349 if (!source.alpha()) 350 return *this; 351 352 int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha(); 353 int a = d / 255; 354 int r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d; 355 int g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d; 356 int b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d; 357 return Color(r, g, b, a); 358 } 359 360 Color Color::blendWithWhite() const 361 { 362 // If the color contains alpha already, we leave it alone. 363 if (hasAlpha()) 364 return *this; 365 366 Color newColor; 367 for (int alpha = cStartAlpha; alpha <= cEndAlpha; alpha += cAlphaIncrement) { 368 // We have a solid color. Convert to an equivalent color that looks the same when blended with white 369 // at the current alpha. Try using less transparency if the numbers end up being negative. 370 int r = blendComponent(red(), alpha); 371 int g = blendComponent(green(), alpha); 372 int b = blendComponent(blue(), alpha); 373 374 newColor = Color(r, g, b, alpha); 375 376 if (r >= 0 && g >= 0 && b >= 0) 377 break; 378 } 379 return newColor; 380 } 381 382 void Color::getRGBA(float& r, float& g, float& b, float& a) const 383 { 384 r = red() / 255.0f; 385 g = green() / 255.0f; 386 b = blue() / 255.0f; 387 a = alpha() / 255.0f; 388 } 389 390 void Color::getRGBA(double& r, double& g, double& b, double& a) const 391 { 392 r = red() / 255.0; 393 g = green() / 255.0; 394 b = blue() / 255.0; 395 a = alpha() / 255.0; 396 } 397 398 void Color::getHSL(double& hue, double& saturation, double& lightness) const 399 { 400 // http://en.wikipedia.org/wiki/HSL_color_space. This is a direct copy of 401 // the algorithm therein, although it's 360^o based and we end up wanting 402 // [0...1) based. It's clearer if we stick to 360^o until the end. 403 double r = static_cast<double>(red()) / 255.0; 404 double g = static_cast<double>(green()) / 255.0; 405 double b = static_cast<double>(blue()) / 255.0; 406 double max = std::max(std::max(r, g), b); 407 double min = std::min(std::min(r, g), b); 408 409 if (max == min) 410 hue = 0.0; 411 else if (max == r) 412 hue = (60.0 * ((g - b) / (max - min))) + 360.0; 413 else if (max == g) 414 hue = (60.0 * ((b - r) / (max - min))) + 120.0; 415 else 416 hue = (60.0 * ((r - g) / (max - min))) + 240.0; 417 418 if (hue >= 360.0) 419 hue -= 360.0; 420 421 // makeRGBAFromHSLA assumes that hue is in [0...1). 422 hue /= 360.0; 423 424 lightness = 0.5 * (max + min); 425 if (max == min) 426 saturation = 0.0; 427 else if (lightness <= 0.5) 428 saturation = ((max - min) / (max + min)); 429 else 430 saturation = ((max - min) / (2.0 - (max + min))); 431 } 432 433 Color colorFromPremultipliedARGB(RGBA32 pixelColor) 434 { 435 int alpha = alphaChannel(pixelColor); 436 if (alpha && alpha < 255) { 437 return Color::createUnchecked( 438 redChannel(pixelColor) * 255 / alpha, 439 greenChannel(pixelColor) * 255 / alpha, 440 blueChannel(pixelColor) * 255 / alpha, 441 alpha); 442 } else 443 return Color(pixelColor); 444 } 445 446 RGBA32 premultipliedARGBFromColor(const Color& color) 447 { 448 unsigned pixelColor; 449 450 unsigned alpha = color.alpha(); 451 if (alpha < 255) { 452 pixelColor = Color::createUnchecked( 453 (color.red() * alpha + 254) / 255, 454 (color.green() * alpha + 254) / 255, 455 (color.blue() * alpha + 254) / 255, 456 alpha).rgb(); 457 } else 458 pixelColor = color.rgb(); 459 460 return pixelColor; 461 } 462 463 } // namespace blink 464