Created
December 17, 2018 02:26
-
-
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
This file contains hidden or 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 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