A PHP function to ensure adequate contrast between two colours. If the two colours don't have enough contrast, both colors get nudged brighter or darker until there's enough contrast between them.
require_once('wcag-contrast.php');
adjustColors('#AAAAAA','#CCCCCC'); // result: [#919191,#eaeaea]
The adjustColors()
function takes three arguments:
$color1
– The first colour, in hex (string) or RGB (array) format.$color2
– The second colour, in hex (string) or RGB (array) format.$threshold
– The contrast threshold to test for. Optional. Default is 'AA'.
The $threshold
argument accepts the following values:
- 'AA'
- 'AAA'
- 'AALarge'
- 'AAALarge'
- 'AAMediumBold'
- 'AAAMediumBold'
- Any contrast ratio as a number