Get color name by HEX or RGB
You can use Name that Color.
Example:
let result = ntc.name('#6195ed');
let rgb_value = result[0]; // #6495ed : RGB value of closest match
let specific_name = result[1]; // Cornflower Blue : Color name of closest match
let is_exact_match = result[2]; // false : True if exact color match
There is also a variation of Name that Color that includes additional parameters:
http://www.color-blindness.com/color-name-hue-tool/js/ntc.js
Example:
let result = ntc.name('#6195ed');
let rgb_value = result[0]; // #6495ed : RGB value of closest match
let specific_name = result[1]; // Cornflower Blue : Color name of closest match
let shade_value = result[2]; // #0000ff : RGB value of shade of closest match
let shade_name = result[3]; // Blue : Color name of shade of closest match
let is_exact_match = result[4]; // false : True if exact color match
From hex color code or RGB to color name using R
Approach is similar to @Richard Ambler's solution to compare distance between test rgb vector and
expansive set of colour mapping from output of colours()
.
The function rgb2col
below with given test rgb values returns approximate matching colour name
Data:
library(scales) #for function show_col
DF = read.table(text="Color Count red green blue
ED1B24 16774 237 27 36
000000 11600 0 0 0
23B14D 5427 35 177 77
FFFFFF 5206 255 255 255
FEF200 3216 254 242 0
ED1B26 344 237 27 38",header=TRUE,stringsAsFactors=FALSE)
#from https://gist.github.com/mbannert/e9fcfa86de3b06068c83
rgb2hex <- function(r,g,b) rgb(r, g, b, maxColorValue = 255)
Function:
rgb2col = function(r,g,b) {
#create colour name vs. rgb mapping table
colourMap = data.frame(colourNames = colours(),t(col2rgb(colours())))
#input test colours
testDF = data.frame(colourNames="testCol",red = r,green = g,blue = b)
#combine both tables
combDF = rbind(testDF,colourMap)
#convert in matrix representation
combMat= (as.matrix(combDF[,-1]))
#add row labels as colour names
rownames(combMat) = combDF[,1]
#compute euclidean distance between test rgb vector and all the colours
#from mapping table
#using dist function compute distance matrix,retain only upper matrix
#find minimum distance point from test vector
#find closest matching colour name
approxMatchCol = which.min(as.matrix(dist(combMat,upper=TRUE))[1,][-1])
#compare test colour with approximate matching colour
scales::show_col(c(rgb2hex(r,g,b),rgb2hex(colourMap[approxMatchCol,2:4])))
#return colour name
return(approxMatchCol)
}
Output:
sapply(1:nrow(DF),function(x) rgb2col(DF[x,"red"],DF[x,"green"],DF[x,"blue"]))
#firebrick2 black seagreen white yellow firebrick2
# 135 24 574 1 652 135
Plots:
How could I get a hex code when I give a color name
The other answers are fine. If you're looking for a gem that's already done the work for you, though, take a look at Color. Its Color::CSS[]
method looks up a color by name and returns a Color::RGB object, which in turn has hex
and html
methods:
require "color"
aliceblue = Color::CSS["aliceblue"]
puts aliceblue.hex
# => f0f8ff
puts aliceblue.html
# => #f0f8ff
Color::RGB also has a by_hex
static method, which will return a named Color::RGB object for the given hex code. The name
method returns the name (if it has one):
require "color"
mystery_color = Color::RGB.by_hex("#ffefd5")
puts mystery_color.name
# => papayawhip
You can see it in action on repl.it: https://repl.it/@jrunning/EqualReasonableSpellchecker (If you get an error the first time you hit the run button, hit it again. repl.it sometimes has trouble with loading gems the first time.)
Convert hex color code to color name
You may use the convenience function color.id
from the plotrix
package*:
Given a color specified as a hex string, find the closest match in the table of known (named) colors.
library(plotrix)
sapply(rainbow(4), color.id)
# $`#FF0000FF`
# [1] "red" "red1"
#
# $`#80FF00FF`
# [1] "chartreuse" "chartreuse1"
#
# $`#00FFFFFF`
# [1] "cyan" "cyan1"
#
# $`#8000FFFF`
# [1] "purple"
*Credits to Jim Lemon and his answer here: Convert color hex code to color names.
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.
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:
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)); ;
}
}
Related Topics
How to Reliably Get Screen Width with the Scrollbar
Set the Webkit-Keyframes From/To Parameter with JavaScript
Why Browser Is Returning Empty String on Style.Height ? How to Get Actual Height of an Element
Fcm - Programmatically Send Push Notification to User Segments
Function in JavaScript That Can Be Called Only Once
Is There an Equivalent of the _Nosuchmethod_ Feature for Properties, or a Way to Implement It in Js
How to Define Two Angular Apps/Modules in One Page
How to Get a JavaScript Stack Trace When I Throw an Exception
How to Get "Get" Request Parameters in JavaScript
CSS and Jquery: Spaces Inside Image Name Break Code of Url()
How to Increase the Font Size Based on the Window Width
Jquery CSS Rendering - Works in Firefox, Not in Chrome
How Does Linux Emulator in JavaScript by Fabrice Bellard Work
Jquery Ajax Success Callback Function Definition
Google Chrome Console.Log() Inconsistency with Objects and Arrays
No 'Access-Control-Allow-Origin' Header Is Present on the Requested Resource Error
How to Find If an Array Contains a Specific String in JavaScript/Jquery