コード例 #1
0
        /// <summary>
        ///     Converts HSB to RGB.
        /// </summary>
        /// <param name="hsv">The HSB structure to convert.</param>
        public static RGB HSBtoRGB(HSB hsb)
        {
            double r = 0;
            double g = 0;
            double b = 0;

            if (hsb.Saturation == 0.0)
            {
                r = g = b = hsb.Brightness;
            }
            else
            {
                // the color wheel consists of 6 sectors. Figure out which sector you're in.
                var sectorPos = hsb.Hue/60.0;
                var sectorNumber = (int) (Math.Floor(sectorPos));
                // get the fractional part of the sector
                var fractionalSector = sectorPos - sectorNumber;

                // calculate values for the three axes of the color.
                var p = hsb.Brightness*(1.0 - hsb.Saturation);
                var q = hsb.Brightness*(1.0 - (hsb.Saturation*fractionalSector));
                var t = hsb.Brightness*(1.0 - (hsb.Saturation*(1 - fractionalSector)));

                // assign the fractional colors to r, g, and b based on the sector the angle is in.
                switch (sectorNumber)
                {
                    case 0:
                        r = hsb.Brightness;
                        g = t;
                        b = p;
                        break;
                    case 1:
                        r = q;
                        g = hsb.Brightness;
                        b = p;
                        break;
                    case 2:
                        r = p;
                        g = hsb.Brightness;
                        b = t;
                        break;
                    case 3:
                        r = p;
                        g = q;
                        b = hsb.Brightness;
                        break;
                    case 4:
                        r = t;
                        g = p;
                        b = hsb.Brightness;
                        break;
                    case 5:
                        r = hsb.Brightness;
                        g = p;
                        b = q;
                        break;
                }
            }

            return new RGB(
                Convert.ToInt32(double.Parse(string.Format("{0:0.00}", r*255.0))),
                Convert.ToInt32(double.Parse(string.Format("{0:0.00}", g*255.0))),
                Convert.ToInt32(double.Parse(string.Format("{0:0.00}", b*255.0)))
                );
        }
コード例 #2
0
        /// <summary>
        ///     Converts HSB to Color.
        /// </summary>
        /// <param name="hsv">the HSB structure to convert.</param>
        public static Color HSBtoColor(HSB hsb)
        {
            var rgb = HSBtoRGB(hsb);

            return(Color.FromArgb((byte)rgb.Red, (byte)rgb.Green, (byte)rgb.Blue));
        }
コード例 #3
0
        /// <summary>
        ///     Converts HSB to Color.
        /// </summary>
        /// <param name="hsv">the HSB structure to convert.</param>
        public static Color HSBtoColor(HSB hsb)
        {
            var rgb = HSBtoRGB(hsb);

            return Color.FromArgb((byte) rgb.Red, (byte) rgb.Green, (byte) rgb.Blue);
        }
コード例 #4
0
        /// <summary>
        ///     Converts HSB to RGB.
        /// </summary>
        /// <param name="hsv">The HSB structure to convert.</param>
        public static RGB HSBtoRGB(HSB hsb)
        {
            double r = 0;
            double g = 0;
            double b = 0;

            if (hsb.Saturation == 0.0)
            {
                r = g = b = hsb.Brightness;
            }
            else
            {
                // the color wheel consists of 6 sectors. Figure out which sector you're in.
                var sectorPos    = hsb.Hue / 60.0;
                var sectorNumber = (int)(Math.Floor(sectorPos));
                // get the fractional part of the sector
                var fractionalSector = sectorPos - sectorNumber;

                // calculate values for the three axes of the color.
                var p = hsb.Brightness * (1.0 - hsb.Saturation);
                var q = hsb.Brightness * (1.0 - (hsb.Saturation * fractionalSector));
                var t = hsb.Brightness * (1.0 - (hsb.Saturation * (1 - fractionalSector)));

                // assign the fractional colors to r, g, and b based on the sector the angle is in.
                switch (sectorNumber)
                {
                case 0:
                    r = hsb.Brightness;
                    g = t;
                    b = p;
                    break;

                case 1:
                    r = q;
                    g = hsb.Brightness;
                    b = p;
                    break;

                case 2:
                    r = p;
                    g = hsb.Brightness;
                    b = t;
                    break;

                case 3:
                    r = p;
                    g = q;
                    b = hsb.Brightness;
                    break;

                case 4:
                    r = t;
                    g = p;
                    b = hsb.Brightness;
                    break;

                case 5:
                    r = hsb.Brightness;
                    g = p;
                    b = q;
                    break;
                }
            }

            return(new RGB(
                       Convert.ToInt32(double.Parse(string.Format("{0:0.00}", r * 255.0))),
                       Convert.ToInt32(double.Parse(string.Format("{0:0.00}", g * 255.0))),
                       Convert.ToInt32(double.Parse(string.Format("{0:0.00}", b * 255.0)))
                       ));
        }