Core Coding®

PHP Visible Text

  PHP Visible Text
Make sure your text is always visible no matter what the background color.
<?php
function visibleText($color) {
$color = trim($color);
if (
$color[0] == '#') {
$color = substr($color, 1);
$pound = '#';
}

if (
strlen($color) == 3) {
$colors = array(hexdec($color[0] . $color[0]), hexdec($color[1] . $color[1]), hexdec($color[2] .
$color[2]));
} elseif (
strlen($color) == 6) {
$colors = array(hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)));
} else {
return
$pound . $color;
}

if (
array_sum($colors) > 255 * 1.5) {
return
$pound . '000';
} else {
return
$pound . 'FFF';
};
};

Go back to resources
Home Mail GitHub