How to Get the Name of Color While Having Its Rgb Value in C#

Get color Name from color class

You can get the name from KnownColor. Try like below

        string name = "Unknown";
foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor)))
{
Color known = Color.FromKnownColor(kc);
if (Color.FromArgb(255,255,140,0).ToArgb() == known.ToArgb())
{
label1.Text = known.Name;
break;
}
}

Here I just hard code your value and return the name in label named 'label1'.

Check this thread http://social.msdn.microsoft.com/Forums/vstudio/en-US/3c80583e-d0a9-45e9-842a-bd7258f1fd2f/get-color-name-in-c?forum=csharpgeneral

C# - How to change Fore Color to custom RGB value

statusStripTestLabel.ForeColor = Color.FromArgb(128, 64, 64); //Alpha is implicitly 255.

statusStripTestLabel.ForeColor = Color.FromArgb(255, 128, 64, 64); //Alpha is explicitly 255. You can change the value of the first parameter to specify the alpha you want.

Get near or equivalent color name from hex decimal value windows apps

Here is my solution:

Firstly, I made a custom Class:

public class ColorReference
{
public string Name { get; set; }
public Vector3 Argb { get; set; }
}

This is to construct the known color which get from this site

private static ColorReference[] GetColorReferences()
{
return new ColorReference[] {
new ColorReference() { Name="AliceBlue", Argb=new Vector3 (
240,248,255) },
new ColorReference() { Name="LightSalmon", Argb=new Vector3 (

255,160,122) },
......
};
}

Secondly, I treat these Vectors as three-dimensional vectors, for a single vector, I can get the closest one based on Vector3.Distance method.
enter image description here

private static ColorReference GetClosestColor(ColorReference[] colorReferences, Vector3 currentColor)
{
ColorReference tMin = null;
float minDist = float.PositiveInfinity;

foreach (ColorReference t in colorReferences)
{
float dist = Vector3.Distance(t.Argb, currentColor);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}

Use the above method to get the nearest color's name:

public static string GetNearestColorName(Vector3 vect)
{
var cr = GetClosestColor(GetColorReferences(), vect);
if( cr != null )
{
return cr.Name;
}
else
return string.Empty;
}

Also need this method to extract argb value from hex demical value:

public static Vector3 GetSystemDrawingColorFromHexString(string hexString)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(hexString, @"[#]([0-9]|[a-f]|[A-F]){6}\b"))
throw new ArgumentException();
int red = int.Parse(hexString.Substring(1, 2), NumberStyles.HexNumber);
int green = int.Parse(hexString.Substring(3, 2), NumberStyles.HexNumber);
int blue = int.Parse(hexString.Substring(5, 2), NumberStyles.HexNumber);
return new Vector3(red, green, blue);
}

Screenshot:

enter image description here

Check my completed demo from here: Github Link

----------Update 07/26/2016--------

For Windows/Phone 8.1, because the Vector3 class is missing, use the following class in your project:

public class Vector3
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }

public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public static float Distance(Vector3 a, Vector3 b)
{
return (float)Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2) + Math.Pow(a.Z - b.Z, 2)); ;
}
}

enter image description here

Convert System.Drawing.Color to RGB and Hex Value

I'm failing to see the problem here. The code looks good to me.

The only thing I can think of is that the try/catch blocks are redundant -- Color is a struct and R, G, and B are bytes, so c can't be null and c.R.ToString(), c.G.ToString(), and c.B.ToString() can't actually fail (the only way I can see them failing is with a NullReferenceException, and none of them can actually be null).

You could clean the whole thing up using the following:

private static String HexConverter(System.Drawing.Color c)
{
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}

private static String RGBConverter(System.Drawing.Color c)
{
return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")";
}

How to get Selected color value in rgb format with using colorPickerUIAdv1 Control of Syncfusion in C# in Winform Application

I have Solved my problem using this line :

 selectedcolor = colorPickerUIAdv1.SelectedColor.ToArgb();

The selected color is getter setter property of int type and i have successfully getting RGB value from colorPickerUIAdv1.
Thank you all for taking your helpful time to answering me or giding me...your solution work not getting question i.e. Selected colors RGB value from colorPickerUIAdv1.



Related Topics



Leave a reply



Submit