Automattic\WooCommerce\EmailEditor\Integrations\Utils

Social_Links_Helper::detect_whiteish_colorpublic staticWC 1.0

Detects if the input color is whiteish. This is a helper function to detect if the input color is white or white-ish colors when provided an input color in format #ffffff or #fff.

Method of the class: Social_Links_Helper{}

No Hooks.

Returns

true|false. True if the color is whiteish, false otherwise.

Usage

$result = Social_Links_Helper::detect_whiteish_color( $input_color );
$input_color(string) (required)
The input color.

Social_Links_Helper::detect_whiteish_color() code WC 10.5.0

public static function detect_whiteish_color( $input_color ) {

	if ( empty( $input_color ) ) {
		return false;
	}

	// Remove # if present.
	$color = ltrim( $input_color, '#' );

	// Convert 3-digit hex to 6-digit hex.
	if ( strlen( $color ) === 3 ) {
		$color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
	}

	// Convert hex to RGB.
	$r = hexdec( substr( $color, 0, 2 ) );
	$g = hexdec( substr( $color, 2, 2 ) );
	$b = hexdec( substr( $color, 4, 2 ) );

	// Calculate brightness using perceived brightness formula.
	// Using the formula: (0.299*R + 0.587*G + 0.114*B).
	$brightness = ( 0.299 * $r + 0.587 * $g + 0.114 * $b );

	// Consider colors with brightness above 240 as whiteish.
	// This threshold can be adjusted based on requirements.
	return $brightness > 240;
}