/// <summary> /// Update the selected color to the preview box. /// </summary> private void UpdateColor() { ColorHSB selectedHSB = new ColorHSB(colorHSB.Hue, colorHSB.Saturation, colorHSB.Brightness); selectedColor = ColorHSB.HSBtoRGB(selectedHSB); ColorPreviewBox.Background = GetBrush(); }
/// <summary> /// Update the color hue and saturation from the position of the color picker. /// </summary> private void UpdateColorPicker() { colorHSB.Hue = ((Canvas.GetLeft(ColorPickPointer) - Canvas.GetLeft(ColorMapImage)) / 255) * 360; colorHSB.Saturation = 1 - ((Canvas.GetTop(ColorPickPointer) - Canvas.GetTop(ColorMapImage)) / 255); ColorHSB gradientHSB = new ColorHSB(colorHSB.Hue, colorHSB.Saturation, 1); UpdateGradient(ColorHSB.HSBtoRGB(gradientHSB)); }
/// <summary> /// Load the pre-selected color into all controls. /// </summary> /// <param name="color">The color to load.</param> public void LoadColor(Color color) { colorHSB = ColorHSB.RGBtoHSB(color); Canvas.SetLeft(ColorPickPointer, Canvas.GetLeft(ColorMapImage) + (colorHSB.Hue / 360) * 256); Canvas.SetTop(ColorPickPointer, Canvas.GetTop(ColorMapImage) + (256 - colorHSB.Saturation * 256)); Canvas.SetTop(GradientPointer, Canvas.GetTop(GradientRectangle) + (256 - colorHSB.Brightness * 256)); ConstraintColorPickPointer(); ConstraintGradientPointer(); UpdateGradient(color); UpdateColor(); UpdateInfo(); }
/// <summary> /// Converts HSB color to RGB color. /// </summary> /// <param name="colorHSB">HSB Color</param> /// <returns>RGB color</returns> public static Color HSBtoRGB(ColorHSB colorHSB) { double hue = colorHSB.hue; double saturation = colorHSB.saturation; double brightness = colorHSB.brightness; double R = 0; double G = 0; double B = 0; if (saturation == 0) { R = G = B = brightness; } else { // Calculate the color sector. double sectorPos = hue / 60.0; int sectorNumber = (int)(Math.Floor(sectorPos)); // Get the fractional. double fractionalSector = sectorPos - sectorNumber; // Calculate the three axes of color. double p = brightness * (1.0 - saturation); double q = brightness * (1.0 - (saturation * fractionalSector)); double t = brightness * (1.0 - (saturation * (1 - fractionalSector))); // Assign fractional colors to RGB based on the sector the angle. switch (sectorNumber) { case 0: R = brightness; G = t; B = p; break; case 1: R = q; G = brightness; B = p; break; case 2: R = p; G = brightness; B = t; break; case 3: R = p; G = q; B = brightness; break; case 4: R = t; G = p; B = brightness; break; case 5: R = brightness; G = p; B = q; break; } } Color color = Color.FromRgb((byte)Double.Parse(String.Format("{0:0.00}", R * 255.0)), (byte)Double.Parse(String.Format("{0:0.00}", G * 255.0)), (byte)Double.Parse(String.Format("{0:0.00}", B * 255.0))); return color; }
/// <summary> /// Converts HSB color to RGB color. /// </summary> /// <param name="colorHSB">HSB Color</param> /// <returns>RGB color</returns> public static Color HSBtoRGB(ColorHSB colorHSB) { double hue = colorHSB.hue; double saturation = colorHSB.saturation; double brightness = colorHSB.brightness; double R = 0; double G = 0; double B = 0; if (saturation == 0) { R = G = B = brightness; } else { // Calculate the color sector. double sectorPos = hue / 60.0; int sectorNumber = (int)(Math.Floor(sectorPos)); // Get the fractional. double fractionalSector = sectorPos - sectorNumber; // Calculate the three axes of color. double p = brightness * (1.0 - saturation); double q = brightness * (1.0 - (saturation * fractionalSector)); double t = brightness * (1.0 - (saturation * (1 - fractionalSector))); // Assign fractional colors to RGB based on the sector the angle. switch (sectorNumber) { case 0: R = brightness; G = t; B = p; break; case 1: R = q; G = brightness; B = p; break; case 2: R = p; G = brightness; B = t; break; case 3: R = p; G = q; B = brightness; break; case 4: R = t; G = p; B = brightness; break; case 5: R = brightness; G = p; B = q; break; } } Color color = Color.FromRgb((byte)Double.Parse(String.Format("{0:0.00}", R * 255.0)), (byte)Double.Parse(String.Format("{0:0.00}", G * 255.0)), (byte)Double.Parse(String.Format("{0:0.00}", B * 255.0))); return(color); }