Exemple #1
0
        /// <summary>
        /// Finds the closest color in this palette to the specified <paramref name="color"/>
        /// </summary>
        /// <param name="color">Color to use to find the closest color</param>
        /// <returns>Index of the closest entry of the specified <paramref name="color"/></returns>
        public int FindClosest(Color color)
        {
            int closestIndex      = 0;
            var colorHsl          = new ColorHSL(color);
            var closestDifference = ColorHSL.Distance(colorHsl, new ColorHSL(this[0]));

            for (int i = 1; i < Count; i++)
            {
                var curDifference = ColorHSL.Distance(colorHsl, new ColorHSL(this[i]));
                if (curDifference < closestDifference)
                {
                    closestIndex      = i;
                    closestDifference = curDifference;
                }
                if (Math.Abs(curDifference) < 0.001f)
                {
                    break;
                }
            }
            return(closestIndex);
        }
Exemple #2
0
		public Color (ColorHSL hsl)
			: this()
		{
			if (hsl.S == 0) {
				// achromatic color (gray scale)
				R = G = B = hsl.L;
			} else {
				float q = (hsl.L < 0.5f) ? (hsl.L * (1f + hsl.S)) : (hsl.L + hsl.S - (hsl.L * hsl.S));
				float p = (2f * hsl.L) - q;

				float Hk = hsl.H / 360f;
				float[] T = new float[3];
				T [0] = Hk + (1f / 3f);    // Tr
				T [1] = Hk;                // Tb
				T [2] = Hk - (1f / 3f);    // Tg

				for (int i=0; i<3; i++) {
					if (T [i] < 0)
						T [i] += 1f;
					if (T [i] > 1)
						T [i] -= 1f;

					if ((T [i] * 6f) < 1) {
						T [i] = p + ((q - p) * 6f * T [i]);
					} else if ((T [i] * 2f) < 1) { //(1.0/6.0)<=T[i] && T[i]<0.5
						T [i] = q;
					} else if ((T [i] * 3f) < 2) { // 0.5<=T[i] && T[i]<(2.0/3.0)
						T [i] = p + (q - p) * ((2f / 3f) - T [i]) * 6f;
					} else
						T [i] = p;
				}

				R = T [0];
				G = T [1];
				B = T [2];
			}
			A = hsl.A;
		}
Exemple #3
0
 public Color(ColorHSL hsl)
     : this(hsl.ToColor())
 {
 }
Exemple #4
0
		public static float Distance (ColorHSL value1, ColorHSL value2)
		{
			return (float)Math.Sqrt (Math.Pow ((value1.H - value2.H) / 360f, 2) + Math.Pow (value1.S - value2.S, 2) + Math.Pow (value1.L - value2.L, 2));
		}