diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-24 21:59:38 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-24 21:59:38 +0000 |
commit | 9ddb1e9d3c36bcd5af1a96cd0452fcb3e3c36dc5 (patch) | |
tree | 16c72480277f3c7ecabcc20fe0a6081db3e887b2 /app/gfx | |
parent | 9d7eed94008640d64ce3e6108829509eaa52c4b1 (diff) | |
download | chromium_src-9ddb1e9d3c36bcd5af1a96cd0452fcb3e3c36dc5.zip chromium_src-9ddb1e9d3c36bcd5af1a96cd0452fcb3e3c36dc5.tar.gz chromium_src-9ddb1e9d3c36bcd5af1a96cd0452fcb3e3c36dc5.tar.bz2 |
Simplify life for people trying to pick a "readable" foreground color by not requiring them to provide two choices. This also speeds the call up a tiny bit by not calculating the background luminance twice.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/220029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27138 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/gfx')
-rw-r--r-- | app/gfx/color_utils.cc | 30 | ||||
-rw-r--r-- | app/gfx/color_utils.h | 13 |
2 files changed, 28 insertions, 15 deletions
diff --git a/app/gfx/color_utils.cc b/app/gfx/color_utils.cc index 1d6cfe4..cf8cbd2 100644 --- a/app/gfx/color_utils.cc +++ b/app/gfx/color_utils.cc @@ -56,16 +56,25 @@ double ConvertSRGB(double eight_bit_component) { (component / 12.92) : pow((component + 0.055) / 1.055, 2.4); } +SkColor LumaInvertColor(const SkColor& color) { + HSL hsl; + SkColorToHSL(color, &hsl); + hsl.l = 1.0 - hsl.l; + return HSLToSkColor(hsl, 255); +} + double RelativeLuminance(SkColor color) { return (0.2126 * ConvertSRGB(SkColorGetR(color))) + (0.7152 * ConvertSRGB(SkColorGetG(color))) + - (0.0722 * ConvertSRGB(SkColorGetB(color))); + (0.0722 * ConvertSRGB(SkColorGetB(color))) + 0.05; } -double ContrastRatio(SkColor color1, SkColor color2) { - const double l1 = RelativeLuminance(color1) + 0.05; - const double l2 = RelativeLuminance(color2) + 0.05; - return (l1 > l2) ? (l1 / l2) : (l2 / l1); +double ContrastRatio(double foreground_luminance, double background_luminance) { + // NOTE: Only pass in numbers obtained from RelativeLuminance(), since those + // are guaranteed to be > 0 and thus not cause a divide-by-zero error here. + return (foreground_luminance > background_luminance) ? + (foreground_luminance / background_luminance) : + (background_luminance / foreground_luminance); } } // namespace @@ -252,11 +261,12 @@ SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha) { (SkColorGetB(background) * (255 - alpha))) / 255); } -SkColor PickMoreReadableColor(SkColor foreground1, - SkColor foreground2, - SkColor background) { - return (ContrastRatio(foreground1, background) >= - ContrastRatio(foreground2, background)) ? foreground1 : foreground2; +SkColor GetReadableColor(SkColor foreground, SkColor background) { + const SkColor foreground2 = LumaInvertColor(foreground); + const double background_luminance = RelativeLuminance(background); + return (ContrastRatio(RelativeLuminance(foreground), background_luminance) >= + ContrastRatio(RelativeLuminance(foreground2), background_luminance)) ? + foreground : foreground2; } SkColor GetSysSkColor(int which) { diff --git a/app/gfx/color_utils.h b/app/gfx/color_utils.h index 682062f..bbf2595 100644 --- a/app/gfx/color_utils.h +++ b/app/gfx/color_utils.h @@ -57,11 +57,14 @@ void BuildLumaHistogram(SkBitmap* bitmap, int histogram[256]); // |alpha| == 0) to |foreground| (for |alpha| == 255). SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha); -// Given two possible foreground colors, return the one that is more readable -// over |background|. -SkColor PickMoreReadableColor(SkColor foreground1, - SkColor foreground2, - SkColor background); +// Given a foreground and background color, try to return a foreground color +// that is "readable" over the background color by luma-inverting the foreground +// color and then picking whichever foreground color has higher contrast against +// the background color. +// +// NOTE: This won't do anything but waste time if the supplied foreground color +// has a luma value close to the midpoint (0.5 in the HSL representation). +SkColor GetReadableColor(SkColor foreground, SkColor background); // Gets a Windows system color as a SkColor SkColor GetSysSkColor(int which); |