diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-03 18:37:44 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-03 18:37:44 +0000 |
commit | 5e06f6a18c8a0414f991bb8758d8d880fc4c1299 (patch) | |
tree | d4ee8d19ef8559da0971f7ffc5913fac06ca84b3 /app | |
parent | 04b336c31ec2ceb208b02fdc55effe405c249177 (diff) | |
download | chromium_src-5e06f6a18c8a0414f991bb8758d8d880fc4c1299.zip chromium_src-5e06f6a18c8a0414f991bb8758d8d880fc4c1299.tar.gz chromium_src-5e06f6a18c8a0414f991bb8758d8d880fc4c1299.tar.bz2 |
Add functions to alpha-blend two colors and pick the more readable of two colors. These will be used to make the UI more readable on non-default themes.
Also removes a function that no one was calling.
BUG=92, 18367
TEST=none
Review URL: http://codereview.chromium.org/190001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25340 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/gfx/color_utils.cc | 43 | ||||
-rw-r--r-- | app/gfx/color_utils.h | 11 |
2 files changed, 50 insertions, 4 deletions
diff --git a/app/gfx/color_utils.cc b/app/gfx/color_utils.cc index 76c17cb..2cacd9b 100644 --- a/app/gfx/color_utils.cc +++ b/app/gfx/color_utils.cc @@ -250,8 +250,47 @@ void BuildLumaHistogram(SkBitmap* bitmap, int histogram[256]) { } } -SkColor SetColorAlpha(SkColor c, SkAlpha alpha) { - return SkColorSetARGB(alpha, SkColorGetR(c), SkColorGetG(c), SkColorGetB(c)); +SkColor AlphaBlend(SkColor foreground, SkColor background, SkAlpha alpha) { + if (alpha == 0) + return background; + else if (alpha == 0xFF) + return foreground; + + return SkColorSetRGB( + ((SkColorGetR(foreground) * alpha) + + (SkColorGetR(background) * (0xFF - alpha))) / 0xFF, + ((SkColorGetG(foreground) * alpha) + + (SkColorGetG(background) * (0xFF - alpha))) / 0xFF, + ((SkColorGetB(foreground) * alpha) + + (SkColorGetB(background) * (0xFF - alpha))) / 0xFF); +} + +// Next three functions' formulas from: +// http://www.w3.org/TR/WCAG20/#relativeluminancedef +// http://www.w3.org/TR/WCAG20/#contrast-ratiodef +static double ConvertSRGB(double eight_bit_component) { + const double component = eight_bit_component / 255.0; + return (component <= 0.03928) ? + (component / 12.92) : pow((component + 0.055) / 1.055, 2.4); +} + +static double RelativeLuminance(SkColor color) { + return (0.2126 * ConvertSRGB(SkColorGetR(color))) + + (0.7152 * ConvertSRGB(SkColorGetG(color))) + + (0.0722 * ConvertSRGB(SkColorGetB(color))); +} + +static 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); +} + +SkColor PickMoreReadableColor(SkColor foreground1, + SkColor foreground2, + SkColor background) { + return (ContrastRatio(foreground1, background) >= + ContrastRatio(foreground2, background)) ? foreground1 : foreground2; } SkColor GetSysSkColor(int which) { diff --git a/app/gfx/color_utils.h b/app/gfx/color_utils.h index f28f003..878561b 100644 --- a/app/gfx/color_utils.h +++ b/app/gfx/color_utils.h @@ -52,8 +52,15 @@ SkColor GetAverageColorOfFavicon(SkBitmap* bitmap, SkAlpha alpha); // this image. void BuildLumaHistogram(SkBitmap* bitmap, int histogram[256]); -// Create a color from a base color and a specific alpha value. -SkColor SetColorAlpha(SkColor c, SkAlpha alpha); +// Returns a blend of the supplied colors, ranging from |background| (for +// |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); // Gets a Windows system color as a SkColor SkColor GetSysSkColor(int which); |