/// <summary> /// Resets the controls color (both HSL and RGB variables) based on the current slider position</summary> private void ResetHSLRGB() { switch (m_eDrawStyle) { case eDrawStyle.Hue: m_hsl.H = 1.0 - (double)m_iMarker_Start_Y / (Height - 9); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Saturation: m_hsl.S = 1.0 - (double)m_iMarker_Start_Y / (Height - 9); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Brightness: m_hsl.L = 1.0 - (double)m_iMarker_Start_Y / (Height - 9); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Red: m_rgb = Color.FromArgb(m_rgb.A, 255 - Round(255 * (double)m_iMarker_Start_Y / (Height - 9)), m_rgb.G, m_rgb.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Green: m_rgb = Color.FromArgb(m_rgb.A, m_rgb.R, 255 - Round(255 * (double)m_iMarker_Start_Y / (Height - 9)), m_rgb.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Blue: m_rgb = Color.FromArgb(m_rgb.A, m_rgb.R, m_rgb.G, 255 - Round(255 * (double)m_iMarker_Start_Y / (Height - 9))); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; } }
/// <summary> /// Draws the marker (circle) inside the color box</summary> /// <param name="x">X-coordinate</param> /// <param name="y">Y-coordinate</param> /// <param name="Unconditional">Whether to draw the marker, even if its position has not changed</param> private void DrawMarker(int x, int y, bool Unconditional) // ***** { // * | * if (x < 0) { x = 0; // * | * } if (x > Width - 4) { x = Width - 4; // * | * } if (y < 0) { y = 0; // * | * } if (y > Height - 4) { y = Height - 4; // *----X----* } // * | * if (m_iMarker_Y == y && m_iMarker_X == x && !Unconditional) // * | * { return; // * | * } // * | * ClearMarker(); // ***** m_iMarker_X = x; m_iMarker_Y = y; Graphics g = CreateGraphics(); Pen pen; AdobeColors.HSL _hsl = GetColor(x, y); // The selected color determines the color of the marker drawn over // it (black or white) if (_hsl.L < (double)200 / 255) { pen = new Pen(Color.White); // White marker if selected color is dark } else if (_hsl.H < (double)26 / 360 || _hsl.H > (double)200 / 360) { if (_hsl.S > (double)70 / 255) { pen = new Pen(Color.White); } else { pen = new Pen(Color.Black); // Else use a black marker for lighter colors } } else { pen = new Pen(Color.Black); } g.DrawEllipse(pen, x - 3, y - 3, 10, 10); // Draw the marker : 11 x 11 circle DrawBorder(); // Force the border to be redrawn, just in case the marker has been drawn over it. }
/// <summary> /// Constructor</summary> public ColorBox() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // Initialize Colors m_hsl = new AdobeColors.HSL(); m_hsl.H = 1.0; m_hsl.S = 1.0; m_hsl.L = 1.0; m_rgb = AdobeColors.HSL_to_RGB(m_hsl); m_eDrawStyle = eDrawStyle.Hue; }
/// <summary> /// Fills in the content of the control showing all values of Luminance (0 to 100%) for the given /// Hue and Saturation</summary> private void Draw_Style_Luminance() { Graphics g = CreateGraphics(); AdobeColors.HSL _hsl = new AdobeColors.HSL(); _hsl.H = m_hsl.H; // Use the H and S values of the current color (m_hsl) _hsl.S = m_hsl.S; for (int i = 0; i < Height - 8; i++) // i represents the current line of pixels we want to draw horizontally { _hsl.L = 1.0 - (double)i / (Height - 8); // L (Luminance) is based on the current vertical position Pen pen = new Pen(AdobeColors.HSL_to_RGB(_hsl)); // Get the Color for this line g.DrawLine(pen, 11, i + 4, Width - 11, i + 4); // Draw the line and loop back for next line } }
// The following functions do the real work of the control, drawing the primary content (the area between the slider) // /// <summary> /// Fills in the content of the control showing all values of Hue (from 0 to 360)</summary> private void Draw_Style_Hue() { Graphics g = CreateGraphics(); AdobeColors.HSL _hsl = new AdobeColors.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 - (double)i / (Height - 8); // H (hue) is based on the current vertical position Pen pen = new Pen(AdobeColors.HSL_to_RGB(_hsl)); // Get the Color for this line g.DrawLine(pen, 11, i + 4, Width - 11, i + 4); // Draw the line and loop back for next line } }
/// <summary> /// Resets the controls color (both HSL and RGB variables) based on the current marker position</summary> private void ResetHSLRGB() { int red, green, blue; switch (m_eDrawStyle) { case eDrawStyle.Hue: m_hsl.S = (double)m_iMarker_X / (Width - 4); m_hsl.L = 1.0 - (double)m_iMarker_Y / (Height - 4); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Saturation: m_hsl.H = (double)m_iMarker_X / (Width - 4); m_hsl.L = 1.0 - (double)m_iMarker_Y / (Height - 4); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Brightness: m_hsl.H = (double)m_iMarker_X / (Width - 4); m_hsl.S = 1.0 - (double)m_iMarker_Y / (Height - 4); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Red: blue = Round(255 * (double)m_iMarker_X / (Width - 4)); green = Round(255 * (1.0 - (double)m_iMarker_Y / (Height - 4))); m_rgb = Color.FromArgb(m_rgb.A, m_rgb.R, green, blue); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Green: blue = Round(255 * (double)m_iMarker_X / (Width - 4)); red = Round(255 * (1.0 - (double)m_iMarker_Y / (Height - 4))); m_rgb = Color.FromArgb(m_rgb.A, red, m_rgb.G, blue); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Blue: red = Round(255 * (double)m_iMarker_X / (Width - 4)); green = Round(255 * (1.0 - (double)m_iMarker_Y / (Height - 4))); m_rgb = Color.FromArgb(m_rgb.A, red, green, m_rgb.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; } }
/// <summary> /// Draws the content of the control, filling in all color values with the provided hue value</summary> private void Draw_Style_Hue() { Graphics g = CreateGraphics(); AdobeColors.HSL hsl_start = new AdobeColors.HSL(); AdobeColors.HSL hsl_end = new AdobeColors.HSL(); hsl_start.H = m_hsl.H; hsl_end.H = m_hsl.H; hsl_start.S = 0.0; hsl_end.S = 1.0; for (int i = 0; i < Height - 4; i++) // For each horizontal line in the control: { hsl_start.L = 1.0 - (double)i / (Height - 4); // Calculate luminance at this line (Hue and Saturation are constant) hsl_end.L = hsl_start.L; LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2, 2, Width - 4, 1), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 0, false); g.FillRectangle(br, new Rectangle(2, i + 2, Width - 4, 1)); } }
/// <summary> /// Draws the content of the control, filling in all color values with the provided luminance or brightness value</summary> private void Draw_Style_Luminance() { Graphics g = CreateGraphics(); AdobeColors.HSL hsl_start = new AdobeColors.HSL(); AdobeColors.HSL hsl_end = new AdobeColors.HSL(); hsl_start.L = m_hsl.L; hsl_end.L = m_hsl.L; hsl_start.S = 1.0; hsl_end.S = 0.0; for (int i = 0; i < Width - 4; i++) // For each vertical line in the control: { hsl_start.H = (double)i / (Width - 4); // Calculate Hue at this line (Saturation and Luminance are constant) hsl_end.H = hsl_start.H; LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2, 2, 1, Height - 4), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false); g.FillRectangle(br, new Rectangle(i + 2, 2, 1, Height - 4)); } }
/// <summary> /// Returns the graphed color at the x, y position on the control</summary> /// <param name="x">X-coordinate</param> /// <param name="y">Y-coordinate</param> /// <returns>Color at position</returns> private AdobeColors.HSL GetColor(int x, int y) { AdobeColors.HSL _hsl = new AdobeColors.HSL(); switch (m_eDrawStyle) { case eDrawStyle.Hue: _hsl.H = m_hsl.H; _hsl.S = (double)x / (Width - 4); _hsl.L = 1.0 - (double)y / (Height - 4); break; case eDrawStyle.Saturation: _hsl.S = m_hsl.S; _hsl.H = (double)x / (Width - 4); _hsl.L = 1.0 - (double)y / (Height - 4); break; case eDrawStyle.Brightness: _hsl.L = m_hsl.L; _hsl.H = (double)x / (Width - 4); _hsl.S = 1.0 - (double)y / (Height - 4); break; case eDrawStyle.Red: _hsl = AdobeColors.RGB_to_HSL(Color.FromArgb(m_rgb.R, Round(255 * (1.0 - (double)y / (Height - 4))), Round(255 * (double)x / (Width - 4)))); break; case eDrawStyle.Green: _hsl = AdobeColors.RGB_to_HSL(Color.FromArgb(Round(255 * (1.0 - (double)y / (Height - 4))), m_rgb.G, Round(255 * (double)x / (Width - 4)))); break; case eDrawStyle.Blue: _hsl = AdobeColors.RGB_to_HSL(Color.FromArgb(Round(255 * (double)x / (Width - 4)), Round(255 * (1.0 - (double)y / (Height - 4))), m_rgb.B)); break; } return(_hsl); }
/// <summary> /// Resets the controls color (both HSL and RGB variables) based on the current slider position</summary> private void ResetHSLRGB() { switch (m_eDrawStyle) { case eDrawStyle.Hue : m_hsl.H = 1.0 - (double)m_iMarker_Start_Y/(Height - 9); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Saturation : m_hsl.S = 1.0 - (double)m_iMarker_Start_Y/(Height - 9); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Brightness : m_hsl.L = 1.0 - (double)m_iMarker_Start_Y/(Height - 9); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Red : m_rgb = Color.FromArgb(m_rgb.A, 255 - Round( 255 * (double)m_iMarker_Start_Y/(Height - 9) ), m_rgb.G, m_rgb.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Green : m_rgb = Color.FromArgb(m_rgb.A, m_rgb.R, 255 - Round( 255 * (double)m_iMarker_Start_Y/(Height - 9) ), m_rgb.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Blue : m_rgb = Color.FromArgb(m_rgb.A, m_rgb.R, m_rgb.G, 255 - Round( 255 * (double)m_iMarker_Start_Y/(Height - 9) )); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; } }
/// <summary> /// Fills in the content of the control showing all values of Luminance (0 to 100%) for the given /// Hue and Saturation</summary> private void Draw_Style_Luminance() { Graphics g = CreateGraphics(); AdobeColors.HSL _hsl = new AdobeColors.HSL(); _hsl.H = m_hsl.H; // Use the H and S values of the current color (m_hsl) _hsl.S = m_hsl.S; for ( int i = 0; i < Height - 8; i++ ) // i represents the current line of pixels we want to draw horizontally { _hsl.L = 1.0 - (double)i/(Height - 8); // L (Luminance) is based on the current vertical position Pen pen = new Pen(AdobeColors.HSL_to_RGB(_hsl)); // Get the Color for this line g.DrawLine(pen, 11, i + 4, Width - 11, i + 4); // Draw the line and loop back for next line } }
// The following functions do the real work of the control, drawing the primary content (the area between the slider) // /// <summary> /// Fills in the content of the control showing all values of Hue (from 0 to 360)</summary> private void Draw_Style_Hue() { Graphics g = CreateGraphics(); AdobeColors.HSL _hsl = new AdobeColors.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 - (double)i/(Height - 8); // H (hue) is based on the current vertical position Pen pen = new Pen(AdobeColors.HSL_to_RGB(_hsl)); // Get the Color for this line g.DrawLine(pen, 11, i + 4, Width - 11, i + 4); // Draw the line and loop back for next line } }
private void m_lbl_Secondary_Color_Click(object sender, System.EventArgs e) { Color secondary = m_lbl_Secondary_Color.BackColor; m_rgb = Color.FromArgb(m_rgb.A, secondary.R, secondary.G, secondary.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); m_ctrl_BigBox.HSL = m_hsl; m_ctrl_ThinBox.HSL = m_hsl; m_lbl_Primary_Color.BackColor = MakeOpaque(m_rgb); m_lbl_Primary_Color.Update(); m_cmyk = AdobeColors.RGB_to_CMYK(m_rgb); m_txt_Hue.Text = Round(m_hsl.H * 360).ToString(); m_txt_Sat.Text = Round(m_hsl.S * 100).ToString(); m_txt_Bright.Text = Round(m_hsl.L * 100).ToString(); m_txt_Red.Text = m_rgb.R.ToString(); m_txt_Green.Text = m_rgb.G.ToString(); m_txt_Blue.Text = m_rgb.B.ToString(); m_txt_Cyan.Text = Round(m_cmyk.C * 100).ToString(); m_txt_Magenta.Text = Round(m_cmyk.M * 100).ToString(); m_txt_Yellow.Text = Round(m_cmyk.Y * 100).ToString(); m_txt_K.Text = Round(m_cmyk.K * 100).ToString(); m_txt_A.Text = m_rgb.A.ToString(); m_txt_Hue.Update(); m_txt_Sat.Update(); m_txt_Red.Update(); m_txt_Green.Update(); m_txt_Blue.Update(); m_txt_Cyan.Update(); m_txt_Magenta.Update(); m_txt_Yellow.Update(); m_txt_K.Update(); m_txt_A.Update(); }
private void m_txt_Hex_Leave(object sender, System.EventArgs e) { string text = m_txt_Hex.Text.ToUpper(); bool has_illegal_chars = false; if ( text.Length <= 0 ) has_illegal_chars = true; foreach ( char letter in text ) { if ( !char.IsNumber(letter) ) { if ( letter >= 'A' && letter <= 'F' ) continue; has_illegal_chars = true; break; } } if ( has_illegal_chars ) { MessageBox.Show("Hex must be a hex value between 00000000 and FFFFFFFF".Localize()); WriteHexData(m_rgb); return; } Color hexColor = ParseHexData(text); if (hexColor.IsEmpty) { WriteHexData(m_rgb); return; } m_rgb = hexColor; m_hsl = AdobeColors.RGB_to_HSL(m_rgb); m_cmyk = AdobeColors.RGB_to_CMYK(m_rgb); m_ctrl_BigBox.HSL = m_hsl; m_ctrl_ThinBox.HSL = m_hsl; m_lbl_Primary_Color.BackColor = m_rgb; UpdateTextBoxes(); }
private void m_ctrl_ThinBox_ColorChanged(object sender, System.EventArgs e) { m_hsl = m_ctrl_ThinBox.HSL; m_rgb = AdobeColors.HSL_to_RGB(m_hsl); m_cmyk = AdobeColors.RGB_to_CMYK(m_rgb); m_txt_Hue.Text = Round(m_hsl.H * 360).ToString(); m_txt_Sat.Text = Round(m_hsl.S * 100).ToString(); m_txt_Bright.Text = Round(m_hsl.L * 100).ToString(); m_txt_Red.Text = m_rgb.R.ToString(); m_txt_Green.Text = m_rgb.G.ToString(); m_txt_Blue.Text = m_rgb.B.ToString(); m_txt_Cyan.Text = Round(m_cmyk.C * 100).ToString(); m_txt_Magenta.Text = Round(m_cmyk.M * 100).ToString(); m_txt_Yellow.Text = Round(m_cmyk.Y * 100).ToString(); m_txt_K.Text = Round(m_cmyk.K * 100).ToString(); m_txt_A.Text = m_rgb.A.ToString(); m_txt_Hue.Update(); m_txt_Sat.Update(); m_txt_Bright.Update(); m_txt_Red.Update(); m_txt_Green.Update(); m_txt_Blue.Update(); m_txt_Cyan.Update(); m_txt_Magenta.Update(); m_txt_Yellow.Update(); m_txt_K.Update(); m_txt_A.Update(); m_ctrl_BigBox.HSL = m_hsl; m_lbl_Primary_Color.BackColor = MakeOpaque(m_rgb); m_lbl_Primary_Color.Update(); WriteHexData(m_rgb); }
private void m_txt_K_Leave(object sender, System.EventArgs e) { string text = m_txt_K.Text; bool has_illegal_chars = false; if ( text.Length <= 0 ) has_illegal_chars = true; else foreach ( char letter in text ) { if ( !char.IsNumber(letter) ) { has_illegal_chars = true; break; } } if ( has_illegal_chars ) { MessageBox.Show("Key must be a number between 0 and 100".Localize()); UpdateTextBoxes(); return; } int key = int.Parse(text); if ( key < 0 ) { m_txt_K.Text = "0"; m_cmyk.K = 0.0; } else if ( key > 100 ) { m_txt_K.Text = "100"; m_cmyk.K = 1.0; } else { m_cmyk.K = (double)key/100; } m_rgb = AdobeColors.CMYK_to_RGB(m_cmyk); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); m_ctrl_BigBox.HSL = m_hsl; m_ctrl_ThinBox.HSL = m_hsl; m_lbl_Primary_Color.BackColor = MakeOpaque(m_rgb); UpdateTextBoxes(); }
/// <summary> /// Sets starting color</summary> /// <param name="starting_color">The starting color</param> /// <param name="enableAlpha">Enable the alpha channel iff true</param> public void SetStartColor(Color starting_color, bool enableAlpha) { m_enableAlpha = enableAlpha; if (enableAlpha) { m_txt_A.Visible = true; m_lbl_A.Visible = true; m_txt_Hex.MaxLength = 8; } else { starting_color = MakeOpaque(starting_color); m_txt_A.Visible = false; m_lbl_A.Visible = false; m_txt_Hex.MaxLength = 6; } m_rgb = starting_color; m_hsl = AdobeColors.RGB_to_HSL(m_rgb); m_cmyk = AdobeColors.RGB_to_CMYK(m_rgb); m_txt_Hue.Text = Round(m_hsl.H * 360).ToString(); m_txt_Sat.Text = Round(m_hsl.S * 100).ToString(); m_txt_Bright.Text = Round(m_hsl.L * 100).ToString(); m_txt_Red.Text = m_rgb.R.ToString(); m_txt_Green.Text = m_rgb.G.ToString(); m_txt_Blue.Text = m_rgb.B.ToString(); m_txt_Cyan.Text = Round(m_cmyk.C * 100).ToString(); m_txt_Magenta.Text = Round(m_cmyk.M * 100).ToString(); m_txt_Yellow.Text = Round(m_cmyk.Y * 100).ToString(); m_txt_K.Text = Round(m_cmyk.K * 100).ToString(); m_txt_A.Text = m_rgb.A.ToString(); m_txt_Hue.Update(); m_txt_Sat.Update(); m_txt_Red.Update(); m_txt_Green.Update(); m_txt_Blue.Update(); m_txt_Cyan.Update(); m_txt_Magenta.Update(); m_txt_Yellow.Update(); m_txt_K.Update(); m_txt_A.Update(); m_ctrl_BigBox.HSL = m_hsl; m_ctrl_ThinBox.HSL = m_hsl; m_lbl_Primary_Color.BackColor = MakeOpaque(starting_color); m_lbl_Secondary_Color.BackColor = MakeOpaque(starting_color); m_rbtn_Hue.Checked = true; WriteHexData(m_rgb); }
/// <summary> /// Redraws only the content over the marker</summary> private void ClearMarker() { Graphics g = CreateGraphics(); // Determine the area that needs to be redrawn int start_x, start_y, end_x, end_y; int red = 0; int green = 0; int blue = 0; AdobeColors.HSL hsl_start = new AdobeColors.HSL(); AdobeColors.HSL hsl_end = new AdobeColors.HSL(); // Find the markers corners start_x = m_iMarker_X - 5; start_y = m_iMarker_Y - 5; end_x = m_iMarker_X + 5; end_y = m_iMarker_Y + 5; // Adjust the area if part of it hangs outside the content area if (start_x < 0) { start_x = 0; } if (start_y < 0) { start_y = 0; } if (end_x > Width - 4) { end_x = Width - 4; } if (end_y > Height - 4) { end_y = Height - 4; } // Redraw the content based on the current draw style: // The code get's a little messy from here switch (m_eDrawStyle) { // S=0,S=1,S=2,S=3.....S=100 // L=100 // L=99 // L=98 Drawstyle // L=97 Hue // ... // L=0 case eDrawStyle.Hue: hsl_start.H = m_hsl.H; hsl_end.H = m_hsl.H; // Hue is constant hsl_start.S = (double)start_x / (Width - 4); // Because we're drawing horizontal lines, s will not change hsl_end.S = (double)end_x / (Width - 4); // from line to line for (int i = start_y; i <= end_y; i++) // For each horizontal line: { hsl_start.L = 1.0 - (double)i / (Height - 4); // Brightness (L) WILL change for each horizontal hsl_end.L = hsl_start.L; // line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1, i + 2, end_x - start_x + 1, 1), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 0, false); g.FillRectangle(br, new Rectangle(start_x + 2, i + 2, end_x - start_x + 1, 1)); } break; // H=0,H=1,H=2,H=3.....H=360 // L=100 // L=99 // L=98 Drawstyle // L=97 Saturation // ... // L=0 case eDrawStyle.Saturation: hsl_start.S = m_hsl.S; hsl_end.S = m_hsl.S; // Saturation is constant hsl_start.L = 1.0 - (double)start_y / (Height - 4); // Because we're drawing vertical lines, L will hsl_end.L = 1.0 - (double)end_y / (Height - 4); // not change from line to line for (int i = start_x; i <= end_x; i++) // For each vertical line: { hsl_start.H = (double)i / (Width - 4); // Hue (H) WILL change for each vertical hsl_end.H = hsl_start.H; // line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(i + 2, start_y + 1, 1, end_y - start_y + 2), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false); g.FillRectangle(br, new Rectangle(i + 2, start_y + 2, 1, end_y - start_y + 1)); } break; // H=0,H=1,H=2,H=3.....H=360 // S=100 // S=99 // S=98 Drawstyle // S=97 Brightness // ... // S=0 case eDrawStyle.Brightness: hsl_start.L = m_hsl.L; hsl_end.L = m_hsl.L; // Luminance is constant hsl_start.S = 1.0 - (double)start_y / (Height - 4); // Because we're drawing vertical lines, S will hsl_end.S = 1.0 - (double)end_y / (Height - 4); // not change from line to line for (int i = start_x; i <= end_x; i++) // For each vertical line: { hsl_start.H = (double)i / (Width - 4); // Hue (H) WILL change for each vertical hsl_end.H = hsl_start.H; // line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(i + 2, start_y + 1, 1, end_y - start_y + 2), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false); g.FillRectangle(br, new Rectangle(i + 2, start_y + 2, 1, end_y - start_y + 1)); } break; // B=0,B=1,B=2,B=3.....B=100 // G=100 // G=99 // G=98 Drawstyle // G=97 Red // ... // G=0 case eDrawStyle.Red: red = m_rgb.R; // Red is constant int start_b = Round(255 * (double)start_x / (Width - 4)); // Because we're drawing horizontal lines, B int end_b = Round(255 * (double)end_x / (Width - 4)); // will not change from line to line for (int i = start_y; i <= end_y; i++) // For each horizontal line: { green = Round(255 - (255 * (double)i / (Height - 4))); // green WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1, i + 2, end_x - start_x + 1, 1), Color.FromArgb(red, green, start_b), Color.FromArgb(red, green, end_b), 0, false); g.FillRectangle(br, new Rectangle(start_x + 2, i + 2, end_x - start_x + 1, 1)); } break; // B=0,B=1,B=2,B=3.....B=100 // R=100 // R=99 // R=98 Drawstyle // R=97 Green // ... // R=0 case eDrawStyle.Green: green = m_rgb.G;; // Green is constant int start_b2 = Round(255 * (double)start_x / (Width - 4)); // Because we're drawing horizontal lines, B int end_b2 = Round(255 * (double)end_x / (Width - 4)); // will not change from line to line for (int i = start_y; i <= end_y; i++) // For each horizontal line: { red = Round(255 - (255 * (double)i / (Height - 4))); // red WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1, i + 2, end_x - start_x + 1, 1), Color.FromArgb(red, green, start_b2), Color.FromArgb(red, green, end_b2), 0, false); g.FillRectangle(br, new Rectangle(start_x + 2, i + 2, end_x - start_x + 1, 1)); } break; // R=0,R=1,R=2,R=3.....R=100 // G=100 // G=99 // G=98 Drawstyle // G=97 Blue // ... // G=0 case eDrawStyle.Blue: blue = m_rgb.B;; // Blue is constant int start_r = Round(255 * (double)start_x / (Width - 4)); // Because we're drawing horizontal lines, R int end_r = Round(255 * (double)end_x / (Width - 4)); // will not change from line to line for (int i = start_y; i <= end_y; i++) // For each horizontal line: { green = Round(255 - (255 * (double)i / (Height - 4))); // green WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1, i + 2, end_x - start_x + 1, 1), Color.FromArgb(start_r, green, blue), Color.FromArgb(end_r, green, blue), 0, false); g.FillRectangle(br, new Rectangle(start_x + 2, i + 2, end_x - start_x + 1, 1)); } break; } g.Dispose(); }
/// <summary> /// Resets the controls color (both HSL and RGB variables) based on the current marker position</summary> private void ResetHSLRGB() { int red, green, blue; switch (m_eDrawStyle) { case eDrawStyle.Hue : m_hsl.S = (double)m_iMarker_X/(Width - 4); m_hsl.L = 1.0 - (double)m_iMarker_Y/(Height - 4); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Saturation : m_hsl.H = (double)m_iMarker_X/(Width - 4); m_hsl.L = 1.0 - (double)m_iMarker_Y/(Height - 4); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Brightness : m_hsl.H = (double)m_iMarker_X/(Width - 4); m_hsl.S = 1.0 - (double)m_iMarker_Y/(Height - 4); m_rgb = AdobeColors.HSL_to_RGB(m_hsl); break; case eDrawStyle.Red : blue = Round(255 * (double)m_iMarker_X/(Width - 4)); green = Round(255 * (1.0 - (double)m_iMarker_Y/(Height - 4))); m_rgb = Color.FromArgb(m_rgb.A, m_rgb.R, green, blue); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Green : blue = Round(255 * (double)m_iMarker_X/(Width - 4)); red = Round(255 * (1.0 - (double)m_iMarker_Y/(Height - 4))); m_rgb = Color.FromArgb(m_rgb.A, red, m_rgb.G, blue); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; case eDrawStyle.Blue : red = Round(255 * (double)m_iMarker_X/(Width - 4)); green = Round(255 * (1.0 - (double)m_iMarker_Y/(Height - 4))); m_rgb = Color.FromArgb(m_rgb.A, red, green, m_rgb.B); m_hsl = AdobeColors.RGB_to_HSL(m_rgb); break; } }
/// <summary> /// Draws the content of the control, filling in all color values with the provided luminance or brightness value</summary> private void Draw_Style_Luminance() { Graphics g = CreateGraphics(); AdobeColors.HSL hsl_start = new AdobeColors.HSL(); AdobeColors.HSL hsl_end = new AdobeColors.HSL(); hsl_start.L = m_hsl.L; hsl_end.L = m_hsl.L; hsl_start.S = 1.0; hsl_end.S = 0.0; for ( int i = 0; i < Width - 4; i++ ) // For each vertical line in the control: { hsl_start.H = (double)i/(Width - 4); // Calculate Hue at this line (Saturation and Luminance are constant) hsl_end.H = hsl_start.H; LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, 1, Height - 4), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false); g.FillRectangle(br,new Rectangle(i + 2, 2, 1, Height - 4)); } g.Dispose(); }
/// <summary> /// Redraws only the content over the marker</summary> private void ClearMarker() { Graphics g = CreateGraphics(); // Determine the area that needs to be redrawn int start_x, start_y, end_x, end_y; int red = 0; int green = 0; int blue = 0; AdobeColors.HSL hsl_start = new AdobeColors.HSL(); AdobeColors.HSL hsl_end = new AdobeColors.HSL(); // Find the markers corners start_x = m_iMarker_X - 5; start_y = m_iMarker_Y - 5; end_x = m_iMarker_X + 5; end_y = m_iMarker_Y + 5; // Adjust the area if part of it hangs outside the content area if ( start_x < 0 ) start_x = 0; if ( start_y < 0 ) start_y = 0; if ( end_x > Width - 4 ) end_x = Width - 4; if ( end_y > Height - 4 ) end_y = Height - 4; // Redraw the content based on the current draw style: // The code get's a little messy from here switch (m_eDrawStyle) { // S=0,S=1,S=2,S=3.....S=100 // L=100 // L=99 // L=98 Drawstyle // L=97 Hue // ... // L=0 case eDrawStyle.Hue : hsl_start.H = m_hsl.H; hsl_end.H = m_hsl.H; // Hue is constant hsl_start.S = (double)start_x/(Width - 4); // Because we're drawing horizontal lines, s will not change hsl_end.S = (double)end_x/(Width - 4); // from line to line for ( int i = start_y; i <= end_y; i++ ) // For each horizontal line: { hsl_start.L = 1.0 - (double)i/(Height - 4); // Brightness (L) WILL change for each horizontal hsl_end.L = hsl_start.L; // line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1,i + 2, end_x - start_x + 1, 1), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 0, false); g.FillRectangle(br,new Rectangle(start_x + 2,i + 2, end_x - start_x + 1 , 1)); } break; // H=0,H=1,H=2,H=3.....H=360 // L=100 // L=99 // L=98 Drawstyle // L=97 Saturation // ... // L=0 case eDrawStyle.Saturation : hsl_start.S = m_hsl.S; hsl_end.S = m_hsl.S; // Saturation is constant hsl_start.L = 1.0 - (double)start_y/(Height - 4); // Because we're drawing vertical lines, L will hsl_end.L = 1.0 - (double)end_y/(Height - 4); // not change from line to line for ( int i = start_x; i <= end_x; i++ ) // For each vertical line: { hsl_start.H = (double)i/(Width - 4); // Hue (H) WILL change for each vertical hsl_end.H = hsl_start.H; // line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(i + 2,start_y + 1, 1, end_y - start_y + 2), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false); g.FillRectangle(br,new Rectangle(i + 2, start_y + 2, 1, end_y - start_y + 1)); } break; // H=0,H=1,H=2,H=3.....H=360 // S=100 // S=99 // S=98 Drawstyle // S=97 Brightness // ... // S=0 case eDrawStyle.Brightness : hsl_start.L = m_hsl.L; hsl_end.L = m_hsl.L; // Luminance is constant hsl_start.S = 1.0 - (double)start_y/(Height - 4); // Because we're drawing vertical lines, S will hsl_end.S = 1.0 - (double)end_y/(Height - 4); // not change from line to line for ( int i = start_x; i <= end_x; i++ ) // For each vertical line: { hsl_start.H = (double)i/(Width - 4); // Hue (H) WILL change for each vertical hsl_end.H = hsl_start.H; // line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(i + 2,start_y + 1, 1, end_y - start_y + 2), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 90, false); g.FillRectangle(br,new Rectangle(i + 2, start_y + 2, 1, end_y - start_y + 1)); } break; // B=0,B=1,B=2,B=3.....B=100 // G=100 // G=99 // G=98 Drawstyle // G=97 Red // ... // G=0 case eDrawStyle.Red : red = m_rgb.R; // Red is constant int start_b = Round(255 * (double)start_x/(Width - 4)); // Because we're drawing horizontal lines, B int end_b = Round(255 * (double)end_x/(Width - 4)); // will not change from line to line for ( int i = start_y; i <= end_y; i++ ) // For each horizontal line: { green = Round(255 - (255 * (double)i/(Height - 4))); // green WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1,i + 2, end_x - start_x + 1, 1), Color.FromArgb(red, green, start_b), Color.FromArgb(red, green, end_b), 0, false); g.FillRectangle(br,new Rectangle(start_x + 2,i + 2, end_x - start_x + 1 , 1)); } break; // B=0,B=1,B=2,B=3.....B=100 // R=100 // R=99 // R=98 Drawstyle // R=97 Green // ... // R=0 case eDrawStyle.Green : green = m_rgb.G;; // Green is constant int start_b2 = Round(255 * (double)start_x/(Width - 4)); // Because we're drawing horizontal lines, B int end_b2 = Round(255 * (double)end_x/(Width - 4)); // will not change from line to line for ( int i = start_y; i <= end_y; i++ ) // For each horizontal line: { red = Round(255 - (255 * (double)i/(Height - 4))); // red WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1,i + 2, end_x - start_x + 1, 1), Color.FromArgb(red, green, start_b2), Color.FromArgb(red, green, end_b2), 0, false); g.FillRectangle(br,new Rectangle(start_x + 2,i + 2, end_x - start_x + 1 , 1)); } break; // R=0,R=1,R=2,R=3.....R=100 // G=100 // G=99 // G=98 Drawstyle // G=97 Blue // ... // G=0 case eDrawStyle.Blue : blue = m_rgb.B;; // Blue is constant int start_r = Round(255 * (double)start_x/(Width - 4)); // Because we're drawing horizontal lines, R int end_r = Round(255 * (double)end_x/(Width - 4)); // will not change from line to line for ( int i = start_y; i <= end_y; i++ ) // For each horizontal line: { green = Round(255 - (255 * (double)i/(Height - 4))); // green WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new Rectangle(start_x + 1,i + 2, end_x - start_x + 1, 1), Color.FromArgb(start_r, green, blue), Color.FromArgb(end_r, green, blue), 0, false); g.FillRectangle(br,new Rectangle(start_x + 2,i + 2, end_x - start_x + 1 , 1)); } break; } g.Dispose(); }
///// <summary> ///// Evaluates the drawing style of the control and calls the appropriate ///// drawing function for content</summary> //private void DrawContent() //{ // switch (m_eDrawStyle) // { // case eDrawStyle.Hue : // Draw_Style_Hue(); // break; // case eDrawStyle.Saturation : // Draw_Style_Saturation(); // break; // case eDrawStyle.Brightness : // Draw_Style_Luminance(); // break; // case eDrawStyle.Red : // Draw_Style_Red(); // break; // case eDrawStyle.Green : // Draw_Style_Green(); // break; // case eDrawStyle.Blue : // Draw_Style_Blue(); // break; // } //} /// <summary> /// Draws the content of the control, filling in all color values with the provided hue value</summary> private void Draw_Style_Hue() { Graphics g = CreateGraphics(); AdobeColors.HSL hsl_start = new AdobeColors.HSL(); AdobeColors.HSL hsl_end = new AdobeColors.HSL(); hsl_start.H = m_hsl.H; hsl_end.H = m_hsl.H; hsl_start.S = 0.0; hsl_end.S = 1.0; for ( int i = 0; i < Height - 4; i++ ) // For each horizontal line in the control: { hsl_start.L = 1.0 - (double)i/(Height - 4); // Calculate luminance at this line (Hue and Saturation are constant) hsl_end.L = hsl_start.L; LinearGradientBrush br = new LinearGradientBrush(new Rectangle(2,2, Width - 4, 1), AdobeColors.HSL_to_RGB(hsl_start), AdobeColors.HSL_to_RGB(hsl_end), 0, false); g.FillRectangle(br,new Rectangle(2,i + 2, Width - 4, 1)); } g.Dispose(); }
/// <summary> /// Returns the graphed color at the x, y position on the control</summary> /// <param name="x">X-coordinate</param> /// <param name="y">Y-coordinate</param> /// <returns>Color at position</returns> private AdobeColors.HSL GetColor(int x, int y) { AdobeColors.HSL _hsl = new AdobeColors.HSL(); switch (m_eDrawStyle) { case eDrawStyle.Hue : _hsl.H = m_hsl.H; _hsl.S = (double)x/(Width - 4); _hsl.L = 1.0 - (double)y/(Height - 4); break; case eDrawStyle.Saturation : _hsl.S = m_hsl.S; _hsl.H = (double)x/(Width - 4); _hsl.L = 1.0 - (double)y/(Height - 4); break; case eDrawStyle.Brightness : _hsl.L = m_hsl.L; _hsl.H = (double)x/(Width - 4); _hsl.S = 1.0 - (double)y/(Height - 4); break; case eDrawStyle.Red : _hsl = AdobeColors.RGB_to_HSL(Color.FromArgb(m_rgb.R, Round(255 * (1.0 - (double)y/(Height - 4))), Round(255 * (double)x/(Width - 4)))); break; case eDrawStyle.Green : _hsl = AdobeColors.RGB_to_HSL(Color.FromArgb(Round(255 * (1.0 - (double)y/(Height - 4))), m_rgb.G, Round(255 * (double)x/(Width - 4)))); break; case eDrawStyle.Blue : _hsl = AdobeColors.RGB_to_HSL(Color.FromArgb(Round(255 * (double)x/(Width - 4)), Round(255 * (1.0 - (double)y/(Height - 4))), m_rgb.B)); break; } return _hsl; }
private void m_txt_A_Leave(object sender, System.EventArgs e) { string text = m_txt_A.Text; bool has_illegal_chars = false; if ( text.Length <= 0 ) has_illegal_chars = true; else foreach ( char letter in text ) { if ( !char.IsNumber(letter) ) { has_illegal_chars = true; break; } } if ( has_illegal_chars ) { MessageBox.Show("Alpha must be a number between 0 and 255".Localize()); UpdateTextBoxes(); return; } int alpha = int.Parse(text); if ( alpha < 0 ) { m_txt_Red.Text = "0"; m_rgb = Color.FromArgb(0, m_rgb.R, m_rgb.G, m_rgb.B); } else if ( alpha > 255 ) { m_txt_Red.Text = "255"; m_rgb = Color.FromArgb(255, m_rgb.R, m_rgb.G, m_rgb.B); } else { m_rgb = Color.FromArgb(alpha, m_rgb.R, m_rgb.G, m_rgb.B); } m_hsl = AdobeColors.RGB_to_HSL(m_rgb); m_cmyk = AdobeColors.RGB_to_CMYK(m_rgb); m_ctrl_BigBox.HSL = m_hsl; m_ctrl_ThinBox.HSL = m_hsl; m_lbl_Primary_Color.BackColor = MakeOpaque(m_rgb); UpdateTextBoxes(); }