2

I am trying to write a simple function which based on RGB code is returning name of the closest "reference" colour. Based on other SO question, I am converting RGB to CIE LAB and calculating distance between input colour and reference colours. Then I am searching for the smallest distance and taking corespondent colour.
Unfortunately suggested solution works only partially. Given the "dark orange" colour is interpreted as red. I tried to improve it and I changed deltaE_ciede76 to deltaE_ciede94 and deltaE_ciede00, based on this article.

Do you have any idea how they solve this problem on the below page?: https://convertingcolors.com/rgb-color-247_104_8.html - please scroll down to the section: Details

It is written: The colour can be described as dark saturated orange.

Could you give me any advice?

simple program:

import numpy as np
from skimage.color import rgb2lab, deltaE_ciede94

def identify_colour(rgb_colour):
    reference = {
        "red"   : [53.23,  80.11, 67.22], # https://convertingcolors.com/cielab-color-53.23_80.11_67.22.html
        "orange": [74.93,  23.94, 78.96], # https://convertingcolors.com/cielab-color-74.93_23.94_78.96.html
    }
    input_colour = rgb2lab([[rgb_colour / 255]])

    selected = None
    d = {}
    for colour, value in reference.items():
        basic_lab = np.asarray(value)
        distance = deltaE_ciede94(basic_lab, input_colour)
        d[colour] = distance

    selected = min(d, key=d.get)
    print("selected: ", selected)
    print(d)
    return selected

def main():
    rgb_colour = np.array([247, 104, 8]) # https://convertingcolors.com/rgb-color-247_104_8.html
    identify_colour(rgb_colour)

if __name__ == '__main__':
    main()
flamingo
  • 148
  • 1
  • 11
  • Wouldn't this just be a 3-space distance problem in RGB coordinates? You would need to derive some distance metric that suits your problem, Eg: `abs(R_i - **R**) + abs(G_i - **G**) + abs(B_i - **B**)`. I assume you have a matrix of given "acceptable" colors which give you **R**, **G** and **B**. – jason m Dec 13 '19 at 14:15
  • 1
    Maybe `HSV`or `HSL` be more suitable? You could also use [kNN](https://scikit-learn.org/stable/modules/neighbors.html) method to do that for you. It's vanilla-like classifier without any memory. – Piotr Rarus Dec 13 '19 at 14:44
  • How many reference colours do you have? Is there a list? – Mark Setchell Dec 13 '19 at 15:25
  • @MarkSetchell I have list with 28 colours. https://pastebin.com/FkFpWLpJ – flamingo Dec 13 '19 at 17:58
  • @jasonm RGB is not recommended here: source: https://stackoverflow.com/a/9019461/9854242 – flamingo Dec 13 '19 at 18:02
  • If you look at the set of slides by David DeSandro on the linked page, you will see that he only uses a set of 12 distinct hues - red, orange, yellow, chartreuse, green, spring green. You are using a much larger set with different contents so you are bound to get different answers from him. So, I am not sure what your question really is. If you want the same answers, shouldn't you use the same hues? – Mark Setchell Jan 01 '20 at 15:29

1 Answers1

1

I can not comment, just answer but as it is my site I wanted to comment on

"Do you have any idea how they solve this problem on the below page?: https://convertingcolors.com/rgb-color-247_104_8.html - Please scroll down to the section: Details."

Have a look at this talk and the slides; this helped me implement this feature: https://www.dotconferences.com/2018/11/david-desandro-read-color-hex-codes

Andreas
  • 69
  • 3
  • Great! thanks a lot for the answer. :) I will also try your approach, but I am still curious if described problem can be solved with distance calculation. – flamingo Dec 13 '19 at 18:32