Index

MMO\core\colors

MMO\core\colors · 5 symbols · generated 30 Sep 2026

function

gradientOf

colors.php:221

Interpolates a color along a linear gradient between two colors, based on a normalized position (0 to 1).

function gradientOf(float $value, string $startColor, string $endColor): string
Namespace
MMO\core\colors
Interpolation happens per RGB channel; $value is clamped so callers don't need to pre-clamp it themselves.
$valuefloatA value between 0 (start color) and 1 (end color). Values outside [0,1] are clamped.
$startColorstringHex color code (e.g., "#DC143C"), or anything accepted by hexToRgb
$endColorstringHex color code (e.g., "#32CD32"), or anything accepted by hexToRgb
returnsstringThe interpolated color as hex (e.g., "#ABC123")
gradientOf(0, '#ff0000', '#00ff00'); // "#ff0000" (start color) gradientOf(1, '#ff0000', '#00ff00'); // "#00ff00" (end color) gradientOf(0.5, '#ff0000', '#00ff00'); // "#808000" (halfway point) //typical use: color a progress/heat indicator from red (0%) to green (100%) gradientOf($percentDone / 100, 'red', 'green');
Call graph
function

hexToRgb

colors.php:172

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).
$hexstringHex color with or without '#' (6-digit or 3-digit shorthand), or a name from HTML_COLORS
returnsarrayArray with keys 'r', 'g', 'b' containing values 0-255
hexToRgb('#DC143C'); // ['r' => 220, 'g' => 20, 'b' => 60] hexToRgb('DC143C'); // same, the leading '#' is optional hexToRgb('#fff'); // ['r' => 255, 'g' => 255, 'b' => 255] -- 3-digit shorthand is expanded first hexToRgb('crimson'); // ['r' => 220, 'g' => 20, 'b' => 60] -- resolved through HTML_COLORS
Call graph
function

luminance

colors.php:252

Compute the WCAG 2.0 relative luminance of a color (0 = darkest black, 1 = brightest white).

function luminance(string $color): float
Namespace
MMO\core\colors
See
https://www.w3.org/TR/WCAG20/#relativeluminancedef
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.
$colorstringHex color code, or anything accepted by hexToRgb
returnsfloatthe relative luminance, between 0 and 1
luminance('#ffffff'); // 1.0 (white) luminance('#000000'); // 0.0 (black) luminance('#ffff00'); // ~0.9278 (yellow reads as very bright)
Call graph

Uses (1)

Used by (1)

function

rgbToHex

colors.php:200

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.

function rgbToHex(int $r, int $g, int $b): string
Namespace
MMO\core\colors
$rintRed (clamped to 0-255)
$gintGreen (clamped to 0-255)
$bintBlue (clamped to 0-255)
returnsstringHex color with '#' (e.g., "#DC143C")
rgbToHex(220, 20, 60); // "#dc143c" rgbToHex(-10, 300, 60); // "#00ff3c" -- out-of-range channels are clamped rather than corrupting the output
Call graph

Used by (1)

function

textColor

colors.php:278

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.
$colorstringthe background's hex color, or anything accepted by hexToRgb
returnsstring'black' or 'white'
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)
Call graph

Uses (1)