Skip to content

Instantly share code, notes, and snippets.

@jeffreyvr
Last active February 15, 2018 14:00
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 jeffreyvr/95ea6f6eccc1bd8645c5cb5e3d284087 to your computer and use it in GitHub Desktop.
Save jeffreyvr/95ea6f6eccc1bd8645c5cb5e3d284087 to your computer and use it in GitHub Desktop.
Color brightness rgb
<?php
function color_brightness($hex) {
$hex = str_replace('#', '', $hex);
$strl = strlen($hex)/3;
$c_r = hexdec(substr($hex, 0, $strl));
$c_g = hexdec(substr($hex, $strl, $strl));
$c_b = hexdec(substr($hex, $strl*2, $strl));
$brightness = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000;
}
$color = "#000";
$brightness = color_brightness($color);
if ( $brightness > 130 ) {
echo 'bright';
} else {
echo 'dark';
}
?>
<div style="height:100px;width:100px;background:<?php echo $color; ?>"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment