1 year ago

#371807

test-img

FaffyWaffles

Given an arbitrary RGB/HSV Value, Output Nearest Defined Color

Say, Given an arbitrary RGB Value and an array of colors with defined values(eg red, blue, green, yellow, etc.), what would be an optimal way of finding the nearest defined color to the arbitrary color?

Some Pseudocode:

        public struct DefinedColors
        {
            public string colorName;
            public Vector3 RGB;
            public Vector3 HSV; //Paying attention to Hue, float h?
            //public string hexdecimal ??
            public DefinedColors(string colorName, Vector3 RGB, Vector3 HSV)
            {
                this.colorName = colorName;
                this.RGB = RGB;
                this.HSV = HSV;
            }
        }
        public DefinedColors[] definedColors = new DefinedColors[6]
        {
            new DefinedColors("red", new Vector3(1,0,0), new Vector3(0,100,100)),
            new DefinedColors("yellow", new Vector3(1,1,0), new Vector3(60,100,100)),
            new DefinedColors("green", new Vector3(0,1,0), new Vector3(120,100,100)),
            new DefinedColors("cyan", new Vector3(0,1,1), new Vector3(180,100,100)),
            new DefinedColors("blue", new Vector3(0,0,1), new Vector3(240,100,100)),         
            new DefinedColors("magenta", new Vector3(1,0,1), new Vector3(300,100,100))
        };
    
    
        string NearestColor(Color color)
        {
            ///Logic 
    
            return definedColors[i].colorName;
        }

I know I could brute force something like

V3 arbitraryRGB;
V3 closest;
foreach(V3 v in foo) 
  if (|v-arbitraryRGB|<closest)
    closest = v;

I considered focusing on the hues of the color, because its a simple 0-360, but it gave me inconsistent results.

And I've read a little about KD Trees, but I don't know if that would be applicable.

c#

unity-game-engine

colors

rgb

closest

0 Answers

Your Answer

Accepted video resources