Chromium Code Reviews| 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 <math.h> | 7 #include <math.h> |
| 8 #if defined(OS_WIN) | 8 #if defined(OS_WIN) |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/numerics/safe_conversions.h" | |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
| 18 #include "skia/ext/skia_utils_win.h" | 19 #include "skia/ext/skia_utils_win.h" |
| 19 #endif | 20 #endif |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 | 22 |
| 22 namespace color_utils { | 23 namespace color_utils { |
| 23 | 24 |
| 24 | 25 |
| 25 // Helper functions ----------------------------------------------------------- | 26 // Helper functions ----------------------------------------------------------- |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 (foreground_luminance / background_luminance) : | 71 (foreground_luminance / background_luminance) : |
| 71 (background_luminance / foreground_luminance); | 72 (background_luminance / foreground_luminance); |
| 72 } | 73 } |
| 73 | 74 |
| 74 } // namespace | 75 } // namespace |
| 75 | 76 |
| 76 | 77 |
| 77 // ---------------------------------------------------------------------------- | 78 // ---------------------------------------------------------------------------- |
| 78 | 79 |
| 79 unsigned char GetLuminanceForColor(SkColor color) { | 80 unsigned char GetLuminanceForColor(SkColor color) { |
| 80 int luma = static_cast<int>((0.3 * SkColorGetR(color)) + | 81 return base::saturated_cast<unsigned char>( |
|
danakj
2014/10/18 18:40:06
nice change here
| |
| 81 (0.59 * SkColorGetG(color)) + | 82 (0.3 * SkColorGetR(color)) + |
| 82 (0.11 * SkColorGetB(color))); | 83 (0.59 * SkColorGetG(color)) + |
| 83 return std::max(std::min(luma, 255), 0); | 84 (0.11 * SkColorGetB(color))); |
| 84 } | 85 } |
| 85 | 86 |
| 86 double RelativeLuminance(SkColor color) { | 87 double RelativeLuminance(SkColor color) { |
| 87 return (0.2126 * ConvertSRGB(SkColorGetR(color))) + | 88 return (0.2126 * ConvertSRGB(SkColorGetR(color))) + |
| 88 (0.7152 * ConvertSRGB(SkColorGetG(color))) + | 89 (0.7152 * ConvertSRGB(SkColorGetG(color))) + |
| 89 (0.0722 * ConvertSRGB(SkColorGetB(color))); | 90 (0.0722 * ConvertSRGB(SkColorGetB(color))); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void SkColorToHSL(SkColor c, HSL* hsl) { | 93 void SkColorToHSL(SkColor c, HSL* hsl) { |
| 93 double r = static_cast<double>(SkColorGetR(c)) / 255.0; | 94 double r = static_cast<double>(SkColorGetR(c)) / 255.0; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // If there's no color, we don't care about hue and can do everything based on | 130 // If there's no color, we don't care about hue and can do everything based on |
| 130 // brightness. | 131 // brightness. |
| 131 if (!saturation) { | 132 if (!saturation) { |
| 132 uint8 light; | 133 uint8 light; |
| 133 | 134 |
| 134 if (lightness < 0) | 135 if (lightness < 0) |
| 135 light = 0; | 136 light = 0; |
| 136 else if (lightness >= 1.0) | 137 else if (lightness >= 1.0) |
| 137 light = 255; | 138 light = 255; |
| 138 else | 139 else |
| 139 light = SkDoubleToFixed(lightness) >> 8; | 140 light = static_cast<uint8>(SkDoubleToFixed(lightness) >> 8); |
| 140 | 141 |
| 141 return SkColorSetARGB(alpha, light, light, light); | 142 return SkColorSetARGB(alpha, light, light, light); |
| 142 } | 143 } |
| 143 | 144 |
| 144 double temp2 = (lightness < 0.5) ? | 145 double temp2 = (lightness < 0.5) ? |
| 145 (lightness * (1.0 + saturation)) : | 146 (lightness * (1.0 + saturation)) : |
| 146 (lightness + saturation - (lightness * saturation)); | 147 (lightness + saturation - (lightness * saturation)); |
| 147 double temp1 = 2.0 * lightness - temp2; | 148 double temp1 = 2.0 * lightness - temp2; |
| 148 return SkColorSetARGB(alpha, | 149 return SkColorSetARGB(alpha, |
| 149 calcHue(temp1, temp2, hue + 1.0 / 3.0), | 150 calcHue(temp1, temp2, hue + 1.0 / 3.0), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 : hsl.h >= lower_bound.h && hsl.h <= upper_bound.h; | 192 : hsl.h >= lower_bound.h && hsl.h <= upper_bound.h; |
| 192 return (upper_bound.h < 0 || lower_bound.h < 0 || matches_hue) && | 193 return (upper_bound.h < 0 || lower_bound.h < 0 || matches_hue) && |
| 193 (upper_bound.s < 0 || lower_bound.s < 0 || | 194 (upper_bound.s < 0 || lower_bound.s < 0 || |
| 194 (hsl.s >= lower_bound.s && hsl.s <= upper_bound.s)) && | 195 (hsl.s >= lower_bound.s && hsl.s <= upper_bound.s)) && |
| 195 (upper_bound.l < 0 || lower_bound.l < 0 || | 196 (upper_bound.l < 0 || lower_bound.l < 0 || |
| 196 (hsl.l >= lower_bound.l && hsl.l <= upper_bound.l)); | 197 (hsl.l >= lower_bound.l && hsl.l <= upper_bound.l)); |
| 197 } | 198 } |
| 198 | 199 |
| 199 SkColor HSLShift(SkColor color, const HSL& shift) { | 200 SkColor HSLShift(SkColor color, const HSL& shift) { |
| 200 HSL hsl; | 201 HSL hsl; |
| 201 int alpha = SkColorGetA(color); | 202 SkAlpha alpha = SkColorGetA(color); |
|
danakj
2014/10/18 18:40:06
why not uint8?
Peter Kasting
2014/10/20 23:38:56
SkAlpha is typedefed to uint8, so they should be t
danakj
2014/10/23 15:35:44
uint8 is a familiar type and we all know what it i
Peter Kasting
2014/10/23 17:29:45
Sorry, I forgot the main rationale here when I rep
danakj
2014/10/23 17:43:22
I think we rely on SkColorGet[ARGB]() to return a
| |
| 202 SkColorToHSL(color, &hsl); | 203 SkColorToHSL(color, &hsl); |
| 203 | 204 |
| 204 // Replace the hue with the tint's hue. | 205 // Replace the hue with the tint's hue. |
| 205 if (shift.h >= 0) | 206 if (shift.h >= 0) |
| 206 hsl.h = shift.h; | 207 hsl.h = shift.h; |
| 207 | 208 |
| 208 // Change the saturation. | 209 // Change the saturation. |
| 209 if (shift.s >= 0) { | 210 if (shift.s >= 0) { |
| 210 if (shift.s <= 0.5) | 211 if (shift.s <= 0.5) |
| 211 hsl.s *= shift.s * 2.0; | 212 hsl.s *= shift.s * 2.0; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 SkColor GetSysSkColor(int which) { | 308 SkColor GetSysSkColor(int which) { |
| 308 #if defined(OS_WIN) | 309 #if defined(OS_WIN) |
| 309 return skia::COLORREFToSkColor(GetSysColor(which)); | 310 return skia::COLORREFToSkColor(GetSysColor(which)); |
| 310 #else | 311 #else |
| 311 NOTIMPLEMENTED(); | 312 NOTIMPLEMENTED(); |
| 312 return SK_ColorLTGRAY; | 313 return SK_ColorLTGRAY; |
| 313 #endif | 314 #endif |
| 314 } | 315 } |
| 315 | 316 |
| 316 } // namespace color_utils | 317 } // namespace color_utils |
| OLD | NEW |