// 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; }
public static Color HSVToRGB(HSVColor hsvColor) { int hi = Convert.ToInt32(Math.Floor(hsvColor.H / 60)) % 6; double f = hsvColor.H / 60 - Math.Floor(hsvColor.H / 60); hsvColor.V = hsvColor.V * 255; byte v = Convert.ToByte(hsvColor.V); byte p = Convert.ToByte(hsvColor.V * (1 - hsvColor.S)); byte q = Convert.ToByte(hsvColor.V * (1 - f * hsvColor.S)); byte t = Convert.ToByte(hsvColor.V * (1 - (1 - f) * hsvColor.S)); if (hi == 0) return Color.FromArgb(hsvColor.A, v, t, p); else if (hi == 1) return Color.FromArgb(hsvColor.A, q, v, p); else if (hi == 2) return Color.FromArgb(hsvColor.A, p, v, t); else if (hi == 3) return Color.FromArgb(hsvColor.A, p, q, v); else if (hi == 4) return Color.FromArgb(hsvColor.A, t, p, v); else return Color.FromArgb(hsvColor.A, v, p, q); }
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (this.State.ContainsKey("CurrentColor")) { SetCurrentColor((HSVColor)this.State["CurrentColor"]); } else if (NavigationContext.QueryString.ContainsKey("currentColor")) { Color color; if (TryGetColorFromText("#" + NavigationContext.QueryString["currentColor"], out color)) SetCurrentColor(ColorConverter.RGBToHSV(color)); } if (NavigationContext.QueryString.ContainsKey("defaultColor")) { Color color; if (TryGetColorFromText("#" + NavigationContext.QueryString["defaultColor"], out color)) defaultColor = ColorConverter.RGBToHSV(color); if (defaultColor == currentColor) { this.Focus(); // So focus doesn't go to TextBox and bring up keyboard this.ResetButton.IsEnabled = false; } } if (NavigationContext.QueryString.ContainsKey("settingName")) settingName = NavigationContext.QueryString["settingName"]; if (NavigationContext.QueryString.ContainsKey("showOpacity")) { bool newValue = Convert.ToBoolean(NavigationContext.QueryString["showOpacity"]); // Only do this if switching from showing to not showing, to guard against multiple navigations to this page // (for example, from locking/unlocking the phone) if (showOpacity && !newValue) { // Hide options for non-opaque colors AlphaPanel.Visibility = Visibility.Collapsed; TransparentPresetColor.Visibility = Visibility.Collapsed; // Spread out remaining UI Canvas.SetTop(ColorPresetsPanel, Canvas.GetTop(ColorPresetsPanel) + 14); Canvas.SetTop(SaturationValuePanel, Canvas.GetTop(SaturationValuePanel) + 30); Canvas.SetTop(HuePanel, Canvas.GetTop(HuePanel) + 50); } showOpacity = newValue; } }