Skip to content

Instantly share code, notes, and snippets.

@ThatGuySam
Created November 1, 2018 21:04
Show Gist options
  • Save ThatGuySam/ad04a42aab61aa06ce8f7fbf88598ff4 to your computer and use it in GitHub Desktop.
Save ThatGuySam/ad04a42aab61aa06ce8f7fbf88598ff4 to your computer and use it in GitHub Desktop.
Gets a contrasting color based on YIQ equation - Source: https://24ways.org/2010/calculating-color-contrast/
function getContrastYIQ(hexcolor){
var r = parseInt(hexcolor.substr(0,2),16);
var g = parseInt(hexcolor.substr(2,2),16);
var b = parseInt(hexcolor.substr(4,2),16);
var yiq = ((r*299)+(g*587)+(b*114))/1000;
return (yiq >= 128) ? 'black' : 'white';
}
<?php
function getContrastYIQ($hexcolor){
$r = hexdec(substr($hexcolor,0,2));
$g = hexdec(substr($hexcolor,2,2));
$b = hexdec(substr($hexcolor,4,2));
$yiq = (($r*299)+($g*587)+($b*114))/1000;
return ($yiq >= 128) ? 'black' : 'white';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment