Skip to content

Instantly share code, notes, and snippets.

@alexmustin
Created October 6, 2022 19:26
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 alexmustin/3414afa9b62249fb47ccc511ec5a475e to your computer and use it in GitHub Desktop.
Save alexmustin/3414afa9b62249fb47ccc511ec5a475e to your computer and use it in GitHub Desktop.
Function to determine if a Hex color is "light" or "dark"
<?php
function isColorLightOrDark( $color ) {
$red = hexdec(substr($color, 1, 2));
$green = hexdec(substr($color, 3, 2));
$blue = hexdec(substr($color, 5, 2));
$result = (($red * 299) + ($green * 587) + ($blue * 114)) / 1000;
if ( intval($result) > 128 ) {
$lightdark = "light";
} else {
$lightdark = "dark";
}
return $lightdark;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment