Converting from RGB ints to Hex
43,843
Solution 1
Try the below:
using System.Drawing;
Color myColor = Color.FromArgb(255, 181, 178);
string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");
Solution 2
Using string interpolation, this can be written as:
$"{r:X2}{g:X2}{b:X2}"
Solution 3
You can use a shorter string format to avoid string concatenations.
string.Format("{0:X2}{1:X2}{2:X2}", r, g, b)
Solution 4
You can use ColorHelper library for this:
using ColorHelper;
RGB rgb = new RGB(100, 0, 100);
HEX hex = ColorConverter.RgbToHex(rgb);
Solution 5
Greetings fellow humans,
//Red Value
int integerRedValue = 0;
//Green Value
int integerGreenValue = 0;
//Blue Value
int integerBlueValue = 0;
string hexValue = integerRedValue.ToString("X2") + integerGreenValue.ToString("X2") + integerBlueValue.ToString("X2");
Related videos on Youtube
Author by
Toadums
Updated on September 22, 2020Comments
-
Toadums over 3 years
What I have is R:255 G:181 B:178, and I am working in C# (for WP8, to be more specific)
I would like to convert this to a hex number to use as a color (to set the pixel color of a WriteableBitmap). What I am doing is the following:
int hex = (255 << 24) | ((byte)R << 16) | ((byte)G << 8) | ((Byte)B<<0);
But when I do this, I just get blue.
Any ideas what I am doing wrong?
Also, to undo this, to check the RGB values, I am going:
int r = ((byte)(hex >> 16)); // = 0 int g = ((byte)(hex >> 8)); // = 0 int b = ((byte)(hex >> 0)); // = 255
-
Sina Iravanian over 11 yearsThere's a hex format of a color used in CSS for example. Do you mean that? For example White -> FFFFFF, Blue -> 0000FF?
-
Alexei Levenkov over 11 yearsWhat do you mean "just get blue" - show what you expect and what you get... (i.e. you current code and values will give FFFFB5B2)
-
Toadums over 11 yearsI mean, instead of getting what ever color: r:255, g:181, b:178 should be (light pink), I just get 0000FF (blue)
-
Sina Iravanian over 11 yearsHave you tried to change the type-casting to
int
instead ofbyte
? For example:int hex = (255 << 24) | ((int)R << 16) | ((int)G << 8) | ((int)B<<0);
-
Alexei Levenkov over 11 yearsByte should be fine...
((255 << 24) | ((byte)255 << 16) | ((byte)181 << 8) | ((Byte)178<<0)).ToString("X")
does not give 0000FF. -
Sina Iravanian over 11 years@Toadums Your code in the question works fine and as expected. The values for r, g, and b are equal to R, G, and B respectively. If you have problem with your code, there should be something happening somewhere else.
-
-
NoPyGod over 11 yearsOr just use the ToString("X2") part on the bytes
-
NoPyGod over 11 yearsYou say "convert this to a hex number" but HEX is not a number, it's a string used to represent a number.
-
Toadums over 11 yearsOh..sorry I guess the hex is just used to make it easier for the user to see. But the pixel array of a bitmap is an int[], so I need the number to be an int (ie. figure out how to add the R,G,B)
-
NoPyGod over 11 yearsWhich property/method of WriteableBitmap requires this int?
-
Toadums over 11 yearsI was just trying to set the WriteableBitmap.Pixel array items...I managed to figure it out (I think..). I Just changed my brackets, as per the comment above ;)