private void RGBColorChanged(Color color, object obj) { Textbox_TextChanged_Cancel(); ColorConverter CC = new ColorConverter(); ColorMode.RGB rgb = new ColorMode.RGB(color.R / 255.0, color.G / 255.0, color.B / 255.0); ColorMode.HSV hsv = CC.RGB2HSV(rgb.R, rgb.G, rgb.B); ColorMode.HSL hsl = CC.RGB2HSL(rgb.R, rgb.G, rgb.B); if (obj != ParaH_HSV) { ParaH_HSV.Text = ((int)Math.Round(hsv.H)).ToString(); } if (obj != ParaS_HSV) { ParaS_HSV.Text = ((int)Math.Round(hsv.S * 100.0)).ToString(); } if (obj != ParaV_HSV) { ParaV_HSV.Text = ((int)Math.Round(hsv.V * 100.0)).ToString(); } if (obj != ParaR_RGB) { ParaR_RGB.Text = ((int)Math.Round(rgb.R * 255.0)).ToString(); } if (obj != ParaG_RGB) { ParaG_RGB.Text = ((int)Math.Round(rgb.G * 255.0)).ToString(); } if (obj != ParaB_RGB) { ParaB_RGB.Text = ((int)Math.Round(rgb.B * 255.0)).ToString(); } if (obj != ParaH_HSL) { ParaH_HSL.Text = ((int)Math.Round(hsl.H)).ToString(); } if (obj != ParaS_HSL) { ParaS_HSL.Text = ((int)Math.Round(hsl.S * 100.0)).ToString(); } if (obj != ParaL_HSL) { ParaL_HSL.Text = ((int)Math.Round(hsl.L * 100.0)).ToString(); } if (obj != Para_HEX) { Para_HEX.Text = new HEXCOLORCONVERTER().COLOR2HEX(color); } Color_Before.Background = Color_Present.Background; Color_Present.Background = new SolidColorBrush(color); HSVCOLOR hsvcolor = new HSVCOLOR(this, hsv.H, hsv.S, hsv.V); Canvas.SetLeft(rectangle, hsvcolor.HSelectorX); Canvas.SetTop(rectangle, hsvcolor.HSelectorY); Canvas.SetLeft(ellipse, hsvcolor.SLSelectorX); Canvas.SetTop(ellipse, hsvcolor.SLSelectorY); HSelector_Changed(obj); SLSelector_Changed(obj); Textbox_TextChanged_Add(); }
private void HSVColorChanged(HSVCOLOR hsvcolor) { Textbox_TextChanged_Cancel(); ColorConverter CC = new ColorConverter(); ColorMode.HSV hsv = new ColorMode.HSV(hsvcolor.H, hsvcolor.S, hsvcolor.V); ColorMode.RGB rgb = CC.HSV2RGB(hsv.H != 360 ? hsv.H : 0, hsv.S, hsv.V); ColorMode.HSL hsl = CC.RGB2HSL(rgb.R, rgb.G, rgb.B); Color color = Color.FromRgb((byte)(rgb.R * 255.0), (byte)(rgb.G * 255.0), (byte)(rgb.B * 255.0)); ParaH_HSV.Text = ((int)Math.Round(hsv.H)).ToString(); ParaS_HSV.Text = ((int)Math.Round(hsv.S * 100.0)).ToString(); ParaV_HSV.Text = ((int)Math.Round(hsv.V * 100.0)).ToString(); ParaR_RGB.Text = ((int)Math.Round(rgb.R * 255.0)).ToString(); ParaG_RGB.Text = ((int)Math.Round(rgb.G * 255.0)).ToString(); ParaB_RGB.Text = ((int)Math.Round(rgb.B * 255.0)).ToString(); ParaH_HSL.Text = ((int)Math.Round(hsl.H)).ToString(); ParaS_HSL.Text = ((int)Math.Round(hsl.S * 100.0)).ToString(); ParaL_HSL.Text = ((int)Math.Round(hsl.L * 100.0)).ToString(); Para_HEX.Text = new HEXCOLORCONVERTER().COLOR2HEX(color); Color_Before.Background = Color_Present.Background; Color_Present.Background = new SolidColorBrush(color); Textbox_TextChanged_Add(); }
private void Para_TextChanged(object sender, TextChangedEventArgs e) { try { //Textbox_TextChanged_Cancel(); TextBox textbox = (TextBox)sender; //Regex.IsMatch(textbox.Text, "^[0-9]+$"); bool isRGB = sender == ParaR_RGB || sender == ParaG_RGB || sender == ParaB_RGB; bool isHSV = sender == ParaH_HSV || sender == ParaS_HSV || sender == ParaV_HSV; bool isHSL = sender == ParaH_HSL || sender == ParaS_HSL || sender == ParaL_HSL; bool isHEX = sender == Para_HEX; if (isRGB) { if (Regex.IsMatch(textbox.Text.Trim(), "^[0-9]*$")) { int R = ParaR_RGB.Text != "" ? Convert.ToInt32(ParaR_RGB.Text) : 0; int G = ParaG_RGB.Text != "" ? Convert.ToInt32(ParaG_RGB.Text) : 0; int B = ParaB_RGB.Text != "" ? Convert.ToInt32(ParaB_RGB.Text) : 0; if ( R >= 0 && R <= 255 && G >= 0 && G <= 255 && B >= 0 && B <= 255 ) { RGBColorChanged(Color.FromRgb((byte)R, (byte)G, (byte)B), sender); } } else { throw new FormatException("输入字符串的格式不正确。"); } } else if (isHSV) { if (Regex.IsMatch(textbox.Text, "^[0-9]*$")) { int H = ParaH_HSV.Text != "" ? Convert.ToInt32(ParaH_HSV.Text) : 0; int S = ParaS_HSV.Text != "" ? Convert.ToInt32(ParaS_HSV.Text) : 0; int V = ParaV_HSV.Text != "" ? Convert.ToInt32(ParaV_HSV.Text) : 0; if ( H >= 0 && H <= 360 && S >= 0 && S <= 100 && V >= 0 && V <= 100 ) { Textbox_TextChanged_Cancel(); ColorConverter CC = new ColorConverter(); HSVCOLOR hsvcolor = new HSVCOLOR(this, H, S / 100.0, V / 100.0); ColorMode.RGB rgb = CC.HSV2RGB(H != 360 ? H : 0, S / 100.0, V / 100.0); ColorMode.HSL hsl = CC.RGB2HSL(rgb.R, rgb.G, rgb.B); Color color = Color.FromRgb((byte)(rgb.R * 255.0), (byte)(rgb.G * 255.0), (byte)(rgb.B * 255.0)); ParaR_RGB.Text = ((int)Math.Round(rgb.R * 255.0)).ToString(); ParaG_RGB.Text = ((int)Math.Round(rgb.G * 255.0)).ToString(); ParaB_RGB.Text = ((int)Math.Round(rgb.B * 255.0)).ToString(); ParaH_HSL.Text = ((int)Math.Round(hsl.H)).ToString(); ParaS_HSL.Text = ((int)Math.Round(hsl.S * 100.0)).ToString(); ParaL_HSL.Text = ((int)Math.Round(hsl.L * 100.0)).ToString(); Para_HEX.Text = new HEXCOLORCONVERTER().COLOR2HEX(color); Canvas.SetLeft(rectangle, hsvcolor.HSelectorX); Canvas.SetTop(rectangle, hsvcolor.HSelectorY); Canvas.SetLeft(ellipse, hsvcolor.SLSelectorX); Canvas.SetTop(ellipse, hsvcolor.SLSelectorY); HSelector_Changed(textbox); SLSelector_Changed(textbox); Color_Before.Background = Color_Present.Background; Color_Present.Background = new SolidColorBrush(color); Textbox_TextChanged_Add(); } } else { throw new FormatException("输入字符串的格式不正确。"); } } else if (isHSL) { if (Regex.IsMatch(textbox.Text, "^[0-9]*$")) { int H = ParaH_HSL.Text != "" ? Convert.ToInt32(ParaH_HSL.Text) : 0; int S = ParaS_HSL.Text != "" ? Convert.ToInt32(ParaS_HSL.Text) : 0; int L = ParaL_HSL.Text != "" ? Convert.ToInt32(ParaL_HSL.Text) : 0; if ( H >= 0 && H <= 360 && S >= 0 && S <= 100 && L >= 0 && L <= 100 ) { Textbox_TextChanged_Cancel(); ColorConverter CC = new ColorConverter(); ColorMode.RGB rgb = CC.HSL2RGB(H != 360 ? H : 0, S / 100.0, L / 100.0); ColorMode.HSV hsv = CC.RGB2HSV(rgb.R, rgb.G, rgb.B); Color color = Color.FromRgb((byte)(rgb.R * 255.0), (byte)(rgb.G * 255.0), (byte)(rgb.B * 255.0)); ParaR_RGB.Text = ((int)Math.Round(rgb.R * 255.0)).ToString(); ParaG_RGB.Text = ((int)Math.Round(rgb.G * 255.0)).ToString(); ParaB_RGB.Text = ((int)Math.Round(rgb.B * 255.0)).ToString(); ParaH_HSV.Text = ((int)Math.Round(hsv.H)).ToString(); ParaS_HSV.Text = ((int)Math.Round(hsv.S * 100.0)).ToString(); ParaV_HSV.Text = ((int)Math.Round(hsv.V * 100.0)).ToString(); Para_HEX.Text = new HEXCOLORCONVERTER().COLOR2HEX(color); HSVCOLOR hsvcolor = new HSVCOLOR(this, hsv.H, hsv.S, hsv.V); Canvas.SetLeft(rectangle, hsvcolor.HSelectorX); Canvas.SetTop(rectangle, hsvcolor.HSelectorY); Canvas.SetLeft(ellipse, hsvcolor.SLSelectorX); Canvas.SetTop(ellipse, hsvcolor.SLSelectorY); HSelector_Changed(textbox); SLSelector_Changed(textbox); Color_Before.Background = Color_Present.Background; Color_Present.Background = new SolidColorBrush(color); Textbox_TextChanged_Add(); } } else { throw new FormatException("输入字符串的格式不正确。"); } } else if (isHEX) { if (Regex.IsMatch(textbox.Text, "^#[0-9A-Fa-f]+$")) { Color color = new HEXCOLORCONVERTER().HEX2COLOR(textbox.Text.PadRight(7, '0')); if (color != ((SolidColorBrush)Color_Present.Background).Color) { RGBColorChanged(Color.FromRgb((byte)color.R, (byte)color.G, (byte)color.B), sender); } } else { XmlDocument doc = new XmlDocument(); doc.LoadXml(Properties.Resources.csscolors); XmlElement root = doc.DocumentElement; if (!Regex.IsMatch(textbox.Text, "'")) { string textboxText = textbox.Text; if (textbox.Text.Length > 0) { if (textbox.Text[0] >= 'a' && textbox.Text[0] <= 'z') { textboxText = textbox.Text.Substring(0, 1).ToUpper() + textbox.Text.Substring(1); } } XmlNodeList nodelist = root.SelectNodes("color[@name='" + textboxText + "']"); if (nodelist.Count > 0) { string value = nodelist[0].Attributes["value"].Value; Color color = new HEXCOLORCONVERTER().HEX2COLOR(value); if (color != ((SolidColorBrush)Color_Present.Background).Color) { RGBColorChanged(Color.FromRgb((byte)color.R, (byte)color.G, (byte)color.B), sender); } } } } } } catch (Exception err) { /* * Thread thread = new Thread(new ThreadStart(() => { * MessageBox.Show(err.Message, "错误"); * })); * thread.Start(); * thread.Join(); */ MessageBox.Show(err.Message, "错误"); } }