[英]Converting a Unreal Color Code to HTML/CSS RGBA or Hexidecimal using PHP
我玩了一个叫方舟:生存进化的游戏。 我最近在游戏中遇到了导出功能,该功能允许打印出生物统计数据,包括颜色代码。
以下是导出时输出内容的示例。
[Colorization]
ColorSet[0]=(R=0.002428,G=0.088656,B=0.124772,A=0.000000)
ColorSet[1]=(R=0.100000,G=0.020000,B=0.020000,A=0.000000)
ColorSet[2]=(R=1.000000,G=0.215861,B=0.000000,A=0.000000)
ColorSet[3]=(R=0.002428,G=0.088656,B=0.124772,A=0.000000)
ColorSet[4]=(R=0.225000,G=0.011337,B=0.005625,A=0.000000)
ColorSet[5]=(R=1.000000,G=0.391573,B=0.039546,A=0.000000)
进一步挖掘后,我意识到这是一个 Unreal 颜色值,不能轻易转换为 HTML 可接受的 RGB/RGBA/十六进制数据类型。 已经有一个名为ArkStatsExtractor 的程序可以解析来自生物导出文件的数据到该程序,使其易于阅读。
查看他们的代码,我发现这个函数(在这个文件中)似乎完成了将这个值转换为 RGB 颜色代码的繁重工作。
/// <summary>
/// Convert the color definition of the unreal engine to default RGB-values
/// </summary>
/// <param name="lc"></param>
/// <returns></returns>
private static int LinearColorComponentToColorComponentClamped(double lc)
{
//int v = (int)(255.999f * (lc <= 0.0031308f ? lc * 12.92f : Math.Pow(lc, 1.0f / 2.4f) * 1.055f - 0.055f)); // this formula is only used since UE4.15
// ARK uses this simplified formula
int v = (int)(255.999f * Math.Pow(lc, 1f / 2.2f));
if (v > 255) return 255;
if (v < 0) return 0;
return v;
}
我使用 PHP 作为服务器后端来进行这种转换。 所以,我了解这种方法的基本功能。 V
等于值,方程被类型转换为整数值,然后做一些数学运算。 然而我对C#的理解完全是一无是处,不明白这个方法是如何得到最终值的。
我在这里基本上需要这一行,做成一个返回 RGB 或 RGB (0-255) 的一部分的 PHP 函数,然后我可以用它来制定实际的 RGB 值,并将其传递给我的数据库。
int v = (int)(255.999f * Math.Pow(lc, 1f / 2.2f));
我的一些问题是:
f
? “f”在这里表示“它是一个浮点数”,例如 14,5,默认情况下是双float numb = 14.5;
,当你这样做时: float numb = 14.5;
你正在做一个替身演员。 所以要正确执行: float numb=14.5f;
Math.pow(x,y) == x^y 这是力量,不是来自“Math.h” https://www.w3schools.com/cpp/cpp_math.asp
对于这个功能不,你只需要使用它3次(lc是红绿或蓝中的一种颜色)
使用@745th 关于语法的注释,翻译应该很简单:
$colors_in = [
['R'=>0.002428,'G'=>0.088656,'B'=>0.124772,'A'=>0.000000],
['R'=>0.100000,'G'=>0.020000,'B'=>0.020000,'A'=>0.000000],
['R'=>1.000000,'G'=>0.215861,'B'=>0.000000,'A'=>0.000000],
['R'=>0.002428,'G'=>0.088656,'B'=>0.124772,'A'=>0.000000],
['R'=>0.225000,'G'=>0.011337,'B'=>0.005625,'A'=>0.000000],
['R'=>1.000000,'G'=>0.391573,'B'=>0.039546,'A'=>0.000000]
];
function convert_ue4($lc) {
// int v = (int)(255.999f * Math.Pow(lc, 1f / 2.2f));
$v = intval(255.999 * pow($lc, 1 / 2.2));
if( $v > 255 ) { return 255; }
if( $v < 0 ) { return 0; }
return $v;
}
var_dump(array_map(
function($arr) { return array_map('convert_ue4', $arr); },
$colors_in
));
输出
array(6) {
[0]=>
array(4) {
["R"]=>
int(16)
["G"]=>
int(85)
["B"]=>
int(99)
["A"]=>
int(0)
}
[1]=>
array(4) {
["R"]=>
int(89)
["G"]=>
int(43)
["B"]=>
int(43)
["A"]=>
int(0)
}
[2]=>
array(4) {
["R"]=>
int(255)
["G"]=>
int(127)
["B"]=>
int(0)
["A"]=>
int(0)
}
[3]=>
array(4) {
["R"]=>
int(16)
["G"]=>
int(85)
["B"]=>
int(99)
["A"]=>
int(0)
}
[4]=>
array(4) {
["R"]=>
int(129)
["G"]=>
int(33)
["B"]=>
int(24)
["A"]=>
int(0)
}
[5]=>
array(4) {
["R"]=>
int(255)
["G"]=>
int(167)
["B"]=>
int(58)
["A"]=>
int(0)
}
}
虽然整个方案似乎只是为了表示线性颜色值而过度紧张,但嘿,他们没有邀请我参加 UE4 设计会议。 :P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.