Search code examples
algorithmcolorsrgbpseudocode

How to convert RGB code to 8 simple intervals (possible ?)


I'm working on my Final Bachelor Project in Computer Science and for now I'm in a dead end. Here is what I got stuck on:

I'm trying to classify any color (rgb code) in any of 8 (eight) simple colors. In short terms I need to find 8 intervals where any colour can be placed and be considered a basic color (red, blue, green, black, yellow, purple, grey, brown ).

example:
(18,218,23) to be classified as "green"
(81,,214,85) also "green"
but
(15,52,16) needs to be "black"
(110,117,110) needs to be "grey"

So there are 256 x 256 x 256 possible colors and I need to divide them in 8 (intervals) basic colors.

I'm waiting for some suggestions.

Cheers !


To be clear (as I've seen in comments) I'm looking for a particular set of 8 colors (red, black, green, brown, blue, purple, grey, yellow). Sorry for the orange above !


Solution

  • Based on your example, I'd start with determining whether all components are approximately the same, or does on stand out. If they are about the same, then decide if the values are small enough to be black or not, then it's grey. If one value is different from the other two, then it is easy to check which is different and pick one of six possible colors accordingly.

    Alternatively, set each component to either 0 or 1 according to a threshold, then you have 8 combinations to map to 8 colors.

    threshold = 100:
    (18,218,23)   -> (0, 1, 0) - to be classified as "green"
    (81,214,85)   -> (0, 1, 0) - also "green"
    (15,52,16)    -> (0, 0, 0) - to be "black"
    (110,117,110) -> (1, 1, 1) - to be "grey"