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_layout.h"
10 #include "mozilla/StaticPrefs_widget.h"
11 #include "ThemeDrawing.h"
12 #include "nsNativeTheme.h"
14 using namespace mozilla::gfx
;
16 namespace mozilla::widget
{
19 ColorPalette(nscolor aAccent
, nscolor aForeground
);
21 constexpr ColorPalette(sRGBColor aAccent
, sRGBColor aForeground
,
22 sRGBColor aLight
, sRGBColor aDark
, sRGBColor aDarker
)
24 mForeground(aForeground
),
27 mAccentDarker(aDarker
) {}
29 constexpr static ColorPalette
Default() {
31 sDefaultAccent
, sDefaultAccentText
,
32 sRGBColor::UnusualFromARGB(0x4d008deb), // Luminance: 25.04791%
33 sRGBColor::UnusualFromARGB(0xff0250bb), // Luminance: 9.33808%
34 sRGBColor::UnusualFromARGB(0xff054096) // Luminance: 5.90106%
38 // Ensure accent color is opaque by blending with white. This serves two
39 // purposes: On one hand, it avoids surprises if we overdraw. On the other, it
40 // makes our math below make more sense, as we want to match the browser
41 // style, which has an opaque accent color.
42 static nscolor
EnsureOpaque(nscolor aAccent
) {
43 if (NS_GET_A(aAccent
) != 0xff) {
44 return NS_ComposeColors(NS_RGB(0xff, 0xff, 0xff), aAccent
);
49 static nscolor
GetLight(nscolor aAccent
) {
50 // The luminance from the light color divided by the one of the accent color
51 // in the default palette.
52 constexpr float kLightLuminanceScale
= 25.048f
/ 13.693f
;
53 const float lightLuminanceAdjust
= ThemeColors::ScaleLuminanceBy(
54 RelativeLuminanceUtils::Compute(aAccent
), kLightLuminanceScale
);
56 RelativeLuminanceUtils::Adjust(aAccent
, lightLuminanceAdjust
);
57 return NS_RGBA(NS_GET_R(lightColor
), NS_GET_G(lightColor
),
58 NS_GET_B(lightColor
), 0x4d);
61 static nscolor
GetDark(nscolor aAccent
) {
62 // Same deal as above (but without the alpha).
63 constexpr float kDarkLuminanceScale
= 9.338f
/ 13.693f
;
64 const float darkLuminanceAdjust
= ThemeColors::ScaleLuminanceBy(
65 RelativeLuminanceUtils::Compute(aAccent
), kDarkLuminanceScale
);
66 return RelativeLuminanceUtils::Adjust(aAccent
, darkLuminanceAdjust
);
69 static nscolor
GetDarker(nscolor aAccent
) {
70 // Same deal as above.
71 constexpr float kDarkerLuminanceScale
= 5.901f
/ 13.693f
;
72 const float darkerLuminanceAdjust
= ThemeColors::ScaleLuminanceBy(
73 RelativeLuminanceUtils::Compute(aAccent
), kDarkerLuminanceScale
);
74 return RelativeLuminanceUtils::Adjust(aAccent
, darkerLuminanceAdjust
);
78 sRGBColor mForeground
;
80 // Note that depending on the exact accent color, lighter/darker might really
82 sRGBColor mAccentLight
;
83 sRGBColor mAccentDark
;
84 sRGBColor mAccentDarker
;
87 static nscolor
GetAccentColor(bool aBackground
, ColorScheme aScheme
) {
88 auto useStandins
= LookAndFeel::UseStandins(
89 !StaticPrefs::widget_non_native_theme_use_theme_accent());
90 return ColorPalette::EnsureOpaque(
91 LookAndFeel::Color(aBackground
? LookAndFeel::ColorID::Accentcolor
92 : LookAndFeel::ColorID::Accentcolortext
,
93 aScheme
, useStandins
));
96 static ColorPalette sDefaultLightPalette
= ColorPalette::Default();
97 static ColorPalette sDefaultDarkPalette
= ColorPalette::Default();
99 ColorPalette::ColorPalette(nscolor aAccent
, nscolor aForeground
) {
100 mAccent
= sRGBColor::FromABGR(aAccent
);
101 mForeground
= sRGBColor::FromABGR(aForeground
);
102 mAccentLight
= sRGBColor::FromABGR(GetLight(aAccent
));
103 mAccentDark
= sRGBColor::FromABGR(GetDark(aAccent
));
104 mAccentDarker
= sRGBColor::FromABGR(GetDarker(aAccent
));
107 ThemeAccentColor::ThemeAccentColor(const ComputedStyle
& aStyle
,
108 ColorScheme aScheme
) {
109 const auto& color
= aStyle
.StyleUI()->mAccentColor
;
110 if (color
.IsColor()) {
111 mAccentColor
.emplace(
112 ColorPalette::EnsureOpaque(color
.AsColor().CalcColor(aStyle
)));
114 MOZ_ASSERT(color
.IsAuto());
115 mDefaultPalette
= aScheme
== ColorScheme::Light
? &sDefaultLightPalette
116 : &sDefaultDarkPalette
;
120 sRGBColor
ThemeAccentColor::Get() const {
122 return mDefaultPalette
->mAccent
;
124 return sRGBColor::FromABGR(*mAccentColor
);
127 sRGBColor
ThemeAccentColor::GetForeground() const {
129 return mDefaultPalette
->mForeground
;
131 return sRGBColor::FromABGR(
132 ThemeColors::ComputeCustomAccentForeground(*mAccentColor
));
135 sRGBColor
ThemeAccentColor::GetLight() const {
137 return mDefaultPalette
->mAccentLight
;
139 return sRGBColor::FromABGR(ColorPalette::GetLight(*mAccentColor
));
142 sRGBColor
ThemeAccentColor::GetDark() const {
144 return mDefaultPalette
->mAccentDark
;
146 return sRGBColor::FromABGR(ColorPalette::GetDark(*mAccentColor
));
149 sRGBColor
ThemeAccentColor::GetDarker() const {
151 return mDefaultPalette
->mAccentDarker
;
153 return sRGBColor::FromABGR(ColorPalette::GetDarker(*mAccentColor
));
156 bool ThemeColors::ShouldBeHighContrast(const nsPresContext
& aPc
) {
157 // We make sure that we're drawing backgrounds, since otherwise layout will
158 // darken our used text colors etc anyways, and that can cause contrast issues
159 // with dark high-contrast themes.
160 return aPc
.GetBackgroundColorDraw() &&
161 PreferenceSheet::PrefsFor(*aPc
.Document())
162 .NonNativeThemeShouldBeHighContrast();
165 ColorScheme
ThemeColors::ColorSchemeForWidget(const nsIFrame
* aFrame
,
166 StyleAppearance aAppearance
,
167 bool aHighContrast
) {
168 if (!nsNativeTheme::IsWidgetScrollbarPart(aAppearance
)) {
169 return LookAndFeel::ColorSchemeForFrame(aFrame
);
171 // Scrollbars are a bit tricky. Their used color-scheme depends on whether the
172 // background they are on is light or dark.
174 // TODO(emilio): This heuristic effectively predates the color-scheme CSS
175 // property. Perhaps we should check whether the style or the document set
176 // `color-scheme` to something that isn't `normal`, and if so go through the
179 return ColorScheme::Light
;
181 if (StaticPrefs::widget_disable_dark_scrollbar()) {
182 return ColorScheme::Light
;
184 return nsNativeTheme::IsDarkBackgroundForScrollbar(
185 const_cast<nsIFrame
*>(aFrame
))
187 : ColorScheme::Light
;
191 void ThemeColors::RecomputeAccentColors() {
192 MOZ_RELEASE_ASSERT(NS_IsMainThread());
194 sDefaultLightPalette
=
195 ColorPalette(GetAccentColor(true, ColorScheme::Light
),
196 GetAccentColor(false, ColorScheme::Light
));
198 sDefaultDarkPalette
= ColorPalette(GetAccentColor(true, ColorScheme::Dark
),
199 GetAccentColor(false, ColorScheme::Dark
));
203 nscolor
ThemeColors::ComputeCustomAccentForeground(nscolor aColor
) {
204 // Contrast ratio is defined in
205 // https://www.w3.org/TR/WCAG20/#contrast-ratiodef as:
207 // (L1 + 0.05) / (L2 + 0.05)
209 // Where L1 is the lighter color, and L2 is the darker one. So we determine
210 // whether we're dark or light and resolve the equation for the target ratio.
212 // So when lightening:
214 // L1 = k * (L2 + 0.05) - 0.05
216 // And when darkening:
218 // L2 = (L1 + 0.05) / k - 0.05
220 const float luminance
= RelativeLuminanceUtils::Compute(aColor
);
222 // We generally prefer white unless we can't because the color is really light
223 // and we can't provide reasonable contrast.
224 const float ratioWithWhite
= 1.05f
/ (luminance
+ 0.05f
);
225 const bool canBeWhite
=
227 StaticPrefs::layout_css_accent_color_min_contrast_ratio();
229 return NS_RGB(0xff, 0xff, 0xff);
231 const float targetRatio
=
232 StaticPrefs::layout_css_accent_color_darkening_target_contrast_ratio();
233 const float targetLuminance
= (luminance
+ 0.05f
) / targetRatio
- 0.05f
;
234 return RelativeLuminanceUtils::Adjust(aColor
, targetLuminance
);
237 nscolor
ThemeColors::AdjustUnthemedScrollbarThumbColor(
238 nscolor aFaceColor
, dom::ElementState aStates
) {
239 // In Windows 10, scrollbar thumb has the following colors:
241 // State | Color | Luminance
242 // -------+----------+----------
243 // Normal | Gray 205 | 61.0%
244 // Hover | Gray 166 | 38.1%
245 // Active | Gray 96 | 11.7%
247 // This function is written based on the ratios between the values.
248 bool isActive
= aStates
.HasState(dom::ElementState::ACTIVE
);
249 bool isHover
= aStates
.HasState(dom::ElementState::HOVER
);
250 if (!isActive
&& !isHover
) {
253 float luminance
= RelativeLuminanceUtils::Compute(aFaceColor
);
256 luminance
= ScaleLuminanceBy(luminance
, 0.192f
);
259 luminance
= ScaleLuminanceBy(luminance
, 0.625f
);
261 return RelativeLuminanceUtils::Adjust(aFaceColor
, luminance
);
264 } // namespace mozilla::widget