Wireshark 5.1.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
ColorMath Class Reference

#include <color_math.h>

Classes

struct  ContrastRatios
struct  TextOnFill

Static Public Member Functions

static double relativeLuminance (const QColor &color)
static qreal contrastRatio (const QColor &color1, const QColor &color2)
static QColor ensureContrast (const QColor &c, const QColor &bg, qreal minRatio)
static bool isDark (const QColor &color)
static constexpr ContrastRatios ratiosFor (ThemeContrast level)
static const ContrastRatios & activeRatios ()
static void setActiveContrast (ThemeContrast level)
static TextOnFill readableOnFill (const QColor &text, const QColor &fill, qreal minRatio)
static QColor readableFill (const QColor &fill, const ContrastRatios &ratios=activeRatios())
static TextOnFill selectionHover (const QColor &base, const QColor &selection, const QColor &text, const ContrastRatios &ratios=activeRatios())
static QColor selectionHoverFill (const QColor &base, const QColor &selection, const ContrastRatios &ratios=activeRatios())
static QColor readableText (const QColor &text, const QColor &surface, const ContrastRatios &ratios=activeRatios())
static bool isReadable (const QColor &text, const QColor &surface, const ContrastRatios &ratios=activeRatios())
static QColor mix (const QColor &a, const QColor &b, qreal ratio=0.5)
static ThemeColorPair mix (const ThemeColorPair &a, const ThemeColorPair &b, qreal ratio=0.5)
static QColor darken (const QColor &c, int percent)
static ThemeColorPair darken (const ThemeColorPair &p, int percent)
static QColor lighten (const QColor &c, int percent)
static ThemeColorPair lighten (const ThemeColorPair &p, int percent)
static QColor withAlpha (const QColor &c, int alpha)
static ThemeColorPair withAlpha (const ThemeColorPair &p, int alpha)
static QColor withAlphaF (const QColor &c, qreal alpha)
static ThemeColorPair withAlphaF (const ThemeColorPair &p, qreal alpha)
static QColor contrastingText (const QColor &surface)
static ThemeColorPair contrastingText (const ThemeColorPair &pair)
static QColor compositeOver (const QColor &surface, const QColor &backdrop)
static QColor contrastingTextOver (const QColor &surface, const QColor &backdrop)
static QColor disabled (const QColor &c, const QColor &background)
static ThemeColorPair disabled (const ThemeColorPair &p, const QColor &background)
static QColor hoverBg (const QColor &c, const QColor &background)
static ThemeColorPair hoverBg (const ThemeColorPair &p, const QColor &background)
static QColor hoverEmphasis (const QColor &c)

Detailed Description

Pure color-math helpers, with two deliberate couplings to the running theme:

  • contrastingText(QColor), when the surface has alpha, asks ThemeManager for the current app mode to pick a fallback backdrop. The caller wants "the right foreground for this translucent surface as rendered right now", which is a theme-runtime question.
  • The active contrast ratio set. setActiveContrast() changes it (process-global, GUI thread; ThemeManager sets it before each apply), and activeRatios() and every default ratios argument read it, so runtime consumers follow the applied level. Code that may derive for another level (ThemeTokenHandler::deriveAll(), ThemePaletteBuilder::build(), theme previews) passes a set explicitly.

Every other method is static and side-effect-free.

Member Function Documentation

◆ activeRatios()

const ColorMath::ContrastRatios & ColorMath::activeRatios ( )
static

The ratio set of the applied contrast level (Standard until setActiveContrast() is called). GUI thread only.

◆ compositeOver()

QColor ColorMath::compositeOver ( const QColor & surface,
const QColor & backdrop )
static

surface composited over the opaque backdrop with the "over" operator; an opaque surface is returned unchanged.

◆ contrastingText()

QColor ColorMath::contrastingText ( const QColor & surface)
static

Returns Qt::black or Qt::white — whichever reads better on the given surface color. If the surface is translucent, picks the app's current dark/light mode as the backdrop.

◆ contrastingTextOver()

QColor ColorMath::contrastingTextOver ( const QColor & surface,
const QColor & backdrop )
static

Same as contrastingText(), but with the caller supplying the backdrop explicitly — useful when rendering onto a known surface (e.g. the welcome header bar) rather than the app palette.

◆ contrastRatio()

qreal ColorMath::contrastRatio ( const QColor & color1,
const QColor & color2 )
static

WCAG contrast ratio between two opaque colors. Higher values are more contrasty; 4.5 is the AA threshold for body text.

◆ darken()

QColor ColorMath::darken ( const QColor & c,
int percent )
static

Mix the given color with black at percent %. Hover typically 8-12 %, pressed 16-20 %, bar backdrop 50-65 %.

◆ disabled()

QColor ColorMath::disabled ( const QColor & c,
const QColor & background )
static

Standard "disabled" fade: mix(color, background, 0.4).

◆ ensureContrast()

QColor ColorMath::ensureContrast ( const QColor & c,
const QColor & bg,
qreal minRatio )
static

Mix c toward contrastingText(bg) until contrastRatio(result, bg) >= minRatio. Binary-searches the mix ratio (24 iterations → < 0.001 precision). Used to enforce WCAG contrast on derived tokens (section headers against the base, the welcome header against its title text) without forcing every theme author to hand-pick the values. When c already meets the target it is returned unchanged. If the math can't reach the target (rare; only when c is already on the same luminance side as contrastingText(bg) and saturated), returns contrastingText(bg) directly — favoring readability over hue.

◆ hoverBg()

QColor ColorMath::hoverBg ( const QColor & c,
const QColor & background )
static

Subtle hover tint: mix(color, background, 0.1).

◆ hoverEmphasis()

QColor ColorMath::hoverEmphasis ( const QColor & c)
static

Hover colour of an icon glyph whose icon names no explicit hover colour: c.lighter(HoverEmphasisFactor), a slight brightening with the hue kept. Shared by the theme icon engines so every glyph lights up alike.

◆ isDark()

bool ColorMath::isDark ( const QColor & color)
static

Returns true if the given color's relative luminance is below the WCAG equal-contrast threshold (≈0.179). Purely a color- math helper; independent of the running app's theme.

◆ isReadable()

bool ColorMath::isReadable ( const QColor & text,
const QColor & surface,
const ContrastRatios & ratios = activeRatios() )
static

True when text already reaches the set's readableText ratio against surface, i.e. readableText() would return it unchanged.

◆ lighten()

QColor ColorMath::lighten ( const QColor & c,
int percent )
static

Mix the given color with white at percent %. Used for text on dark surfaces (30-70 %).

◆ mix()

QColor ColorMath::mix ( const QColor & a,
const QColor & b,
qreal ratio = 0.5 )
static

Linear blend of two colors at ratio r. r=0 returns a, r=1 returns b. Alpha-aware.

◆ ratiosFor()

constexpr ContrastRatios ColorMath::ratiosFor ( ThemeContrast level)
inlinestaticconstexpr

The ratio set for a contrast level.

◆ readableFill()

QColor ColorMath::readableFill ( const QColor & fill,
const ContrastRatios & ratios = activeRatios() )
static

fill unchanged when its contrastingText() reads on it at the set's readableText ratio, else moved away from that text until it does.

◆ readableOnFill()

ColorMath::TextOnFill ColorMath::readableOnFill ( const QColor & text,
const QColor & fill,
qreal minRatio )
static

text lifted to minRatio against fill; when it cannot get there, fill moves away from the lifted text until the pair reads at minRatio.

◆ readableText()

QColor ColorMath::readableText ( const QColor & text,
const QColor & surface,
const ContrastRatios & ratios = activeRatios() )
static

text lifted to the set's readableText ratio against surface; returned unchanged when it already reads.

◆ relativeLuminance()

double ColorMath::relativeLuminance ( const QColor & color)
static

WCAG relative luminance in the [0, 1] range.

◆ selectionHover()

ColorMath::TextOnFill ColorMath::selectionHover ( const QColor & base,
const QColor & selection,
const QColor & text,
const ContrastRatios & ratios = activeRatios() )
static

Hover fill and text for an item view: the fill is selectionHoverFill(), the text text made readable on it. In High the fill moves toward readability of text as long as it stays distinct from selection.

◆ selectionHoverFill()

QColor ColorMath::selectionHoverFill ( const QColor & base,
const QColor & selection,
const ContrastRatios & ratios = activeRatios() )
static

Hover fill for an item view, derived from that view's own selection color so the view uses one highlight hue. A 55 % tint of selection over base, pushed away from selection until the two are the set's distinctSurface ratio apart, so a hovered row never reads as a second selection. Dark selections sit too close to the base for any in-between tint to be distinct from both, so in dark schemes the fill ends up lighter than the selection.

◆ setActiveContrast()

void ColorMath::setActiveContrast ( ThemeContrast level)
static

Make the ratio set of level the active one. Called by ThemeManager before it derives the palette and tokens.

◆ withAlpha()

QColor ColorMath::withAlpha ( const QColor & c,
int alpha )
static

Set the alpha channel to an integer 0-255.

◆ withAlphaF()

QColor ColorMath::withAlphaF ( const QColor & c,
qreal alpha )
static

Set the alpha channel to a float 0.0-1.0.


The documentation for this class was generated from the following files: