WP_Duotone::colord_parse_hex( string $hex ): array|null

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Parses any valid Hex3, Hex4, Hex6 or Hex8 string and converts it to an RGBA object.

Description

Direct port of colord’s parseHex function.

Parameters

$hexstringrequired
The hex string to parse.

Return

array|null An array of RGBA values or null if the hex string is invalid.

Source

private static function colord_parse_hex( $hex ) {
	$is_match = preg_match(
		'/^#([0-9a-f]{3,8})$/i',
		$hex,
		$hex_match
	);

	if ( ! $is_match ) {
		return null;
	}

	$hex = $hex_match[1];

	if ( 4 >= strlen( $hex ) ) {
		return array(
			'r' => (int) base_convert( $hex[0] . $hex[0], 16, 10 ),
			'g' => (int) base_convert( $hex[1] . $hex[1], 16, 10 ),
			'b' => (int) base_convert( $hex[2] . $hex[2], 16, 10 ),
			'a' => 4 === strlen( $hex ) ? round( base_convert( $hex[3] . $hex[3], 16, 10 ) / 255, 2 ) : 1,
		);
	}

	if ( 6 === strlen( $hex ) || 8 === strlen( $hex ) ) {
		return array(
			'r' => (int) base_convert( substr( $hex, 0, 2 ), 16, 10 ),
			'g' => (int) base_convert( substr( $hex, 2, 2 ), 16, 10 ),
			'b' => (int) base_convert( substr( $hex, 4, 2 ), 16, 10 ),
			'a' => 8 === strlen( $hex ) ? round( (int) base_convert( substr( $hex, 6, 2 ), 16, 10 ) / 255, 2 ) : 1,
		);
	}

	return null;
}

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.