PHP Visible Text 8,497 views

Make sure your text is always visible no matter what the background color.

 1<?php 2function visibleText($color) { 3  $color = trim($color); 4  if ($color[0] == '#') { 5    $color = substr($color, 1); 6    $pound = '#'; 7  } 8 9  if (strlen($color) == 3) {10    $colors = array(hexdec($color[0] . $color[0]), hexdec($color[1] . $color[1]), hexdec($color[2] . $color[2]));11  } elseif (strlen($color) == 6) {12    $colors = array(hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)));13  } else {14    return $pound . $color;15  }1617  if (array_sum($colors) > 255 * 1.5) {18    return $pound . '000';19  } else {20    return $pound . 'FFF';21  };22};