ColorContrast
Utility for calculating color contrast ratios based on WCAG 2.x standards.
Provides methods to calculate contrast between colors, determine required
ratios for different WCAG levels, and check if a contrast meets those levels.
Methods
calculate
static
public static function calculate(ColorInterface $a, ColorInterface $b): float
Calculate the contrast ratio between two colors.
Returns a float between 1 and 21.
Parameters
| Name | Type | Default |
|---|---|---|
$a |
ColorInterface | - |
$b |
ColorInterface | - |
meets
static
public static function meets(float $ratio, WcagLevel $level, bool $largeText): bool
Check if a contrast ratio meets a specific WCAG threshold.
Parameters
| Name | Type | Default |
|---|---|---|
$ratio |
float | - |
$level |
WcagLevel | "AA" |
$largeText |
bool | false |
meetsFor
static
public static function meetsFor(ColorInterface $a, ColorInterface $b, WcagLevel $level, bool $largeText): bool
Check if the contrast between two colors meets a specific WCAG threshold.
Parameters
| Name | Type | Default |
|---|---|---|
$a |
ColorInterface | - |
$b |
ColorInterface | - |
$level |
WcagLevel | "AA" |
$largeText |
bool | false |
requiredRatio
static
public static function requiredRatio(WcagLevel $level, bool $largeText): float
Get the required contrast ratio for a specific WCAG level.
- AA normal: 4.5
- AA large: 3.0
- AAA normal: 7.0
- AAA large: 4.5
Parameters
| Name | Type | Default |
|---|---|---|
$level |
WcagLevel | "AA" |
$largeText |
bool | false |