函数文档

wp_tinycolor_hsl_to_rgb()

💡 云策文档标注

概述

wp_tinycolor_hsl_to_rgb() 是一个 WordPress 函数,用于将 HSL 颜色对象转换为 RGB 颜色对象,并对值进行转换和四舍五入。该函数是 TinyColor 库函数的直接移植版本,自 WordPress 6.3.0 起已被弃用。

关键要点

  • 函数 wp_tinycolor_hsl_to_rgb() 接受一个 HSL 数组参数,返回一个转换后的 RGB 数组。
  • 内部使用 wp_tinycolor_bound01() 将 HSL 值归一化到 [0, 1] 范围,并包含颜色转换逻辑。
  • 该函数自 WordPress 6.3.0 起被标记为弃用,建议开发者避免在新代码中使用。
  • 相关函数包括 wp_tinycolor_bound01()、wp_tinycolor_hue_to_rgb() 和 _deprecated_function()。

代码示例

function wp_tinycolor_hsl_to_rgb( $hsl_color ) {
    _deprecated_function( __FUNCTION__, '6.3.0' );

    $h = wp_tinycolor_bound01( $hsl_color['h'], 360 );
    $s = wp_tinycolor_bound01( $hsl_color['s'], 100 );
    $l = wp_tinycolor_bound01( $hsl_color['l'], 100 );

    if ( 0 === $s ) {
        // Achromatic.
        $r = $l;
        $g = $l;
        $b = $l;
    } else {
        $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
        $p = 2 * $l - $q;
        $r = wp_tinycolor_hue_to_rgb( $p, $q, $h + 1/3 );
        $g = wp_tinycolor_hue_to_rgb( $p, $q, $h );
        $b = wp_tinycolor_hue_to_rgb( $p, $q, $h - 1/3 );
    }

    return array(
        'r' => round( $r * 255 ),
        'g' => round( $g * 255 ),
        'b' => round( $b * 255 ),
    );
}

注意事项

  • 该函数已被弃用,开发者应寻找替代方案或更新代码以避免依赖弃用功能。
  • 函数内部逻辑涉及颜色空间转换,使用时需确保输入 HSL 数组格式正确。

📄 原文内容

Converts an HSL object to an RGB object with converted and rounded values.

Description

Direct port of TinyColor’s function, lightly simplified to maintain consistency with TinyColor.

Parameters

$hsl_colorarrayrequired
HSL object.

Return

array Rounded and converted RGB object.

Source

function wp_tinycolor_hsl_to_rgb( $hsl_color ) {
	_deprecated_function( __FUNCTION__, '6.3.0' );

	$h = wp_tinycolor_bound01( $hsl_color['h'], 360 );
	$s = wp_tinycolor_bound01( $hsl_color['s'], 100 );
	$l = wp_tinycolor_bound01( $hsl_color['l'], 100 );

	if ( 0 === $s ) {
		// Achromatic.
		$r = $l;
		$g = $l;
		$b = $l;
	} else {
		$q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
		$p = 2 * $l - $q;
		$r = wp_tinycolor_hue_to_rgb( $p, $q, $h + 1 / 3 );
		$g = wp_tinycolor_hue_to_rgb( $p, $q, $h );
		$b = wp_tinycolor_hue_to_rgb( $p, $q, $h - 1 / 3 );
	}

	return array(
		'r' => $r * 255,
		'g' => $g * 255,
		'b' => $b * 255,
	);
}

Changelog

Version Description
6.3.0 Deprecated.
5.8.0 Introduced.