7

Can anyone tell me how to convert three int vales r, g, b into a string color(hexa value) in C#

Stecya
  • 22,896
  • 10
  • 72
  • 102
saikamesh
  • 4,569
  • 11
  • 53
  • 93

6 Answers6

7
int red = 255;
int green = 255;
int blue = 255;

string theHexColor = "#" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
Tejs
  • 40,736
  • 10
  • 68
  • 86
  • 1
    Depends on your format: ie ARGB would simply be putting your alpha.ToString("X") in front of red, etc. The big point is that the ToString("X") method will return an integer value in Hex format. – Tejs Mar 29 '11 at 18:48
  • 3
    than in your example its better to write `string color = string.Format("#{0:X}{1:X}{2:X}", r,g,b);` – Stecya Mar 29 '11 at 18:51
  • Sure, that's equally as valid and more succinct! – Tejs Mar 29 '11 at 18:52
  • 1
    Further information about this method can be found here http://msdn.microsoft.com/en-us/library/8wch342y.aspx because I for one never knew about this technique until today :) – AndyPerfect Mar 29 '11 at 18:55
  • 8
    This produces an unparsable string. Use green=0 for example. You have to use "X:2" – Hans Passant Mar 29 '11 at 19:15
  • That should be `X2` not `X:2`. Here's the full thing: `string.Format("${0:X2}{1:X2}{2:X2}", r, g, b)` – jep Oct 12 '13 at 16:49
6

Try this

 string s = Color.FromArgb(255, 143, 143, 143).Name;
Stecya
  • 22,896
  • 10
  • 72
  • 102
  • I just want to point out that .Name property will not give hex value when Color is not created from Argb. (like Color.Red.Name will return "Red" value ) – HABJAN Mar 29 '11 at 18:57
  • Yeah, I know but saikamesh said that he wants to get string from RGB values – Stecya Mar 29 '11 at 18:59
  • He said he wanted "hexa value". – Hans Passant Mar 29 '11 at 19:18
  • The error message I get when I try this is 'System.Windows.Media.Color' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'System.Windows.Media.Color' could be found (are you missing a using directive or an assembly reference?) – saikamesh Mar 30 '11 at 04:26
  • 1
    you should specify that you use wpf or silverligt,`System.Windows.Media.Color` don't have definition for Name, but `System.Drawing.Color` has. – Stecya Mar 30 '11 at 06:02
3

Rgb to Hex:

string hexColor = string.Format("0x{0:X8}", Color.FromArgb(r, g, b).ToArgb());

Hex to Rgb:

int rgbColor = int.Parse("FF", System.Globalization.NumberStyles.AllowHexSpecifier);
HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • 1
    Gives error : 'System.Windows.Media.Color' does not contain a definition for 'ToArgb' and no extension method 'ToArgb' accepting a first argument of type 'System.Windows.Media.Color' could be found (are you missing a using directive or an assembly reference?) – saikamesh Mar 30 '11 at 04:28
1

Please find the following extension method:

public static class ColorExtensions
{
    public static string ToRgb(this int argb)
    {
        var r = ((argb >> 16) & 0xff);
        var g = ((argb >> 8) & 0xff);
        var b = (argb & 0xff);

        return string.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
    }
}

and here you are the usage:

        int colorBlack = 0x000000;
        int colorWhite = 0xffffff;

        Assert.That(colorBlack.ToRgb(), Is.EqualTo("000000"));
        Assert.That(colorWhite.ToRgb(), Is.EqualTo("FFFFFF"));
desunit
  • 847
  • 8
  • 12
0

Resolving this issue for a PowerPoint shape object's Fill.ForeColor.RGB member, I find the RGB values are actually BGR (blue, green, red), so the solution for a C# PowerPoint addin, converting Fill.ForeColor.RGB to a string is:

string strColor = "";
var b = ((shpTemp.Fill.ForeColor.RGB >> 16) & 0xff);
var g = ((shpTemp.Fill.ForeColor.RGB >> 8) & 0xff);
var r = (shpTemp.Fill.ForeColor.RGB & 0xff);

strColor = string.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
-1

What I did was the following:

    int reducedRed = getRed(long color)/128; // this gives you a number 1 or 0. 
    int reducedGreen = getGreen(long color)/128; // same thing for the green value; 
    int reducedBlue = getBlue(long color)/128; //same thing for blue
    int reducedColor = reducedBlue + reducedGreen*2 + reducedRed*4 ;
    // reduced Color is a number between 0 -7 

Once you have the reducedColor then perform a switch between 0 to 7.

    switch(reducedColor){
       case 0: return "000000";      // corresponds to Black
       case 1: return “0000FF";         // corresponds to Blue
       case 2: return "00FF00";       // corresponds to Green
       case 3: return "00FFFF";        // corresponds to Cyan 
       case 4: return “FF0000";          // corresponds to Red
       case 5: return "FF00FF";       //corresponds to Purple
       case 6: return "FFFF00";     //corresponds to Yellow
       case 7: return  “FFFFFF";       //corresponds to White
    }

O