| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/color_utils.h" | 5 #include "ui/gfx/color_utils.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/color_palette.h" | 16 #include "ui/gfx/color_palette.h" |
| 17 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 17 | 18 |
| 18 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 19 #include <windows.h> | 20 #include <windows.h> |
| 20 #include "skia/ext/skia_utils_win.h" | 21 #include "skia/ext/skia_utils_win.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 namespace color_utils { | 24 namespace color_utils { |
| 24 | 25 |
| 25 | 26 |
| 26 // Helper functions ----------------------------------------------------------- | 27 // Helper functions ----------------------------------------------------------- |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 } | 128 } |
| 128 | 129 |
| 129 SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha) { | 130 SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha) { |
| 130 double hue = hsl.h; | 131 double hue = hsl.h; |
| 131 double saturation = hsl.s; | 132 double saturation = hsl.s; |
| 132 double lightness = hsl.l; | 133 double lightness = hsl.l; |
| 133 | 134 |
| 134 // If there's no color, we don't care about hue and can do everything based on | 135 // If there's no color, we don't care about hue and can do everything based on |
| 135 // brightness. | 136 // brightness. |
| 136 if (!saturation) { | 137 if (!saturation) { |
| 137 uint8_t light; | 138 const uint8_t light = |
| 138 | 139 base::saturated_cast<uint8_t>(gfx::ToRoundedInt(lightness * 255)); |
| 139 if (lightness < 0) | |
| 140 light = 0; | |
| 141 else if (lightness >= 1.0) | |
| 142 light = 255; | |
| 143 else | |
| 144 light = static_cast<uint8_t>(SkDoubleToFixed(lightness) >> 8); | |
| 145 | |
| 146 return SkColorSetARGB(alpha, light, light, light); | 140 return SkColorSetARGB(alpha, light, light, light); |
| 147 } | 141 } |
| 148 | 142 |
| 149 double temp2 = (lightness < 0.5) ? | 143 double temp2 = (lightness < 0.5) ? |
| 150 (lightness * (1.0 + saturation)) : | 144 (lightness * (1.0 + saturation)) : |
| 151 (lightness + saturation - (lightness * saturation)); | 145 (lightness + saturation - (lightness * saturation)); |
| 152 double temp1 = 2.0 * lightness - temp2; | 146 double temp1 = 2.0 * lightness - temp2; |
| 153 return SkColorSetARGB(alpha, calcHue(temp1, temp2, hue + 1.0 / 3.0), | 147 return SkColorSetARGB(alpha, calcHue(temp1, temp2, hue + 1.0 / 3.0), |
| 154 calcHue(temp1, temp2, hue), | 148 calcHue(temp1, temp2, hue), |
| 155 calcHue(temp1, temp2, hue - 1.0 / 3.0)); | 149 calcHue(temp1, temp2, hue - 1.0 / 3.0)); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 if (IsDark(text_color)) { | 330 if (IsDark(text_color)) { |
| 337 // For black text, this comes out to kChromeIconGrey. | 331 // For black text, this comes out to kChromeIconGrey. |
| 338 return color_utils::AlphaBlend(SK_ColorWHITE, text_color, | 332 return color_utils::AlphaBlend(SK_ColorWHITE, text_color, |
| 339 SkColorGetR(gfx::kChromeIconGrey)); | 333 SkColorGetR(gfx::kChromeIconGrey)); |
| 340 } | 334 } |
| 341 // The dimming is less dramatic when darkening a light color. | 335 // The dimming is less dramatic when darkening a light color. |
| 342 return color_utils::AlphaBlend(SK_ColorBLACK, text_color, 0x33); | 336 return color_utils::AlphaBlend(SK_ColorBLACK, text_color, 0x33); |
| 343 } | 337 } |
| 344 | 338 |
| 345 } // namespace color_utils | 339 } // namespace color_utils |
| OLD | NEW |