예제 #1
0
        public static Color ToColor(Hsb hsb)
        {
            int mid;

            var max = ColorHelper.Round(hsb.Brightness * 255);
            var min = ColorHelper.Round((1.0 - hsb.Saturation) * (hsb.Brightness / 1.0) * 255);
            var 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));
        }
예제 #2
0
 public void CmykUpdate()
 {
     this.Rgb = this.Cmyk;
     this.Hsb = this.Cmyk;
 }
예제 #3
0
 public void RgbUpdate()
 {
     this.Hsb  = this.Rgb;
     this.Cmyk = this.Rgb;
 }
예제 #4
0
 public MyColor(Color color)
 {
     this.Rgb  = color;
     this.Hsb  = color;
     this.Cmyk = color;
 }
예제 #5
0
        public static Hsb ToHSB(Color color)
        {
            var 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;
            }

            var 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);
        }