I am creating an application to find the most used color of an image, i am up to getting the RGB value of the color, but how to get the color name, help plz.
The rgb() function define colors using the Red-green-blue (RGB) model. An RGB color value is specified with: rgb(red, green, blue). Each parameter defines the intensity of that color and can be an integer between 0 and 255 or a percentage value (from 0% to 100%).
RGB color is best suited for on-screen applications, such as graphic design. Each color channel is expressed from 0 (least saturated) to 255 (most saturated). This means that 16,777,216 different colors can be represented in the RGB color space.
It really comes down to math and getting a value between 0-1. Since 255 is the maximum value, dividing by 255 expresses a 0-1 representation. Each channel (Red, Green, and Blue are each channels) is 8 bits, so they are each limited to 256, in this case 255 since 0 is included.
As noted in comments, the KnownColor
enumeration can be used to make this simpler:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
class Test
{
static void Main()
{
Color color = Color.FromArgb(255, 0, 0);
Console.WriteLine(color.Name); // ffff0000
var colorLookup = Enum.GetValues(typeof(KnownColor))
.Cast<KnownColor>()
.Select(Color.FromKnownColor)
.ToLookup(c => c.ToArgb());
// There are some colours with multiple entries...
foreach (var namedColor in colorLookup[color.ToArgb()])
{
Console.WriteLine(namedColor.Name);
}
}
}
Original answer
Color.FromArgb
will give you a Color
, but it will never have a name. You need to use reflection to get the named colours, as far as I'm aware.
Here's another version of Cole Campbell's solution which I was working up at the same time...
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
class Test
{
static void Main()
{
Color color = Color.FromArgb(255, 0, 0);
Console.WriteLine(color.Name); // ffff0000
var colorLookup = typeof(Color)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Select(f => (Color) f.GetValue(null, null))
.Where(c => c.IsNamedColor)
.ToLookup(c => c.ToArgb());
// There are some colours with multiple entries...
foreach (var namedColor in colorLookup[color.ToArgb()])
{
Console.WriteLine(namedColor.Name);
}
}
}
This method uses reflection to examine the predefined colors on the Color
class and compare them against the color passed in as an argument. This can be optimized further, but it should give you an idea of the general technique.
private static String GetColorName(Color color)
{
var predefined = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);
var match = (from p in predefined where ((Color)p.GetValue(null, null)).ToArgb() == color.ToArgb() select (Color)p.GetValue(null, null));
if (match.Any())
return match.First().Name;
return String.Empty;
}
You should be able to use the Color class from System.Drawing namespace, it has a static method FromARGB that returns a Color object. It has several overloads, one allowing you to enter the RGB values like so:
var color = Color.FromArgb(100, 5,5,5).Name;
For something quick and simple, try this (in WPF):
public string GetNameOfColor(Color color) {
var colorProperty = typeof(Colors).GetProperties().FirstOrDefault(p =>
(Color)(p.GetValue(p, null)) == color);
return (colorProperty != null) ? colorProperty.Name : color.ToString();
}
In Visual Studio 2010, the p.GetValue(p, null)
is required. In Visual Studio 2013+ you can just use p.GetValue(p)
.
The advantage of this technique, besides its brevity, is that it doesn't require references to System.Drawing
or System.Reflection
but allows the user to remain inside the System.Windows
namespace, which is WPF. It does require a reference to System.Windows.Media
which you should probably already have if you are working with Colors in WPF. If you are like me, you try not to add System.Drawing
to your WPF applications without a good need. As to why stay inside the WPF namespace, its a matter of preference. See for instance discussions at WPF v/s System.Drawing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With