AForge.Imaging.ColorReduction.ColorImageQuantizer.GetClosestColor C# (CSharp) Method

GetClosestColor() private method

private GetClosestColor ( int red, int green, int blue ) : int
red int
green int
blue int
return int
        private int GetClosestColor( int red, int green, int blue )
        {
            Color color = Color.FromArgb( red, green, blue );

            if ( ( useCaching ) && ( cache.ContainsKey( color ) ) )
            {
                return cache[color];
            }

            int colorIndex = 0;
            int minError = int.MaxValue;

            for ( int i = 0, n = paletteToUse.Length; i < n; i++ )
            {
                int dr = red - paletteToUse[i].R;
                int dg = green - paletteToUse[i].G;
                int db = blue - paletteToUse[i].B;

                int error = dr * dr + dg * dg + db * db;

                if ( error < minError )
                {
                    minError = error;
                    colorIndex = (byte) i;
                }
            }

            if ( useCaching )
            {
                cache.Add( color, colorIndex );
            }

            return colorIndex;
        }