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 #ifndef mozilla_widget_ThemeColors_h
7 #define mozilla_widget_ThemeColors_h
9 #include "mozilla/dom/Document.h"
10 #include "mozilla/gfx/Types.h"
11 #include "mozilla/LookAndFeel.h"
14 namespace mozilla::widget
{
16 static constexpr gfx::sRGBColor
sDefaultAccent(
17 gfx::sRGBColor::UnusualFromARGB(0xff0060df)); // Luminance: 13.69346%
18 static constexpr gfx::sRGBColor
sDefaultAccentForeground(
19 gfx::sRGBColor::OpaqueWhite());
21 class ThemeAccentColor
{
23 using sRGBColor
= mozilla::gfx::sRGBColor
;
24 using ComputedStyle
= mozilla::ComputedStyle
;
26 Maybe
<nscolor
> mAccentColor
;
29 explicit ThemeAccentColor(const ComputedStyle
& aStyle
);
30 virtual ~ThemeAccentColor() = default;
32 sRGBColor
Get() const;
33 sRGBColor
GetForeground() const;
34 sRGBColor
GetLight() const;
35 sRGBColor
GetDark() const;
36 sRGBColor
GetDarker() const;
39 // Widget color information associated to a particular frame.
42 using Document
= mozilla::dom::Document
;
43 using sRGBColor
= mozilla::gfx::sRGBColor
;
44 using LookAndFeel
= mozilla::LookAndFeel
;
45 using StyleSystemColor
= mozilla::StyleSystemColor
;
46 using AccentColor
= ThemeAccentColor
;
48 const AccentColor mAccentColor
;
50 const bool mHighContrast
;
51 const LookAndFeel::ColorScheme mColorScheme
;
54 explicit ThemeColors(const nsIFrame
* aFrame
, StyleAppearance aAppearance
)
55 : mAccentColor(*aFrame
->Style()),
56 mDoc(*aFrame
->PresContext()->Document()),
57 mHighContrast(ShouldBeHighContrast(*aFrame
->PresContext())),
58 mColorScheme(ColorSchemeForWidget(aFrame
, aAppearance
, mHighContrast
)) {
60 virtual ~ThemeColors() = default;
62 [[nodiscard
]] static float ScaleLuminanceBy(float aLuminance
, float aFactor
) {
63 return aLuminance
>= 0.18f
? aLuminance
* aFactor
: aLuminance
/ aFactor
;
66 const AccentColor
& Accent() const { return mAccentColor
; }
67 bool HighContrast() const { return mHighContrast
; }
68 bool IsDark() const { return mColorScheme
== LookAndFeel::ColorScheme::Dark
; }
70 nscolor
SystemNs(StyleSystemColor aColor
) const {
71 return LookAndFeel::Color(aColor
, mColorScheme
,
72 LookAndFeel::ShouldUseStandins(mDoc
, aColor
));
75 sRGBColor
System(StyleSystemColor aColor
) const {
76 return sRGBColor::FromABGR(SystemNs(aColor
));
79 template <typename Compute
>
80 sRGBColor
SystemOrElse(StyleSystemColor aColor
, Compute aCompute
) const {
81 if (auto color
= LookAndFeel::GetColor(
83 LookAndFeel::ShouldUseStandins(mDoc
, aColor
))) {
84 return sRGBColor::FromABGR(*color
);
89 std::pair
<sRGBColor
, sRGBColor
> SystemPair(StyleSystemColor aFirst
,
90 StyleSystemColor aSecond
) const {
91 return std::make_pair(System(aFirst
), System(aSecond
));
94 // Whether we should use system colors (for high contrast mode).
95 static bool ShouldBeHighContrast(const nsPresContext
&);
96 static ColorScheme
ColorSchemeForWidget(const nsIFrame
*, StyleAppearance
,
99 static void RecomputeAccentColors();
101 static nscolor
ComputeCustomAccentForeground(nscolor aColor
);
103 static nscolor
AdjustUnthemedScrollbarThumbColor(nscolor aFaceColor
,
104 EventStates aStates
);
107 } // namespace mozilla::widget