Esempio n. 1
0
        private static float Normalize(float hue, HueUnit hueUnit)
        {
            // Convert to turns
            switch (hueUnit)
            {
            case HueUnit.Degrees:
                hue /= 360;
                break;

            case HueUnit.Gradians:
                hue /= 400;
                break;

            case HueUnit.Radians:
                hue /= 2 * (float)Math.PI;
                break;
            }

            // Normalize hue to range [0, 1)
            hue = hue % 1;

            if (hue < 0)
            {
                hue += 1;
            }

            return(hue);
        }
Esempio n. 2
0
        public HueValue(float hue, HueUnit hueUnit)
        {
            var info       = HueUnitInfo.Get(hueUnit);
            var normalized = Normalize(hue, info.Period);

            Suffix = info.Suffix;
            Turns  = normalized / info.Period;
            Value  = normalized;
        }
Esempio n. 3
0
        public static HueUnitInfo Get(HueUnit hueUnit)
        {
            foreach (var unitInfo in Values)
            {
                if (unitInfo.Key == hueUnit)
                {
                    return(unitInfo);
                }
            }

            throw new ArgumentException($"Unknown hue unit {hueUnit}.", nameof(hueUnit));
        }
Esempio n. 4
0
        public void Add(float hue, HueUnit hueUnit)
        {
            if (float.IsNaN(hue))
            {
                throw new ArgumentException("NaN is not a valid hue.", nameof(hue));
            }
            if (float.IsPositiveInfinity(hue))
            {
                throw new ArgumentException("Positive infinity is not a valid hue.", nameof(hue));
            }
            if (float.IsNegativeInfinity(hue))
            {
                throw new ArgumentException("Negative infinity is not a valid hue.", nameof(hue));
            }

            hues.Add(new HueValue(hue, hueUnit));
        }
Esempio n. 5
0
 public bool Remove(float hue, HueUnit hueUnit)
 {
     return(hues.Remove(new HueValue(hue, hueUnit)));
 }
Esempio n. 6
0
 public bool Contains(float hue, HueUnit hueUnit)
 {
     return(hues.Contains(new HueValue(hue, hueUnit)));
 }
Esempio n. 7
0
 public HueUnitInfo(HueUnit key, string suffix, float period)
 {
     Key    = key;
     Suffix = suffix;
     Period = period;
 }
Esempio n. 8
0
 /// <summary>
 /// Removes the specified hue from this collection.
 /// </summary>
 /// <param name="hue">Hue. Will be normalized to turns in the range [0, 1).</param>
 /// <param name="hueUnit">The unit of <paramref name="hue"/>.</param>
 /// <returns><c>true</c> if the hue was found and removed from the collection.</returns>
 public bool Remove(float hue, HueUnit hueUnit)
 {
     return(hues.Remove(Normalize(hue, hueUnit)));
 }
Esempio n. 9
0
 /// <summary>
 /// Checks whether a specified hue exists in this collection.
 /// </summary>
 /// <param name="hue">Hue to find in the collection.</param>
 /// <param name="hueUnit">Unit of <paramref name="hueUnit"/>.</param>
 /// <returns><c>true</c> if found, otherwise <c>false</c>.</returns>
 /// <remarks>
 /// The specified hue will be normalized to turns in the range [0, 1). This means that if
 /// you have added the hue 180 degrees to the collection and then asks this method if the hue
 /// 1.5 turns is existing in the collection, it will return <c>true</c>.
 /// </remarks>
 public bool Contains(float hue, HueUnit hueUnit)
 {
     return(hues.Contains(Normalize(hue, hueUnit)));
 }