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

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.

Related

Can I sort any given point on the HSV color space to a limited number of base colors, i.e. 12 or 16 that have clear names?

I want a software to be able to convert a given "pipetted" input color from a photography with a HSV-representation into a human-readable descriptor of a base color. So no matter which combination of HSV-values I pick from a color wheel, it would be converted to the closest "neighbor" of a given, limited number of base colors.
Maybe this is a similar question, because it also ennumerated base colors?

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.

Given a color, how do I find which color it's closest to?

Let's say that I have a list of valid color values like [0x67FF82, 0x808080, 0xffffff, ...] and given an input color, in hex, I want to find which color in the list of acceptable colors that the input color is closest to.
My thought is that I'd find the color in which the absolute value of the difference of the red, green, and blue values is smallest. Is this correct?
It sounds like you're looking for a way to quantify the "distance" between colors - in math, they'd call it a metric. Many people are intuitively pretty comfortable with the Euclidean metric for example - it's simply the distance between two points as measured with a ruler. In the case of colors, things are more complicated because of subjective perception of different colors.
There's a pretty mathy wikipedia article about color difference, which includes links to different implementations.
The difference or distance between two colors is a metric of interest in color science. It allows people to quantify a notion that would otherwise be described with adjectives, to the detriment of anyone whose work is color critical. Common definitions make use of the Euclidean distance in a device independent color space.
In particular, there's Python Colormath, an implementation in python that converts between different color encodings and also seems to have a function for calculating the distance between two colors. If you happen to be coding in python, that sounds helpful, although I unfortunately don't have any personal experience with that tool. There's also similar resources available for MATLAB and Excel provided by the authors of CIE2000, a leading color-difference formula.

How to convert a hex value into a color name

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.

Resources