public ColorVerticalSliderUserControl() { InitializeComponent(); mHSL = new ColorSpaceConverter.HSL(1.0, 1.0, 1.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); mDrawStyle = EDrawStyle.Hue; mSliderPos = 0; }
public ColorPaletteUserControl() { InitializeComponent(); mHSL = new ColorSpaceConverter.HSL(1.0, 1.0, 1.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); mDrawStyle = EDrawStyle.Hue; mMarkerPos = new Point(0, 0); }
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); }
private void ResetColor() { int red, green, blue; switch (mDrawStyle) { case EDrawStyle.Hue: mHSL.S = mMarkerPos.X / (Width - 4.0); mHSL.L = 1.0 - mMarkerPos.Y / (Height - 4.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); break; case EDrawStyle.Saturation: mHSL.H = mMarkerPos.X / (Width - 4.0); mHSL.L = 1.0 - mMarkerPos.Y / (Height - 4.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); break; case EDrawStyle.Luminance: mHSL.H = mMarkerPos.X / (Width - 4.0); mHSL.S = 1.0 - mMarkerPos.Y / (Height - 4.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); break; case EDrawStyle.Red: blue = Round(255 * mMarkerPos.X / (Width - 4.0)); green = Round(255 * (1.0 - mMarkerPos.Y / (Height - 4.0))); mRGB = Color.FromArgb(mRGB.R, green, blue); mHSL = ColorSpaceConverter.RGB2HSL(mRGB); break; case EDrawStyle.Green: blue = Round(255 * mMarkerPos.X / (Width - 4.0)); red = Round(255 * (1.0 - mMarkerPos.Y / (Height - 4.0))); mRGB = Color.FromArgb(red, mRGB.G, blue); mHSL = ColorSpaceConverter.RGB2HSL(mRGB); break; case EDrawStyle.Blue: red = Round(255 * mMarkerPos.X / (Width - 4.0)); green = Round(255 * (1.0 - mMarkerPos.Y / (Height - 4.0))); mRGB = Color.FromArgb(red, green, mRGB.B); mHSL = ColorSpaceConverter.RGB2HSL(mRGB); break; }; }
// 内部函数, 获取当前位置颜色 private ColorSpaceConverter.HSL GetColor(Point pos) { int red, green, blue; ColorSpaceConverter.HSL hsl = new ColorSpaceConverter.HSL(); switch (mDrawStyle) { case EDrawStyle.Hue: hsl.H = mHSL.H; hsl.S = pos.X / (Width - 4.0); hsl.L = 1.0 - pos.Y / (Height - 4.0); break; case EDrawStyle.Saturation: hsl.S = mHSL.S; hsl.H = pos.X / (Width - 4.0); hsl.L = 1.0 - pos.Y / (Height - 4.0); break; case EDrawStyle.Luminance: hsl.L = mHSL.L; hsl.H = pos.X / (Width - 4.0); hsl.S = 1.0 - pos.Y / (Height - 4.0); break; case EDrawStyle.Red: blue = Round(255 * mMarkerPos.X / (Width - 4.0)); green = Round(255 * (1.0 - mMarkerPos.Y / (Height - 4.0))); hsl = ColorSpaceConverter.RGB2HSL(Color.FromArgb(mRGB.R, green, blue)); break; case EDrawStyle.Green: blue = Round(255 * mMarkerPos.X / (Width - 4.0)); red = Round(255 * (1.0 - mMarkerPos.Y / (Height - 4.0))); hsl = ColorSpaceConverter.RGB2HSL(Color.FromArgb(red, mRGB.G, blue)); break; case EDrawStyle.Blue: red = Round(255 * mMarkerPos.X / (Width - 4.0)); green = Round(255 * (1.0 - mMarkerPos.Y / (Height - 4.0))); hsl = ColorSpaceConverter.RGB2HSL(Color.FromArgb(red, green, mRGB.B)); break; }; return hsl; }
private void DrawContent_Saturation() { // H=0, H=1, H=2, H=3, H=4 ... H=100 // L=100 // L=99 // L=98 DrawStyleSaturation // L=97 // ... // L=0 Bitmap memBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics g = Graphics.FromImage(memBitmap); ColorSpaceConverter.HSL startHSL = new ColorSpaceConverter.HSL(); ColorSpaceConverter.HSL endHSL = new ColorSpaceConverter.HSL(); startHSL.S = mHSL.S; startHSL.L = 1.0; endHSL.S = mHSL.S; endHSL.L = 0.0; for (int i = 0; i < Width - 4; ++i) { startHSL.H = i / (Width - 4.0); endHSL.H = startHSL.H; LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(2, 2, 1, Height - 4), ColorSpaceConverter.HSL2RGB(startHSL), ColorSpaceConverter.HSL2RGB(endHSL), 90, false); g.FillRectangle(brush, new Rectangle(i + 2, 2, 1, Height - 4)); } DrawMarkerOnCanvas(g, mMarkerPos); PaintEventArgs e = new PaintEventArgs(CreateGraphics(), ClientRectangle); e.Graphics.DrawImage(memBitmap, 0, 0); memBitmap.Dispose(); g.Dispose(); }
// 内部函数 private void DrawContent_Hue() { // S=0, S=1, S=2, S=3, S=4 ... S=100 // L=100 // L=99 // L=98 DrawStyleHue // L=97 // ... // L=0 Bitmap memBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics g = Graphics.FromImage(memBitmap); ColorSpaceConverter.HSL startHSL = new ColorSpaceConverter.HSL(); ColorSpaceConverter.HSL endHSL = new ColorSpaceConverter.HSL(); startHSL.H = mHSL.H; startHSL.S = 0.0; endHSL.H = mHSL.H; endHSL.S = 1.0; // For each Horizontal line in the control, // caculate luminance at this line (Hue and Saturation are constant) for (int i = 0; i < Height - 4; ++i) { startHSL.L = 1.0 - i / (Height - 4.0); endHSL.L = startHSL.L; LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(2, 2, Width - 4, 1), ColorSpaceConverter.HSL2RGB(startHSL), ColorSpaceConverter.HSL2RGB(endHSL), 0, false); g.FillRectangle(brush, new Rectangle(2, i + 2, Width - 4, 1)); } // DrawMarkerOnCanvas(g, mMarkerPos); // PaintEventArgs e = new PaintEventArgs(CreateGraphics(), ClientRectangle); e.Graphics.DrawImage(memBitmap, 0, 0); memBitmap.Dispose(); g.Dispose(); }
// 内部函数,重新绘制当前位置的Marker为背景色 private void ClearMarker() { // Determine the area that needs to be redrawn, find the markers corners int startX = mMarkerPos.X - 5; int startY = mMarkerPos.Y - 5; int endX = mMarkerPos.X + 5; int endY = mMarkerPos.Y + 5; // Adjust the area if part of it hangs outside the content area if (startX < 0) startX = 0; if (startY < 0) startY = 0; if (endX > Width - 4) endX = Width - 4; if (endY > Height - 4) endY = Height - 4; // Redraw the content based on the current draw style ColorSpaceConverter.HSL startHSL = new ColorSpaceConverter.HSL(); ColorSpaceConverter.HSL endHSL = new ColorSpaceConverter.HSL(); Graphics g = this.CreateGraphics(); int red, green, blue; switch (mDrawStyle) { // S=0, S=1, S=2, S=3, S=4 ... S=100 // L=100 // L=99 // L=98 DrawStyleHue // L=97 // ... // L=0 case EDrawStyle.Hue : startHSL.H = mHSL.H; ///< Hue is constant endHSL.H = mHSL.H; ///< Hue is constant startHSL.S = startX / (Width - 4.0); ///< Because we are drawing horizontal lines, endHSL.S = endX / (Width - 4.0); ///< S not change from line to line for (int i = startY; i <= endY; ++i) ///< for each horizontal line { startHSL.L = 1 - i / (Height - 4.0); ///< Hue will change for each horizontal endHSL.L = startHSL.L; ///< line drawn LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(startX + 1, i + 2, endX - startX + 1, 1), ColorSpaceConverter.HSL2RGB(startHSL), ColorSpaceConverter.HSL2RGB(endHSL), 0, false); g.FillRectangle(brush, new Rectangle(startX + 2, i + 2, endX - startX + 1, 1)); } break; // H=0, H=1, H=2, H=3, H=4 ... H=360 // L=100 // L=99 // L=98 DrawStyleSaturation // L=97 // ... // L=0 case EDrawStyle.Saturation: startHSL.S = mHSL.S; ///< Saturation is constant endHSL.S = mHSL.S; ///< Saturation is constant startHSL.L = 1 - startY / (Height - 4.0); ///< Because we are drawing vertical lines, endHSL.L = 1 - endY / (Height - 4.0); ///< L not change from line to line for (int i = startX; i <= endX; ++i) ///< for each vertical line { startHSL.H = i / (Width - 4.0); ///< Hue will change for each vertical endHSL.H = startHSL.H; ///< line drawn LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(i + 2, startY + 1, 1, endY - startY + 2), ColorSpaceConverter.HSL2RGB(startHSL), ColorSpaceConverter.HSL2RGB(endHSL), 90, false); g.FillRectangle(brush, new Rectangle(i + 2, startY + 2, 1, endY - startY + 1)); } break; // H=0, H=1, H=2, H=3, H=4 ... H=100 // S=100 // S=99 // S=98 DrawStyleLuminance // S=97 // ... // S=0 case EDrawStyle.Luminance: startHSL.L = mHSL.L; ///< Luminance is constant endHSL.L = mHSL.L; ///< Luminance is constant startHSL.S = 1 - startY / (Height - 4.0); ///< Because we are drawing vertical lines endHSL.S = 1 - endY / (Height - 4.0); ///< S not change from line to line for (int i = startX; i <= endX; ++i) ///< for each vertical line { startHSL.H = i / (Width - 4.0); ///< Luminance will change for each vertical endHSL.H = startHSL.H; ///< line drawn LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(i + 2, startY+1, 1, endY-startY+2), ColorSpaceConverter.HSL2RGB(startHSL), ColorSpaceConverter.HSL2RGB(endHSL), 90, false); g.FillRectangle(brush, new Rectangle(i + 2, startY + 2, 1, endY - startY + 1)); } break; // B=0, B=1, B=2, B=3, B=4 ... B=100 // G=100 // G=99 // G=98 DrawStyleRed // G=97 // ... // G=0 case EDrawStyle.Red: red = mRGB.R; ///< Red is constant int startB = Round(255 * startX / (Width - 4.0)); int endB = Round(255 * endX / (Width - 4.0)); for (int i = startY; i <= endY; ++i) { green = Round(255 - (255 * i / (Height - 4.0))); LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(startX + 1, i + 2, endX-startX + 1, 1), Color.FromArgb(red, green, startB), Color.FromArgb(red, green, endB), 0, false); g.FillRectangle(brush, new Rectangle(startX + 2, i + 2, endX - startX + 1, 1)); } break; // B=0, B=1, B=2, B=3, B=4 ... B=100 // R=100 // R=99 // R=98 DrawStyleGreen // R=97 // ... // R=0 case EDrawStyle.Green: green = mRGB.G; int startB2 = Round(255 * startX / (Width - 4.0)); int endB2 = Round(255 * endX / (Width - 4.0)); for (int i = startY; i <= endY; ++i) { red = Round(255 - (255 * i / (Height - 4.0))); LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(startX + 1, i + 2, endX - startX + 1, 1), Color.FromArgb(red, green, startB2), Color.FromArgb(red, green, endB2), 0, false); g.FillRectangle(brush, new Rectangle(startX + 2, i + 2, endX - startX + 1, 1)); } break; // R=0, R=1, R=2, R=3, R=4 ... R=100 // G=100 // G=99 // G=98 DrawStyleGreen // G=97 // ... // G=0 case EDrawStyle.Blue: blue = mRGB.B; int startR = Round(255 * startX / (Width - 4.0)); int endR = Round(255 * endX / (Width - 4.0)); for (int i = startY; i <= endY; ++i) { green = Round(255 - (255 * i / (Height - 4.0))); LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(startX + 1, i + 2, endX - startX + 1, 1), Color.FromArgb(startR, green, blue), Color.FromArgb(endR, green, blue), 0, false); g.FillRectangle(brush, new Rectangle(startX + 2, i + 2, endX - startX + 1, 1)); } break; } }
private void ResetColor() { switch (mDrawStyle) { case EDrawStyle.Hue: mHSL.H = 1 - mSliderPos / (Height - 9.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); break; case EDrawStyle.Saturation: mHSL.S = 1 - mSliderPos / (Height - 9.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); break; case EDrawStyle.Luminance: mHSL.L = 1 - mSliderPos / (Height - 9.0); mRGB = ColorSpaceConverter.HSL2RGB(mHSL); break; case EDrawStyle.Red: mRGB = Color.FromArgb(255 - Round(255 * mSliderPos / (Height - 9.0)), mRGB.G, mRGB.B); mHSL = ColorSpaceConverter.RGB2HSL(mRGB); break; case EDrawStyle.Green: mRGB = Color.FromArgb(mRGB.R, 255 - Round(255 * mSliderPos / (Height - 9.0)), mRGB.B); mHSL = ColorSpaceConverter.RGB2HSL(mRGB); break; case EDrawStyle.Blue: mRGB = Color.FromArgb(mRGB.R, mRGB.G, 255 - Round(255 * mSliderPos / (Height - 9.0))); mHSL = ColorSpaceConverter.RGB2HSL(mRGB); break; }; }
// Fills in the content of the control showing all values of Saturation (from 0 to 100%) // for the given Hue and Luminance private void DrawContent_Saturation() { Bitmap memBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics g = Graphics.FromImage(memBitmap); ColorSpaceConverter.HSL hsl = new ColorSpaceConverter.HSL(); hsl.H = mHSL.H; ///< Use the H and L values of the current color hsl.L = mHSL.L; ///< for (int i = 0; i < Height - 8; ++i) ///< i represents the current line of pixels we want to draw horizontally { hsl.S = 1 - i / (Height - 8.0); ///< S(Saturation) is based on the current vertical position Pen pencil = new Pen(ColorSpaceConverter.HSL2RGB(hsl)); ///< Get the color for this line g.DrawLine(pencil, 11, i + 4, Width - 11, i + 4); } PaintEventArgs e = new PaintEventArgs(CreateGraphics(), ClientRectangle); e.Graphics.DrawImage(memBitmap, 0, 0); memBitmap.Dispose(); g.Dispose(); }
// Fills in the content of the control showing all values of Luminance (from 0 to 100%) // for the given Huw and Saturation private void DrawContent_Luminance() { Bitmap memBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics g = Graphics.FromImage(memBitmap); ColorSpaceConverter.HSL hsl = new ColorSpaceConverter.HSL(); hsl.H = mHSL.H; ///< Use the H and S values of the current color hsl.S = mHSL.S; ///< for (int i = 0; i < Height - 8; ++i) { hsl.L = 1 - i / (Height - 8.0); Pen pencil = new Pen(ColorSpaceConverter.HSL2RGB(hsl)); g.DrawLine(pencil, 11, i + 4, Width - 11, i + 4); } PaintEventArgs e = new PaintEventArgs(CreateGraphics(), ClientRectangle); e.Graphics.DrawImage(memBitmap, 0, 0); memBitmap.Dispose(); g.Dispose(); }
// Fills in the content of the control showing all values of Hue (from 0 to 360) private void DrawContent_Hue() { Bitmap memBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics g = Graphics.FromImage(memBitmap); ColorSpaceConverter.HSL hsl = new ColorSpaceConverter.HSL(); hsl.S = 1.0; ///< S and L will both be at 100% for this DrawStyle hsl.L = 1.0; ///< for (int i = 0; i < Height - 8; ++i) ///< i represents the current line of pixels we want to draw horizontally { hsl.H = 1.0 - i / (Height - 8.0); Pen pencil = new Pen(ColorSpaceConverter.HSL2RGB(hsl)); ///< H(Hue) is based on the current vertical position g.DrawLine(pencil, 11, i + 4, Width - 11, i + 4); ///< Get the color for this line } PaintEventArgs e = new PaintEventArgs(CreateGraphics(), ClientRectangle); e.Graphics.DrawImage(memBitmap, 0, 0); memBitmap.Dispose(); g.Dispose(); }