Created
March 25, 2019 21:28
-
-
Save Frolki1-Dev/158865a54d6bc01b0b560a22664c43dd to your computer and use it in GitHub Desktop.
[PHP] Get the brightness of a color
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 | |
/** | |
* Get the brightness of the color. | |
* The return value is between 0 and 1. | |
* 0 = light | |
* 1 = dark | |
* | |
* @param string $hex The hex code of the color | |
* @return float|int | |
*/ | |
function get_brightness($hex) { | |
$hex = str_replace('#', '', $hex); | |
$c_r = hexdec(substr($hex, 0, 2)); | |
$c_g = hexdec(substr($hex, 2, 2)); | |
$c_b = hexdec(substr($hex, 4, 2)); | |
return (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000 / 255; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment