/// <summary> /// Converts HSB to Color. /// </summary> /// <param name="hsv">the HSB structure to convert.</param> public static Color HSBtoColor(HSB hsb) { RGB rgb = HSBtoRGB(hsb); return(Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue)); }
/// <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) { r = g = b = hsb.Brightness; } else { // the color wheel consists of 6 sectors. Figure out which sector you're in. double sectorPos = hsb.Hue / 60.0; int sectorNumber = (int)(Math.Floor(sectorPos)); // get the fractional part of the sector double fractionalSector = sectorPos - sectorNumber; // calculate values for the three axes of the color. double p = hsb.Brightness * (1.0 - hsb.Saturation); double q = hsb.Brightness * (1.0 - (hsb.Saturation * fractionalSector)); double 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))) )); }