Skip to content

Instantly share code, notes, and snippets.

@Frolki1-Dev
Created March 25, 2019 21:28
Show Gist options
  • Save Frolki1-Dev/158865a54d6bc01b0b560a22664c43dd to your computer and use it in GitHub Desktop.
Save Frolki1-Dev/158865a54d6bc01b0b560a22664c43dd to your computer and use it in GitHub Desktop.
[PHP] Get the brightness of a color
<?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