The following PHP function will return an inverse HEX color with PHP. It's handy if you need to print readable text on a colored background. It's not always aesthetically pleasing so it's usually best to use a black, neutral, or white text overlay on a background based on the background color's numeric value.
1
<?php
2
/*
3
Return an Inverse Color With PHP
4
http://www.beliefmedia.com/code/php-snippets/inverse-color-php
5
*/
6
7
function beliefmedia_inverse_color($color) {
8
9
10
if (strlen($color) != 6) $color = str_repeat(substr($color, 0, 1), 2) . str_repeat(substr($color, 1, 1), 2) . str_repeat(substr($color, 2, 1), 2);
11
12
for ($x = 0; $x < 3; $x++) {
13
14
15
16
}
17
18
return '#' . $hex;
19
}
20
21
/* Usage (returns #000000) */
22
echo beliefmedia_inverse_color($color = 'ffffff');
23
24
/* Usage (returns #00ffff) */
25
echo beliefmedia_inverse_color($color = 'ff0000');
Other color functions can be found on our blog by way of the color tag.