wc_light_or_dark

Detect if we should use a light or dark color on a background color.

Description

(string) wc_light_or_dark( (mixed) $color, (string) $dark = '#000000', (string) $light = '#FFFFFF' ); 

Returns (string)

Parameters (3)

0. $color (mixed)
The color.
1. $dark — Optional. (string) => '#000000'
(default: #000000)
2. $light — Optional. (string) => '#FFFFFF'
(default: #FFFFFF)

Usage

  1. if ( !function_exists( 'wc_light_or_dark' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-formatting-functions.php'; 
  3.  
  4. // The color. 
  5. $color = null; 
  6.  
  7. // (default: '#000000') 
  8. $dark = '#000000'; 
  9.  
  10. // (default: '#FFFFFF') 
  11. $light = '#FFFFFF'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_light_or_dark($color, $dark, $light); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-formatting-functions.php  
  1. function wc_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { 
  2.  
  3. $hex = str_replace( '#', '', $color ); 
  4.  
  5. $c_r = hexdec( substr( $hex, 0, 2 ) ); 
  6. $c_g = hexdec( substr( $hex, 2, 2 ) ); 
  7. $c_b = hexdec( substr( $hex, 4, 2 ) ); 
  8.  
  9. $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; 
  10.  
  11. return $brightness > 155 ? $dark : $light;