Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/gfx/color_utils.h" | 5 #include "app/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 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 // Next three functions' formulas from: | 49 // Next three functions' formulas from: |
| 50 // http://www.w3.org/TR/WCAG20/#relativeluminancedef | 50 // http://www.w3.org/TR/WCAG20/#relativeluminancedef |
| 51 // http://www.w3.org/TR/WCAG20/#contrast-ratiodef | 51 // http://www.w3.org/TR/WCAG20/#contrast-ratiodef |
| 52 | 52 |
| 53 double ConvertSRGB(double eight_bit_component) { | 53 double ConvertSRGB(double eight_bit_component) { |
| 54 const double component = eight_bit_component / 255.0; | 54 const double component = eight_bit_component / 255.0; |
| 55 return (component <= 0.03928) ? | 55 return (component <= 0.03928) ? |
| 56 (component / 12.92) : pow((component + 0.055) / 1.055, 2.4); | 56 (component / 12.92) : pow((component + 0.055) / 1.055, 2.4); |
| 57 } | 57 } |
| 58 | 58 |
| 59 SkColor LumaInvertColor(const SkColor& color) { | |
| 60 HSL hsl; | |
| 61 SkColorToHSL(color, &hsl); | |
| 62 hsl.l = 1.0 - hsl.l; | |
| 63 return HSLToSkColor(hsl, 255); | |
| 64 } | |
| 65 | |
| 59 double RelativeLuminance(SkColor color) { | 66 double RelativeLuminance(SkColor color) { |
| 60 return (0.2126 * ConvertSRGB(SkColorGetR(color))) + | 67 return (0.2126 * ConvertSRGB(SkColorGetR(color))) + |
| 61 (0.7152 * ConvertSRGB(SkColorGetG(color))) + | 68 (0.7152 * ConvertSRGB(SkColorGetG(color))) + |
| 62 (0.0722 * ConvertSRGB(SkColorGetB(color))); | 69 (0.0722 * ConvertSRGB(SkColorGetB(color))) + 0.05; |
| 63 } | 70 } |
| 64 | 71 |
| 65 double ContrastRatio(SkColor color1, SkColor color2) { | 72 double ContrastRatio(double foreground_luminance, double background_luminance) { |
| 66 const double l1 = RelativeLuminance(color1) + 0.05; | 73 return (foreground_luminance > background_luminance) ? |
|
sky
2009/09/24 21:46:32
Do you need to deal with getting 0,0 passed in, wh
Peter Kasting
2009/09/24 21:50:06
In theory yes... in practice the results from Rela
| |
| 67 const double l2 = RelativeLuminance(color2) + 0.05; | 74 (foreground_luminance / background_luminance) : |
| 68 return (l1 > l2) ? (l1 / l2) : (l2 / l1); | 75 (background_luminance / foreground_luminance); |
| 69 } | 76 } |
| 70 | 77 |
| 71 } // namespace | 78 } // namespace |
| 72 | 79 |
| 73 // ---------------------------------------------------------------------------- | 80 // ---------------------------------------------------------------------------- |
| 74 | 81 |
| 75 void SkColorToHSL(SkColor c, HSL* hsl) { | 82 void SkColorToHSL(SkColor c, HSL* hsl) { |
| 76 double r = static_cast<double>(SkColorGetR(c)) / 255.0; | 83 double r = static_cast<double>(SkColorGetR(c)) / 255.0; |
| 77 double g = static_cast<double>(SkColorGetG(c)) / 255.0; | 84 double g = static_cast<double>(SkColorGetG(c)) / 255.0; |
| 78 double b = static_cast<double>(SkColorGetB(c)) / 255.0; | 85 double b = static_cast<double>(SkColorGetB(c)) / 255.0; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 return foreground; | 252 return foreground; |
| 246 return SkColorSetRGB( | 253 return SkColorSetRGB( |
| 247 ((SkColorGetR(foreground) * alpha) + | 254 ((SkColorGetR(foreground) * alpha) + |
| 248 (SkColorGetR(background) * (255 - alpha))) / 255, | 255 (SkColorGetR(background) * (255 - alpha))) / 255, |
| 249 ((SkColorGetG(foreground) * alpha) + | 256 ((SkColorGetG(foreground) * alpha) + |
| 250 (SkColorGetG(background) * (255 - alpha))) / 255, | 257 (SkColorGetG(background) * (255 - alpha))) / 255, |
| 251 ((SkColorGetB(foreground) * alpha) + | 258 ((SkColorGetB(foreground) * alpha) + |
| 252 (SkColorGetB(background) * (255 - alpha))) / 255); | 259 (SkColorGetB(background) * (255 - alpha))) / 255); |
| 253 } | 260 } |
| 254 | 261 |
| 255 SkColor PickMoreReadableColor(SkColor foreground1, | 262 SkColor GetReadableColor(SkColor foreground, SkColor background) { |
| 256 SkColor foreground2, | 263 const SkColor foreground2 = LumaInvertColor(foreground); |
| 257 SkColor background) { | 264 const double background_luminance = RelativeLuminance(background); |
| 258 return (ContrastRatio(foreground1, background) >= | 265 return (ContrastRatio(RelativeLuminance(foreground), background_luminance) >= |
| 259 ContrastRatio(foreground2, background)) ? foreground1 : foreground2; | 266 ContrastRatio(RelativeLuminance(foreground2), background_luminance)) ? |
| 267 foreground : foreground2; | |
| 260 } | 268 } |
| 261 | 269 |
| 262 SkColor GetSysSkColor(int which) { | 270 SkColor GetSysSkColor(int which) { |
| 263 #if defined(OS_WIN) | 271 #if defined(OS_WIN) |
| 264 return skia::COLORREFToSkColor(GetSysColor(which)); | 272 return skia::COLORREFToSkColor(GetSysColor(which)); |
| 265 #else | 273 #else |
| 266 NOTIMPLEMENTED(); | 274 NOTIMPLEMENTED(); |
| 267 return SK_ColorLTGRAY; | 275 return SK_ColorLTGRAY; |
| 268 #endif | 276 #endif |
| 269 } | 277 } |
| 270 | 278 |
| 271 } // namespace color_utils | 279 } // namespace color_utils |
| OLD | NEW |