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");
Share:
43,843

Related videos on Youtube

Toadums
Author by

Toadums

Updated on September 22, 2020

Comments

  • Toadums
    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
      Sina Iravanian over 11 years
      There's a hex format of a color used in CSS for example. Do you mean that? For example White -> FFFFFF, Blue -> 0000FF?
    • Alexei Levenkov
      Alexei Levenkov over 11 years
      What 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
      Toadums over 11 years
      I mean, instead of getting what ever color: r:255, g:181, b:178 should be (light pink), I just get 0000FF (blue)
    • Sina Iravanian
      Sina Iravanian over 11 years
      Have you tried to change the type-casting to int instead of byte? For example: int hex = (255 << 24) | ((int)R << 16) | ((int)G << 8) | ((int)B<<0);
    • Alexei Levenkov
      Alexei Levenkov over 11 years
      Byte should be fine... ((255 << 24) | ((byte)255 << 16) | ((byte)181 << 8) | ((Byte)178<<0)).ToString("X") does not give 0000FF.
    • Sina Iravanian
      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
    NoPyGod over 11 years
    Or just use the ToString("X2") part on the bytes
  • NoPyGod
    NoPyGod over 11 years
    You say "convert this to a hex number" but HEX is not a number, it's a string used to represent a number.
  • Toadums
    Toadums over 11 years
    Oh..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
    NoPyGod over 11 years
    Which property/method of WriteableBitmap requires this int?
  • Toadums
    Toadums over 11 years
    I 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 ;)