Support » Plugin: Dominant Color » Getting Color Brightness

  • For anyone wanting to set the text color based on the brightness of the dominant color, I created this function.

    function dc_dynamic_color( $color, $dark = '#000', $light = '#FFF' ) {
    	/* Dynamic Text Color */
    	$hex = str_replace("#", "", $color);
    
    	$r = hexdec(substr($hex,0,2));
    	$g = hexdec(substr($hex,2,2));
    	$b = hexdec(substr($hex,4,2));
    
    	if ($r + $g + $b > 382) {
    		$text_color = $dark;
    	} else {
    		$text_color = $light;
    	}
    	
    	return $text_color;
    }
    add_action( '', 'dc_dynamic_color' );

    dc_dynamic_color('#bc130b') would return #FFF.

    You can also pass css classes in $dark and $light.

    NOTE: You must use hexadecimal for $color.

    • This topic was modified 4 years, 2 months ago by dadiocoleman.
  • The topic ‘Getting Color Brightness’ is closed to new replies.