示例#1
0
        /// <summary>
        /// Creates color from HSB color space with random hue and saturation and brighness equal to 1.
        /// </summary>
        /// <returns></returns>
        public static Color CreateColorWithRandomHue()
        {
            double   hue      = random.NextDouble() * 360;
            HsbColor hsbColor = new HsbColor(hue, 1, 1);

            return(hsbColor.ToArgb());
        }
示例#2
0
 public override bool Equals(object obj)
 {
     if (obj is HsbColor)
     {
         HsbColor c = (HsbColor)obj;
         return(c.alpha == alpha &&
                c.brightness == brightness &&
                c.hue == hue &&
                c.saturation == saturation);
     }
     else
     {
         return(false);
     }
 }
示例#3
0
        public static Color[] CreateRandomColors(int colorNum)
        {
            double startHue = random.NextDouble() * 360;

            Color[] res     = new Color[colorNum];
            double  hueStep = 360.0 / colorNum;

            for (int i = 0; i < res.Length; i++)
            {
                double hue = startHue + i * hueStep;
                res[i] = new HsbColor(hue, 1, 1).ToArgb();
            }

            return(res);
        }
示例#4
0
 public static HsbColor ToHsbColor(this Color color)
 {
     return(HsbColor.FromArgb(color));
 }
示例#5
0
        public static HsbColor FromArgb(Color color)
        {
            double limit = 255;

            double r = color.R / limit;
            double g = color.G / limit;
            double b = color.B / limit;

            double max = Math.Max(Math.Max(r, g), b);
            double min = Math.Min(Math.Min(r, g), b);

            double len = max - min;

            double brightness = max;             // 0.5 * (max + min);
            double sat;
            double hue;

            if (max == 0 || len == 0)
            {
                sat = hue = 0;
            }
            else
            {
                sat = len / max;
                if (r == max)
                {
                    hue = (g - b) / len;
                }
                else if (g == max)
                {
                    hue = 2 + (b - r) / len;
                }
                else
                {
                    hue = 4 + (r - g) / len;
                }
            }

            hue *= 60;
            if (hue < 0)
            {
                hue += 360;
            }


            //hue = max == min ? 0 :
            //    (max == r && g >= b) ? 60 * (g - b) / len :
            //    (max == r && g < b) ? 60 * (g - b) / len + 360 :
            //    max == g ? 60 * (b - r) / len + 120 : 60 * (r - g) / len + 240;

            //sat = len /  brightness;
            //sat = (sat == 0 || max == min) ? 0 :
            //    (0 <= sat && sat <= 0.5) ? len / (max + min) :
            //    (0.5 < sat && sat < 1) ? len / (2 - 2 * sat) : 1;

            HsbColor res = new HsbColor();

            res.hue        = hue;
            res.saturation = sat;
            res.brightness = brightness;
            res.alpha      = color.A / limit;
            return(res);
        }