J. Robinson
J. Robinson

Reputation: 929

Converting a Unreal Color Code to HTML/CSS RGBA or Hexidecimal using PHP

I play a game called Ark: Survival Evolved. I have recently come across the export function in game that allows a print out of a creatures stats, including color codes.

A following is an example of what is output upon export.

[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)

Upon further digging I've realized that this is a Unreal color value, and can not be easily converted to an HTML acceptable RGB/RGBA/Hexidecimal datatype. There is a program that is already made called ArkStatsExtractor that parses the data from the creatures exported file to that program that makes it easily readable.

Looking into their code, I have found this function (in this file) that appears to do the heavy lifting of converting this value into an RGB color code.

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

I'm using PHP as the servers backend to do this conversion. So, I understand the basic functionality of this method. V is equal to value, and the equation is type casted to an integer value, then does some math. However my understanding of C# is absolutely nothing, and I don't understand how the method gets the final value.

I basically need this line here, made into a PHP function that returns the RGB, or one portion of RGB (0-255) that I can then use to formulate the actual RGB value, and pass that along to my database.

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

Some of my questions are:

  1. Why is there a letter f after some of the values in this mathematical equation?
  2. What exactly does Math.Pow do? Is this a Unreal function, or is this a function that is custom made by the owner of the repo.
  3. Is there more information needed to complete this conversion? Something in another file maybe?

Upvotes: 0

Views: 857

Answers (2)

Sammitch
Sammitch

Reputation: 32232

With @745th's note on the syntax, the translation should be simple:

$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
));

Output

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

Though the whole scheme seems overwrought just to represent linear color values, but hey they didn't invite me to the UE4 design meetings. :P

Upvotes: 1

745th
745th

Reputation: 61

  1. The "f" is here to say "it's a float " for example 14,5, is a double by default, and when you do: float numb = 14.5; you are doing a cast from a double. So to do correctly : float numb=14.5f;

  2. Math.pow(x,y) == x^y it's the power and no it's come from "Math.h" https://www.w3schools.com/cpp/cpp_math.asp

  3. For this function no, you just need to use it 3 times (lc is one color from red green or blue)

Upvotes: 1

Related Questions