summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/gfx/color_utils.cc30
-rw-r--r--app/gfx/color_utils.h13
-rw-r--r--chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc6
-rw-r--r--chrome/browser/views/location_bar_view.cc24
4 files changed, 36 insertions, 37 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);
diff --git a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc
index 7efee67..ea1908e 100644
--- a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc
+++ b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc
@@ -67,13 +67,11 @@ SkColor GetColor(ResultViewState state, ColorKind kind) {
color_utils::AlphaBlend(colors[SELECTED][BACKGROUND],
colors[NORMAL][BACKGROUND], 64);
colors[HOVERED][TEXT] = colors[NORMAL][TEXT];
- const SkColor kDarkURL = SkColorSetRGB(0, 128, 0);
- const SkColor kLightURL = SkColorSetRGB(128, 255, 128);
for (int i = 0; i < NUM_STATES; ++i) {
colors[i][DIMMED_TEXT] =
color_utils::AlphaBlend(colors[i][TEXT], colors[i][BACKGROUND], 128);
- colors[i][URL] = color_utils::PickMoreReadableColor(kDarkURL, kLightURL,
- colors[i][BACKGROUND]);
+ colors[i][URL] = color_utils::GetReadableColor(SkColorSetRGB(0, 128, 0),
+ colors[i][BACKGROUND]);
}
initialized = true;
}
diff --git a/chrome/browser/views/location_bar_view.cc b/chrome/browser/views/location_bar_view.cc
index 4becb2f..0e3e104 100644
--- a/chrome/browser/views/location_bar_view.cc
+++ b/chrome/browser/views/location_bar_view.cc
@@ -238,27 +238,15 @@ SkColor LocationBarView::GetColor(bool is_secure, ColorKind kind) {
colors[SECURE][DEEMPHASIZED_TEXT] =
color_utils::AlphaBlend(colors[SECURE][TEXT],
colors[SECURE][BACKGROUND], 128);
- const SkColor kDarkNotSecureText = SkColorSetRGB(200, 0, 0);
- const SkColor kLightNotSecureText = SkColorSetRGB(255, 55, 55);
- colors[NOT_SECURE][SECURITY_TEXT] =
- color_utils::PickMoreReadableColor(kDarkNotSecureText,
- kLightNotSecureText,
- colors[NOT_SECURE][BACKGROUND]);
+ colors[NOT_SECURE][SECURITY_TEXT] = color_utils::GetReadableColor(
+ SkColorSetRGB(200, 0, 0), colors[NOT_SECURE][BACKGROUND]);
colors[SECURE][SECURITY_TEXT] = SkColorSetRGB(0, 150, 20);
colors[NOT_SECURE][SECURITY_INFO_BUBBLE_TEXT] =
colors[NOT_SECURE][SECURITY_TEXT];
- const SkColor kDarkSecureInfoBubbleText = SkColorSetRGB(0, 153, 51);
- const SkColor kLightSecureInfoBubbleText = SkColorSetRGB(102, 255, 152);
- colors[SECURE][SECURITY_INFO_BUBBLE_TEXT] =
- color_utils::PickMoreReadableColor(kDarkSecureInfoBubbleText,
- kLightSecureInfoBubbleText,
- colors[NOT_SECURE][BACKGROUND]);
- const SkColor kDarkSchemeStrikeout = SkColorSetRGB(210, 0, 0);
- const SkColor kLightSchemeStrikeout = SkColorSetRGB(255, 45, 45);
- colors[NOT_SECURE][SCHEME_STRIKEOUT] =
- color_utils::PickMoreReadableColor(kDarkSchemeStrikeout,
- kLightSchemeStrikeout,
- colors[NOT_SECURE][BACKGROUND]);
+ colors[SECURE][SECURITY_INFO_BUBBLE_TEXT] = color_utils::GetReadableColor(
+ SkColorSetRGB(0, 153, 51), colors[NOT_SECURE][BACKGROUND]);
+ colors[NOT_SECURE][SCHEME_STRIKEOUT] = color_utils::GetReadableColor(
+ SkColorSetRGB(210, 0, 0), colors[NOT_SECURE][BACKGROUND]);
colors[SECURE][SCHEME_STRIKEOUT] = 0; // Unused
initialized = true;
}