Skip to content

Instantly share code, notes, and snippets.

@ProjectCleverWeb
Created June 29, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProjectCleverWeb/a1241dd39fdf06670245250822a90368 to your computer and use it in GitHub Desktop.
Save ProjectCleverWeb/a1241dd39fdf06670245250822a90368 to your computer and use it in GitHub Desktop.
<?php
/**
* Takes an input background color and tells you if you should use a white or black for the text color
*/
$input_color = '49DCBB';
$red = base_convert(substr($input_color, 0, 2), 16, 10);
$green = base_convert(substr($input_color, 2, 2), 16, 10);
$blue = base_convert(substr($input_color, 4, 2), 16, 10);
$text_color = 'FFFFFF'; // use white text
if (((($red * 299) + ($green * 587) + ($blue * 114)) / 1000) > 127) {
$text_color = '000000'; // use black text
}
echo $text_color;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment