首页 > 解决方案 > php函数将RYB颜色转换为RGB颜色

问题描述

我在 php.ini 中。

我有一个具有该值的 RYB 颜色:

$rybColor = array("r"=>0,"y"=255",b="255")

我想将其转换为 RGB 以获得

$rgbColor = array("r"=>0,"g"=>255,"b"=>0)

那有可能吗?

我在 javascript 链接中找到了一个脚本, 但对我来说有点复杂。我坚持价值观的规范化..

标签: phparrayscolors

解决方案


绝对地。

这是您链接的Python 版本JavaScript 版本的快速PHP 版本:

// 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)
//   }

推荐阅读