Bug 1756130 [wpt PR 32898] - [CSP] Enhance unsafe-eval test to check both realms...
[gecko.git] / widget / ThemeColors.h
blobbefbbd73181c0e01d85e69594d8991f6b22f095c
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 sDefaultAccentForeground(
19 gfx::sRGBColor::OpaqueWhite());
21 class ThemeAccentColor {
22 protected:
23 using sRGBColor = mozilla::gfx::sRGBColor;
24 using ComputedStyle = mozilla::ComputedStyle;
26 Maybe<nscolor> mAccentColor;
28 public:
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.
40 class ThemeColors {
41 protected:
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;
49 const Document& mDoc;
50 const bool mHighContrast;
51 const LookAndFeel::ColorScheme mColorScheme;
53 public:
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(
82 aColor, mColorScheme,
83 LookAndFeel::ShouldUseStandins(mDoc, aColor))) {
84 return sRGBColor::FromABGR(*color);
86 return aCompute();
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,
97 bool aHighContrast);
99 static void RecomputeAccentColors();
101 static nscolor ComputeCustomAccentForeground(nscolor aColor);
103 static nscolor AdjustUnthemedScrollbarThumbColor(nscolor aFaceColor,
104 EventStates aStates);
107 } // namespace mozilla::widget
109 #endif