Example #1
0
        public string GetOrCreateColor(Color existingColor, int tolerance, string newColorName)
        {
            string closest      = string.Empty;
            int    closestSoFar = Int32.MaxValue;

            foreach (KeyValuePair <string, Color> colorPair in mCustomColorLookup)
            {
                int rDiff = (int)Mathf.Abs(existingColor.r - colorPair.Value.r) * 255;
                int gDiff = (int)Mathf.Abs(existingColor.g - colorPair.Value.g) * 255;
                int bDiff = (int)Mathf.Abs(existingColor.b - colorPair.Value.b) * 255;
                if (rDiff < closestSoFar && rDiff <= tolerance &&
                    gDiff < closestSoFar && gDiff <= tolerance &&
                    bDiff < closestSoFar && bDiff <= tolerance)
                {
                    closestSoFar = Mathf.Max(rDiff, gDiff);
                    closestSoFar = Mathf.Max(closestSoFar, bDiff);
                    closest      = colorPair.Key;
                }
            }

            if (string.IsNullOrEmpty(closest))
            {
                ColorKey ck = new ColorKey();
                ck.Name  = newColorName;
                ck.color = existingColor;
                ColorKeys.Add(ck);
                UnityEditor.EditorUtility.SetDirty(gameObject);
                UnityEditor.EditorUtility.SetDirty(this);
                closest = newColorName;
            }

            return(closest);
        }