Home > Enterprise >  How to get a difference between colors in percentages in Unity?
How to get a difference between colors in percentages in Unity?

Time:06-30

The task is to get a specific standardized color (it will be referred to as 100% desired purity) by mixing different proposed colors. I could compare their RGB parameters and see if the mixture matches the reference (true/false). However, the condition is that there can be different shades of that desired color. Some of those shades are close to the reference (somewhere between 80% to 90% alike) and some are not (70% and lower). So basically if it has at least 80% similarity you win, lower than that value - you lose. I just can't figure out how to make this color check.

Example: There is an array of possible colors to mix:

Color[] aColors = { new Color(1f, 1f, 0f), new Color(0.3f, 0.9f, 0f),
                    new Color(1f, 0.6f, 0f), new Color(1f, 0f, 0f),
                    new Color(0.5f,0f,0f), new Color(0.5f, 0f, 0.9f),
                    new Color(0f, 0.3f, 0f)};

The reference color is Lime:

Color(0.43f,0.74f,0f)

The correct pick for the mixture from the array is next (mixing yellow and green):

resutColor = aColors[0]   aColors[1]/ 2

When mixing a shade of dark red with green also gives a somewhat lime color and this should be taken into consideration by specifying how close the result is to the reference shade in percentages

A dark red color:

new Color(0.5f,0f,0f)

CodePudding user response:

Unity provides functionality to get the Hue, Saturation and Value values for a Color:

https://docs.unity3d.com/ScriptReference/Color.RGBToHSV.html

Then you can make a check by averaging the differences in each of the HSV values for different colors, which should feel more fair than doing so with the RGB components.

However, bear in mind that not all colors are made equal - for example, lime and emerald are as distant from each other as red and yellow are, in the additive color wheel (which is what HSV models). If you wish to tackle this conundrum, then there is a world of research in color spaces to be done.

  • Related