Bug 1865610: part 1) Add partial support for the "fetchpriority" attribute for loadin...
[gecko.git] / widget / ThemeColors.h
blob706f10a0e363bc8e81e30403e94cd45664a25cea
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 const Document& mDoc;
52 const bool mHighContrast;
53 const ColorScheme mColorScheme;
54 const AccentColor mAccentColor;
56 public:
57 explicit ThemeColors(const nsIFrame* aFrame, StyleAppearance aAppearance)
58 : mDoc(*aFrame->PresContext()->Document()),
59 mHighContrast(ShouldBeHighContrast(*aFrame->PresContext())),
60 mColorScheme(ColorSchemeForWidget(aFrame, aAppearance, mHighContrast)),
61 mAccentColor(*aFrame->Style(), mColorScheme) {}
62 virtual ~ThemeColors() = default;
64 [[nodiscard]] static float ScaleLuminanceBy(float aLuminance, float aFactor) {
65 return aLuminance >= 0.18f ? aLuminance * aFactor : aLuminance / aFactor;
68 const AccentColor& Accent() const { return mAccentColor; }
69 bool HighContrast() const { return mHighContrast; }
70 bool IsDark() const { return mColorScheme == ColorScheme::Dark; }
72 nscolor SystemNs(StyleSystemColor aColor) const {
73 return LookAndFeel::Color(aColor, mColorScheme,
74 LookAndFeel::ShouldUseStandins(mDoc, aColor));
77 sRGBColor System(StyleSystemColor aColor) const {
78 return sRGBColor::FromABGR(SystemNs(aColor));
81 template <typename Compute>
82 sRGBColor SystemOrElse(StyleSystemColor aColor, Compute aCompute) const {
83 if (auto color = LookAndFeel::GetColor(
84 aColor, mColorScheme,
85 LookAndFeel::ShouldUseStandins(mDoc, aColor))) {
86 return sRGBColor::FromABGR(*color);
88 return aCompute();
91 std::pair<sRGBColor, sRGBColor> SystemPair(StyleSystemColor aFirst,
92 StyleSystemColor aSecond) const {
93 return std::make_pair(System(aFirst), System(aSecond));
96 // Whether we should use system colors (for high contrast mode).
97 static bool ShouldBeHighContrast(const nsPresContext&);
98 static ColorScheme ColorSchemeForWidget(const nsIFrame*, StyleAppearance,
99 bool aHighContrast);
101 static void RecomputeAccentColors();
103 static nscolor ComputeCustomAccentForeground(nscolor aColor);
105 static nscolor AdjustUnthemedScrollbarThumbColor(nscolor aFaceColor,
106 dom::ElementState aStates);
109 } // namespace mozilla::widget
111 #endif