将RYB颜色转换为RGB颜色的php函数

标签 php arrays colors

我在 php。

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

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

我想将它转换为 RGB 以获得

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

这有可能吗?

我在 javascript 中找到了一个脚本 link 但对我来说有点复杂。我坚持值(value)观的规范化..

最佳答案

当然。

这是一个快速 PHP versionJavaScript versionPython 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)
//   }

关于将RYB颜色转换为RGB颜色的php函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57939732/

相关文章:

php连接多个json文件并按id分组

Java 子字符串和数组

c++ - 如何声明一个数组,然后再分配它?

ios - 带有滤色器的 UIWebView

c++ - 将 12 位颜色值转换为 8 位颜色值 C++

php - 两个日期之间的小数月数

php - 使用递归函数在列表中将 SQL 帖子相互分组

javascript - 在 jQuery 中设置动态选择器

java - Eclipse 错误消息 ArrayList()

ios - 在 spritekit 中更改节点颜色