I'm writing a tool to modify a game, and the game uses float's to store RGB data (ranging from 0.0 -> 1.0). How would you best suggest I go about converting from float RGB Data to a Hex String/Byte RGB Data?
var rgbString = string.Format("#{0}{1}{2}",
((int)(redValue * 255)).ToString("X2"),
((int)(greenValue * 255)).ToString("X2"),
((int)(blueValue * 255)).ToString("X2"));
edited (perhaps more readable code):
var rgbString = string.Format("#{0:X2}{1:X2}{2:X2}",
(int)(redValue * 255),
(int)(greenValue * 255),
(int)(blueValue * 255));
float rVal = 0.5f;
float gVal = 0.94140625f;
float bVal = 0.21484375f;
byte rByte = (byte)(rVal * 256);
byte gByte = (byte)(gVal * 256);
byte bByte = (byte)(bVal * 256);
string rgb = rByte.ToString("X2") + gByte.ToString("X2") + bByte.ToString("X2");
Result:
80F137
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With