Esempio n. 1
0
        /// <summary>
        /// 转换颜色
        /// </summary>
        /// <param name="hsb">HSB对象</param>
        /// <returns></returns>
        public static Color ToColor(HSB hsb)
        {
            int Mid;

            int    Max = ColorHelper.Round(hsb.Brightness * 255);
            int    Min = ColorHelper.Round((1.0 - hsb.Saturation) * (hsb.Brightness / 1.0) * 255);
            double q   = (double)(Max - Min) / 255;

            if (hsb.Hue >= 0 && hsb.Hue <= (double)1 / 6)
            {
                Mid = ColorHelper.Round(((hsb.Hue - 0) * q) * 1530 + Min);
                return(Color.FromArgb(Max, Mid, Min));
            }
            if (hsb.Hue <= (double)1 / 3)
            {
                Mid = ColorHelper.Round(-((hsb.Hue - (double)1 / 6) * q) * 1530 + Max);
                return(Color.FromArgb(Mid, Max, Min));
            }
            if (hsb.Hue <= 0.5)
            {
                Mid = ColorHelper.Round(((hsb.Hue - (double)1 / 3) * q) * 1530 + Min);
                return(Color.FromArgb(Min, Max, Mid));
            }
            if (hsb.Hue <= (double)2 / 3)
            {
                Mid = ColorHelper.Round(-((hsb.Hue - 0.5) * q) * 1530 + Max);
                return(Color.FromArgb(Min, Mid, Max));
            }
            if (hsb.Hue <= (double)5 / 6)
            {
                Mid = ColorHelper.Round(((hsb.Hue - (double)2 / 3) * q) * 1530 + Min);
                return(Color.FromArgb(Mid, Min, Max));
            }
            if (hsb.Hue <= 1.0)
            {
                Mid = ColorHelper.Round(-((hsb.Hue - (double)5 / 6) * q) * 1530 + Max);
                return(Color.FromArgb(Max, Min, Mid));
            }
            return(Color.FromArgb(0, 0, 0));
        }
Esempio n. 2
0
 public void CMYKUpdate()
 {
     this.RGB = this.CMYK;
     this.HSB = this.CMYK;
 }
Esempio n. 3
0
 public void RGBUpdate()
 {
     this.HSB  = this.RGB;
     this.CMYK = this.RGB;
 }
Esempio n. 4
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="color"></param>
 public MyColor(Color color)
 {
     this.RGB  = color;
     this.HSB  = color;
     this.CMYK = color;
 }
Esempio n. 5
0
        public static HSB ToHSB(Color color)
        {
            HSB hsb = new HSB();

            int Max, Min;

            if (color.R > color.G)
            {
                Max = color.R; Min = color.G;
            }
            else
            {
                Max = color.G; Min = color.R;
            }
            if (color.B > Max)
            {
                Max = color.B;
            }
            else if (color.B < Min)
            {
                Min = color.B;
            }

            int Diff = Max - Min;

            hsb.Brightness = (double)Max / 255;

            if (Max == 0)
            {
                hsb.Saturation = 0;
            }
            else
            {
                hsb.Saturation = (double)Diff / Max;
            }

            double q;

            if (Diff == 0)
            {
                q = 0;
            }
            else
            {
                q = (double)60 / Diff;
            }

            if (Max == color.R)
            {
                if (color.G < color.B)
                {
                    hsb.Hue = (360 + q * (color.G - color.B)) / 360;
                }
                else
                {
                    hsb.Hue = q * (color.G - color.B) / 360;
                }
            }
            else if (Max == color.G)
            {
                hsb.Hue = (120 + q * (color.B - color.R)) / 360;
            }
            else if (Max == color.B)
            {
                hsb.Hue = (240 + q * (color.R - color.G)) / 360;
            }
            else
            {
                hsb.Hue = 0.0;
            }

            return(hsb);
        }