使用 PHP 将虚幻颜色代码转换为 HTML/CSS RGBA 或十六进制

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/Hexidecimal 数据类型。已经有一个名为 ArkStatsExtractor 的程序可以将生物导出文件中的数据解析为该程序,使其易于阅读。

查看他们的代码,我发现这个函数(在 this file 中)似乎完成了将此值转换为 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#一窍不通,不明白这个方法是怎么得到最终值的。

我在这里基本上需要这一行,制作成 PHP 函数,returns RGB,或 RGB (0-255) 的一部分,然后我可以用它来制定实际的 RGB值,并将其传递到我的数据库。

int v = (int)(255.999f * Math.Pow(lc, 1f / 2.2f));

我的一些问题是:

  1. 为什么这个数学方程中的某些值后面有一个字母 f
  2. Math.Pow 究竟是做什么的?这是 Unreal 函数,还是 repo 所有者自定义的函数。
  3. 是否需要更多信息才能完成此转换?可能在另一个文件中?
  1. “f”在这里表示“它是一个浮点数”,例如 14,5,默认情况下是双精度数,当您这样做时: float numb = 14.5; 你正在做双人演员。所以要正确地做:float numb=14.5f;

  2. Math.pow(x,y) == x^y 这是幂,不,它来自“Math.h” https://www.w3schools.com/cpp/cpp_math.asp

  3. 这个功能没有,你只需要使用它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