php - php函数将RYB颜色转换为RGB颜色
问题描述
我在 php.ini 中。
我有一个具有该值的 RYB 颜色:
$rybColor = array("r"=>0,"y"=255",b="255")
我想将其转换为 RGB 以获得
$rgbColor = array("r"=>0,"g"=>255,"b"=>0)
那有可能吗?
我在 javascript 链接中找到了一个脚本, 但对我来说有点复杂。我坚持价值观的规范化..
解决方案
绝对地。
这是您链接的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)
// }
推荐阅读
- php - 如何在外部模板文件中使用复杂的花括号
- flutter - 为包含带空格的数据的 url 调用 http get
- jooq - Jooq 数据库/模式名称映射
- python - 无法使用请求和 BeautifulSoup 定位元素
- java - Apache Beam 如何根据日期值过滤数据
- node.js - 使用 multer 上传多个文件,然后将它们保存在两个不同的 mongoose 字段中
- r - 如何为 XGBoost 中的数字预测生成特征重要性图?
- wpf - WPF XAML 设计器在从 .NETframework 迁移到 .NETCore5 后引发空引用异常
- excel - Excel VBA:Application.max 和 worksheetfunction.max
- php - Laravel 8 中的 Google SMTP 问题