public ColorWheel() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); //wheelRegion = new PdnRegion(); hsvColor = new HsvColor(0, 0, 0); }
public static RgbColor FromHsv(HsvColor hsv) { return hsv.ToRgb(); }
private Color[] GetColors() { Color[] colors = new Color[colorTesselation]; for (int i = 0; i < colorTesselation; i++) { int hue = (i * 360) / colorTesselation; colors[i] = new HsvColor(hue, 100, 100).ToColor(); } return colors; }
private void GrabColor(Point mouseXY) { // center our coordinate system so the middle is (0,0), and positive Y is facing up int cx = mouseXY.X - (Width / 2); int cy = mouseXY.Y - (Height / 2); double theta = Math.Atan2(cy, cx); if (theta < 0) { theta += 2 * Math.PI; } double alpha = Math.Sqrt((cx * cx) + (cy * cy)); int h = (int)((theta / (Math.PI * 2)) * 360.0); int s = (int)Math.Min(100.0, (alpha / (double)(Width / 2)) * 100); int v = 100; hsvColor = new HsvColor(h, s, v); OnColorChanged(); Invalidate(true); }
/// <summary> /// Whenever a color is changed via HSV methods, call this and the RGB /// counterparts will be sync'd up. /// </summary> /// <param name="newColor">The HSV color that should be converted to RGB.</param> private void SyncRgbFromHsv(HsvColor newColor) { if (ignore == 0) { ignore++; RgbColor rgbColor = newColor.ToRgb(); Utility.SetNumericUpDownValue(redUpDown, rgbColor.Red); Utility.SetNumericUpDownValue(greenUpDown, rgbColor.Green); Utility.SetNumericUpDownValue(blueUpDown, rgbColor.Blue); string hexText = GetHexNumericUpDownValue(rgbColor.Red, rgbColor.Green, rgbColor.Blue); hexBox.Text = hexText; SetColorGradientValuesRgb(rgbColor.Red, rgbColor.Green, rgbColor.Blue); SetColorGradientMinMaxColorsRgb(rgbColor.Red, rgbColor.Green, rgbColor.Blue); SetColorGradientMinMaxColorsAlpha((int)alphaUpDown.Value); ignore--; } }
private void UpDown_ValueChanged(object sender, System.EventArgs e) { if (sender == alphaUpDown || sender == alphaGradientControl) { if (sender == alphaGradientControl) { if (alphaUpDown.Value != (decimal)alphaGradientControl.Value) { alphaUpDown.Value = (decimal)alphaGradientControl.Value; } } else { if (alphaGradientControl.Value != (int)alphaUpDown.Value) { alphaGradientControl.Value = (int)alphaUpDown.Value; } } PushIgnoreChangedEvents(); switch (WhichUserColor) { case WhichUserColor.Primary: ColorBgra newPrimaryColor = ColorBgra.FromBgra(lastPrimaryColor.B, lastPrimaryColor.G, lastPrimaryColor.R, (byte)alphaGradientControl.Value); this.userPrimaryColor = newPrimaryColor; OnUserPrimaryColorChanged(newPrimaryColor); break; case WhichUserColor.Secondary: ColorBgra newSecondaryColor = ColorBgra.FromBgra(lastSecondaryColor.B, lastSecondaryColor.G, lastSecondaryColor.R, (byte)alphaGradientControl.Value); this.userSecondaryColor = newSecondaryColor; OnUserSecondaryColorChanged(newSecondaryColor); break; default: throw new InvalidEnumArgumentException("WhichUserColor property"); } PopIgnoreChangedEvents(); Update(); } else if (IgnoreChangedEvents) { return; } else { PushIgnoreChangedEvents(); if (sender == redUpDown || sender == greenUpDown || sender == blueUpDown) { string hexText = GetHexNumericUpDownValue((int)redUpDown.Value, (int)greenUpDown.Value, (int)blueUpDown.Value); hexBox.Text = hexText; ColorBgra rgbColor = ColorBgra.FromBgra((byte)blueUpDown.Value, (byte)greenUpDown.Value, (byte)redUpDown.Value, (byte)alphaUpDown.Value); SetColorGradientMinMaxColorsRgb(rgbColor.R, rgbColor.G, rgbColor.B); SetColorGradientMinMaxColorsAlpha(rgbColor.A); SetColorGradientValuesRgb(rgbColor.R, rgbColor.G, rgbColor.B); SetColorGradientMinMaxColorsAlpha(rgbColor.A); SyncHsvFromRgb(rgbColor); OnUserColorChanged(rgbColor); } else if (sender == hexBox) { int hexInt = 0; if (hexBox.Text.Length > 0) { try { hexInt = int.Parse(hexBox.Text,System.Globalization.NumberStyles.HexNumber); } // Needs to be changed so it reads what the RGB values were last catch (FormatException) { hexInt = 0; hexBox.Text = ""; } catch (OverflowException) { hexInt = 16777215; hexBox.Text = "FFFFFF"; } if (!((hexInt <= 16777215) && (hexInt >= 0))) { hexInt = 16777215; hexBox.Text = "FFFFFF"; } } int newRed = ((hexInt & 0xff0000) >> 16); int newGreen = ((hexInt & 0x00ff00) >> 8); int newBlue = (hexInt & 0x0000ff); Utility.SetNumericUpDownValue(redUpDown, newRed); Utility.SetNumericUpDownValue(greenUpDown, newGreen); Utility.SetNumericUpDownValue(blueUpDown, newBlue); SetColorGradientMinMaxColorsRgb(newRed, newGreen, newBlue); SetColorGradientValuesRgb(newRed, newGreen, newBlue); SetColorGradientMinMaxColorsAlpha((int)alphaUpDown.Value); ColorBgra rgbColor = ColorBgra.FromBgra((byte)newBlue, (byte)newGreen, (byte)newRed, (byte)alphaUpDown.Value); SyncHsvFromRgb(rgbColor); OnUserColorChanged(rgbColor); } else if (sender == hueUpDown || sender == saturationUpDown || sender == valueUpDown) { HsvColor oldHsvColor = colorWheel.HsvColor; HsvColor newHsvColor = new HsvColor((int)hueUpDown.Value, (int)saturationUpDown.Value, (int)valueUpDown.Value); if (oldHsvColor != newHsvColor) { colorWheel.HsvColor = newHsvColor; SetColorGradientValuesHsv(newHsvColor.Hue, newHsvColor.Saturation, newHsvColor.Value); SetColorGradientMinMaxColorsHsv(newHsvColor.Hue, newHsvColor.Saturation, newHsvColor.Value); SyncRgbFromHsv(newHsvColor); RgbColor rgbColor = newHsvColor.ToRgb(); OnUserColorChanged(ColorBgra.FromBgra((byte)rgbColor.Blue, (byte)rgbColor.Green, (byte)rgbColor.Red, (byte)alphaUpDown.Value)); } } PopIgnoreChangedEvents(); } }
private void HsvGradientControl_ValueChanged(object sender, IndexEventArgs e) { if (IgnoreChangedEvents) { return; } int hue; if (sender == hueGradientControl) { hue = (hueGradientControl.Value * 360) / 255; } else { hue = (int)hueUpDown.Value; } int saturation; if (sender == saturationGradientControl) { saturation = (saturationGradientControl.Value * 100) / 255; } else { saturation = (int)saturationUpDown.Value; } int value; if (sender == valueGradientControl) { value = (valueGradientControl.Value * 100) / 255; } else { value = (int)valueUpDown.Value; } HsvColor hsvColor = new HsvColor(hue, saturation, value); colorWheel.HsvColor = hsvColor; RgbColor rgbColor = hsvColor.ToRgb(); ColorBgra color = ColorBgra.FromBgra((byte)rgbColor.Blue, (byte)rgbColor.Green, (byte)rgbColor.Red, (byte)alphaUpDown.Value); Utility.SetNumericUpDownValue(hueUpDown, hsvColor.Hue); Utility.SetNumericUpDownValue(saturationUpDown, hsvColor.Saturation); Utility.SetNumericUpDownValue(valueUpDown, hsvColor.Value); Utility.SetNumericUpDownValue(redUpDown, rgbColor.Red); Utility.SetNumericUpDownValue(greenUpDown, rgbColor.Green); Utility.SetNumericUpDownValue(blueUpDown, rgbColor.Blue); string hexText = GetHexNumericUpDownValue(rgbColor.Red, rgbColor.Green, rgbColor.Blue); hexBox.Text = hexText; switch (WhichUserColor) { case WhichUserColor.Primary: UserPrimaryColor = color; OnRelinquishFocus(); break; case WhichUserColor.Secondary: UserSecondaryColor = color; OnRelinquishFocus(); break; default: throw new InvalidEnumArgumentException("WhichUserColor property"); } Update(); }
private void SetColorGradientMinMaxColorsHsv(int h, int s, int v) { Color[] hueColors = new Color[361]; for (int newH = 0; newH <= 360; ++newH) { HsvColor hsv = new HsvColor(newH, 100, 100); hueColors[newH] = hsv.ToColor(); } this.hueGradientControl.CustomGradient = hueColors; Color[] satColors = new Color[101]; for (int newS = 0; newS <= 100; ++newS) { HsvColor hsv = new HsvColor(h, newS, v); satColors[newS] = hsv.ToColor(); } this.saturationGradientControl.CustomGradient = satColors; this.valueGradientControl.MaxColor = new HsvColor(h, s, 100).ToColor(); this.valueGradientControl.MinColor = new HsvColor(h, s, 0).ToColor(); }