Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / widget / ThemeColors.h
blob0e0fd044bbde927ab84b6b0a68aa3ccd11b5023a
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"
12 #include "nsIFrame.h"
14 namespace mozilla::widget {
16 static constexpr gfx::sRGBColor sDefaultAccent(
17 gfx::sRGBColor::UnusualFromARGB(0xff0060df)); // Luminance: 13.69346%
18 static constexpr gfx::sRGBColor sDefaultAccentText(
19 gfx::sRGBColor::OpaqueWhite());
21 struct ColorPalette;
23 class ThemeAccentColor {
24 protected:
25 using sRGBColor = mozilla::gfx::sRGBColor;
26 using ComputedStyle = mozilla::ComputedStyle;
28 Maybe<nscolor> mAccentColor;
29 const ColorPalette* mDefaultPalette = nullptr;
31 public:
32 explicit ThemeAccentColor(const ComputedStyle&, ColorScheme);
33 virtual ~ThemeAccentColor() = default;
35 sRGBColor Get() const;
36 sRGBColor GetForeground() const;
37 sRGBColor GetLight() const;
38 sRGBColor GetDark() const;
39 sRGBColor GetDarker() const;
42 // Widget color information associated to a particular frame.
43 class ThemeColors {
44 protected:
45 using Document = mozilla::dom::Document;
46 using sRGBColor = mozilla::gfx::sRGBColor;
47 using LookAndFeel = mozilla::LookAndFeel;
48 using StyleSystemColor = mozilla::StyleSystemColor;
49 using AccentColor = ThemeAccentColor;
51 struct HighContrastInfo {
52 bool mHighContrast = false;
53 bool mMustUseLightSystemColors = false;
56 const Document& mDoc;
57 const HighContrastInfo mHighContrastInfo;
58 const ColorScheme mColorScheme;
59 const AccentColor mAccentColor;
61 public:
62 explicit ThemeColors(const nsIFrame* aFrame, StyleAppearance aAppearance)
63 : mDoc(*aFrame->PresContext()->Document()),
64 mHighContrastInfo(ShouldBeHighContrast(*aFrame->PresContext())),
65 mColorScheme(
66 ColorSchemeForWidget(aFrame, aAppearance, mHighContrastInfo)),
67 mAccentColor(*aFrame->Style(), mColorScheme) {}
68 virtual ~ThemeColors() = default;
70 [[nodiscard]] static float ScaleLuminanceBy(float aLuminance, float aFactor) {
71 return aLuminance >= 0.18f ? aLuminance * aFactor : aLuminance / aFactor;
74 const AccentColor& Accent() const { return mAccentColor; }
75 bool HighContrast() const { return mHighContrastInfo.mHighContrast; }
76 bool IsDark() const { return mColorScheme == ColorScheme::Dark; }
78 nscolor SystemNs(StyleSystemColor aColor) const {
79 return LookAndFeel::Color(aColor, mColorScheme,
80 LookAndFeel::ShouldUseStandins(mDoc, aColor));
83 sRGBColor System(StyleSystemColor aColor) const {
84 return sRGBColor::FromABGR(SystemNs(aColor));
87 template <typename Compute>
88 sRGBColor SystemOrElse(StyleSystemColor aColor, Compute aCompute) const {
89 if (auto color = LookAndFeel::GetColor(
90 aColor, mColorScheme,
91 LookAndFeel::ShouldUseStandins(mDoc, aColor))) {
92 return sRGBColor::FromABGR(*color);
94 return aCompute();
97 std::pair<sRGBColor, sRGBColor> SystemPair(StyleSystemColor aFirst,
98 StyleSystemColor aSecond) const {
99 return std::make_pair(System(aFirst), System(aSecond));
102 // Whether we should use system colors (for high contrast mode).
103 static HighContrastInfo ShouldBeHighContrast(const nsPresContext&);
104 static ColorScheme ColorSchemeForWidget(const nsIFrame*, StyleAppearance,
105 const HighContrastInfo&);
107 static void RecomputeAccentColors();
109 static nscolor ComputeCustomAccentForeground(nscolor aColor);
111 static nscolor AdjustUnthemedScrollbarThumbColor(nscolor aFaceColor,
112 dom::ElementState aStates);
115 } // namespace mozilla::widget
117 #endif