Skip to content

Commit

Permalink
Implement AccentColor and AccentColorText system colors
Browse files Browse the repository at this point in the history
This was proposed and resolved here:
w3c/csswg-drafts#7347

This patch matches the firefox behavior. We don't currently have a way
of getting custom accent colors on any platform besides MacOS, and it
would seem that firefox doesn't do so either.

This is gated behind an experimental flag for now.

Bug: 1338061
Change-Id: I97393303f5b6fb141acaaf525d9f353bea49a604
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3764721
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Reviewed-by: Mason Freed <masonf@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1098956}
  • Loading branch information
josepharhar authored and Chromium LUCI CQ committed Jan 30, 2023
1 parent f2f9b99 commit f14e290
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 26 deletions.
2 changes: 2 additions & 0 deletions third_party/blink/renderer/core/css/css.dict
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,8 @@
"spelling-error"
"grammar-error"
"color-contrast"
"accentcolor"
"accentcolortext"

# at-rules
"@charset"
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/core/css/css_value_keywords.json5
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@
"transparent",
"-webkit-link",
"-webkit-activelink",
"accentcolor",
"accentcolortext",
"activeborder",
"activecaption",
"activetext",
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/core/css/style_color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ bool StyleColor::IsSystemColorIncludingDeprecated(CSSValueID id) {

bool StyleColor::IsSystemColor(CSSValueID id) {
switch (id) {
case CSSValueID::kAccentcolor:
case CSSValueID::kAccentcolortext:
case CSSValueID::kActivetext:
case CSSValueID::kButtonborder:
case CSSValueID::kButtonface:
Expand Down
26 changes: 26 additions & 0 deletions third_party/blink/renderer/core/layout/layout_theme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,14 @@ Color LayoutTheme::DefaultSystemColor(
// https://www.w3.org/TR/css-color-4/#deprecated-system-colors.

switch (css_value_id) {
case CSSValueID::kAccentcolor:
return RuntimeEnabledFeatures::CSSSystemAccentColorEnabled()
? GetAccentColorOrDefault(color_scheme)
: Color();
case CSSValueID::kAccentcolortext:
return RuntimeEnabledFeatures::CSSSystemAccentColorEnabled()
? GetAccentColorText(color_scheme)
: Color();
case CSSValueID::kActivetext:
return Color::FromRGBA32(0xFFFF0000);
case CSSValueID::kButtonborder:
Expand Down Expand Up @@ -844,4 +852,22 @@ void LayoutTheme::UpdateForcedColorsState() {
ForcedColors::kNone;
}

Color LayoutTheme::GetAccentColorOrDefault(
mojom::blink::ColorScheme color_scheme) const {
// This is from the kAccent color from NativeThemeBase::GetControlColor
const Color kDefaultAccentColor = Color(0x00, 0x75, 0xFF);
Color accent_color = GetSystemAccentColor(color_scheme);
return accent_color == Color() ? kDefaultAccentColor : accent_color;
}

Color LayoutTheme::GetAccentColorText(
mojom::blink::ColorScheme color_scheme) const {
Color accent_color = GetAccentColorOrDefault(color_scheme);
// This logic matches AccentColorText in Firefox. If the accent color to draw
// text on is dark, then use white. If it's light, then use dark.
return color_utils::GetRelativeLuminance4f(accent_color.toSkColor4f()) <= 128
? Color::kWhite
: Color::kBlack;
}

} // namespace blink
11 changes: 10 additions & 1 deletion third_party/blink/renderer/core/layout/layout_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,18 @@ class CORE_EXPORT LayoutTheme : public RefCounted<LayoutTheme> {
mojom::blink::ColorScheme color_scheme) const {
return false;
}
virtual Color GetAccentColor(mojom::blink::ColorScheme color_scheme) const {
// GetSystemAccentColor returns transparent unless there is a special value
// from the OS color scheme.
virtual Color GetSystemAccentColor(
mojom::blink::ColorScheme color_scheme) const {
return Color();
}
// GetAccentColorOrDefault will return GetAccentColor if there is a value from
// the OS, otherwise it will return the default accent color.
Color GetAccentColorOrDefault(mojom::blink::ColorScheme color_scheme) const;
// GetAccentColorText returns black or white depending on which can be
// rendered with enough contrast on the result of GetAccentColorOrDefault.
Color GetAccentColorText(mojom::blink::ColorScheme color_scheme) const;

bool InForcedColorsMode() const { return in_forced_colors_mode_; }

Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/layout/layout_theme_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LayoutThemeMac final : public LayoutThemeDefault {
bool SupportsSelectionForegroundColors() const override { return false; }
bool IsAccentColorCustomized(
mojom::blink::ColorScheme color_scheme) const override;
Color GetAccentColor(mojom::blink::ColorScheme color_scheme) const override;
Color GetSystemAccentColor(mojom::blink::ColorScheme color_scheme) const override;

protected:
// Controls color values returned from FocusRingColor().
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/layout/layout_theme_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Color GetSystemColor(MacSystemColorID color_id,
return true;
}

Color LayoutThemeMac::GetAccentColor(
Color LayoutThemeMac::GetSystemAccentColor(
mojom::blink::ColorScheme color_scheme) const {
if (@available(macOS 10.14, *)) {
return GetSystemColor(MacSystemColorID::kControlAccentColor, color_scheme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ absl::optional<SkColor> GetAccentColor(const ComputedStyle& style) {
mojom::blink::ColorScheme color_scheme = style.UsedColorScheme();
LayoutTheme& layout_theme = LayoutTheme::GetTheme();
if (layout_theme.IsAccentColorCustomized(color_scheme)) {
return layout_theme.GetAccentColor(color_scheme).Rgb();
return layout_theme.GetSystemAccentColor(color_scheme).Rgb();
}

return absl::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@
name: "CSSStyleQueries",
status: "stable"
},
{
name: "CSSSystemAccentColor",
status: "experimental",
},
// Support for CSS Toggles, https://tabatkins.github.io/css-toggle/
{
name: "CSSToggles",
Expand Down

This file was deleted.

0 comments on commit f14e290

Please sign in to comment.