How to convert a hex value into a color name - colors

Is there an algorithm (I've searched and haven't come up with much) that, given a hex value, is capable of returning the color name? It doesn't have to be anything too specific, any shade of blue coming back as just 'blue' is fine.
Or short of that, is there a large list somewhere that consists of hex values and their corresponding color family. I've found lists, but they only seem to be a couple hundred colors long.
I saw this post, but the solution there seems to only break the color down into three regions.
EDIT
I altered the RGB text file so that each color would only fall into the following color ranges:
white
grey
magenta
brown
black
blue
green
turquoise
yellow
orange
red

As you said you had already found lists, then it is simple.
Calculate Euclidean Distance of the color for RGB (for instance), and call that color the nearest name.
By the way, over a hundred colors is actually a big number for human languages, and even for just a hundred colors, ordinary people may not able to tell the differences between quite a few colors.

rgb.txt is the usual list used for color names. Just find the closest triplet and call it that.

Related

Given two different colour hex codes find the colour which contrasts them both equally

If I have two arbitrary colours, knowing their hex codes, is it possible to find the color which which contrasts each of them equally while being as contrasting to both as possible?
For example for Pure white and Pure Black the color which contrasted both equally would be grey, but is there a way to extend this to any two colours?
It doesn't matter to me which colourspace is used.
Im not sure if there is a name for this, but I was only able to find information on finding one color which contrasted another.

How to get a color by substracting the other from their combination?

I have a table like this one: https://docs.google.com/spreadsheets/d/1Kn4vfbHwpif7u-6ZTznFpBJFNHhnStETPIQVyQq8xgY/edit#gid=0 with bottom / top color and the Red, Green, Blue (RGB) of the result (where it states 'Preparation' for the bottom color means the canvas so essentially its the RGB of the top color - those rows are the 'single colors').
I am looking for a relation between the double colors and the single colors. E.g. could I somehow subtract one color from their combination and get the other? Either by using the RGB values or using the images in some software?
Note that the type/formula/function/relation that I am looking for will be only for specific single colors and their combinations which I have already measured (do not care if it is valid beyond the dataset that I am working with)

What is the relationship between color space RGB, XYZ and the color matching function?

What is the relationship between color spaces (RGB, XYZ) and the color matching function? Let's say we have some color matching function in the color space XYZ (3 row matrix). We also have the transformation matrix which translates from XYZ coordinates to RGB coordinates.
My understanding is that there is some visual input, which is made up of the color spectrum S(y). The human eye does not see the world - it only sees its interpretation of the world. The human eye has 3 cone types LMS, each of which is responsible for processing RED, GREEN, or BLUE. The human eye sees the spectral color only because it's eye sums over RED, GREEN, BLUE vector, and this sum matches the color of the input. In order to match the color, there is a color matching function, which takes the input spectrum and produces the weights by which to multiply the primary RED, GREEN, BLUE color vector. These then get added and their output visually matches the spectral input, even though the spectrum had many many frequencies added, while the human eye was only adding 3. So we went from HUGE space to space where we can describe all with 3 vectors, summed as dictated by the color matching function.
The spectral input, color primaries, and color matching functions behave as described above and can be summarized in this formula:
where pi is the 3d vector of primary colors, c - color matching function is also a vector of 3 components, and finally s is the spectral input.
We have XYZ color space, and a corresponding color matching function which does what is described above. We are then given matrix T, which transforms XYZ coordinates to RGB coordinates. We already know T, and we need to use it to produce a new color matching function for the RGB color space.
I do not understand how the color space relates to choice of primaries pi(λ) and the choice of color functions ci(λ1).
I have been trying to understand about colours from months and after some research, i believe I have some insights which probably can help me answer your question.
I do not understand how the color space relates to choice of primaries
pi(λ)
Primaries are nothing but the wavelength of the colors that we choose to use for making all the other colors in space and that also defines the gamut of the colour space. So if you play with the applet provided in the link that is given below you can see that the whole gamut in the colour space changes when you change your primary.
Have a look at Alternative primaries and gamuts section.
Now I do not know how much you understand the RGB and XYZ or what do you mean when you say RGB here (assuming you are referring to sRGB gamut values); XYZ are actually Tristimulus values which are called rho, beta and gamma as shown in the image above and just for simplicity XYZ are converted to xy space from where you get your standard sRGB gamut.
Please go through this if you are interested in understanding how colour sensors work and converting sensor values to XYZ matrix
Please comment if I have missed any information or answer needs editing.
I think lots of issues with color selection are due technical problems people had to solve. Usually you are not trying to reproduce colors as accurately as possible, but to make them pleasant looking, cheap, fast to calculate on cpu.... If someone watches plains of New Zealand on TV he is very unlikely to know they really look like, but almost certainly wants to enjoy the picture and pay little for it.
Several reasons why you might want to use different color matching functions might include:
You are taking pictures under non-white light and you want your picture to look natural.
You are taking underwater pictures and want to compensate for the fact that water attenuates different frequencies at different speeds.
Your sensor is not perfect and you want to compensate for that.
On the other hand you might want to change your primaries due to some reason. For example your images might be taking a picture of a scene with limited amount of colors. By nudging your primaries a little you might get a "fuller" picture.
Finally sometimes you just have to compensate for some of the limitations you have with your devices. Your phosphorus on CRT TV will impose some restrictions. So will the noise in air when transmitting using PAL. On the other hand if you go digital you might be forced to have less than 36 bits per pixel. In that case you will have to make compromises and this will give you opportunity to lose as little as possible.
If you want a short tutorial visit Cambridge in colour.
Here is a Szeliski's textbook on photography, look at chapters 1 2 and 10.
Poyton has list of common transformations.

Mapping RGB/hex color codes to general color categories

Is there a dataset that maps each of the ~16M RGB or hex color values to a general color family/category - e.g. red, purple, orange, beige, brown, etc. - that I could access programmatically or load into a database or JSON document to cross-refence the color codes against? The use case is to classify the results of PIL color detection of swatch files into a small set of color pickers for a shopping site. It would also work if the mapping is a bit more granular, say 100-200 categories, since it would be easy enough to map those to my target 10-15 myself. I have some knowledge of kNN classification and will work with that if I have to, but it would be so much easier to use a static mapping if one already exists.
You can use a table such as the one in X11
http://www.astrouw.edu.pl/~jskowron/colors-x11/rgb.html
In order to find color proximity, it's best to transform the colors to Lab color space first, so that euclidean distances have more meaning, and then nearest neighbor would give good results.
You could convert from RGB to CIE Lab color space wherein Euclidian distance between two color selections is perceptually more meaningful. Here is the link to all relevant color space transformation formulae used in OpenCV's color conversion method (cvtColor): http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html
Since your use case is to compare two swatches, I would advise you to use texture descriptors (http://www.robots.ox.ac.uk/~vgg/research/texclass/with.html) in addition to color information for better results.

How can I loop through colors?

I would like to change smoothly the background color of a view over time.
Choosing 2 colors c1 and c2, it is easy in the rgb space to make a color function of the time c(t) = t.c1 + (1-t).c2, which will be a gradient between the two
However the rgb space is 3D and I am looking for a way to enumerate all colors smoothly.
I know there are billions of colors possible but coding RGB on 4x4x4 bits would be ok for my project.
My question is independent of the choice of the programming language
I have tried things like (pseudo code / Python)
for red in xrange(16):
for green in xrange(16):
for blue in xrange(16):
color = rgb(red, green, blue)
however this is not smooth at all, it is like a "step function" if you see what I mean (color will go from plain green to no green at all and a little red for instance)
Any idea how to do that ?
In that case I think you're better of with the HSV values of a color. You can use the same tactic and change only one of these HSV values at a time, to make the color shift more fluently.
Check this out (HSV)
EDIT:
After looking a bit further, I found this argument why NOT to use HSV.
This might be a good solution to your problem:
Solution

Resources