Converts a hex color (6-digit, or the 3-digit CSS shorthand) or a named HTML color to an RGB array.
function hexToRgb(string $hex): array
Namespace
MMO\core\colors
NOTE: the 8-digit `#RRGGBBAA` form is read but its alpha byte is silently ignored (only 'transparent' in
HTML_COLORS uses it, and it happens to be all zeroes, so this has never surfaced as an issue in practice).
This is the perceptual-brightness formula behind WCAG contrast-ratio calculations: it first linearizes each
sRGB channel (undoing the gamma curve monitors apply) before weighting them by the eye's sensitivity to each
channel (green contributes the most, blue the least) -- a plain average of R/G/B would not match how bright
a color actually looks.
Converts RGB values to a hex color. Out-of-range channels are clamped to 0-255: without this, a negative value fed to dechex() would produce a huge two's-complement hex string (e.g. dechex(-1) is 16 'f's) and silently corrupt the result instead of producing a valid color.
Pick whichever of 'black'/'white' text reads better on top of the given background color.
function textColor(string $color): string
Namespace
MMO\core\colors
0.179 is the WCAG relative-luminance crossover point at which black and white text reach equal contrast
against the background (derived from the WCAG contrast-ratio formula `(L1+0.05)/(L2+0.05)`); above it,
black text has the better contrast, at or below it, white does.
textColor('#ffffff'); // 'black' (black text on a white background)
textColor('#000000'); // 'white' (white text on a black background)
textColor('#ffcc00'); // 'black' (amber is bright enough for black text)