public ColorPickForm() { InitializeComponent(); mColorPicked = new Color(); mColorHSL = new ColorSpaceConverter.HSL(); mColorCMYK = new ColorSpaceConverter.CMYK(); radioButtonHSL_H.Checked = true; ///< 默认模式 // 注册事件响应 colorPaletteUserControl.Scroll += new System.EventHandler(this.colorPaletteUserControl_Scroll); colorVerticalSliderUserControl.Scroll += new System.EventHandler(this.colorVerticalSliderUserControl_Scroll); // 设置黑色为默认颜色 ColorPicked = Color.FromArgb(0, 0, 0); }
private void textBoxRGB_R_Leave(object sender, EventArgs e) { string colorText = textBoxRGB_R.Text; // 非法字符检测 bool hasIllegalChar = false; if (colorText.Length <= 0) { hasIllegalChar = true; } else { foreach (char letter in colorText) { if (!char.IsNumber(letter)) { hasIllegalChar = true; break; } } } // 计算新的颜色值 if (hasIllegalChar) { MessageBox.Show("Red must be a number value between 0 and 255!"); UpdateTextBoxes(); ///< 更新文本框 } else { int colorValue = int.Parse(colorText); if (colorValue < 0) { MessageBox.Show("Red must be a number value between 0 and 255, Closese value inserted!"); textBoxRGB_R.Text = "0"; mColorPicked = Color.FromArgb(0, mColorPicked.G, mColorPicked.B); } else if (colorValue > 255) { MessageBox.Show("Red must be a number value between 0 and 255, Closese value inserted!"); textBoxRGB_R.Text = "255"; mColorPicked = Color.FromArgb(255, mColorPicked.G, mColorPicked.B); } else { mColorPicked = Color.FromArgb(colorValue, mColorPicked.G, mColorPicked.B); } mColorHSL = Common.ColorSpaceConverter.RGB2HSL(mColorPicked); mColorCMYK = Common.ColorSpaceConverter.RGB2CMYK(mColorPicked); colorPaletteUserControl.RGB = mColorPicked; ///< 用RGB值更新主控件 colorVerticalSliderUserControl.RGB = mColorPicked; ///< 用RGB值更新辅助控件 UpdateTextBoxes(); ///< 更新文本框 } }
private void colorVerticalSliderUserControl_Scroll(object sender, System.EventArgs e) { // 从辅助颜色控件获取当前颜色 mColorPicked = colorVerticalSliderUserControl.RGB; mColorHSL =colorVerticalSliderUserControl.HSL; mColorCMYK = Common.ColorSpaceConverter.RGB2CMYK(colorVerticalSliderUserControl.RGB); // 更新界面 UpdateTextBoxes(); // 用HSL值更新主控件(数值精度比较高) colorPaletteUserControl.HSL = mColorHSL; // 发送改变消息 if (OnColorChanged != null) OnColorChanged(this, e); }