Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Unified Diff: app/gfx/color_utils.cc

Issue 220029: Simplify life for people trying to pick a "readable" foreground color by not ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « app/gfx/color_utils.h ('k') | chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: app/gfx/color_utils.cc
===================================================================
--- app/gfx/color_utils.cc (revision 27031)
+++ app/gfx/color_utils.cc (working copy)
@@ -56,16 +56,23 @@
(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) {
+ 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
+ (foreground_luminance / background_luminance) :
+ (background_luminance / foreground_luminance);
}
} // namespace
@@ -252,11 +259,12 @@
(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) {
« no previous file with comments | « app/gfx/color_utils.h ('k') | chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698