Bug 1882714 [wpt PR 44850] - Update wpt metadata, a=testonly
[gecko.git] / widget / ThemeColors.h
blob739a7ca0f0ff4d1647c5c204b49ba7cc157c76fd
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(ColorSchemeForWidget(aFrame, aAppearance, mHighContrastInfo)),
66 mAccentColor(*aFrame->Style(), mColorScheme) {}
67 virtual ~ThemeColors() = default;
69 [[nodiscard]] static float ScaleLuminanceBy(float aLuminance, float aFactor) {
70 return aLuminance >= 0.18f ? aLuminance * aFactor : aLuminance / aFactor;
73 const AccentColor& Accent() const { return mAccentColor; }
74 bool HighContrast() const { return mHighContrastInfo.mHighContrast; }
75 bool IsDark() const { return mColorScheme == ColorScheme::Dark; }
77 nscolor SystemNs(StyleSystemColor aColor) const {
78 return LookAndFeel::Color(aColor, mColorScheme,
79 LookAndFeel::ShouldUseStandins(mDoc, aColor));
82 sRGBColor System(StyleSystemColor aColor) const {
83 return sRGBColor::FromABGR(SystemNs(aColor));
86 template <typename Compute>
87 sRGBColor SystemOrElse(StyleSystemColor aColor, Compute aCompute) const {
88 if (auto color = LookAndFeel::GetColor(
89 aColor, mColorScheme,
90 LookAndFeel::ShouldUseStandins(mDoc, aColor))) {
91 return sRGBColor::FromABGR(*color);
93 return aCompute();
96 std::pair<sRGBColor, sRGBColor> SystemPair(StyleSystemColor aFirst,
97 StyleSystemColor aSecond) const {
98 return std::make_pair(System(aFirst), System(aSecond));
101 // Whether we should use system colors (for high contrast mode).
102 static HighContrastInfo ShouldBeHighContrast(const nsPresContext&);
103 static ColorScheme ColorSchemeForWidget(const nsIFrame*, StyleAppearance,
104 const HighContrastInfo&);
106 static void RecomputeAccentColors();
108 static nscolor ComputeCustomAccentForeground(nscolor aColor);
110 static nscolor AdjustUnthemedScrollbarThumbColor(nscolor aFaceColor,
111 dom::ElementState aStates);
114 } // namespace mozilla::widget
116 #endif