private void SetPickedColor(Color pColor) { Bitmap cView = new Bitmap(colorBox.Width, colorBox.Height); using (Graphics g = Graphics.FromImage(cView)) { g.Clear(pColor); using (SolidBrush sb = new SolidBrush(ColorUtils.Invert(pColor))) { using (Pen p = new Pen(sb, 1)) { g.DrawRectangle(p, new Rectangle(1, 1, cView.Width - 3, cView.Height - 3)); } } } HSLColor hlc = HSLColor.FromColor(pColor); HSVColor hvc = HSVColor.FromColor(pColor); RGBColor rgc = RGBColor.FromColor(pColor); HEXColor hxc = HEXColor.FromColor(pColor); lbl_HTML.Text = hxc.ToString(); lbl_RGB.Text = rgc.ToString(); lbl_HSL.Text = hlc.ToString(); lbl_HSV.Text = hvc.ToString(); colorBox.Image?.Dispose(); colorBox.Image = cView; }
// Could have been a dependency property that pieces of the UI bind to, but the interaction is complex enough // to make this the easier route: public void SetCurrentColor(HSVColor value) { this.currentColor = value; Color rgbColor = ColorConverter.HSVToRGB(value); this.ResetButton.IsEnabled = true; this.PreviewTile.Fill = new SolidColorBrush(rgbColor); // Only update the TextBox if this isn't being done in response to typing. That prevents // text like "red" from changing to "#FF0000" and prevents the cursor from jumping to the beginning. if (!keyPressed) this.ColorTextBox.Text = value.ToString(); this.HueGradientStop.Color = ColorConverter.HSVToRGB(new HSVColor { A = 255, H = value.H, S = 1, V = 1 }); Canvas.SetLeft(this.HueThumb, (value.H / 360) * this.HueSlider.Width - this.HueThumb.Width / 2); // Update AlphaSlider and AlphaThumb: Color opaqueColor = rgbColor; opaqueColor.A = 255; Color transparentColor = rgbColor; transparentColor.A = 0; this.alphaGradientStop.Color = opaqueColor; this.alphaGradientStop2.Color = transparentColor; Canvas.SetLeft(this.AlphaThumb, ((double)value.A / 255) * this.AlphaSlider.Width - this.AlphaThumb.Width / 2); // Update SaturationValueMap: Point saturationValuePoint = new Point(value.S * this.SaturationValueMap.Width, value.V * this.SaturationValueMap.Height); this.SelectionLineX1.X1 = this.SelectionLineX1.X2 = saturationValuePoint.X; this.SelectionLineX2.X1 = this.SelectionLineX2.X2 = saturationValuePoint.X; this.SelectionLineY1.Y1 = this.SelectionLineY1.Y2 = saturationValuePoint.Y; this.SelectionLineY2.Y1 = this.SelectionLineY2.Y2 = saturationValuePoint.Y; }