Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting RGB float data to hex string [closed]

Tags:

c#

rgb

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?

like image 846
Alexander Forbes-Reed Avatar asked Oct 22 '25 01:10

Alexander Forbes-Reed


2 Answers

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));
like image 191
paul Avatar answered Oct 23 '25 14:10

paul


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

like image 20
SynerCoder Avatar answered Oct 23 '25 14:10

SynerCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!