Take colors from image and reduce amount to preset list of colors

What I’m trying to do is this: load image in Dynamo, set out pixel raster and get the colours at every pixel. I’ve gotten this far. But if there are 4000 pixels, chances are, I end up with 4000 colours. So I’m trying to reduce this amount.
I’ve managed to simplify the colours to 27 basic colours (RGB value is either 0, 128 or 255). Each colour gets a value between 1-27 so I can map these values to a parameter in my curtain panel.
But I don’t like these colours, there’s too much green and not enough red-orange.


So I was wondering if, instead of simplifying the colours, I could set up maybe 12 colours and have Dynamo match up the colours from the image to the closest colours of my pre-set list?

KHA_PrimaryColourOnSurface.dyn (76.3 KB)

Were you talking about something like this ?

Basically, i computed the distance of a color to all the basic colors (as though as they were all points) and I extracted the nearest color, instead of rounding the values.

You can change your basic colors at will, and you can define another type of distance (in my example, i just took the euclidian distance, i.e. the square root of the sum of the squares of the differences between each coordinate : https://en.wikipedia.org/wiki/Euclidean_distance)

Nearest color.dyn (33.7 KB)

6 Likes

Applying Euclidean distance to determine the color difference and find the closest match from a list of colors.
ReduceColorPalette.dyn (29.0 KB)

6 Likes

@Vikram_Subbaiah @mellouze Thanks for the suggestions, I got the script working.

I have one more question: how come the bottom part of my image always gets cut of?


KHA_PrimaryColourOnSurface V2.dyn (31.2 KB)
RGB_Revit.xlsx (14.3 KB)