| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 #include "platform/graphics/Color.h" | 27 #include "platform/graphics/Color.h" |
| 28 | 28 |
| 29 #include "platform/Decimal.h" | 29 #include "platform/Decimal.h" |
| 30 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
| 31 #include "wtf/HexNumber.h" | 31 #include "wtf/HexNumber.h" |
| 32 #include "wtf/MathExtras.h" | 32 #include "wtf/MathExtras.h" |
| 33 #include "wtf/dtoa.h" | 33 #include "wtf/dtoa.h" |
| 34 #include "wtf/text/StringBuilder.h" | 34 #include "wtf/text/StringBuilder.h" |
| 35 | 35 |
| 36 using namespace std; | |
| 37 | |
| 38 namespace blink { | 36 namespace blink { |
| 39 | 37 |
| 40 #if !COMPILER(MSVC) | 38 #if !COMPILER(MSVC) |
| 41 // FIXME: Use C++11 strong enums to avoid static data member with initializer de
finition problems. | 39 // FIXME: Use C++11 strong enums to avoid static data member with initializer de
finition problems. |
| 42 const RGBA32 Color::black; | 40 const RGBA32 Color::black; |
| 43 const RGBA32 Color::white; | 41 const RGBA32 Color::white; |
| 44 const RGBA32 Color::darkGray; | 42 const RGBA32 Color::darkGray; |
| 45 const RGBA32 Color::gray; | 43 const RGBA32 Color::gray; |
| 46 const RGBA32 Color::lightGray; | 44 const RGBA32 Color::lightGray; |
| 47 const RGBA32 Color::transparent; | 45 const RGBA32 Color::transparent; |
| 48 #endif | 46 #endif |
| 49 | 47 |
| 50 static const RGBA32 lightenedBlack = 0xFF545454; | 48 static const RGBA32 lightenedBlack = 0xFF545454; |
| 51 static const RGBA32 darkenedWhite = 0xFFABABAB; | 49 static const RGBA32 darkenedWhite = 0xFFABABAB; |
| 52 | 50 |
| 53 RGBA32 makeRGB(int r, int g, int b) | 51 RGBA32 makeRGB(int r, int g, int b) |
| 54 { | 52 { |
| 55 return 0xFF000000 | max(0, min(r, 255)) << 16 | max(0, min(g, 255)) << 8 | m
ax(0, min(b, 255)); | 53 return 0xFF000000 | std::max(0, std::min(r, 255)) << 16 | std::max(0, std::m
in(g, 255)) << 8 | std::max(0, std::min(b, 255)); |
| 56 } | 54 } |
| 57 | 55 |
| 58 RGBA32 makeRGBA(int r, int g, int b, int a) | 56 RGBA32 makeRGBA(int r, int g, int b, int a) |
| 59 { | 57 { |
| 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)); | 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)); |
| 61 } | 59 } |
| 62 | 60 |
| 63 static int colorFloatToRGBAByte(float f) | 61 static int colorFloatToRGBAByte(float f) |
| 64 { | 62 { |
| 65 // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's roundi
ng | 63 // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's roundi
ng |
| 66 return max(0, min(static_cast<int>(lroundf(255.0f * f)), 255)); | 64 return std::max(0, std::min(static_cast<int>(lroundf(255.0f * f)), 255)); |
| 67 } | 65 } |
| 68 | 66 |
| 69 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a) | 67 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a) |
| 70 { | 68 { |
| 71 return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | color
FloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b); | 69 return colorFloatToRGBAByte(a) << 24 | colorFloatToRGBAByte(r) << 16 | color
FloatToRGBAByte(g) << 8 | colorFloatToRGBAByte(b); |
| 72 } | 70 } |
| 73 | 71 |
| 74 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha) | 72 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha) |
| 75 { | 73 { |
| 76 RGBA32 rgbOnly = color & 0x00FFFFFF; | 74 RGBA32 rgbOnly = color & 0x00FFFFFF; |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 { | 282 { |
| 285 // Hardcode this common case for speed. | 283 // Hardcode this common case for speed. |
| 286 if (m_color == black) | 284 if (m_color == black) |
| 287 return lightenedBlack; | 285 return lightenedBlack; |
| 288 | 286 |
| 289 const float scaleFactor = nextafterf(256.0f, 0.0f); | 287 const float scaleFactor = nextafterf(256.0f, 0.0f); |
| 290 | 288 |
| 291 float r, g, b, a; | 289 float r, g, b, a; |
| 292 getRGBA(r, g, b, a); | 290 getRGBA(r, g, b, a); |
| 293 | 291 |
| 294 float v = max(r, max(g, b)); | 292 float v = std::max(r, std::max(g, b)); |
| 295 | 293 |
| 296 if (v == 0.0f) | 294 if (v == 0.0f) |
| 297 // Lightened black with alpha. | 295 // Lightened black with alpha. |
| 298 return Color(0x54, 0x54, 0x54, alpha()); | 296 return Color(0x54, 0x54, 0x54, alpha()); |
| 299 | 297 |
| 300 float multiplier = min(1.0f, v + 0.33f) / v; | 298 float multiplier = std::min(1.0f, v + 0.33f) / v; |
| 301 | 299 |
| 302 return Color(static_cast<int>(multiplier * r * scaleFactor), | 300 return Color(static_cast<int>(multiplier * r * scaleFactor), |
| 303 static_cast<int>(multiplier * g * scaleFactor), | 301 static_cast<int>(multiplier * g * scaleFactor), |
| 304 static_cast<int>(multiplier * b * scaleFactor), | 302 static_cast<int>(multiplier * b * scaleFactor), |
| 305 alpha()); | 303 alpha()); |
| 306 } | 304 } |
| 307 | 305 |
| 308 Color Color::dark() const | 306 Color Color::dark() const |
| 309 { | 307 { |
| 310 // Hardcode this common case for speed. | 308 // Hardcode this common case for speed. |
| 311 if (m_color == white) | 309 if (m_color == white) |
| 312 return darkenedWhite; | 310 return darkenedWhite; |
| 313 | 311 |
| 314 const float scaleFactor = nextafterf(256.0f, 0.0f); | 312 const float scaleFactor = nextafterf(256.0f, 0.0f); |
| 315 | 313 |
| 316 float r, g, b, a; | 314 float r, g, b, a; |
| 317 getRGBA(r, g, b, a); | 315 getRGBA(r, g, b, a); |
| 318 | 316 |
| 319 float v = max(r, max(g, b)); | 317 float v = std::max(r, std::max(g, b)); |
| 320 float multiplier = max(0.0f, (v - 0.33f) / v); | 318 float multiplier = std::max(0.0f, (v - 0.33f) / v); |
| 321 | 319 |
| 322 return Color(static_cast<int>(multiplier * r * scaleFactor), | 320 return Color(static_cast<int>(multiplier * r * scaleFactor), |
| 323 static_cast<int>(multiplier * g * scaleFactor), | 321 static_cast<int>(multiplier * g * scaleFactor), |
| 324 static_cast<int>(multiplier * b * scaleFactor), | 322 static_cast<int>(multiplier * b * scaleFactor), |
| 325 alpha()); | 323 alpha()); |
| 326 } | 324 } |
| 327 | 325 |
| 328 Color Color::combineWithAlpha(float otherAlpha) const | 326 Color Color::combineWithAlpha(float otherAlpha) const |
| 329 { | 327 { |
| 330 return colorWithOverrideAlpha(rgb(), (alpha() / 255.f) * otherAlpha); | 328 return colorWithOverrideAlpha(rgb(), (alpha() / 255.f) * otherAlpha); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 (color.green() * alpha + 254) / 255, | 454 (color.green() * alpha + 254) / 255, |
| 457 (color.blue() * alpha + 254) / 255, | 455 (color.blue() * alpha + 254) / 255, |
| 458 alpha).rgb(); | 456 alpha).rgb(); |
| 459 } else | 457 } else |
| 460 pixelColor = color.rgb(); | 458 pixelColor = color.rgb(); |
| 461 | 459 |
| 462 return pixelColor; | 460 return pixelColor; |
| 463 } | 461 } |
| 464 | 462 |
| 465 } // namespace blink | 463 } // namespace blink |
| OLD | NEW |