Commit f479e8d7 authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Use color classifier's brightness algorithm for dark theme detection.

This is a follow-up to a comment in https://crrev.com/c/1637036 about
moving IsLight() to within DarkModeColorClassifier().

This change removes IsLight() and makes CalculateColorBrightness() from
the .cc file publicly available via a static method in
DarkModeColorClassifier.

Change-Id: I71497cbd4d6776134debade82960df7af01b1157
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1755054Reviewed-by: default avatarStephen Chenney <schenney@chromium.org>
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Cr-Commit-Position: refs/heads/master@{#687368}
parent 623bd293
......@@ -16,20 +16,27 @@
namespace blink {
namespace {
const int kAlphaThreshold = 100;
const int kBrightnessThreshold = 128;
// TODO(https://crbug.com/925949): Add detection and classification of
// background image color. Most sites with dark background images also have a
// dark background color set, so this is less of a priority than it would be
// otherwise.
bool HasLightBackground(const LayoutView& root) {
const ComputedStyle& style = root.StyleRef();
if (style.HasBackground()) {
Color color = style.VisitedDependentColor(GetCSSPropertyBackgroundColor());
return IsLight(color);
}
// If we can't easily determine the background color, default to inverting the
// page.
return true;
if (!style.HasBackground())
return true;
Color color = style.VisitedDependentColor(GetCSSPropertyBackgroundColor());
if (color.Alpha() < kAlphaThreshold)
return true;
return DarkModeColorClassifier::CalculateColorBrightness(color) >
kBrightnessThreshold;
}
DarkMode GetMode(const Settings& frame_settings) {
......
......@@ -9,20 +9,6 @@
namespace blink {
namespace {
// Determine perceived brightness of a color.
//
// Based on this algorithm suggested by the W3:
// https://www.w3.org/TR/AERT/#color-contrast
//
// We don't use HSL or HSV here because perceived brightness is a function of
// hue as well as lightness/value.
int CalculateColorBrightness(const Color& color) {
int weighted_red = color.Red() * 299;
int weighted_green = color.Green() * 587;
int weighted_blue = color.Blue() * 114;
return (weighted_red + weighted_green + weighted_blue) / 1000;
}
class SimpleColorClassifier : public DarkModeColorClassifier {
public:
static std::unique_ptr<SimpleColorClassifier> NeverInvert() {
......@@ -83,25 +69,16 @@ class InvertHighBrightnessColorsClassifier : public DarkModeColorClassifier {
} // namespace
// Values below which a color is considered sufficiently transparent that a
// lighter color behind it would make the final color as seen by the user light.
// TODO(https://crbug.com/925949): This is a placeholder value. It should be
// replaced with a better researched value before launching dark mode.
const float kOpacityThreshold = 0.4;
// TODO(https://crbug.com/925949): Find a better algorithm for this.
bool IsLight(const Color& color) {
// Use the alpha value as the opacity.
float opacity = (color.Alpha() / 255);
// Assume the color is light if the background is sufficiently transparent.
if (opacity < kOpacityThreshold) {
return true;
}
double hue;
double saturation;
double lightness;
color.GetHSL(hue, saturation, lightness);
return lightness > 0.5;
// Based on this algorithm suggested by the W3:
// https://www.w3.org/TR/AERT/#color-contrast
//
// We don't use HSL or HSV here because perceived brightness is a function of
// hue as well as lightness/value.
int DarkModeColorClassifier::CalculateColorBrightness(const Color& color) {
int weighted_red = color.Red() * 299;
int weighted_green = color.Green() * 587;
int weighted_blue = color.Blue() * 114;
return (weighted_red + weighted_green + weighted_blue) / 1000;
}
std::unique_ptr<DarkModeColorClassifier>
......
......@@ -14,10 +14,11 @@
namespace blink {
bool PLATFORM_EXPORT IsLight(const Color& color);
class PLATFORM_EXPORT DarkModeColorClassifier {
public:
// Determine perceived brightness of a color.
static int CalculateColorBrightness(const Color& color);
static std::unique_ptr<DarkModeColorClassifier> MakeTextColorClassifier(
const DarkModeSettings& settings);
static std::unique_ptr<DarkModeColorClassifier> MakeBackgroundColorClassifier(
......
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