Commit c643d9b7 authored by Bruce Dawson's avatar Bruce Dawson Committed by Commit Bot

Use float math in color_utils.cc

Switch from double to float in color_utils.cc. The HSL struct and
CalculateBoringScore still use double in their API because changing
those requires enough changes to justify being done in a different CL.

Note that SkColorToRgbaString intentionally uses a double precision
constant for 255 so that the calculation is done to double precision
so that spurious rounding in the result string is avoided.

Bug: 863135
Change-Id: Id5b678ed6e1c40475ec516bde2c902ba27bb783d
Reviewed-on: https://chromium-review.googlesource.com/1135550
Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578543}
parent a5767cd6
...@@ -198,7 +198,7 @@ std::vector<unsigned char> RenderIconBitmap(const GURL& icon_url, ...@@ -198,7 +198,7 @@ std::vector<unsigned char> RenderIconBitmap(const GURL& icon_url,
// If luminance is too high, the white text will become unreadable. Invert // If luminance is too high, the white text will become unreadable. Invert
// the background color to achieve better constrast. The constant comes // the background color to achieve better constrast. The constant comes
// W3C Accessibility standards. // W3C Accessibility standards.
if (color_utils::GetRelativeLuminance(background_color) > 0.179) if (color_utils::GetRelativeLuminance(background_color) > 0.179f)
background_color = color_utils::InvertColor(background_color); background_color = color_utils::InvertColor(background_color);
DrawCircleInCanvas(&canvas, size, background_color); DrawCircleInCanvas(&canvas, size, background_color);
......
...@@ -673,16 +673,16 @@ SkColor ThemeService::GetSeparatorColor(SkColor tab_color, ...@@ -673,16 +673,16 @@ SkColor ThemeService::GetSeparatorColor(SkColor tab_color,
// However, if the frame is already very dark or very light, respectively, // However, if the frame is already very dark or very light, respectively,
// this won't contrast sufficiently with the frame color, so we'll need to // this won't contrast sufficiently with the frame color, so we'll need to
// reverse when we're lightening and darkening. // reverse when we're lightening and darkening.
const double tab_luminance = color_utils::GetRelativeLuminance(tab_color); const float tab_luminance = color_utils::GetRelativeLuminance(tab_color);
const double frame_luminance = color_utils::GetRelativeLuminance(frame_color); const float frame_luminance = color_utils::GetRelativeLuminance(frame_color);
const bool lighten = tab_luminance < frame_luminance; const bool lighten = tab_luminance < frame_luminance;
SkColor separator_color = lighten ? SK_ColorWHITE : SK_ColorBLACK; SkColor separator_color = lighten ? SK_ColorWHITE : SK_ColorBLACK;
double separator_luminance = color_utils::GetRelativeLuminance( float separator_luminance = color_utils::GetRelativeLuminance(
color_utils::AlphaBlend(separator_color, frame_color, kAlpha)); color_utils::AlphaBlend(separator_color, frame_color, kAlpha));
// The minimum contrast ratio here is just under the ~1.1469 in the default MD // The minimum contrast ratio here is just under the ~1.1469 in the default MD
// incognito theme. We want the separator to still darken the frame in that // incognito theme. We want the separator to still darken the frame in that
// theme, but that's about as low of contrast as we're willing to accept. // theme, but that's about as low of contrast as we're willing to accept.
const double kMinContrastRatio = 1.1465; const float kMinContrastRatio = 1.1465f;
if (color_utils::GetContrastRatio(separator_luminance, frame_luminance) >= if (color_utils::GetContrastRatio(separator_luminance, frame_luminance) >=
kMinContrastRatio) kMinContrastRatio)
return SkColorSetA(separator_color, kAlpha); return SkColorSetA(separator_color, kAlpha);
...@@ -700,7 +700,7 @@ SkColor ThemeService::GetSeparatorColor(SkColor tab_color, ...@@ -700,7 +700,7 @@ SkColor ThemeService::GetSeparatorColor(SkColor tab_color,
// The reversed separator doesn't contrast enough with the tab. Compute the // The reversed separator doesn't contrast enough with the tab. Compute the
// resulting luminance from adjusting the tab color, instead of the frame // resulting luminance from adjusting the tab color, instead of the frame
// color, by the separator color. // color, by the separator color.
const double target_luminance = color_utils::GetRelativeLuminance( const float target_luminance = color_utils::GetRelativeLuminance(
color_utils::AlphaBlend(separator_color, tab_color, kAlpha)); color_utils::AlphaBlend(separator_color, tab_color, kAlpha));
// Now try to compute an alpha for the separator such that, when blended with // Now try to compute an alpha for the separator such that, when blended with
...@@ -709,7 +709,7 @@ SkColor ThemeService::GetSeparatorColor(SkColor tab_color, ...@@ -709,7 +709,7 @@ SkColor ThemeService::GetSeparatorColor(SkColor tab_color,
// possible range of alpha values. // possible range of alpha values.
SkAlpha alpha = 128; SkAlpha alpha = 128;
for (int delta = lighten ? 64 : -64; delta != 0; delta /= 2) { for (int delta = lighten ? 64 : -64; delta != 0; delta /= 2) {
const double luminance = color_utils::GetRelativeLuminance( const float luminance = color_utils::GetRelativeLuminance(
color_utils::AlphaBlend(separator_color, frame_color, alpha)); color_utils::AlphaBlend(separator_color, frame_color, alpha));
if (luminance == target_luminance) if (luminance == target_luminance)
break; break;
......
...@@ -456,7 +456,7 @@ TEST_F(ThemeServiceTest, SeparatorColor) { ...@@ -456,7 +456,7 @@ TEST_F(ThemeServiceTest, SeparatorColor) {
EXPECT_EQ(theme_color, separator_color); EXPECT_EQ(theme_color, separator_color);
// For the default theme, the separator should darken the frame. // For the default theme, the separator should darken the frame.
double frame_luminance = color_utils::GetRelativeLuminance(frame_color); float frame_luminance = color_utils::GetRelativeLuminance(frame_color);
EXPECT_LT(color_utils::GetRelativeLuminance(separator_color), EXPECT_LT(color_utils::GetRelativeLuminance(separator_color),
frame_luminance); frame_luminance);
...@@ -466,8 +466,8 @@ TEST_F(ThemeServiceTest, SeparatorColor) { ...@@ -466,8 +466,8 @@ TEST_F(ThemeServiceTest, SeparatorColor) {
// "tab" (frame color) since otherwise the contrast the contrast with the // "tab" (frame color) since otherwise the contrast the contrast with the
// "tab color" would be too minimal. // "tab color" would be too minimal.
separator_color = GetSeparatorColor(frame_color, tab_color); separator_color = GetSeparatorColor(frame_color, tab_color);
double tab_luminance = color_utils::GetRelativeLuminance(tab_color); float tab_luminance = color_utils::GetRelativeLuminance(tab_color);
double separator_luminance = float separator_luminance =
color_utils::GetRelativeLuminance(separator_color); color_utils::GetRelativeLuminance(separator_color);
EXPECT_LT(separator_luminance, tab_luminance); EXPECT_LT(separator_luminance, tab_luminance);
EXPECT_LT(separator_luminance, frame_luminance); EXPECT_LT(separator_luminance, frame_luminance);
...@@ -513,7 +513,7 @@ TEST_F(ThemeServiceTest, SeparatorColor) { ...@@ -513,7 +513,7 @@ TEST_F(ThemeServiceTest, SeparatorColor) {
// And if we reverse the colors, the separator should lighten the "frame" // And if we reverse the colors, the separator should lighten the "frame"
// (tab color). // (tab color).
separator_color = GetSeparatorColor(frame_color, tab_color); separator_color = GetSeparatorColor(frame_color, tab_color);
double tab_luminance = color_utils::GetRelativeLuminance(tab_color); float tab_luminance = color_utils::GetRelativeLuminance(tab_color);
EXPECT_GT(color_utils::GetRelativeLuminance(separator_color), EXPECT_GT(color_utils::GetRelativeLuminance(separator_color),
tab_luminance); tab_luminance);
...@@ -521,7 +521,7 @@ TEST_F(ThemeServiceTest, SeparatorColor) { ...@@ -521,7 +521,7 @@ TEST_F(ThemeServiceTest, SeparatorColor) {
// should also be lighter than the tab color since otherwise the contrast // should also be lighter than the tab color since otherwise the contrast
// with the tab would be too minimal. // with the tab would be too minimal.
separator_color = GetSeparatorColor(tab_color, SK_ColorBLACK); separator_color = GetSeparatorColor(tab_color, SK_ColorBLACK);
double separator_luminance = float separator_luminance =
color_utils::GetRelativeLuminance(separator_color); color_utils::GetRelativeLuminance(separator_color);
EXPECT_GT(separator_luminance, 0); EXPECT_GT(separator_luminance, 0);
EXPECT_GT(separator_luminance, tab_luminance); EXPECT_GT(separator_luminance, tab_luminance);
......
...@@ -414,7 +414,7 @@ std::unique_ptr<views::View> CreateShippingOptionLabel( ...@@ -414,7 +414,7 @@ std::unique_ptr<views::View> CreateShippingOptionLabel(
} }
SkColor GetForegroundColorForBackground(SkColor background_color) { SkColor GetForegroundColorForBackground(SkColor background_color) {
constexpr double kLightForegroundRatioThreshold = 3; constexpr float kLightForegroundRatioThreshold = 3;
if (background_color != 0 && if (background_color != 0 &&
color_utils::GetContrastRatio(background_color, SK_ColorWHITE) >= color_utils::GetContrastRatio(background_color, SK_ColorWHITE) >=
kLightForegroundRatioThreshold) { kLightForegroundRatioThreshold) {
......
This diff is collapsed.
...@@ -23,18 +23,18 @@ struct HSL { ...@@ -23,18 +23,18 @@ struct HSL {
// The minimum contrast between text and background that is still readable. // The minimum contrast between text and background that is still readable.
// This value is taken from w3c accessibility guidelines. // This value is taken from w3c accessibility guidelines.
constexpr double kMinimumReadableContrastRatio = 4.5; constexpr float kMinimumReadableContrastRatio = 4.5f;
// Determines the contrast ratio of two colors or two relative luminance values // Determines the contrast ratio of two colors or two relative luminance values
// (as computed by RelativeLuminance()), calculated according to // (as computed by RelativeLuminance()), calculated according to
// http://www.w3.org/TR/WCAG20/#contrast-ratiodef . // http://www.w3.org/TR/WCAG20/#contrast-ratiodef .
GFX_EXPORT double GetContrastRatio(SkColor color_a, SkColor color_b); GFX_EXPORT float GetContrastRatio(SkColor color_a, SkColor color_b);
GFX_EXPORT double GetContrastRatio(double luminance_a, double luminance_b); GFX_EXPORT float GetContrastRatio(float luminance_a, float luminance_b);
// The relative luminance of |color|, that is, the weighted sum of the // The relative luminance of |color|, that is, the weighted sum of the
// linearized RGB components, normalized to 0..1, per BT.709. See // linearized RGB components, normalized to 0..1, per BT.709. See
// http://www.w3.org/TR/WCAG20/#relativeluminancedef . // http://www.w3.org/TR/WCAG20/#relativeluminancedef .
GFX_EXPORT double GetRelativeLuminance(SkColor color); GFX_EXPORT float GetRelativeLuminance(SkColor color);
// The luma of |color|, that is, the weighted sum of the gamma-compressed R'G'B' // The luma of |color|, that is, the weighted sum of the gamma-compressed R'G'B'
// components, per BT.601, a.k.a. the Y' in Y'UV. See // components, per BT.601, a.k.a. the Y' in Y'UV. See
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment