Wireshark 5.1.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
color_math.h
Go to the documentation of this file.
1
9
10#ifndef COLOR_MATH_H
11#define COLOR_MATH_H
12
14
15#include <QColor>
16
36{
37public:
38 // ---------------------------------------------------------------
39 // Luminance & contrast
40 // ---------------------------------------------------------------
41
43 static double relativeLuminance(const QColor &color);
44
47 static qreal contrastRatio(const QColor &color1, const QColor &color2);
48
59 static QColor ensureContrast(const QColor &c, const QColor &bg, qreal minRatio);
60
64 static bool isDark(const QColor &color);
65
97
99 static constexpr ContrastRatios ratiosFor(ThemeContrast level)
100 {
101 const bool high = level == ThemeContrast::High;
102 ContrastRatios r {};
103 r.level = level;
104 r.readableText = high ? ReadableTextRatioHigh : ReadableTextRatio;
105 r.largeText = high ? LargeTextRatioHigh : LargeTextRatio;
106 r.nonText = high ? NonTextRatioHigh : NonTextRatio;
107 r.divider = high ? DividerRatioHigh : DividerRatio;
108 r.distinctSurface = high ? DistinctSurfaceRatioHigh : DistinctSurfaceRatio;
109 r.cardSurfaceDark = high ? CardSurfaceRatioDarkHigh : CardSurfaceRatioDark;
110 r.icon = high ? IconContrastRatioHigh : IconContrastRatio;
111 return r;
112 }
113
116 static const ContrastRatios &activeRatios();
117
120 static void setActiveContrast(ThemeContrast level);
121
123 struct TextOnFill {
124 QColor text;
125 QColor fill;
126 };
127
131 static TextOnFill readableOnFill(const QColor &text, const QColor &fill, qreal minRatio);
132
136 static QColor readableFill(const QColor &fill, const ContrastRatios &ratios = activeRatios());
137
142 static TextOnFill selectionHover(const QColor &base, const QColor &selection,
143 const QColor &text,
144 const ContrastRatios &ratios = activeRatios());
145
154 static QColor selectionHoverFill(const QColor &base, const QColor &selection,
155 const ContrastRatios &ratios = activeRatios());
156
159 static QColor readableText(const QColor &text, const QColor &surface,
160 const ContrastRatios &ratios = activeRatios());
161
164 static bool isReadable(const QColor &text, const QColor &surface,
165 const ContrastRatios &ratios = activeRatios());
166
167 // ---------------------------------------------------------------
168 // Mix / shade
169 // ---------------------------------------------------------------
170
173 static QColor mix(const QColor &a, const QColor &b, qreal ratio = 0.5);
174 static ThemeColorPair mix(const ThemeColorPair &a, const ThemeColorPair &b, qreal ratio = 0.5);
175
178 static QColor darken(const QColor &c, int percent);
179 static ThemeColorPair darken(const ThemeColorPair &p, int percent);
180
183 static QColor lighten(const QColor &c, int percent);
184 static ThemeColorPair lighten(const ThemeColorPair &p, int percent);
185
186 // ---------------------------------------------------------------
187 // Alpha
188 // ---------------------------------------------------------------
189
191 static QColor withAlpha(const QColor &c, int alpha);
192 static ThemeColorPair withAlpha(const ThemeColorPair &p, int alpha);
193
195 static QColor withAlphaF(const QColor &c, qreal alpha);
196 static ThemeColorPair withAlphaF(const ThemeColorPair &p, qreal alpha);
197
198 // ---------------------------------------------------------------
199 // Contrast & state
200 // ---------------------------------------------------------------
201
205 static QColor contrastingText(const QColor &surface);
207
210 static QColor compositeOver(const QColor &surface, const QColor &backdrop);
211
216 static QColor contrastingTextOver(const QColor &surface, const QColor &backdrop);
217
219 static QColor disabled(const QColor &c, const QColor &background);
220 static ThemeColorPair disabled(const ThemeColorPair &p, const QColor &background);
221
223 static QColor hoverBg(const QColor &c, const QColor &background);
224 static ThemeColorPair hoverBg(const ThemeColorPair &p, const QColor &background);
225
230 static QColor hoverEmphasis(const QColor &c);
231
232private:
233 // The ratio values behind ratiosFor(). Private so that every
234 // consumer goes through a ContrastRatios set and follows the
235 // active contrast level. The ...High siblings are the high
236 // contrast targets: WCAG AAA for text (7:1 body, 4.5:1 large
237 // text) and stronger non-text, divider and surface distances.
238 static constexpr qreal DistinctSurfaceRatio = 1.5;
239 static constexpr qreal DistinctSurfaceRatioHigh = 3.0;
240 static constexpr qreal ReadableTextRatio = 4.5;
241 static constexpr qreal ReadableTextRatioHigh = 7.0;
242 static constexpr qreal CardSurfaceRatioDark = 1.25;
243 static constexpr qreal CardSurfaceRatioDarkHigh = 1.6;
244 static constexpr qreal DividerRatio = 1.6;
245 static constexpr qreal DividerRatioHigh = 3.0;
246 static constexpr qreal NonTextRatio = 3.0;
247 static constexpr qreal NonTextRatioHigh = 4.5;
248 static constexpr qreal LargeTextRatio = 3.0;
249 static constexpr qreal LargeTextRatioHigh = 4.5;
250 static constexpr qreal IconContrastRatio = 1.6;
251 static constexpr qreal IconContrastRatioHigh = 4.5;
252
253 // Percentage passed to QColor::lighter() by hoverEmphasis().
254 // lighter() multiplies the HSV value by factor / 100, so 115 makes a
255 // glyph about 15% brighter and keeps its hue; a colour already at full
256 // value loses saturation instead and moves toward white. The step is
257 // small on purpose: hover should read as the same glyph lighting up,
258 // not as a different colour (a 30% mix toward the text colour was
259 // rejected as too dark). Legibility is not this factor's job: the
260 // contrast-adapting icon engine runs the result through
261 // ensureContrast() against the field afterwards. An int, not a
262 // qreal, because lighter() takes an integer percentage; the qreal
263 // constants above are contrast ratios only.
264 static constexpr int HoverEmphasisFactor = 115;
265};
266
267#endif /* COLOR_MATH_H */
Definition color_math.h:36
static bool isDark(const QColor &color)
Definition color_math.cpp:143
static QColor withAlpha(const QColor &c, int alpha)
Definition color_math.cpp:205
static bool isReadable(const QColor &text, const QColor &surface, const ContrastRatios &ratios=activeRatios())
Definition color_math.cpp:137
static const ContrastRatios & activeRatios()
Definition color_math.cpp:75
static constexpr ContrastRatios ratiosFor(ThemeContrast level)
Definition color_math.h:99
static void setActiveContrast(ThemeContrast level)
Definition color_math.cpp:80
static QColor contrastingTextOver(const QColor &surface, const QColor &backdrop)
Definition color_math.cpp:270
static QColor selectionHoverFill(const QColor &base, const QColor &selection, const ContrastRatios &ratios=activeRatios())
Definition color_math.cpp:122
static QColor ensureContrast(const QColor &c, const QColor &bg, qreal minRatio)
Definition color_math.cpp:51
static QColor lighten(const QColor &c, int percent)
Definition color_math.cpp:187
static QColor compositeOver(const QColor &surface, const QColor &backdrop)
Definition color_math.cpp:256
static TextOnFill selectionHover(const QColor &base, const QColor &selection, const QColor &text, const ContrastRatios &ratios=activeRatios())
Definition color_math.cpp:103
static QColor disabled(const QColor &c, const QColor &background)
Definition color_math.cpp:277
static QColor mix(const QColor &a, const QColor &b, qreal ratio=0.5)
Definition color_math.cpp:152
static QColor readableFill(const QColor &fill, const ContrastRatios &ratios=activeRatios())
Definition color_math.cpp:94
static QColor darken(const QColor &c, int percent)
Definition color_math.cpp:173
static qreal contrastRatio(const QColor &color1, const QColor &color2)
Definition color_math.cpp:42
static QColor withAlphaF(const QColor &c, qreal alpha)
Definition color_math.cpp:217
static QColor contrastingText(const QColor &surface)
Definition color_math.cpp:233
static QColor hoverEmphasis(const QColor &c)
Definition color_math.cpp:297
static QColor readableText(const QColor &text, const QColor &surface, const ContrastRatios &ratios=activeRatios())
Definition color_math.cpp:131
static double relativeLuminance(const QColor &color)
Definition color_math.cpp:34
static QColor hoverBg(const QColor &c, const QColor &background)
Definition color_math.cpp:287
static TextOnFill readableOnFill(const QColor &text, const QColor &fill, qreal minRatio)
Definition color_math.cpp:85
Definition color_math.h:71
qreal cardSurfaceDark
Definition color_math.h:92
qreal icon
Definition color_math.h:95
ThemeContrast level
The contrast level this set belongs to.
Definition color_math.h:72
qreal distinctSurface
Definition color_math.h:87
qreal largeText
Definition color_math.h:77
qreal divider
Definition color_math.h:83
qreal readableText
Definition color_math.h:74
qreal nonText
Definition color_math.h:80
Definition color_math.h:123
Definition theme_manager.h:47
ThemeContrast
Definition theme_manager.h:26