How to use .NET to obtain a color from the color hex code?

How (for example, from a hexadecimal color code #FFDFD991) obtained in color?

I was reading the file, and is getting a hexadecimal color codes. I need to create the corresponding hexadecimal color code System.Windows.Media.Colorexamples. Is there a built-in method to do this framework?


#1st Floor

You can see Silverlight / WPF uses hexadecimal values to set oval hex color :

your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)

#2nd Floor

The following three variants of the same colors for given. A final advantage is highlighted in the 2010 IDE Visual Studio (ReSharper may be doing), and use the appropriate color.

var cc1 = System.Drawing.ColorTranslator.FromHtml("#479DEE");

var cc2 = System.Drawing.Color.FromArgb(0x479DEE);

var cc3 = System.Drawing.Color.FromArgb(0x47, 0x9D, 0xEE);

#3rd floor

WPF:

using System.Windows.Media;

//hex to color
Color color = (Color)ColorConverter.ConvertFromString("#7AFF7A7A");

//color to hex
string hexcolor = color.ToString();

#4th floor

I assume you mean the type of HTML RGB codes (called hex code, such as # FFCC66), use ColorTranslator categories:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#FFCC66");

However, if you are using ARGB hexadecimal codes, you can use the System.Windows.Media namespace ColorConverter categories:

Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color;
//or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ;

#5th Floor

If you HashCode expressed as .GetHashCode(), then I'm afraid you will not be able to return. Not a two-way hash function, you can only "forward" and can not return.

If you need color based on hexadecimal values ​​of the colors, follow Oded advice.


#6th floor

use

System.Drawing.Color.FromArgb(myHashCode);

#7th floor

If you do not want to use ColorTranslator, you can easily:

string colorcode = "#FFFFFF00";
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);

ARGB hexadecimal color codes just representation of the value.

edit

If you need to use four instead of an integer value, you can use this value (in conjunction with more comments):

string colorcode = "#FFFFFF00";    
colorcode = colorcode.TrimStart('#');

Color col; // from System.Drawing or System.Windows.Media
if (colorcode.Length == 6)
    col = Color.FromArgb(255, // hardcoded opaque
                int.Parse(colorcode.Substring(0,2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(2,2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(4,2), NumberStyles.HexNumber));
else // assuming length of 8
    col = Color.FromArgb(
                int.Parse(colorcode.Substring(0, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(2, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(4, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(6, 2), NumberStyles.HexNumber));

Note 1 : NumberStyles in the System.Globalization.
Note 2 : Please provide your own error checking (color code should be a hexadecimal value of 6 or 8 characters)


Building # 8

I assume this is the code ... ARGB you mean System.Drawing.Colorstill System.Windows.Media.Color? The latter for example in WPF. I have not seen anyone mention it, so just in case you are looking for it:

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

House # 9

For anyone trying to convert from a hexadecimal color code for the color system, the article have become the first choice. So, I think I will add a comprehensive solution to deal with six (RGB) and 8 (ARGB) hexadecimal values.

The default settings of Microsoft, when the conversion from the RGB values ​​for the ARGB

The alpha value is implicitly 255 (fully opaque).

This means that by adding the FF to six (RGB) hexadecimal color codes, it becomes eight ARGB hexadecimal color code. Thus, it is possible to create a simple method and the RGB ARGB hexadecimal processing, and convert them to the appropriate Color structure.

    public static System.Drawing.Color GetColorFromHexValue(string hex)
    {
        string cleanHex = hex.Replace("0x", "").TrimStart('#');

        if (cleanHex.Length == 6)
        {
            //Affix fully opaque alpha hex value of FF (225)
            cleanHex = "FF" + cleanHex;
        }

        int argb;

        if (Int32.TryParse(cleanHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out argb))
        {
            return System.Drawing.Color.FromArgb(argb);
        }

        //If method hasn't returned a color yet, then there's a problem
        throw new ArgumentException("Invalid Hex value. Hex must be either an ARGB (8 digits) or RGB (6 digits)");

    }

This is by Hans Kai Siting (Hans Kesting) inspired answer.


#10th floor

To do this, use Windows Store application, followed by @Hans Kesting and @Jink answer:

    string colorcode = "#FFEEDDCC";
    int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
    tData.DefaultData = Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                          (byte)((argb & 0xff0000) >> 0x10),
                          (byte)((argb & 0xff00) >> 8),
                          (byte)(argb & 0xff));

House # 11

    private Color FromHex(string hex)
    {
        if (hex.StartsWith("#"))
            hex = hex.Substring(1);

        if (hex.Length != 6) throw new Exception("Color not valid");

        return Color.FromArgb(
            int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
            int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
            int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
    }

House # 12

I need to hexadecimal color code into System.Drawing.Color, Alice Blue shadow especially in the WPF form as a background, found that the time required to find the answers longer than expected:

using System.Windows.Media;

-

System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml("#EFF3F7");
this.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(myColor.A, myColor.R, myColor.G, myColor.B));

House # 13

You can use the following code:

Color color = System.Drawing.ColorTranslator.FromHtml("#FFDFD991");

House # 14

The most basic is:

Color.ParseColor("#ff43a047")

House # 15

There is also a simple extension method:

static class ExtensionMethods
{
    public static Color ToColor(this uint argb)
    {
        return Color.FromArgb((byte)((argb & -16777216)>> 0x18),      
                              (byte)((argb & 0xff0000)>> 0x10),   
                              (byte)((argb & 0xff00) >> 8),
                              (byte)(argb & 0xff));
    }
}

is using:

Color color = 0xFFDFD991.ToColor();
Original articles published 0 · won praise 0 · Views 2234

Guess you like

Origin blog.csdn.net/p15097962069/article/details/103891380