Home Forums Feature Requests Colorpicker get darkness of the color Reply To: Colorpicker get darkness of the color

  • I created a function based on a Stack overflow answer (can’t find the page anymore otherwise I would have linked to it).

    function get_darker_or_light_class_based_on_hex ( $hex_color ) {
    	$hex = str_replace( '#', '', $hex_color );
    	$r = hexdec(substr($hex,0,2));
    	$g = hexdec(substr($hex,2,2));
    	$b = hexdec(substr($hex,4,2));
    	if($r + $g + $b < 450){
    		$class = 'dark-bg';
    		$class = 'light-bg';
    	return $class;

    I had to change the 450 value to get it to work with my colors. And in my template part I use <?php echo get_darker_or_light_class_based_on_hex(get_sub_field('bg-color'); ?> to get the right background class.

    Hope someone finds this usefull.