/// <summary>
        /// Init the ColorBox
        /// </summary>
        /// <param name="size">The size of the new ColorBox</param>
        public ColorBox(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.mWidth = size.Width;
            this.mHeight = size.Height;

            this.mDisabled = disabled;

            this.DrawControl();
        }
        /// <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>
        /// 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>
        /// Draws in Hue Style
        /// </summary>
        private void DrawStyleHue()
        {
            var hslStart = new Hsl();
            var hslEnd = new Hsl();
            if ((this.mHsl == null))
            {
                this.mHsl = new Hsl();
                this.mHsl.H = 1;
                this.mHsl.S = 1;
                this.mHsl.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>
        /// 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>
        /// Converts the HSL color format to RGB
        /// </summary>
        /// <param name="hsl"></param>
        /// <returns>Converted color</returns>
        public static Color HslToRgb(Hsl hsl)
        {
            var max = 0;
            var min = 0;
            var mid = 0;
            double q = 0;

            max = Convert.ToInt32(Math.Round(hsl.L * 255d));
            min = Convert.ToInt32(Math.Round((1.0d - hsl.S) * (hsl.L / 1.0d) * 255d));
            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>
        /// 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();

            var max = 0;
            var min = 0;
            var diff = 0;
            var sum = 0;

            // 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;
            }

            diff = max - min;
            sum = max + min;

            // Luminance (aka Brightness)
            hsl.L = Convert.ToDouble(max / 255d);

            // Saturation
            if (max == 0)
            {
                hsl.S = 0;
            }
            else
            {
                hsl.S = 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
            double q = 0;
            if (diff == 0)
            {
                q = 0;
            }
            else
            {
                q = 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>
 /// 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();
     hsl.H = hue;
     return Utilities.HslToRgb(hsl);
 }
 /// <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();
     hsl.S = saturation;
     return Utilities.HslToRgb(hsl);
 }
Exemple #10
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();
     hsl.L = brightness;
     return Utilities.HslToRgb(hsl);
 }
Exemple #11
0
        /// <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;
            }
        }
Exemple #12
0
        /// <summary>
        /// Draws in Saturation Style
        /// </summary>
        private void DrawStyleSaturation()
        {
            var hsl = new Hsl();

            hsl.H = this.mHsl.H;
            hsl.L = this.mHsl.L;
            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);

                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());
            }
            Line.End();
        }
Exemple #13
0
        /// <summary>
        /// Init the VerticalAlphaSlider
        /// </summary>
        /// <param name="size"></param>
        public VerticalAlphaSlider(Size size, bool disabled = false)
        {
            this.mHsl = new Hsl();
            this.mHsl.H = 1;
            this.mHsl.S = 0;
            this.mHsl.L = 1;

            this.mRgb = Utilities.HslToRgb(this.mHsl);
            this.mEDrawStyle = EDrawStyle.Brightness;

            this.mHeight = size.Height;
            this.mWidth = size.Width;

            this.mDisabled = disabled;

            this.DrawControl();
        }
        /// <summary>
        /// Init the VerticalColorSlider
        /// </summary>
        /// <param name="size"></param>
        public VerticalColorSlider(Size size)
        {
            this.mHsl = new Hsl();
            this.mHsl.H = 1;
            this.mHsl.S = 1;
            this.mHsl.L = 1;

            this.mRgb = Utilities.HslToRgb(this.mHsl);
            this.mEDrawStyle = EDrawStyle.Hue;

            this.mHeight = size.Height;
            this.mWidth = size.Width;

            this.DrawControl();
        }