【发布时间】:2020-01-16 06:44:01
【问题描述】:
我在php中。
我有一个具有该值的 RYB 颜色:
$rybColor = array("r"=>0,"y"=255",b="255")
我想把它转换成RGB以便得到
$rgbColor = array("r"=>0,"g"=>255,"b"=>0)
这可能吗?
我在 javascript 中找到了一个脚本 link 但这对我来说有点复杂。我坚持价值观的规范化..
【问题讨论】:
我在php中。
我有一个具有该值的 RYB 颜色:
$rybColor = array("r"=>0,"y"=255",b="255")
我想把它转换成RGB以便得到
$rgbColor = array("r"=>0,"g"=>255,"b"=>0)
这可能吗?
我在 javascript 中找到了一个脚本 link 但这对我来说有点复杂。我坚持价值观的规范化..
【问题讨论】:
绝对的。
这是您链接的JavaScript version 的PHP version 的快速Python version:
// RYB color to RGB color
function RYB2RGB($iRed, $iYellow, $iBlue){
// Remove the whiteness from the color.
$iWhite = min($iRed, $iYellow, $iBlue);
$iRed -= $iWhite;
$iYellow -= $iWhite;
$iBlue -= $iWhite;
$iMaxYellow = max($iRed, $iYellow, $iBlue);
// Get the green out of the yellow and blue
$iGreen = min($iYellow, $iBlue);
$iYellow -= $iGreen;
$iBlue -= $iGreen;
if ($iBlue > 0 && $iGreen > 0)
{
$iBlue *= 2.0;
$iGreen *= 2.0;
}
// Redistribute the remaining yellow.
$iRed += $iYellow;
$iGreen += $iYellow;
// Normalize to values.
$iMaxGreen = max($iRed, $iGreen, $iBlue);
if ($iMaxGreen > 0)
{
$iN = $iMaxYellow / $iMaxGreen;
$iRed *= $iN;
$iGreen *= $iN;
$iBlue *= $iN;
}
// Add the white back $in.
$iRed += $iWhite;
$iGreen += $iWhite;
$iBlue += $iWhite;
// Save the RGB
$RGB = [floor($iRed), floor($iGreen), floor($iBlue)];
return $RGB
}
$R = 98;
$y = 152;
$b = 223;
var_dump( RYB2RGB( $R, $y, $b ) ); //
// array(3) {
// [0]=>
// float(98)
// [1]=>
// float(193)
// [2]=>
// float(223)
// }
【讨论】: