user963241
user963241

Reputation: 7048

Selecting nearest RGB colour from std::map

I have a map of pre-defined colors and a given color to select one nearest match. If i will use distance formula, would I be calculating the distance from each pre-defined color in map rather than just finding a key if it is between the matching limit?

Upvotes: 2

Views: 647

Answers (2)

ravenspoint
ravenspoint

Reputation: 20615

Colours are a bit more complex than 3D. You have to think about brightness as well as how red, green or blue the colour might be. So is 1,0,0 closer to 255,0,0 than to 0,1,0? So perhaps colours need to be thought of as four dimensional? In addition, human response to colours is not linear, so if you just look at the mathematical distance between two RGB values, you will measuring a 'distance' that does not agree with the distance a human would judge between the two colours.

For this kind or work, RGB is a very unsatisfactory measure of colour ( though it is convenient for controlling the hardware ) Many people recommend using the HSL system, which allows you to set the brightness independently of 'colour' and which is supposed to more closely correspond to the human response to color.

For more about using HSL in this kind of work, and for code to convert between HSL and RGB, see my previous answer Function for creating color wheels

Upvotes: 0

Pete Kirkham
Pete Kirkham

Reputation: 49331

Yes, you would need to iterate over every colour in the map and calculate the distance. Also, colours are 3D whereas std::map is 1D, you probably want something like a 3D kd tree rather than a 1D map for fast lookup if you have a lot of colours. Though just iterating over a few colours won't be very expensive.

Upvotes: 3

Related Questions