/// <summary> /// Resets the Color /// </summary> private void ResetHslrgb() { int red; int green; int blue; switch (this.mEDrawStyle) { case EDrawStyle.Hue: this.mHsl.S = Convert.ToDouble(this.mMarkerX / (this.mWidth - 4d)); this.mHsl.L = 1.0 - Convert.ToDouble(this.mMarkerY / (this.mHeight - 4d)); this.mRgb = Utilities.HslToRgb(this.mHsl); break; case EDrawStyle.Saturation: this.mHsl.H = Convert.ToDouble(this.mMarkerX / (this.mWidth - 4d)); this.mHsl.L = 1.0 - Convert.ToDouble(this.mMarkerY / (this.mHeight - 4d)); this.mRgb = Utilities.HslToRgb(this.mHsl); break; case EDrawStyle.Brightness: this.mHsl.H = Convert.ToDouble(this.mMarkerX / (this.mWidth - 4d)); this.mHsl.S = 1.0 - Convert.ToDouble(this.mMarkerY / (this.mHeight - 4d)); this.mRgb = Utilities.HslToRgb(this.mHsl); break; case EDrawStyle.Red: blue = Convert.ToInt32(Math.Round(255 * Convert.ToDouble(this.mMarkerX / (this.mWidth - 4d)))); green = Convert.ToInt32(Math.Round(255 * (1.0 - Convert.ToDouble(this.mMarkerY / (this.mHeight - 4d))))); this.mRgb = Color.FromArgb(this.mRgb.R, green, blue); this.mHsl = Utilities.RgbToHsl(this.mRgb); break; case EDrawStyle.Green: blue = Convert.ToInt32(Math.Round(255 * Convert.ToDouble(this.mMarkerX / (this.mWidth - 4d)))); red = Convert.ToInt32(Math.Round(255 * (1.0 - Convert.ToDouble(this.mMarkerY / (this.mHeight - 4d))))); this.mRgb = Color.FromArgb(red, this.mRgb.G, blue); this.mHsl = Utilities.RgbToHsl(this.mRgb); break; case EDrawStyle.Blue: red = Convert.ToInt32(Math.Round(255 * Convert.ToDouble(this.mMarkerX / (this.mWidth - 4d)))); green = Convert.ToInt32(Math.Round(255 * (1.0 - Convert.ToDouble(this.mMarkerY / (this.mHeight - 4d))))); this.mRgb = Color.FromArgb(red, green, this.mRgb.B); this.mHsl = Utilities.RgbToHsl(this.mRgb); break; } }
/// <summary> /// Init the VerticalColorSlider /// </summary> /// <param name="size"></param> /// <param name="disabled"></param> public VerticalColorSlider(Size size, bool disabled = false) { this.mHsl = new Hsl { H = 1, S = 1, L = 1 }; this.mRgb = Utilities.HslToRgb(this.mHsl); this.mEDrawStyle = EDrawStyle.Hue; this.mHeight = size.Height; this.mWidth = size.Width; this.mDisabled = disabled; this.DrawControl(); }
/// <summary> /// Draws in Saturation Style /// </summary> private void DrawStyleSaturation() { var hslStart = new Hsl(); var hslEnd = new Hsl(); hslStart.S = this.mHsl.S; hslEnd.S = this.mHsl.S; hslStart.L = 1.0; hslEnd.L = 0.0; for (var i = 0; i <= this.mWidth - 4; i++) { hslStart.H = Convert.ToDouble(i / (this.mWidth - 4d)); // Calculate Hue at this line (Saturation and Luminance are constant) hslEnd.H = hslStart.H; var colorStart = Utilities.HslToRgb(hslStart); var colorEnd = Utilities.HslToRgb(hslEnd); this.GradientRect( i + this.mPos.X, this.mPos.Y, 2, this.mHeight - 4, colorStart.ToSharpDxColor(), colorEnd.ToSharpDxColor(), Orientation.Horizontal); } }
/// <summary> /// Gets the Color of the given Position /// </summary> /// <param name="x">Position X</param> /// <param name="y">Position Y</param> /// <returns>HSL Color</returns> private Hsl GetColor(int x, int y) { var hsl = new Hsl(); switch (this.mEDrawStyle) { case EDrawStyle.Hue: hsl.H = this.mHsl.H; hsl.S = Convert.ToDouble(x / (this.mWidth - 4d)); hsl.L = 1.0 - Convert.ToDouble(y / (this.mHeight - 4d)); break; case EDrawStyle.Saturation: hsl.S = this.mHsl.S; hsl.H = Convert.ToDouble(x / (this.mWidth - 4d)); hsl.L = 1.0 - Convert.ToDouble(y / (this.mHeight - 4d)); break; case EDrawStyle.Brightness: hsl.L = this.mHsl.L; hsl.H = Convert.ToDouble(x / (this.mWidth - 4d)); hsl.S = 1.0 - Convert.ToDouble(y / (this.mHeight - 4d)); break; case EDrawStyle.Red: hsl = Utilities.RgbToHsl( Color.FromArgb( this.mRgb.R, Convert.ToInt32(Math.Round(255 * (1.0 - Convert.ToDouble(y / (this.mHeight - 4d))))), Convert.ToInt32(Math.Round(255 * Convert.ToDouble(x / (this.mWidth - 4d)))))); break; case EDrawStyle.Green: hsl = Utilities.RgbToHsl( Color.FromArgb( Convert.ToInt32(Math.Round(255 * (1.0 - Convert.ToDouble(y / (this.mHeight - 4d))))), this.mRgb.G, Convert.ToInt32(Math.Round(255 * Convert.ToDouble(x / (this.mWidth - 4d)))))); break; case EDrawStyle.Blue: hsl = Utilities.RgbToHsl( Color.FromArgb( Convert.ToInt32(Math.Round(255 * Convert.ToDouble(x / (this.mWidth - 4d)))), Convert.ToInt32(Math.Round(255 * (1.0 - Convert.ToDouble(y / (this.mHeight - 4d))))), this.mRgb.B)); break; } return hsl; }
/// <summary> /// Converts the RGB color format to HSL /// </summary> /// <param name="c"></param> /// <returns>Converted color</returns> public static Hsl RgbToHsl(Color c) { var hsl = new Hsl(); int max; int min; // Of the RBG Values - assign the highest value to _Max and the lowest to _min if (c.R > c.G) { max = c.R; min = c.G; } else { max = c.G; min = c.R; } if (c.B > max) { max = c.B; } if (c.B < min) { min = c.B; } var diff = max - min; // Luminance (aka Brightness) hsl.L = Convert.ToDouble(max / 255d); // Saturation hsl.S = max == 0 ? 0 : Convert.ToDouble(diff / (double)max); // Hue // R is situated at the angle of 360 eller noll degrees // G vid 120 degrees // B vid 240 degrees var q = diff == 0 ? 0 : Convert.ToDouble(60d / diff); if (max == Convert.ToInt32(c.R)) { if (Convert.ToInt32(c.G) < Convert.ToInt32(c.B)) { hsl.H = Convert.ToDouble(360d + (q * (Convert.ToInt32(c.G) - Convert.ToInt32(c.B)))) / 360d; } else { hsl.H = Convert.ToDouble(q * (Convert.ToInt32(c.G) - Convert.ToInt32(c.B))) / 360d; } } else if (max == Convert.ToInt32(c.G)) { hsl.H = Convert.ToDouble(120d + (q * (Convert.ToInt32(c.B) - Convert.ToInt32(c.R)))) / 360d; } else if (max == Convert.ToInt32(c.B)) { hsl.H = Convert.ToDouble(240d + (q * (Convert.ToInt32(c.R) - Convert.ToInt32(c.G)))) / 360d; } return hsl; }
/// <summary> /// Draws in Hue Style /// </summary> private void DrawStyleHue() { var hslStart = new Hsl(); var hslEnd = new Hsl(); if ((this.mHsl == null)) { this.mHsl = new Hsl { H = 1, S = 1, L = 1 }; this.mRgb = Utilities.HslToRgb(this.mHsl); } hslStart.H = this.mHsl.H; hslEnd.H = this.mHsl.H; hslStart.S = 0.0; hslEnd.S = 1.0; for (var i = 0; i <= this.mHeight - 4; i = i + 2) { hslStart.L = 1.0 - Convert.ToDouble(i / (this.mHeight - 4d)); // Calculate luminance at this line (Hue and Saturation are constant) hslEnd.L = hslStart.L; var colorStart = Utilities.HslToRgb(hslStart); var colorEnd = Utilities.HslToRgb(hslEnd); this.GradientRect( this.mPos.X, i + this.mPos.Y, this.mWidth - 4, 2, colorStart.ToSharpDxColor(), colorEnd.ToSharpDxColor(), Orientation.Vertical); } }
/// <summary> /// Sets saturation of c by saturation /// </summary> /// <param name="c"></param> /// <param name="saturation"></param> /// <returns>New color</returns> public Color SetSaturation(Color c, double saturation) { var hsl = new Hsl { S = saturation }; return Utilities.HslToRgb(hsl); }
/// <summary> /// Converts the HSL color format to RGB /// </summary> /// <param name="hsl"></param> /// <returns>Converted color</returns> public static Color HslToRgb(Hsl hsl) { int mid; var max = Convert.ToInt32(Math.Round(hsl.L * 255d)); var min = Convert.ToInt32(Math.Round((1.0d - hsl.S) * (hsl.L / 1.0d) * 255d)); var q = Convert.ToDouble((max - min) / 255d); if (hsl.H >= 0 & hsl.H <= (1d / 6d)) { mid = Convert.ToInt32(Math.Round(((hsl.H - 0d) * q) * 1530 + min)); return Color.FromArgb(max, mid, min); } if (hsl.H <= (1d / 3d)) { mid = Convert.ToInt32(Math.Round(-((hsl.H - Convert.ToDouble(1d / 6d)) * q) * 1530 + max)); return Color.FromArgb(mid, max, min); } if (hsl.H <= 0.5) { mid = Convert.ToInt32(Math.Round(((hsl.H - Convert.ToDouble(1d / 3d)) * q) * 1530 + min)); return Color.FromArgb(min, max, mid); } if (hsl.H <= (2d / 3d)) { mid = Convert.ToInt32(Math.Round(-((hsl.H - 0.5d) * q) * 1530 + max)); return Color.FromArgb(min, mid, max); } if (hsl.H <= (5d / 6d)) { mid = Convert.ToInt32(Math.Round(((hsl.H - Convert.ToDouble(2d / 3d)) * q) * 1530 + min)); return Color.FromArgb(mid, min, max); } if (hsl.H <= 1.0) { mid = Convert.ToInt32(Math.Round(-((hsl.H - (5d / 6d)) * q) * 1530 + max)); return Color.FromArgb(max, min, mid); } return Color.FromArgb(0, 0, 0); }
/// <summary> /// Sets brightness of c by brightness /// </summary> /// <param name="c"></param> /// <param name="brightness"></param> /// <returns>New color</returns> public Color SetBrightness(Color c, double brightness) { var hsl = new Hsl { L = brightness }; return Utilities.HslToRgb(hsl); }
/// <summary> /// Sets hue of c by hue /// </summary> /// <param name="c"></param> /// <param name="hue"></param> /// <returns>New color</returns> public Color SetHue(Color c, double hue) { var hsl = new Hsl { H = hue }; return Utilities.HslToRgb(hsl); }
/// <summary> /// Modifies saturation of c by saturation /// </summary> /// <param name="c"></param> /// <param name="saturation"></param> /// <returns>New color</returns> public Color ModifySaturation(Color c, double saturation) { var hsl = new Hsl(); hsl.S *= saturation; return Utilities.HslToRgb(hsl); }
/// <summary> /// Modifies hue of c by hue /// </summary> /// <param name="c"></param> /// <param name="hue"></param> /// <returns>New color</returns> public Color ModifyHue(Color c, double hue) { var hsl = new Hsl(); hsl.H *= hue; return Utilities.HslToRgb(hsl); }
/// <summary> /// Modifies brightness of c by brightness /// </summary> /// <param name="c"></param> /// <param name="brightness"></param> /// <returns>New color</returns> public Color ModifyBrightness(Color c, double brightness) { var hsl = new Hsl(); hsl.L *= brightness; return Utilities.HslToRgb(hsl); }
/// <summary> /// Resets the Color /// </summary> private void ResetHslrgb() { switch (this.mEDrawStyle) { case EDrawStyle.Hue: this.mHsl.H = 1.0 - Convert.ToDouble(this.ArrowPos / (this.mHeight - 9d)); this.mRgb = Utilities.HslToRgb(this.mHsl); break; case EDrawStyle.Saturation: this.mHsl.S = 1.0 - Convert.ToDouble(this.ArrowPos / (this.mHeight - 9d)); this.mRgb = Utilities.HslToRgb(this.mHsl); break; case EDrawStyle.Brightness: this.mHsl.L = 1.0 - Convert.ToDouble(this.ArrowPos / (this.mHeight - 9d)); this.mRgb = Utilities.HslToRgb(this.mHsl); break; case EDrawStyle.Red: this.mRgb = Color.FromArgb( 255 - Convert.ToInt32(Math.Round(255d * Convert.ToDouble(this.ArrowPos / (this.mHeight - 9d)))), this.mRgb.G, this.mRgb.B); this.mHsl = Utilities.RgbToHsl(this.mRgb); break; case EDrawStyle.Green: this.mRgb = Color.FromArgb( this.mRgb.R, 255 - Convert.ToInt32(Math.Round(255d * Convert.ToDouble(this.ArrowPos / (this.mHeight - 9d)))), this.mRgb.B); this.mHsl = Utilities.RgbToHsl(this.mRgb); break; case EDrawStyle.Blue: this.mRgb = Color.FromArgb( this.mRgb.R, this.mRgb.G, 255 - Convert.ToInt32(Math.Round(255d * Convert.ToDouble(this.ArrowPos / (this.mHeight - 9d))))); this.mHsl = Utilities.RgbToHsl(this.mRgb); break; } }
/// <summary> /// Draws in Saturation Style /// </summary> private void DrawStyleSaturation() { var hsl = new Hsl { H = this.mHsl.H, L = this.mHsl.L }; Utils.Line.Begin(); for (var iCx = 0; iCx <= this.mHeight - 9; iCx++) { hsl.S = 1.0 - Convert.ToDouble(iCx / (this.mHeight - 9d)); var col = Utilities.HslToRgb(hsl); Utils.Line.Draw( new[] { new Vector2(this.Position.X + 11, this.Position.Y + iCx + 4), new Vector2(this.Position.X + this.mWidth - 11, this.Position.Y + iCx + 4) }, col.ToSharpDxColor()); } Utils.Line.End(); }