ThemeColors
ThemeColors is an all-static utility class that centralizes color
management for Blackchirp’s user interface. Every color it returns is derived
from the active system palette and adjusted so that status indicators, accent
text, and SVG icons remain readable under both light and dark system themes.
It is the right starting point for any new UI surface that requires a
theme-respecting accent color or a status-feedback color.
Color roles
The ColorRole enum divides the available colors into three families:
Status colors — used for feedback and state indicators:
StatusSuccess— valid states and success messages.StatusWarning— caution states and warning messages.StatusError— invalid states and error messages.StatusInfo— informational or neutral-positive messages.StatusNeutral— default or unclassified status.
Text colors — used for emphasis levels in labels and tooltips:
SubtleText— secondary or less-important text.EmphasisText— highlighted or important text.DisabledText— inactive or disabled text. Also used as the default color role for the disabled state increateThemedIconWithStates().
Icon colors — used when rendering SVG icons:
IconPrimary— main interface element icons.IconSecondary— supporting or secondary icons.IconAccent— special-highlight icons.
Palette-derived colors and CSS
getThemeAwareColor(role, widget) returns a QColor appropriate for the
current theme. Pass a widget pointer to use that widget’s palette for context;
pass nullptr to fall back to the application palette. getCSSColor()
returns the same color as a hex string (e.g., "#1a9e3f") suitable for use
in Qt stylesheets.
isDarkTheme(widget) inspects the palette and returns true when a dark
system theme is active. UI code that renders custom graphics can use this to
choose appropriate base colors before calling getThemeAwareColor().
Contrast helpers
ensureContrast(color, background, minContrastRatio) adjusts color
until its WCAG contrast ratio against background meets
minContrastRatio. The default target of 4.5 corresponds to WCAG AA
compliance for normal-weight text. calculateContrastRatio(color1, color2)
performs the raw calculation; the ratio ranges from 1.0 (identical colors) to
21.0 (black on white).
SVG icon helpers
createThemedIcon(svgPath, colorRole, widget) loads an SVG from a Qt
resource path and recolors it using the color for colorRole, returning a
QIcon sized for the platform’s icon metrics.
createThemedIconWithStates(svgPath, enabledRole, disabledRole, widget)
produces a QIcon with separate colors for enabled and disabled states,
which Qt uses automatically when a toolbar button or menu action is disabled.
API Reference
-
class ThemeColors
All-static utility class for theme-aware colors and icons.
Every color is derived from the system palette and adjusted for contrast so that status indicators and icons remain readable under both light and dark system themes.
See also
Public Types
-
enum ColorRole
Semantic color roles used throughout the UI.
Values:
-
enumerator StatusSuccess
Success messages and valid states.
-
enumerator StatusWarning
Warning messages and caution states.
-
enumerator StatusError
Error messages and invalid states.
-
enumerator StatusInfo
Informational messages and neutral states.
-
enumerator StatusNeutral
Default or unclassified status.
-
enumerator SubtleText
Secondary or less-important text.
-
enumerator EmphasisText
Important or highlighted text.
-
enumerator DisabledText
Inactive or disabled text.
-
enumerator IconPrimary
Main interface element icons.
-
enumerator IconSecondary
Supporting element icons.
-
enumerator IconAccent
Special-highlight icons.
-
enumerator StatusSuccess
Public Static Functions
-
static QColor getThemeAwareColor(ColorRole role, const QWidget *widget = nullptr)
Returns a palette-derived color for role.
- Parameters:
role – Color role to resolve.
widget – Widget whose palette is used as context; pass
nullptrto use the application palette.
- Returns:
QColor appropriate for the active theme.
-
static QString getCSSColor(ColorRole role, const QWidget *widget = nullptr)
Returns a CSS hex string for role (e.g.,
"#ff0000").- Parameters:
role – Color role to resolve.
widget – Widget whose palette is used as context; pass
nullptrto use the application palette.
- Returns:
QString suitable for use in a Qt stylesheet.
-
static bool isDarkTheme(const QWidget *widget = nullptr)
Returns
truewhen the active palette is a dark theme.- Parameters:
widget – Widget whose palette is inspected; pass
nullptrfor the application palette.
-
static QColor ensureContrast(const QColor &color, const QColor &background, double minContrastRatio = 4.5)
Adjusts color until its contrast ratio against background meets minContrastRatio.
The default target of 4.5 corresponds to WCAG AA compliance for normal text. Returns color unchanged if the ratio is already satisfied.
- Parameters:
color – Color to adjust.
background – Background color to contrast against.
minContrastRatio – Minimum acceptable contrast ratio (default: 4.5).
- Returns:
Adjusted QColor.
-
static double calculateContrastRatio(const QColor &color1, const QColor &color2)
Calculates the WCAG contrast ratio between two colors.
The ratio ranges from 1.0 (identical colors) to 21.0 (black on white).
- Parameters:
color1 – First color.
color2 – Second color.
- Returns:
Contrast ratio.
-
static QIcon createThemedIcon(const QString &svgResourcePath, ColorRole colorRole = IconPrimary, const QWidget *widget = nullptr)
Creates a QIcon from an SVG resource with the color for colorRole.
- Parameters:
svgResourcePath – Qt resource path to the SVG (e.g.,
":/icons/play.svg").colorRole – Color role applied to the icon (default: IconPrimary).
widget – Widget for palette context (default:
nullptr).
- Returns:
QIcon with theme-appropriate coloring.
-
static QIcon createThemedIconWithStates(const QString &svgResourcePath, ColorRole enabledColorRole = IconPrimary, ColorRole disabledColorRole = DisabledText, const QWidget *widget = nullptr)
Creates a QIcon with separate colors for enabled and disabled states.
- Parameters:
svgResourcePath – Qt resource path to the SVG.
enabledColorRole – Color role for the enabled state (default: IconPrimary).
disabledColorRole – Color role for the disabled state (default: DisabledText).
widget – Widget for palette context (default:
nullptr).
- Returns:
QIcon with per-state theme-appropriate coloring.
Private Static Functions
-
static QColor getBaseColor(ColorRole role, const QWidget *widget)
Returns the base palette-derived color for role.
-
static QColor adjustBrightness(const QColor &color, double factor)
Adjusts the brightness of color by factor.
factor ranges from -1.0 (fully dark) to 1.0 (fully bright).
-
static double relativeLuminance(const QColor &color)
Computes the WCAG relative luminance of color.
Returns a value in [0.0, 1.0] where 0.0 is black and 1.0 is white.
-
enum ColorRole