/// <summary> /// Draws the content of the control filling in all color values with the provided Blue value. /// </summary> protected void Draw_Style_Blue() { Graphics g = Graphics.FromImage(m_Output); int blue = AdobeColors.ConvertHDR2LDR(m_RGB).B; for (int i = 0; i < this.Height - 4; i++) // For each horizontal line in the control: { // Calculate Green at this line (Red and Blue are constant) int green = Round(255 - (255 * (double)i / (this.Height - 4))); LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.Rectangle(2, 2, this.Width - 4, 1), Color.FromArgb(0, green, blue), Color.FromArgb(255, green, blue), 0, false); g.FillRectangle(br, new System.Drawing.Rectangle(2, i + 2, this.Width - 4, 1)); } }
protected void sliderControlHSL_Scroll(object sender, System.EventArgs e) { m_dontRefresh = DONT_REFRESH.COLOR_SLIDER; RGB = AdobeColors.HSB2RGB(sliderControlHSL.HSL); // Handle special cases where saturation is 0 (shades of gray) and it's not possible to devise a hue // Simply use the hue dictated by the color slider... if (sliderControlHSL.HSL.y < 1e-4f) { float3 TempHSL = AdobeColors.RGB2HSB((float3)m_RGB); TempHSL.x = sliderControlHSL.HSL.x; colorBoxControl.HSL = TempHSL; } }
/// <summary> /// Fills in the content of the control showing all values of Blue (0 to 1) for the given Red and Green. /// </summary> protected void Draw_Style_Blue() { Graphics g = Graphics.FromImage(m_Output); for (int i = 0; i < this.Height - 8; i++) // i represents the current line of pixels we want to draw horizontally { float blue = 1.0f - (float)i / (Height - 8); // blue is based on the current vertical position Pen pen = new Pen(AdobeColors.ConvertHDR2LDR(new float3(m_RGB.x, m_RGB.y, blue))); // Get the Color for this line g.DrawLine(pen, 11, i + 4, this.Width - 11, i + 4); // Draw the line and loop back for next line pen.Dispose(); } g.Dispose(); }
/// <summary> /// Fills in the content of the control showing all values of Luminance (0 to 100%) for the given /// Hue and Saturation. /// </summary> protected void Draw_Style_Luminance() { Graphics g = Graphics.FromImage(m_Output); float3 HSL = this.HSL; // Use the H and S values of the current color (m_HSL) for (int i = 0; i < this.Height - 8; i++) // i represents the current line of pixels we want to draw horizontally { HSL.z = 1.0f - (float)i / (Height - 8); // L (Luminance) is based on the current vertical position Pen pen = new Pen(AdobeColors.HSL_to_RGB_LDR(HSL)); // Get the Color for this line g.DrawLine(pen, 11, i + 4, this.Width - 11, i + 4); // Draw the line and loop back for next line pen.Dispose(); } g.Dispose(); }
protected void textBoxHexa_Validating(object sender, CancelEventArgs e) { string text = textBoxHexa.Text.ToUpper(); bool has_illegal_chars = false; if (textBoxHexa.Text.Length != 8) { 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 0x00000000 and 0xFFFFFFFF"); WriteHexData(m_RGB); return; } // Parse value string a_text, r_text, g_text, b_text; int a, r, g, b; r_text = textBoxHexa.Text.Substring(0, 2); g_text = textBoxHexa.Text.Substring(2, 2); b_text = textBoxHexa.Text.Substring(4, 2); a_text = textBoxHexa.Text.Substring(6, 2); a = int.Parse(a_text, System.Globalization.NumberStyles.HexNumber); r = int.Parse(r_text, System.Globalization.NumberStyles.HexNumber); g = int.Parse(g_text, System.Globalization.NumberStyles.HexNumber); b = int.Parse(b_text, System.Globalization.NumberStyles.HexNumber); ColorHDR = AdobeColors.RGB_LDR_to_RGB_HDR(r, g, b, a); }
/// <summary> /// Returns the graphed color at the x,y position on the control /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> protected float3 GetColor(int x, int y) { float InvWidth = 1.0f / (Width - 4); float InvHeight = 1.0f / (Height - 4); float3 Result = float3.One; float3 HSL = this.HSL; switch (m_DrawStyle) { case ColorPickerForm.DRAW_STYLE.Hue: Result.x = HSL.x; Result.y = x * InvWidth; Result.z = 1.0f - y * InvHeight; break; case ColorPickerForm.DRAW_STYLE.Saturation: Result.y = HSL.y; Result.x = x * InvWidth; Result.z = 1.0f - y * InvHeight; break; case ColorPickerForm.DRAW_STYLE.Brightness: Result.z = HSL.z; Result.x = x * InvWidth; Result.y = 1.0f - y * InvHeight; break; case ColorPickerForm.DRAW_STYLE.Red: Result = AdobeColors.RGB2HSB(new float3(m_RGB.x, 1.0f - y * InvHeight, x * InvWidth)); break; case ColorPickerForm.DRAW_STYLE.Green: Result = AdobeColors.RGB2HSB(new float3(1.0f - y * InvHeight, m_RGB.y, x * InvWidth)); break; case ColorPickerForm.DRAW_STYLE.Blue: Result = AdobeColors.RGB2HSB(new float3(x * InvWidth, 1.0f - y * InvHeight, m_RGB.z)); break; } return(Result); }
/// <summary> /// Resets the marker position of the slider to match the controls color. Gives the option of redrawing the slider. /// </summary> /// <param name="Redraw">Set to true if you want the function to redraw the slider after determining the best position</param> protected void Reset_Marker(bool Redraw) { float3 HSL = this.HSL; switch (m_DrawStyle) { case ColorPickerForm.DRAW_STYLE.Hue: m_iMarker_X = Round((this.Width - 4) * HSL.y); m_iMarker_Y = Round((this.Height - 4) * (1.0 - Clamp(HSL.z))); break; case ColorPickerForm.DRAW_STYLE.Saturation: m_iMarker_X = Round((this.Width - 4) * HSL.x); m_iMarker_Y = Round((this.Height - 4) * (1.0 - Clamp(HSL.z))); break; case ColorPickerForm.DRAW_STYLE.Brightness: m_iMarker_X = Round((this.Width - 4) * HSL.x); m_iMarker_Y = Round((this.Height - 4) * (1.0 - HSL.y)); break; case ColorPickerForm.DRAW_STYLE.Red: m_iMarker_X = Round((this.Width - 4) * (double)AdobeColors.ConvertHDR2LDR(m_RGB).B); m_iMarker_Y = Round((this.Height - 4) * (1.0 - (double)AdobeColors.ConvertHDR2LDR(m_RGB).G)); break; case ColorPickerForm.DRAW_STYLE.Green: m_iMarker_X = Round((this.Width - 4) * (double)AdobeColors.ConvertHDR2LDR(m_RGB).B); m_iMarker_Y = Round((this.Height - 4) * (1.0 - (double)AdobeColors.ConvertHDR2LDR(m_RGB).R)); break; case ColorPickerForm.DRAW_STYLE.Blue: m_iMarker_X = Round((this.Width - 4) * (double)AdobeColors.ConvertHDR2LDR(m_RGB).R); m_iMarker_Y = Round((this.Height - 4) * (1.0 - (double)AdobeColors.ConvertHDR2LDR(m_RGB).G)); break; } if (Redraw) { DrawMarker(m_iMarker_X, m_iMarker_Y, true); } }
// 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> protected void Draw_Style_Hue() { Graphics g = Graphics.FromImage(m_Output); float3 HSL = new float3(); HSL.y = 1.0f; // S and L will both be at 100% for this DrawStyle HSL.z = 1.0f; for (int i = 0; i < this.Height - 8; i++) // i represents the current line of pixels we want to draw horizontally { HSL.x = 1.0f - (float)i / (Height - 8); // H (hue) is based on the current vertical position Pen pen = new Pen(AdobeColors.HSL_to_RGB_LDR(HSL)); // Get the Color for this line g.DrawLine(pen, 11, i + 4, this.Width - 11, i + 4); // Draw the line and loop back for next line pen.Dispose(); } g.Dispose(); }
/// <summary> /// Resets the controls color (both HSL and RGB variables) based on the current slider position /// </summary> protected void ResetHSLRGB() { float3 HSL = this.HSL; float InvHeight = 1.0f / (Height - 9); switch (m_DrawStyle) { case ColorPickerForm.DRAW_STYLE.Hue: HSL.x = 1.0f - m_iMarker_Start_Y * InvHeight; m_RGB = AdobeColors.HSB2RGB(HSL); break; case ColorPickerForm.DRAW_STYLE.Saturation: HSL.y = 1.0f - m_iMarker_Start_Y * InvHeight; m_RGB = AdobeColors.HSB2RGB(HSL); break; case ColorPickerForm.DRAW_STYLE.Brightness: HSL.z = 1.0f - m_iMarker_Start_Y * InvHeight; m_RGB = AdobeColors.HSB2RGB(HSL); break; case ColorPickerForm.DRAW_STYLE.Red: m_RGB = new float3(1.0f - (float)(m_iMarker_Start_Y * InvHeight), m_RGB.y, m_RGB.z); break; case ColorPickerForm.DRAW_STYLE.Green: m_RGB = new float3(m_RGB.x, 1.0f - (float)(m_iMarker_Start_Y * InvHeight), m_RGB.z); break; case ColorPickerForm.DRAW_STYLE.Blue: m_RGB = new float3(m_RGB.x, m_RGB.y, 1.0f - (float)(m_iMarker_Start_Y * InvHeight)); break; } }
/// <summary> /// Resets the controls color (both HSL and RGB variables) based on the current marker position /// </summary> protected void ResetHSLRGB() { float3 HSL = GetColor(m_iMarker_X, m_iMarker_Y); m_RGB = AdobeColors.HSB2RGB(HSL); }
/// <summary> /// Returns a RGB color from a 3D vector /// </summary> /// <param name="_Vector">The vector to get the RGB color from</param> /// <returns>The color from the vector</returns> /// <remarks>You should check if the vector can be cast into a color without damage using the above "GetVectorDamage()" method</remarks> public static Color ConvertHDR2LDR(float3 _Vector) { return(AdobeColors.ConvertHDR2LDR(new float3(Math.Max(MIN_COMPONENT_VALUE, _Vector.x), Math.Max(MIN_COMPONENT_VALUE, _Vector.y), Math.Max(MIN_COMPONENT_VALUE, _Vector.z)))); }
/// <summary> /// Draws the content of the control filling in all color values with the provided Hue value. /// </summary> protected void Draw_Style_Hue() { Graphics g = Graphics.FromImage(m_Output); float3 HSL = this.HSL; float3 hsl_start = float3.Zero; float3 hsl_end = float3.Zero; hsl_start.x = HSL.x; hsl_end.x = HSL.x; hsl_start.y = 0.0f; hsl_end.y = 1.0f; for (int i = 0; i < this.Height - 4; i++) // For each horizontal line in the control: { hsl_start.z = 1.0f - (float)i / (Height - 4); // Calculate luminance at this line (Hue and Saturation are constant) hsl_end.z = hsl_start.z; LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.Rectangle(2, 2, this.Width - 4, 1), AdobeColors.HSL_to_RGB_LDR(hsl_start), AdobeColors.HSL_to_RGB_LDR(hsl_end), 0, false); g.FillRectangle(br, new System.Drawing.Rectangle(2, i + 2, this.Width - 4, 1)); } }
/// <summary> /// Redraws only the content over the marker /// </summary> protected void ClearMarker() { Graphics g = Graphics.FromImage(m_Output); // 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; float3 hsl_start = float3.Zero; float3 hsl_end = float3.Zero; // 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 > this.Width - 4) { end_x = this.Width - 4; } if (end_y > this.Height - 4) { end_y = this.Height - 4; } float3 HSL = this.HSL; // Redraw the content based on the current draw style: // The code is getting a little messy from here switch (m_DrawStyle) { // S=0,S=1,S=2,S=3.....S=100 // L=100 // L=99 // L=98 Drawstyle // L=97 Hue // ... // L=0 case ColorPickerForm.DRAW_STYLE.Hue: hsl_start.x = HSL.x; hsl_end.x = HSL.x; // Hue is constant hsl_start.y = (float)start_x / (Width - 4); // Because we're drawing horizontal lines, s will not change hsl_end.y = (float)end_x / (Width - 4); // from line to line for (int i = start_y; i <= end_y; i++) // For each horizontal line: { hsl_start.z = 1.0f - (float)i / (Height - 4); // Brightness (L) WILL change for each horizontal hsl_end.z = hsl_start.z; // line drawn LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.Rectangle(start_x + 1, i + 2, end_x - start_x + 1, 1), AdobeColors.HSL_to_RGB_LDR(hsl_start), AdobeColors.HSL_to_RGB_LDR(hsl_end), 0, false); g.FillRectangle(br, new System.Drawing.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 ColorPickerForm.DRAW_STYLE.Saturation: hsl_start.y = HSL.y; hsl_end.y = HSL.y; // Saturation is constant hsl_start.z = 1.0f - (float)start_y / (Height - 4); // Because we're drawing vertical lines, L will hsl_end.z = 1.0f - (float)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.x = (float)i / (Width - 4); // Hue (H) WILL change for each vertical hsl_end.x = hsl_start.x; // line drawn LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.Rectangle(i + 2, start_y + 1, 1, end_y - start_y + 2), AdobeColors.HSL_to_RGB_LDR(hsl_start), AdobeColors.HSL_to_RGB_LDR(hsl_end), 90, false); g.FillRectangle(br, new System.Drawing.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 ColorPickerForm.DRAW_STYLE.Brightness: hsl_start.z = HSL.z; hsl_end.z = HSL.z; // Luminance is constant hsl_start.y = 1.0f - (float)start_y / (Height - 4); // Because we're drawing vertical lines, S will hsl_end.y = 1.0f - (float)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.x = (float)i / (Width - 4); // Hue (H) WILL change for each vertical hsl_end.x = hsl_start.x; // line drawn LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.Rectangle(i + 2, start_y + 1, 1, end_y - start_y + 2), AdobeColors.HSL_to_RGB_LDR(hsl_start), AdobeColors.HSL_to_RGB_LDR(hsl_end), 90, false); g.FillRectangle(br, new System.Drawing.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 ColorPickerForm.DRAW_STYLE.Red: red = AdobeColors.ConvertHDR2LDR(m_RGB).R; // Red is constant int start_b = Round(255 * (double)start_x / (this.Width - 4)); // Because we're drawing horizontal lines, B int end_b = Round(255 * (double)end_x / (this.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 / (this.Height - 4))); // green WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.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 System.Drawing.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 ColorPickerForm.DRAW_STYLE.Green: green = AdobeColors.ConvertHDR2LDR(m_RGB).G; // Green is constant int start_b2 = Round(255 * (double)start_x / (this.Width - 4)); // Because we're drawing horizontal lines, B int end_b2 = Round(255 * (double)end_x / (this.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 / (this.Height - 4))); // red WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.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 System.Drawing.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 ColorPickerForm.DRAW_STYLE.Blue: blue = AdobeColors.ConvertHDR2LDR(m_RGB).B; // Blue is constant int start_r = Round(255 * (double)start_x / (this.Width - 4)); // Because we're drawing horizontal lines, R int end_r = Round(255 * (double)end_x / (this.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 / (this.Height - 4))); // green WILL change for each horizontal line drawn LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.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 System.Drawing.Rectangle(start_x + 2, i + 2, end_x - start_x + 1, 1)); } break; } }
// protected void m_cmd_OK_Click(object sender, System.EventArgs e) { // this.DialogResult = DialogResult.OK; // this.Close(); // } // // // protected void m_cmd_Cancel_Click(object sender, System.EventArgs e) { // this.DialogResult = DialogResult.Cancel; // this.Close(); // } #endregion protected void colorBoxControl_Scroll(object sender, System.EventArgs e) { m_dontRefresh = DONT_REFRESH.COLOR_BOX; RGB = AdobeColors.HSB2RGB(colorBoxControl.HSL); }
/// <summary> /// Returns a RGBA color from a 4D vector /// </summary> /// <param name="_Vector">The vector to get the RGBA color from</param> /// <returns>The color from the vector</returns> /// <remarks>You should check if the vector can be cast into a color without damage using the above "GetVectorDamage()" method</remarks> public static Color ConvertHDR2LDR(float4 _Vector) { return(Color.FromArgb(Math.Max(0, Math.Min(255, (int)(255.0f * _Vector.w))), AdobeColors.ConvertHDR2LDR(new float3(Math.Max(MIN_COMPONENT_VALUE, _Vector.x), Math.Max(MIN_COMPONENT_VALUE, _Vector.y), Math.Max(MIN_COMPONENT_VALUE, _Vector.z))))); }
/// <summary> /// Draws the content of the control filling in all color values with the provided Luminance or Brightness value. /// </summary> protected void Draw_Style_Brightness() { Graphics g = Graphics.FromImage(m_Output); float3 HSL = this.HSL; float3 hsl_start = float3.Zero; float3 hsl_end = float3.Zero; hsl_start.z = HSL.z; hsl_end.z = HSL.z; hsl_start.y = 1.0f; hsl_end.y = 0.0f; for (int i = 0; i < Width - 4; i++) // For each vertical line in the control: { hsl_start.x = (float)i / (Width - 4); // Calculate Hue at this line (Saturation and Luminance are constant) hsl_end.x = hsl_start.x; LinearGradientBrush br = new LinearGradientBrush(new System.Drawing.Rectangle(2, 2, 1, this.Height - 4), AdobeColors.HSL_to_RGB_LDR(hsl_start), AdobeColors.HSL_to_RGB_LDR(hsl_end), 90, false); g.FillRectangle(br, new System.Drawing.Rectangle(i + 2, 2, 1, this.Height - 4)); } }
public static Color HSL_to_RGB_LDR(float3 _HSL) { float3 RGB = AdobeColors.HSB2RGB(new float3(_HSL.x, _HSL.y, Math.Min(1.0f, _HSL.z))); return(Color.FromArgb((int)Math.Max(0.0f, Math.Min(255.0f, RGB.x * 255.0f)), (int)Math.Max(0.0f, Math.Min(255.0f, RGB.y * 255.0f)), (int)Math.Max(0.0f, Math.Min(255.0f, RGB.z * 255.0f)))); }