Created
October 6, 2022 19:26
-
-
Save alexmustin/3414afa9b62249fb47ccc511ec5a475e to your computer and use it in GitHub Desktop.
Function to determine if a Hex color is "light" or "dark"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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