박지석
박지석

Reputation: 47

Convert sub color name to parant color name

I referenced a question https://stackoverflow.com/a/9694246/98276 to get a color name by RGB values. But there are too many color names that exist. So what I want to do is if I get color name "maroon", change to "red". also, if get "cadet blue", change to "blue"

actual_name, closest_name = get_colour_name(requested_colour)
if actual_name == None:
    actual_name = get_parantColor(closest_name) #if closest_name was maroon, return red.

Upvotes: 3

Views: 143

Answers (1)

jenx
jenx

Reputation: 86

This question can be answered in many ways. It really depends on two things:

  1. what your base pool of colors is, and
  2. what measure of similarity you want to use (i.e., what the translation (matching) function from arbitrary color to base color should look like) -- the same one as webcolors from the referenced question does or different?

For instance, one very simple approach would be 'red', 'green', 'blue' as your color pool and the translation function could inspect the red, green and blue components of the color and return whichever is highest. For example, for the color RGB(128, 0, 0) (maroon) you would return 'red' since the red component is the most prominent one. (However, what do you do if two or all three components are present equally? That is something that you would need to define, too.)

Another solution in case the number of colors you can encounter is limited would be to create a dictionary that maps your input color to one of your base colors: {'maroon': 'red', 'cadet blue': 'blue', ...} and use that.

The solution really depends on how you define the problem. The two points at the beginning can help with that.

Upvotes: 1

Related Questions