WP_Duotone::colord_parse_hex()private staticWP 6.3.0

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

Direct port of colord's parseHex function.

Метод класса: WP_Duotone{}

Хуков нет.

Возвращает

Массив|null. An array of RGBA values or null if the hex string is invalid.

Использование

$result = WP_Duotone::colord_parse_hex( $hex );
$hex(строка) (обязательный)
The hex string to parse.

Список изменений

С версии 6.3.0 Введена.

Код WP_Duotone::colord_parse_hex() WP 6.6.2

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;
}