How to add all close rgb values together

Viewed 53

I want to add all rgb values that are close

Example:

rgb = [(0, 0, 78),(0, 254, 255),(255, 30, 90),(255, 0, 60),(106,190,30),(21,30,28),(0,0,0)]

Output:

blue = [((0, 0, 78),(0, 254, 255),]
pink = [(255, 30, 90),(255, 0, 60),]
green = [(106,190,30)]
gray = [(21,30,28)]
black = [(0,0,0)]

and i want to have the ability to make the range/difference like to be in the same color it must be lower than or equal a 10 shade difference

And then i want to add them to a dictionary as keys and values in this format:

rgb = {"blue":(All the rgb values in the list blue,ect...}

All automatic

1 Answers

Colour comparisons really depend on what you mean by being 'nearly the same'. Where I need to compare colours in some way I find conversion to HSV is useful. Instead of colours being represented as intensities of colour components, it is represented as Hue, Saturation, Value. Hue is the colour, Saturation is how intense the colour is and Value is how bright it is. Look for a function that does this conversion, and there are plenty of code snippets available for this.

With HSV you can more easily group colours together. You can try doing a weighted average of all three with the weights indicating the importance of each of the three attributes. With a single number you can then easily group them.

Related