示例#1
0
        /// <summary>
        /// Checks whether two colors are similar to each other.
        /// </summary>
        /// <param name="colorA">First color for comparison.</param>
        /// <param name="colorB">Gets compared to colorA.</param>
        /// <param name="percentage">How similar the colors have to be between 0 and 1.</param>
        public static bool AreSimilar(RgbColor colorA, RgbColor colorB, float percentage)
        {
            ContractCheck.ValidPercentage(percentage, nameof(percentage));
            ContractCheck.ArgumentNotNull(colorA, nameof(colorA));
            ContractCheck.ArgumentNotNull(colorB, nameof(colorB));

            RgbColor c1 = new RgbColor(0, 0, 0);
            RgbColor c2 = new RgbColor(0, 0, 0);

            c1.R = Math.Min(colorA.R, colorB.R);
            c1.G = Math.Min(colorA.G, colorB.G);
            c1.B = Math.Min(colorA.B, colorB.B);

            c2.R = Math.Max(colorA.R, colorB.R);
            c2.G = Math.Max(colorA.G, colorB.G);
            c2.B = Math.Max(colorA.B, colorB.B);

            return(!(c1.R * 1.0 / c2.R < percentage ||
                     c1.G * 1.0 / c2.G < percentage ||
                     c1.B * 1.0 / c2.B < percentage));
        }
示例#2
0
        /// <summary>
        /// Generates a random color that differs at least 20% from another color and has a higher chance for distinct colors.
        /// </summary>
        /// <param name="differFromThis">Color the generated color should differ from.</param>
        /// <returns>The new generated random color.</returns>
        public static RgbColor GetRandom(RgbColor differFromThis)
        {
            if (differFromThis == null)
            {
                return(GetRandom());
            }
            byte     r;
            byte     g;
            byte     b;
            RgbColor returnColor;

            do
            {
                int rnd = _rndm.Next(0, 6);
                r = (byte)_rndm.Next(0, 120);
                g = (byte)_rndm.Next(0, 120);
                b = (byte)_rndm.Next(0, 120);

                switch (rnd)
                {
                case 0: r = 255; break;

                case 1: g = 255; break;

                case 2: b = 255; break;

                case 3: r = 255; g = 255; break;

                case 4: r = 255; b = 255; break;

                case 5: g = 255; b = 255; break;

                default:
                    break;
                }
                returnColor = new RgbColor(r, g, b);
            } while (differFromThis != null && AreSimilar(returnColor, differFromThis, 0.2f));
            return(returnColor);
        }
示例#3
0
 /// <summary>
 /// Sets all colors of the image to a new color.
 /// </summary>
 /// <param name="color">The new color.</param>
 public void SetAll(RgbColor color)
 {
     Leds.ForEach(x => x.SetColor(color));
 }
示例#4
0
 /// <summary>
 /// Sets new rgb values based on another color instance.
 /// </summary>
 /// <param name="col">The source for the new rgb values.</param>
 public void SetColor(RgbColor col)
 {
     R = col.R;
     G = col.G;
     B = col.B;
 }