1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "ThemeColors.h"
8 #include "mozilla/RelativeLuminanceUtils.h"
9 #include "mozilla/StaticPrefs_widget.h"
10 #include "ThemeDrawing.h"
11 #include "nsNativeTheme.h"
13 using namespace mozilla::gfx
;
15 namespace mozilla::widget
{
18 ColorPalette(nscolor aAccent
, nscolor aForeground
);
20 constexpr ColorPalette(sRGBColor aAccent
, sRGBColor aForeground
,
21 sRGBColor aLight
, sRGBColor aDark
, sRGBColor aDarker
)
23 mForeground(aForeground
),
26 mAccentDarker(aDarker
) {}
28 constexpr static ColorPalette
Default() {
30 sDefaultAccent
, sDefaultAccentText
,
31 sRGBColor::UnusualFromARGB(0x4d008deb), // Luminance: 25.04791%
32 sRGBColor::UnusualFromARGB(0xff0250bb), // Luminance: 9.33808%
33 sRGBColor::UnusualFromARGB(0xff054096) // Luminance: 5.90106%
37 // Ensure accent color is opaque by blending with white. This serves two
38 // purposes: On one hand, it avoids surprises if we overdraw. On the other, it
39 // makes our math below make more sense, as we want to match the browser
40 // style, which has an opaque accent color.
41 static nscolor
EnsureOpaque(nscolor aAccent
) {
42 if (NS_GET_A(aAccent
) != 0xff) {
43 return NS_ComposeColors(NS_RGB(0xff, 0xff, 0xff), aAccent
);
48 static nscolor
GetLight(nscolor aAccent
) {
49 // The luminance from the light color divided by the one of the accent color
50 // in the default palette.
51 constexpr float kLightLuminanceScale
= 25.048f
/ 13.693f
;
52 const float lightLuminanceAdjust
= ThemeColors::ScaleLuminanceBy(
53 RelativeLuminanceUtils::Compute(aAccent
), kLightLuminanceScale
);
55 RelativeLuminanceUtils::Adjust(aAccent
, lightLuminanceAdjust
);
56 return NS_RGBA(NS_GET_R(lightColor
), NS_GET_G(lightColor
),
57 NS_GET_B(lightColor
), 0x4d);
60 static nscolor
GetDark(nscolor aAccent
) {
61 // Same deal as above (but without the alpha).
62 constexpr float kDarkLuminanceScale
= 9.338f
/ 13.693f
;
63 const float darkLuminanceAdjust
= ThemeColors::ScaleLuminanceBy(
64 RelativeLuminanceUtils::Compute(aAccent
), kDarkLuminanceScale
);
65 return RelativeLuminanceUtils::Adjust(aAccent
, darkLuminanceAdjust
);
68 static nscolor
GetDarker(nscolor aAccent
) {
69 // Same deal as above.
70 constexpr float kDarkerLuminanceScale
= 5.901f
/ 13.693f
;
71 const float darkerLuminanceAdjust
= ThemeColors::ScaleLuminanceBy(
72 RelativeLuminanceUtils::Compute(aAccent
), kDarkerLuminanceScale
);
73 return RelativeLuminanceUtils::Adjust(aAccent
, darkerLuminanceAdjust
);
77 sRGBColor mForeground
;
79 // Note that depending on the exact accent color, lighter/darker might really
81 sRGBColor mAccentLight
;
82 sRGBColor mAccentDark
;
83 sRGBColor mAccentDarker
;
86 static nscolor
GetAccentColor(bool aBackground
, ColorScheme aScheme
) {
87 auto useStandins
= LookAndFeel::UseStandins(
88 !StaticPrefs::widget_non_native_theme_use_theme_accent());
89 return ColorPalette::EnsureOpaque(
90 LookAndFeel::Color(aBackground
? LookAndFeel::ColorID::Accentcolor
91 : LookAndFeel::ColorID::Accentcolortext
,
92 aScheme
, useStandins
));
95 static ColorPalette sDefaultLightPalette
= ColorPalette::Default();
96 static ColorPalette sDefaultDarkPalette
= ColorPalette::Default();
98 ColorPalette::ColorPalette(nscolor aAccent
, nscolor aForeground
) {
99 mAccent
= sRGBColor::FromABGR(aAccent
);
100 mForeground
= sRGBColor::FromABGR(aForeground
);
101 mAccentLight
= sRGBColor::FromABGR(GetLight(aAccent
));
102 mAccentDark
= sRGBColor::FromABGR(GetDark(aAccent
));
103 mAccentDarker
= sRGBColor::FromABGR(GetDarker(aAccent
));
106 ThemeAccentColor::ThemeAccentColor(const ComputedStyle
& aStyle
,
107 ColorScheme aScheme
) {
108 const auto& color
= aStyle
.StyleUI()->mAccentColor
;
109 if (color
.IsColor()) {
110 mAccentColor
.emplace(
111 ColorPalette::EnsureOpaque(color
.AsColor().CalcColor(aStyle
)));
113 MOZ_ASSERT(color
.IsAuto());
114 mDefaultPalette
= aScheme
== ColorScheme::Light
? &sDefaultLightPalette
115 : &sDefaultDarkPalette
;
119 sRGBColor
ThemeAccentColor::Get() const {
121 return mDefaultPalette
->mAccent
;
123 return sRGBColor::FromABGR(*mAccentColor
);
126 sRGBColor
ThemeAccentColor::GetForeground() const {
128 return mDefaultPalette
->mForeground
;
130 return sRGBColor::FromABGR(
131 ThemeColors::ComputeCustomAccentForeground(*mAccentColor
));
134 sRGBColor
ThemeAccentColor::GetLight() const {
136 return mDefaultPalette
->mAccentLight
;
138 return sRGBColor::FromABGR(ColorPalette::GetLight(*mAccentColor
));
141 sRGBColor
ThemeAccentColor::GetDark() const {
143 return mDefaultPalette
->mAccentDark
;
145 return sRGBColor::FromABGR(ColorPalette::GetDark(*mAccentColor
));
148 sRGBColor
ThemeAccentColor::GetDarker() const {
150 return mDefaultPalette
->mAccentDarker
;
152 return sRGBColor::FromABGR(ColorPalette::GetDarker(*mAccentColor
));
155 bool ThemeColors::ShouldBeHighContrast(const nsPresContext
& aPc
) {
156 // We make sure that we're drawing backgrounds, since otherwise layout will
157 // darken our used text colors etc anyways, and that can cause contrast issues
158 // with dark high-contrast themes.
159 return aPc
.GetBackgroundColorDraw() &&
160 PreferenceSheet::PrefsFor(*aPc
.Document())
161 .NonNativeThemeShouldBeHighContrast();
164 ColorScheme
ThemeColors::ColorSchemeForWidget(const nsIFrame
* aFrame
,
165 StyleAppearance aAppearance
,
166 bool aHighContrast
) {
167 if (!nsNativeTheme::IsWidgetScrollbarPart(aAppearance
)) {
168 return LookAndFeel::ColorSchemeForFrame(aFrame
);
170 // Scrollbars are a bit tricky. Their used color-scheme depends on whether the
171 // background they are on is light or dark.
173 // TODO(emilio): This heuristic effectively predates the color-scheme CSS
174 // property. Perhaps we should check whether the style or the document set
175 // `color-scheme` to something that isn't `normal`, and if so go through the
178 return ColorScheme::Light
;
180 if (StaticPrefs::widget_disable_dark_scrollbar()) {
181 return ColorScheme::Light
;
183 return nsNativeTheme::IsDarkBackground(const_cast<nsIFrame
*>(aFrame
))
185 : ColorScheme::Light
;
189 void ThemeColors::RecomputeAccentColors() {
190 MOZ_RELEASE_ASSERT(NS_IsMainThread());
192 sDefaultLightPalette
=
193 ColorPalette(GetAccentColor(true, ColorScheme::Light
),
194 GetAccentColor(false, ColorScheme::Light
));
196 sDefaultDarkPalette
= ColorPalette(GetAccentColor(true, ColorScheme::Dark
),
197 GetAccentColor(false, ColorScheme::Dark
));
201 nscolor
ThemeColors::ComputeCustomAccentForeground(nscolor aColor
) {
202 // Contrast ratio is defined in
203 // https://www.w3.org/TR/WCAG20/#contrast-ratiodef as:
205 // (L1 + 0.05) / (L2 + 0.05)
207 // Where L1 is the lighter color, and L2 is the darker one. So we determine
208 // whether we're dark or light and resolve the equation for the target ratio.
210 // So when lightening:
212 // L1 = k * (L2 + 0.05) - 0.05
214 // And when darkening:
216 // L2 = (L1 + 0.05) / k - 0.05
218 const float luminance
= RelativeLuminanceUtils::Compute(aColor
);
220 // We generally prefer white unless we can't because the color is really light
221 // and we can't provide reasonable contrast.
222 const float ratioWithWhite
= 1.05f
/ (luminance
+ 0.05f
);
223 const bool canBeWhite
=
225 StaticPrefs::layout_css_accent_color_min_contrast_ratio();
227 return NS_RGB(0xff, 0xff, 0xff);
229 const float targetRatio
=
230 StaticPrefs::layout_css_accent_color_darkening_target_contrast_ratio();
231 const float targetLuminance
= (luminance
+ 0.05f
) / targetRatio
- 0.05f
;
232 return RelativeLuminanceUtils::Adjust(aColor
, targetLuminance
);
235 nscolor
ThemeColors::AdjustUnthemedScrollbarThumbColor(
236 nscolor aFaceColor
, dom::ElementState aStates
) {
237 // In Windows 10, scrollbar thumb has the following colors:
239 // State | Color | Luminance
240 // -------+----------+----------
241 // Normal | Gray 205 | 61.0%
242 // Hover | Gray 166 | 38.1%
243 // Active | Gray 96 | 11.7%
245 // This function is written based on the ratios between the values.
246 bool isActive
= aStates
.HasState(dom::ElementState::ACTIVE
);
247 bool isHover
= aStates
.HasState(dom::ElementState::HOVER
);
248 if (!isActive
&& !isHover
) {
251 float luminance
= RelativeLuminanceUtils::Compute(aFaceColor
);
254 luminance
= ScaleLuminanceBy(luminance
, 0.192f
);
257 luminance
= ScaleLuminanceBy(luminance
, 0.625f
);
259 return RelativeLuminanceUtils::Adjust(aFaceColor
, luminance
);
262 } // namespace mozilla::widget