Skip to content

Instantly share code, notes, and snippets.

@DevWael
Created December 17, 2018 02:26
Show Gist options
  • Select an option

  • Save DevWael/bc2972278a844ff99b9f153c1bc0a6fd to your computer and use it in GitHub Desktop.

Select an option

Save DevWael/bc2972278a844ff99b9f153c1bc0a6fd to your computer and use it in GitHub Desktop.
detect hex color is dark or light and return css class to use in to invert colors
<?php
function prefix_hex_is_light( $color ) {
if ( ! $color ) {
return false;
}
$hex = str_replace( '#', '', $color );
$c_r = hexdec( substr( $hex, 0, 2 ) );
$c_g = hexdec( substr( $hex, 2, 2 ) );
$c_b = hexdec( substr( $hex, 4, 2 ) );
$brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
if ( $brightness < 128 ) {
return 'lighting-colors';//the color is dark
} else {
return 'darking-colors';//the color is light
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment