wp_tinycolor_string_to_rgb()
云策文档标注
概述
wp_tinycolor_string_to_rgb() 是一个已弃用的 WordPress 函数,用于解析 CSS 颜色字符串(如 hex、hsl、rgb),并返回 RGB 数组。它基于 TinyColor v1.4.2 的 JavaScript 实现,专门处理 react-color 输出的颜色格式。
关键要点
- 函数已弃用:自 WordPress 6.3.0 起标记为弃用,建议开发者避免使用。
- 功能:解析 CSS 颜色字符串,支持 hex(如 #fff、#ffffff)、hsl、rgb 格式,包括透明处理。
- 实现:直接移植自 TinyColor,使用正则表达式匹配颜色字符串,并调用辅助函数如 wp_tinycolor_rgb_to_rgb() 进行转换。
- 参数:接受一个必需的字符串参数 $color_str,表示 CSS 颜色字符串。
- 返回值:返回一个包含 'r'、'g'、'b'、'a' 键的数组,表示 RGB 和 alpha 值。
代码示例
function wp_tinycolor_string_to_rgb( $color_str ) {
_deprecated_function( __FUNCTION__, '6.3.0' );
$color_str = strtolower( trim( $color_str ) );
// 正则表达式定义和匹配逻辑
// 示例:匹配 rgb 字符串
$rgb_regexp = '/^rgb' . $permissive_match3 . '$/';
if ( preg_match( $rgb_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => $match[1],
'g' => $match[2],
'b' => $match[3],
)
);
$rgb['a'] = 1;
return $rgb;
}
// 其他格式(如 rgba、hsl、hex)的类似处理
}注意事项
- 此函数已弃用,在 WordPress 6.3.0 及更高版本中不应使用,以避免未来兼容性问题。
- 仅支持 react-color 输出的颜色格式,可能不覆盖所有 CSS 颜色变体。
- 函数内部依赖其他弃用函数如 _wp_tinycolor_bound_alpha(),需注意整体弃用链。
原文内容
Parses hex, hsl, and rgb CSS strings using the same regex as TinyColor v1.4.2 used in the JavaScript. Only colors output from react-color are implemented.
Description
Direct port of TinyColor’s function, lightly simplified to maintain consistency with TinyColor.
Parameters
$color_strstringrequired-
CSS color string.
Source
function wp_tinycolor_string_to_rgb( $color_str ) {
_deprecated_function( __FUNCTION__, '6.3.0' );
$color_str = strtolower( trim( $color_str ) );
$css_integer = '[-\+]?\d+%?';
$css_number = '[-\+]?\d*\.\d+%?';
$css_unit = '(?:' . $css_number . ')|(?:' . $css_integer . ')';
$permissive_match3 = '[\s|\(]+(' . $css_unit . ')[,|\s]+(' . $css_unit . ')[,|\s]+(' . $css_unit . ')\s*\)?';
$permissive_match4 = '[\s|\(]+(' . $css_unit . ')[,|\s]+(' . $css_unit . ')[,|\s]+(' . $css_unit . ')[,|\s]+(' . $css_unit . ')\s*\)?';
$rgb_regexp = '/^rgb' . $permissive_match3 . '$/';
if ( preg_match( $rgb_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => $match[1],
'g' => $match[2],
'b' => $match[3],
)
);
$rgb['a'] = 1;
return $rgb;
}
$rgba_regexp = '/^rgba' . $permissive_match4 . '$/';
if ( preg_match( $rgba_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => $match[1],
'g' => $match[2],
'b' => $match[3],
)
);
$rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] );
return $rgb;
}
$hsl_regexp = '/^hsl' . $permissive_match3 . '$/';
if ( preg_match( $hsl_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_hsl_to_rgb(
array(
'h' => $match[1],
's' => $match[2],
'l' => $match[3],
)
);
$rgb['a'] = 1;
return $rgb;
}
$hsla_regexp = '/^hsla' . $permissive_match4 . '$/';
if ( preg_match( $hsla_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_hsl_to_rgb(
array(
'h' => $match[1],
's' => $match[2],
'l' => $match[3],
)
);
$rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] );
return $rgb;
}
$hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
if ( preg_match( $hex8_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => base_convert( $match[1], 16, 10 ),
'g' => base_convert( $match[2], 16, 10 ),
'b' => base_convert( $match[3], 16, 10 ),
)
);
$rgb['a'] = _wp_tinycolor_bound_alpha(
base_convert( $match[4], 16, 10 ) / 255
);
return $rgb;
}
$hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
if ( preg_match( $hex6_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => base_convert( $match[1], 16, 10 ),
'g' => base_convert( $match[2], 16, 10 ),
'b' => base_convert( $match[3], 16, 10 ),
)
);
$rgb['a'] = 1;
return $rgb;
}
$hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
if ( preg_match( $hex4_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => base_convert( $match[1] . $match[1], 16, 10 ),
'g' => base_convert( $match[2] . $match[2], 16, 10 ),
'b' => base_convert( $match[3] . $match[3], 16, 10 ),
)
);
$rgb['a'] = _wp_tinycolor_bound_alpha(
base_convert( $match[4] . $match[4], 16, 10 ) / 255
);
return $rgb;
}
$hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
if ( preg_match( $hex3_regexp, $color_str, $match ) ) {
$rgb = wp_tinycolor_rgb_to_rgb(
array(
'r' => base_convert( $match[1] . $match[1], 16, 10 ),
'g' => base_convert( $match[2] . $match[2], 16, 10 ),
'b' => base_convert( $match[3] . $match[3], 16, 10 ),
)
);
$rgb['a'] = 1;
return $rgb;
}
/*
* The JS color picker considers the string "transparent" to be a hex value,
* so we need to handle it here as a special case.
*/
if ( 'transparent' === $color_str ) {
return array(
'r' => 0,
'g' => 0,
'b' => 0,
'a' => 0,
);
}
}