Reputation: 929
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:
f
after some of the values in this mathematical equation?Upvotes: 0
Views: 857
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
Reputation: 61
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;
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
For this function no, you just need to use it 3 times (lc is one color from red green or blue)
Upvotes: 1