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

A QIcon that keeps an SVG's own colours and adapts each one for contrast against the surface it is painted on. More...

#include <contrast_adapt_icon.h>

Inheritance diagram for ContrastAdaptIcon:

Public Member Functions

 ContrastAdaptIcon (const QString &svg_resource_path, QPalette::ColorRole surface_role=QPalette::Window, QSize size=QSize(16, 16))
 ContrastAdaptIcon (const QString &svg_resource_path, const QColor &explicit_background, QSize size=QSize(16, 16))
 Explicit-background variant.
 ContrastAdaptIcon (const char *name)
 Convenience constructor for creating a contrast-adapted icon from a stock name.
void setPalette (const QPalette &palette)
 Set a custom palette for drawing this icon instead of the application palette.
void setTokenColors (const QHash< ThemeManager::ThemeToken, QColor > &colors)
 Paint ws:map-token colours from colors instead of the live theme.

Detailed Description

A QIcon that keeps an SVG's own colours and adapts each one for contrast against the surface it is painted on.

Companion to ThemedIcon, and a deliberate inverse of it: ThemedIcon throws the SVG's colours away and flattens the silhouette to one theme token, which destroys multi-colour icons. ContrastAdaptIcon instead treats the SVG as authoritative — it reads the colours out of the markup and runs each through ColorMath::ensureContrast(colour, background, minContrast), nudging only lightness (hue preserved) and only when a colour falls below the target ratio. Multi-colour and per-state colours survive; the code merely guarantees the icon stays legible whatever the theme paints behind it. minContrast is the icon ratio of the active contrast level (ColorMath::activeRatios()), so high contrast adapts the drawn colours further. Colours bound to a theme token (see below) are not adapted unless the binding asks for it.

The background is resolved from QIcon::Mode (see contrast_adapt_icon.cpp for why it must be), so theme/light-dark changes re-render automatically via the pixmap cache keyed on the resolved background.

QIcon::Disabled renders the usual contrast-adapted icon at 40% opacity, like ThemedIcon.

Per-state colours: an icon that should recolour on hover/press declares the alternates in the SVG itself, with one element per base colour (ignored by the renderer):

<ws:swap base="#2960f6" active="#2050f5" selected="#1c47f5" normal-on="#2e7d32"/>

In Active/Selected mode the engine swaps that base colour for the named alternate before contrast-adapting it. The optional "normal-on" attribute names the colour for QIcon::On (e.g. a checked toggle button); a mode-specific active/selected colour takes precedence over it when both apply.

A drawn colour can instead track the theme by binding it to a ThemeManager::ThemeToken, by name:

<ws:map-token color="#212121" token="PaletteText"/> <ws:map-token color="#4e72a5" token="FilterApply" active="emphasis" adapt="contrast"/> <ws:map-token color="#888a85" active="FilterClear" adapt="contrast"/>

"token" paints every use of that colour with the token's current theme colour, or the colour given with setTokenColors(), in every mode. "active" and "selected" bind QIcon::Active / QIcon::Selected only: a token name, or "emphasis" for a slight brightening of the "token" colour. Selected falls back to "active". Without "token", the other modes keep the drawn colour, contrast-adapted as usual. Token colours are used as-is, unless adapt="contrast" asks for the same contrast adaptation drawn colours get. A mapping wins over a <ws:swap> for the same colour. Icons that declare nothing simply keep their drawn colours in every mode.

Constructor & Destructor Documentation

◆ ContrastAdaptIcon() [1/3]

ContrastAdaptIcon::ContrastAdaptIcon ( const QString & svg_resource_path,
QPalette::ColorRole surface_role = QPalette::Window,
QSize size = QSize(16, 16) )
explicit
Parameters
svg_resource_pathQt resource path, e.g. ":/svg_icons/capture-start.svg".
surface_rolePalette role for the non-selected background. QPalette::Window for toolbar/menu/dialog chrome; pass QPalette::Base for icons inside item views.
sizeNominal render size used when a null size is asked.

The minimum contrast ratio is not a per-call parameter: it is the icon ratio of the active contrast level (ColorMath::activeRatios()), read at render time, so a contrast level change re-renders the icon.

◆ ContrastAdaptIcon() [2/3]

ContrastAdaptIcon::ContrastAdaptIcon ( const QString & svg_resource_path,
const QColor & explicit_background,
QSize size = QSize(16, 16) )

Explicit-background variant.

For call sites whose surface cannot be inferred from QIcon::Mode — e.g. an icon drawn on a packet-list row tinted by a colouring rule. The given background is used for every mode.

◆ ContrastAdaptIcon() [3/3]

ContrastAdaptIcon::ContrastAdaptIcon ( const char * name)
inlineexplicit

Convenience constructor for creating a contrast-adapted icon from a stock name.

Parameters
nameThe base name of the SVG icon resource without a path or .svg extension.

Member Function Documentation

◆ setPalette()

void ContrastAdaptIcon::setPalette ( const QPalette & palette)

Set a custom palette for drawing this icon instead of the application palette.

Affects every copy of this icon. By default the application palette is used.

◆ setTokenColors()

void ContrastAdaptIcon::setTokenColors ( const QHash< ThemeManager::ThemeToken, QColor > & colors)

Paint ws:map-token colours from colors instead of the live theme.

For previews of a theme that is not the active one. A token without a valid entry in colors still resolves via the live theme (or the palette for Palette* tokens). A fully transparent colour paints as "none", e.g. to drop a fill and keep only the outline. Affects every copy of this icon, like setPalette().


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