Example #1
0
        /// <summary>
        /// Converts a colour from HSL to RGB
        /// </summary>
        /// <remarks>Adapted from the algoritm in Foley and Van-Dam</remarks>
        /// <param name="hsl">The HSL value</param>
        /// <returns>A Color structure containing the equivalent RGB values</returns>
        public static Color HSL_to_RGB(HSL hsl)
        {
            double r = 0, g = 0, b = 0;
            double temp1, temp2;

            if (hsl.L == 0) r = g = b = 0;
            else
            {
                if (hsl.S == 0) r = g = b = hsl.L;
                else
                {
                    temp2 = ((hsl.L <= 0.5) ? hsl.L*(1.0 + hsl.S) : hsl.L + hsl.S - (hsl.L*hsl.S));
                    temp1 = 2.0*hsl.L - temp2;

                    var t3 = new[] { hsl.H + 1.0/3.0, hsl.H, hsl.H - 1.0/3.0 };
                    var clr = new double[] { 0, 0, 0 };
                    for (int i = 0; i < 3; i++)
                    {
                        if (t3[i] < 0) t3[i] += 1.0;
                        if (t3[i] > 1) t3[i] -= 1.0;

                        if (6.0*t3[i] < 1.0) clr[i] = temp1 + (temp2 - temp1)*t3[i]*6.0;
                        else if (2.0*t3[i] < 1.0) clr[i] = temp2;
                        else if (3.0*t3[i] < 2.0) clr[i] = (temp1 + (temp2 - temp1)*((2.0/3.0) - t3[i])*6.0);
                        else clr[i] = temp1;
                    }
                    r = clr[0];
                    g = clr[1];
                    b = clr[2];
                }
            }

            return Color.FromArgb((int)(255*r), (int)(255*g), (int)(255*b));
        }
Example #2
0
        public object Convert(Object value, Type targetType, object param, System.Globalization.CultureInfo culture)
        {
            var is_read_only = (bool)value;

            Color c;
            String sc;

            if ((sc = param as String) != null)
                c = sc.ToColor();
            else if (param is Color)
                c = (Color)param;
            else if (param is HSL)
                c = ((HSL)param).ToRGB();
            else
                throw new InvalidOperationException();

            if (!is_read_only)
            {
                var hsl = new HSL(c);
                hsl.S *= .90;
                hsl.L *= 1.08;
                c = hsl.ToRGB();
            }
            return new SolidColorBrush(c);
        }
Example #3
0
        //
        /// <summary> 
        /// Converts RGB to HSL 
        /// </summary> 
        /// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks> 
        /// <param name="c">A Color to convert</param> 
        /// <returns>An HSL value</returns> 
        public static HSL GetHSL(Color c)
        {
            HSL hsl = new HSL();

            hsl.H = c.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360
            hsl.L = c.GetBrightness();
            hsl.S = c.GetSaturation();

            return hsl;
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ColorSliderVertical"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public ColorSliderVertical(string name) : base(name)
        {
            // Initialize Colors
            this.hsl = new HSL {
                H = 1.0, S = 1.0, L = 1.0
            };
            this.rgba = AdobeColors.HSLToRGB(this.hsl);

            // pick a format to show
            this.drawStyle = DrawStyle.Hue;

            this.Padding = 9;

            Config.Width  = Theme.ControlHeight;
            Config.Height = Theme.ControlWidth;
        }
Example #5
0
        static Color TintColor(Color c, Color tint)
        {
            double th, ts, tl;

            HSL.RGB2HSL(tint, out th, out ts, out tl);

            double ch, cs, cl;

            HSL.RGB2HSL(c, out ch, out cs, out cl);

            Color color = HSL.HSL2RGB(th, ts, cl);

            color.A = c.A;

            return(color);
        }
        private void SetColor(Point mousePoint)
        {
            int    hue;
            double sat, lum;

            switch (_selectMode)
            {
            default:
                hue = (int)(((double)mousePoint.X / Width) * 360);
                sat = _drawHsl.Saturation;
                lum = 1 - (double)mousePoint.Y / Height;
                break;
            }

            SelectedHsl = new HSL(hue, sat, lum);
        }
Example #7
0
        //

        /// <summary>

        /// Converts RGB to HSL

        /// </summary>

        /// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks>

        /// <param name="c">A Color to convert</param>

        /// <returns>An HSL value</returns>

        public static HSL RGB_to_HSL(Color c)
        {
            HSL hsl = new HSL();



            hsl.H = c.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360

            hsl.L = c.GetBrightness();

            hsl.S = c.GetSaturation();



            return(hsl);
        }
Example #8
0
        public RGB Convert(HSL input)
        {
            double h = input.H / 60.0, s = input.S, l = input.L;

            double r = l, g = l, b = l;

            if (s > 0)
            {
                var chroma = (1.0 - (2.0 * l - 1.0).Absolute()) * s;
                var x      = chroma * (1.0 - ((h % 2.0) - 1).Absolute());

                var result = new Vector(0.0, 0, 0);

                if (0 <= h && h <= 1)
                {
                    result = new Vector(chroma, x, 0);
                }
                else if (1 <= h && h <= 2)
                {
                    result = new Vector(x, chroma, 0);
                }
                else if (2 <= h && h <= 3)
                {
                    result = new Vector(0.0, chroma, x);
                }
                else if (3 <= h && h <= 4)
                {
                    result = new Vector(0.0, x, chroma);
                }
                else if (4 <= h && h <= 5)
                {
                    result = new Vector(x, 0, chroma);
                }
                else if (5 <= h && h <= 6)
                {
                    result = new Vector(chroma, 0, x);
                }

                var m = l - (0.5 * chroma);

                r = result[0] + m;
                g = result[1] + m;
                b = result[2] + m;
            }

            return(new RGB(r, g, b));
        }
Example #9
0
        public void GenerateHSLCharts()
        {
            var pixelFormat = PixelFormat.Format24bppRgb;

            Directory.CreateDirectory("hsl");

            for (var l = 0; l < 256; l++)
            {
                using (var bitmap = new Bitmap(360, 256, pixelFormat))
                {
                    var bits = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, pixelFormat);
                    try
                    {
                        unsafe
                        {
                            void *pData = bits.Scan0.ToPointer();
                            byte *pRow  = (byte *)pData;

                            for (var s = 0; s < 256; s++)
                            {
                                BitmapRGB *pCol = (BitmapRGB *)pRow;

                                for (var h = 0; h < 360; h++)
                                {
                                    var hsl = new HSL((ushort)h, (byte)s, (byte)l);
                                    var rgb = hsl.ToRGB();

                                    pCol->R = rgb.R;
                                    pCol->G = rgb.G;
                                    pCol->B = rgb.B;

                                    pCol++;
                                }

                                pRow += bits.Stride;
                            }
                        }
                    }
                    finally
                    {
                        bitmap.UnlockBits(bits);
                    }

                    bitmap.Save($@"hsl\{l}.bmp", ImageFormat.Bmp);
                }
            }
        }
Example #10
0
        static Color TintColor(Color c, Color tint)
        {
            return(SimpleTint(c, tint));

#if asd
            double th, ts, tl;
            HSL.RGB2HSL(tint, out th, out ts, out tl);

            double ch, cs, cl;
            HSL.RGB2HSL(c, out ch, out cs, out cl);

            Color color = HSL.HSL2RGB(th, ts, cl);
            color.A = c.A;

            return(color);
#endif
        }
Example #11
0
            public static Color ToRGB(HSL hsl)
            {
                if (hsl.S == 0)
                {
                    var rgb = (int)(255 * hsl.L);
                    return(Color.FromArgb(rgb, rgb, rgb));
                }

                var q2 = hsl.L <= .5 ? hsl.L * (1 + hsl.S) : hsl.L + hsl.S - hsl.L * hsl.S;
                var q1 = 2 * hsl.L - q2;

                int r = (int)(255 * HueToRGB(q1, q2, hsl.H + 120));
                int g = (int)(255 * HueToRGB(q1, q2, hsl.H));
                int b = (int)(255 * HueToRGB(q1, q2, hsl.H - 120));

                return(Color.FromArgb(r, g, b));
            }
Example #12
0
        /// <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 (_drawStyle)
            {
            case eDrawStyle.Hue:
                _hsl.S = (double)_markerX / (this.Width - 4);
                _hsl.L = 1.0 - (double)_markerY / (this.Height - 4);
                _rgb   = ColorManager.HSL_to_RGB(_hsl);
                break;

            case eDrawStyle.Saturation:
                _hsl.H = (double)_markerX / (this.Width - 4);
                _hsl.L = 1.0 - (double)_markerY / (this.Height - 4);
                _rgb   = ColorManager.HSL_to_RGB(_hsl);
                break;

            case eDrawStyle.Brightness:
                _hsl.H = (double)_markerX / (this.Width - 4);
                _hsl.S = 1.0 - (double)_markerY / (this.Height - 4);
                _rgb   = ColorManager.HSL_to_RGB(_hsl);
                break;

            case eDrawStyle.Red:
                blue  = Round(255 * (double)_markerX / (this.Width - 4));
                green = Round(255 * (1.0 - (double)_markerY / (this.Height - 4)));
                _rgb  = Color.FromArgb(_rgb.R, green, blue);
                _hsl  = ColorManager.RGB_to_HSL(_rgb);
                break;

            case eDrawStyle.Green:
                blue = Round(255 * (double)_markerX / (this.Width - 4));
                red  = Round(255 * (1.0 - (double)_markerY / (this.Height - 4)));
                _rgb = Color.FromArgb(red, _rgb.G, blue);
                _hsl = ColorManager.RGB_to_HSL(_rgb);
                break;

            case eDrawStyle.Blue:
                red   = Round(255 * (double)_markerX / (this.Width - 4));
                green = Round(255 * (1.0 - (double)_markerY / (this.Height - 4)));
                _rgb  = Color.FromArgb(red, green, _rgb.B);
                _hsl  = ColorManager.RGB_to_HSL(_rgb);
                break;
            }
        }
Example #13
0
        private static ColorResult ConvertCMYK(ColorSpace resultSpace, ColorSource source)
        {
            ColorResult color;

            double c = source.component0;
            double m = source.component1;
            double y = source.component2;
            double k = source.component3;

            switch (resultSpace)
            {
            case ColorSpace.RGBSpace:
                RGB rgb = ColorConverter.CMYKtoRGB(c, m, y, k);
                color = new ColorResult(rgb.Red, rgb.Green, rgb.Blue);
                break;

            case ColorSpace.HSBSpace:
                HSB hsb = ColorConverter.CMYKtoHSB(c, m, y, k);
                color = new ColorResult(hsb.Hue, hsb.Saturation, hsb.Brightness);
                break;

            case ColorSpace.HSLSpace:
                HSL hsl = ColorConverter.CMYKtoHSL(c, m, y, k);
                color = new ColorResult(hsl.Hue, hsl.Saturation, hsl.Luminance);
                break;

            case ColorSpace.LabSpace:
                Lab lab = ColorConverter.CMYKtoLab(c, m, y, k);
                color = ScaleCIELabOutputRange(lab);
                break;

            case ColorSpace.XYZSpace:
                XYZ xyz = ColorConverter.CMYKtoXYZ(c, m, y, k);
                color = new ColorResult(xyz.X, xyz.Y, xyz.Z);
                break;

            case ColorSpace.GraySpace:
                color = new ColorResult(ColorConverter.CMYKtoRGB(c, m, y, k).GetIntensity(source.maxChannelValue));
                break;

            default:
                throw new InvalidEnumArgumentException("Unsupported color space conversion", (int)resultSpace, typeof(ColorSpace));
            }

            return(color);
        }
Example #14
0
    /// <summary>
    /// Returns a list of colors that are monochromatic in relation to the source
    /// </summary>
    public static List <Color> Monochromatic(Color sourceColor, int howMany = 4)
    {
        List <Color> palette = new List <Color>();

        HSL source = HSL.fromColor(sourceColor);                //Convert to HSL

        for (int i = 0; i < howMany; i++)
        {
            HSL hsl = HSL.Copy(source);         //Create a copy of the source HSL
            hsl.Lightness *= (howMany / 2 - i); //Change the lightness of it
            Color color = hsl.toColor();        //Convert it back
            color.a = sourceColor.a;            //Add back in the Alpha value
            palette.Add(color);                 //Add it to the palette
        }

        return(palette);
    }
        /// <summary>
        /// Create a dot to test the position of colors on the shade triangle
        /// </summary>
        private void TestDot(Graphics g, double sat, double lum)
        {
            var W         = _testDotSize;
            var HSL       = new HSL(_hsl.H, sat, lum);
            var pt        = LocateColorMarker(HSL);
            var Rectangle = new Rectangle(pt.X - W / 2, pt.Y - W / 2, W, W);

            using (var SolidBrush = new SolidBrush(ColorManager.HSL_to_RGB(HSL)))
            {
                g.FillEllipse(SolidBrush, Rectangle);
                if (_dotOutline)
                {
                    g.DrawEllipse(Pens.Black, Rectangle);
                }
            }
            HSL = null;
        }
        public static HSV ToHsv(this HSL hsl)
        {
            if (hsl == default(HSL))
            {
                return(default(HSV));
            }

            double ll = hsl.L;
            double ss = (ll <= 0.5) ? ll : 1.0 - ll;
            double V  = ll + ss;
            double S  = (2 * ss) / (ll + ss);

            return(new HSV()
            {
                H = hsl.H, S = S, V = V
            });
        }
        public static Color ToComplement(this Color c)
        {
            HSL hsl = c.ToHsl();

            hsl.H += 0.5f;
            if (hsl.H > 1)
            {
                hsl.H -= 1;
            }
            Color ret = hsl.ToColor();

            if (ret == c)
            {
                hsl.L = 1 - hsl.L;
            }
            return(hsl.ToColor());
        }
        /// <summary>
        /// Determines the next color to use in the bar based on the drawing style and the last color used.
        /// </summary>
        /// <param name="oldColor">Last color used.</param>
        /// <param name="sgn">Multiplication value for the delta. Used to subtract the value, instead of adding it.</param>
        private HSL GetDeltaColor(HSL oldColor, int sgn, bool allowWrap)
        {
            var HSL        = new HSL();
            var Hue        = oldColor.Hue;
            var Saturation = oldColor.Saturation;
            var Luminance  = oldColor.Luminance;
            var Alpha      = oldColor.Alpha;

            if (_reverseFill)
            {
                sgn *= -1;
            }

            switch (_drawStyle)
            {
            case eDrawStyle.Hue:
            case eDrawStyle.HueOffset:
                Hue += (_deltaValue * sgn);
                Hue  = AdjustValue(Hue, allowWrap);
                return(new HSL(Hue, Saturation, Luminance, Alpha));

            case eDrawStyle.Luminance:
                Luminance += (_deltaValue * sgn);
                Luminance  = AdjustValue(Luminance, allowWrap);
                return(new HSL(Hue, Saturation, Luminance, Alpha));

            case eDrawStyle.Saturation:
            case eDrawStyle.SaturationWithInversion:
                Saturation += (_deltaValue * sgn);
                Saturation  = AdjustValue(Saturation, allowWrap);

                Luminance += ((_deltaValue * sgn) / 2);
                Luminance  = AdjustValue(Luminance, allowWrap);
                return(new HSL(Hue, Saturation, Luminance, Alpha));


            case eDrawStyle.Transparency:
                Alpha += (_deltaValue * sgn);
                Alpha  = AdjustValue(Alpha, allowWrap);
                return(new HSL(Hue, Saturation, Luminance, Alpha));

            default:
                return(ColorManager.RGB_to_HSL(SystemColors.Control));
            }
        }
Example #19
0
        protected Bitmap ResultLuminanceToOutputBitmapAsColor()
        {
            // normalize luminance
            float max = float.MinValue;
            float min = float.MaxValue;

            // data for normalization from 0 to 255
            for (int x = 0; x < _inputBitmap.Width; x++)
            {
                for (int y = 0; y < _inputBitmap.Height; y++)
                {
                    if (_outputLuminance[y, x] > max)
                    {
                        max = _outputLuminance[y, x];
                    }
                    if (_outputLuminance[y, x] < min)
                    {
                        min = _outputLuminance[y, x];
                    }
                }
            }

            Bitmap resultBitmap = new Bitmap(_inputBitmap);

            for (int x = 0; x < _inputBitmap.Width; x++)
            {
                for (int y = 0; y < _inputBitmap.Height; y++)
                {
                    // make it color
                    var luminanceOutputScaledFrom0to1 = (_outputLuminance[y, x] - min) / (max - min);
                    HSL hslOutput = new HSL()
                    {
                        Luminance  = luminanceOutputScaledFrom0to1,
                        Saturation = _inputSaturation[y, x],
                        Hue        = _inputHue[y, x]
                    };
                    RGB rgbOutput = new RGB();
                    HSL.ToRGB(hslOutput, rgbOutput);
                    Color color = Color.FromArgb(rgbOutput.Red, rgbOutput.Green, rgbOutput.Blue);
                    resultBitmap.SetPixel(x, y, color);
                }
            }

            return(resultBitmap);
        }
Example #20
0
        private static ColorResult ConvertHSB(ColorSpace resultSpace, ColorSource source)
        {
            ColorResult color;

            double h = source.component0;
            double s = source.component1;
            double b = source.component2;

            switch (resultSpace)
            {
            case ColorSpace.RGBSpace:
                RGB rgb = ColorConverter.HSBtoRGB(h, s, b);
                color = new ColorResult(rgb.Red, rgb.Green, rgb.Blue);
                break;

            case ColorSpace.CMYKSpace:
                CMYK cmyk = ColorConverter.HSBtoCMYK(h, s, b);
                color = new ColorResult(cmyk.Cyan, cmyk.Magenta, cmyk.Yellow, cmyk.Black);
                break;

            case ColorSpace.HSLSpace:
                HSL hsl = ColorConverter.HSBtoHSL(h, s, b);
                color = new ColorResult(hsl.Hue, hsl.Saturation, hsl.Luminance);
                break;

            case ColorSpace.LabSpace:
                Lab lab = ColorConverter.HSBToLab(h, s, b);
                color = ScaleCIELabOutputRange(lab);
                break;

            case ColorSpace.XYZSpace:
                XYZ xyz = ColorConverter.HSBtoXYZ(h, s, b);
                color = new ColorResult(xyz.X, xyz.Y, xyz.Z);
                break;

            case ColorSpace.GraySpace:
                color = new ColorResult(ColorConverter.HSBtoRGB(h, s, b).GetIntensity(source.maxChannelValue));
                break;

            default:
                throw new InvalidEnumArgumentException(nameof(resultSpace), (int)resultSpace, typeof(ColorSpace));
            }

            return(color);
        }
Example #21
0
        /// <summary>
        /// Converts a color from HSL to RGB</summary>
        /// <remarks>Adapted from the algorithm in Foley and Van-Dam</remarks>
        /// <param name="hsl">The HSL value</param>
        /// <returns>A Color structure containing the equivalent RGB values</returns>
        public static Color HSL_to_RGB(HSL hsl)
        {
            int    Max, Mid, Min;
            double q;
            int    alpha = Round(255 * hsl.A);

            Max = Round(hsl.L * 255);
            Min = Round((1.0 - hsl.S) * (hsl.L / 1.0) * 255);
            q   = (double)(Max - Min) / 255;

            if (hsl.H >= 0 && hsl.H <= (double)1 / 6)
            {
                Mid = Round(((hsl.H - 0) * q) * 1530 + Min);
                return(Color.FromArgb(alpha, Max, Mid, Min));
            }
            else if (hsl.H <= (double)1 / 3)
            {
                Mid = Round(-((hsl.H - (double)1 / 6) * q) * 1530 + Max);
                return(Color.FromArgb(alpha, Mid, Max, Min));
            }
            else if (hsl.H <= 0.5)
            {
                Mid = Round(((hsl.H - (double)1 / 3) * q) * 1530 + Min);
                return(Color.FromArgb(alpha, Min, Max, Mid));
            }
            else if (hsl.H <= (double)2 / 3)
            {
                Mid = Round(-((hsl.H - 0.5) * q) * 1530 + Max);
                return(Color.FromArgb(alpha, Min, Mid, Max));
            }
            else if (hsl.H <= (double)5 / 6)
            {
                Mid = Round(((hsl.H - (double)2 / 3) * q) * 1530 + Min);
                return(Color.FromArgb(alpha, Mid, Min, Max));
            }
            else if (hsl.H <= 1.0)
            {
                Mid = Round(-((hsl.H - (double)5 / 6) * q) * 1530 + Max);
                return(Color.FromArgb(alpha, Max, Min, Mid));
            }
            else
            {
                return(Color.FromArgb(alpha, 0, 0, 0));
            }
        }
Example #22
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int pixelSize = Bitmap.GetPixelFormatSize(image.PixelFormat) / 8;

            int startX = rect.Left;
            int startY = rect.Top;
            int stopX  = startX + rect.Width;
            int stopY  = startY + rect.Height;
            int offset = image.Stride - rect.Width * pixelSize;

            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            // do the job
            byte *ptr = (byte *)image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += (startY * image.Stride + startX * pixelSize);

            // for each row
            for (int y = startY; y < stopY; y++)
            {
                // for each pixel
                for (int x = startX; x < stopX; x++, ptr += pixelSize)
                {
                    rgb.Red   = ptr[RGB.R];
                    rgb.Green = ptr[RGB.G];
                    rgb.Blue  = ptr[RGB.B];

                    // convert to HSL
                    AForgeCore.Imaging.HSL.FromRGB(rgb, hsl);

                    // modify hue value
                    hsl.Hue = hue;

                    // convert back to RGB
                    AForgeCore.Imaging.HSL.ToRGB(hsl, rgb);

                    ptr[RGB.R] = rgb.Red;
                    ptr[RGB.G] = rgb.Green;
                    ptr[RGB.B] = rgb.Blue;
                }
                ptr += offset;
            }
        }
Example #23
0
        private static ColorResult ConvertLAB(ColorSpace resultSpace, ColorSource source)
        {
            ColorResult color;

            double l = source.component0;
            double a = source.component1;
            double b = source.component2;

            switch (resultSpace)
            {
            case ColorSpace.RGBSpace:
                RGB rgb = ColorConverter.LabtoRGB(l, a, b);
                color = new ColorResult(rgb.Red, rgb.Green, rgb.Blue);
                break;

            case ColorSpace.CMYKSpace:
                CMYK cmyk = ColorConverter.LabtoCMYK(l, a, b);
                color = new ColorResult(cmyk.Cyan, cmyk.Magenta, cmyk.Yellow, cmyk.Black);
                break;

            case ColorSpace.HSBSpace:
                HSB hsb = ColorConverter.LabtoHSB(l, a, b);
                color = new ColorResult(hsb.Hue, hsb.Saturation, hsb.Brightness);
                break;

            case ColorSpace.HSLSpace:
                HSL hsl = ColorConverter.LabtoHSL(l, a, b);
                color = new ColorResult(hsl.Hue, hsl.Saturation, hsl.Luminance);
                break;

            case ColorSpace.XYZSpace:
                XYZ xyz = ColorConverter.LabtoXYZ(l, a, b);
                color = new ColorResult(xyz.X, xyz.Y, xyz.Z);
                break;

            case ColorSpace.GraySpace:
                color = new ColorResult(ColorConverter.LabtoRGB(l, a, b).GetIntensity(source.maxChannelValue));
                break;

            default:
                throw new InvalidEnumArgumentException("Unsupported color space conversion", (int)resultSpace, typeof(ColorSpace));
            }

            return(color);
        }
Example #24
0
        private static ColorResult ConvertRGB(ColorSpace resultSpace, ColorSource source)
        {
            ColorResult color;

            double red   = source.component0;
            double green = source.component1;
            double blue  = source.component2;

            switch (resultSpace)
            {
            case ColorSpace.CMYKSpace:
                CMYK cmyk = ColorConverter.RGBtoCMYK(red, green, blue);
                color = new ColorResult(cmyk.Cyan, cmyk.Magenta, cmyk.Yellow, cmyk.Black);
                break;

            case ColorSpace.GraySpace:
                color = new ColorResult(ColorConverter.GetRGBIntensity(red, green, blue, source.maxChannelValue));
                break;

            case ColorSpace.HSBSpace:
                HSB hsb = ColorConverter.RGBtoHSB(red, green, blue);
                color = new ColorResult(hsb.Hue, hsb.Saturation, hsb.Brightness);
                break;

            case ColorSpace.HSLSpace:
                HSL hsl = ColorConverter.RGBtoHSL(red, green, blue);
                color = new ColorResult(hsl.Hue, hsl.Saturation, hsl.Luminance);
                break;

            case ColorSpace.LabSpace:
                Lab lab = ColorConverter.RGBtoLab(red, green, blue);
                color = ScaleCIELabOutputRange(lab);
                break;

            case ColorSpace.XYZSpace:
                XYZ xyz = ColorConverter.RGBtoXYZ(red, green, blue);
                color = new ColorResult(xyz.X, xyz.Y, xyz.Z);
                break;

            default:
                throw new InvalidEnumArgumentException("Unsupported color space conversion", (int)resultSpace, typeof(ColorSpace));
            }

            return(color);
        }
Example #25
0
        private static ColorResult ConvertXYZ(ColorSpace resultSpace, ColorSource source)
        {
            ColorResult color;

            double x = source.component0;
            double y = source.component1;
            double z = source.component2;

            switch (resultSpace)
            {
            case ColorSpace.RGBSpace:
                RGB rgb = ColorConverter.XYZtoRGB(x, y, z);
                color = new ColorResult(rgb.Red, rgb.Green, rgb.Blue);
                break;

            case ColorSpace.CMYKSpace:
                CMYK cmyk = ColorConverter.XYZtoCMYK(x, y, z);
                color = new ColorResult(cmyk.Cyan, cmyk.Magenta, cmyk.Yellow, cmyk.Black);
                break;

            case ColorSpace.HSBSpace:
                HSB hsb = ColorConverter.XYZtoHSB(x, y, z);
                color = new ColorResult(hsb.Hue, hsb.Saturation, hsb.Brightness);
                break;

            case ColorSpace.HSLSpace:
                HSL hsl = ColorConverter.XYZtoHSL(x, y, z);
                color = new ColorResult(hsl.Hue, hsl.Saturation, hsl.Luminance);
                break;

            case ColorSpace.LabSpace:
                Lab lab = ColorConverter.XYZtoLab(x, y, z);
                color = ScaleCIELabOutputRange(lab);
                break;

            case ColorSpace.GraySpace:
                color = new ColorResult(ColorConverter.XYZtoRGB(x, y, z).GetIntensity(source.maxChannelValue));
                break;

            default:
                throw new InvalidEnumArgumentException("Unsupported color space conversion", (int)resultSpace, typeof(ColorSpace));
            }

            return(color);
        }
Example #26
0
        internal static double GetHueDist(Matrix <double> values, Matrix <double> targets, int subdivisions)
        {
            var hueCounter = 0;
            var valDist    = new double[10];
            var tarDist    = new double[10];

            var sizeRatio = targets.RowCount / values.RowCount;

            for (int i = 0; i < values.RowCount; i++)
            {
                var rgb = new RGB((byte)(values[i, 0] * 255), (byte)(values[i, 1] * 255), (byte)(values[i, 2] * 255));
                var hue = HSL.FromRGB(rgb).Hue;

                int bucket = (int)hue / 36;
                if (bucket > 9)
                {
                    bucket = 9;
                }

                valDist[bucket]++;
                // hueCounter += hue;
            }

            for (int i = 0; i < targets.RowCount; i++)
            {
                var rgb = new RGB((byte)(targets[i, 0] * 255), (byte)(targets[i, 1] * 255), (byte)(targets[i, 2] * 255));
                var hue = HSL.FromRGB(rgb).Hue;

                int bucket = (int)hue / 36;
                if (bucket > 9)
                {
                    bucket = 9;
                }

                tarDist[bucket]++;
                //hueCounter += hue;
            }

            valDist = valDist.Select(x => x / (subdivisions * subdivisions)).ToArray();
            tarDist = tarDist.Select(x => x / (sizeRatio * (subdivisions * subdivisions))).ToArray();

            var distDist = FeatureDistance(valDist, tarDist);

            return(distDist);
        }
        public static HSV RGB2HSV(RGB rgb)
        {
            float R = rgb.Red / 255;
            float G = rgb.Green / 255;
            float B = rgb.Blue / 255;

            float max   = Math.Max(Math.Max(R, B), G);
            float min   = Math.Min(Math.Min(R, B), G);
            float delta = max - min;
            float Sat   = (delta == 0) ? 0 : (Sat = delta / max);

            HSL   hsl = RGB2HSL(rgb);
            float H   = hsl.Hue;
            float S   = Sat;
            float V   = max;

            return(new HSV(H, S, V));
        }
Example #28
0
        // slider controls hue
        // x = saturation 0 -> 100
        // y = Lightness 100 -> 0
        protected override void DrawHSLHue()
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                HSL start = new HSL((int)selectedColor.HSL.Hue360, 0, 0, selectedColor.ARGB.A);
                HSL end   = new HSL((int)selectedColor.HSL.Hue360, 100, 0, selectedColor.ARGB.A);

                for (int y = 0; y < clientHeight; y++)
                {
                    start.Lightness = end.Lightness = (float)(1.0 - ((double)y / (clientHeight)));

                    using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, clientWidth, 1), start, end, LinearGradientMode.Horizontal))
                    {
                        g.FillRectangle(brush, new Rectangle(0, y, clientWidth, 1));
                    }
                }
            }
        }
Example #29
0
        // slider controls lightness
        // x = hue 0 -> 360
        // y = saturation 100 -> 0
        protected override void DrawHSLLightness()
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                HSL start = new HSL(0, 100, (int)selectedColor.HSL.Lightness100, selectedColor.ARGB.A);
                HSL end   = new HSL(0, 0, (int)selectedColor.HSL.Lightness100, selectedColor.ARGB.A);

                for (int x = 0; x < clientWidth; x++)
                {
                    start.Hue = end.Hue = (float)((double)x / (clientHeight));

                    using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 1, clientHeight), start, end, LinearGradientMode.Vertical))
                    {
                        g.FillRectangle(brush, new Rectangle(x, 0, 1, clientHeight));
                    }
                }
            }
        }
Example #30
0
        public mFilterHSL(wDomain HueValue, wDomain SaturationValue, wDomain LuminanceValue, bool isOut, Color fillColor)
        {
            Hue       = HueValue;
            Sat       = SaturationValue;
            Lum       = LuminanceValue;
            IsOut     = isOut;
            FillColor = fillColor;


            BitmapType = mFilter.BitmapTypes.None;

            Effect = new HSLFiltering(new Accord.IntRange((int)Hue.T0, (int)Hue.T1), new Accord.Range((float)Sat.T0, (float)Sat.T1), new Accord.Range((float)Lum.T0, (float)Lum.T1));

            Effect.FillOutsideRange = IsOut;
            Effect.FillColor        = HSL.FromRGB(new RGB(FillColor));

            filter = Effect;
        }
Example #31
0
        // Adapted from the algorithm in Foley and Van-Dam
        // (Computer Graphics: Principles and Practice)
        public static Color HSL2RGB(HSL hsl)
        {
            int max, mid, min;
            double q;

            max = Round(hsl.L * 255);
            min = Round((1.0 - hsl.S) * (hsl.L / 1.0) * 255);
            q = (max - min) / 255.0;

            if (hsl.H >= 0 && hsl.H <= 1 / 6.0)
            {
                mid = Round(((hsl.H - 0) * q) * 1530 + min);
                return Color.FromArgb(max, mid, min);
            }
            else if (hsl.H <= 1 / 3.0)
            {
                mid = Round(-((hsl.H - 1 / 6.0) * q) * 1530 + max);
                return Color.FromArgb(mid, max, min);
            }
            else if (hsl.H <= 1 / 2.0)
            {
                mid = Round(((hsl.H - 1 / 3.0) * q) * 1530 + min);
                return Color.FromArgb(min, max, mid);
            }
            else if (hsl.H <= 2 / 3.0)
            {
                mid = Round(-((hsl.H - 1 / 2.0) * q) * 1530 + max);
                return Color.FromArgb(min, mid, max);
            }
            else if (hsl.H <= 5 / 6.0)
            {
                mid = Round(((hsl.H - 2 / 3.0) * q) * 1530 + min);
                return Color.FromArgb(mid, min, max);
            }
            else if (hsl.H <= 1.0)
            {
                mid = Round(-((hsl.H - 5 / 6.0) * q) * 1530 + max);
                return Color.FromArgb(max, min, mid);
            }
            else
            {
                return Color.Black;
            }
        }
Example #32
0
        private static ColorResult ConvertGray(ColorSpace resultSpace, ColorSource source)
        {
            ColorResult color;

            double gray = source.component0;

            switch (resultSpace)
            {
            case ColorSpace.RGBSpace:
                color = new ColorResult(gray, gray, gray);
                break;

            case ColorSpace.CMYKSpace:
                CMYK cmyk = ColorConverter.RGBtoCMYK(gray, gray, gray);
                color = new ColorResult(cmyk.Cyan, cmyk.Magenta, cmyk.Yellow, cmyk.Black);
                break;

            case ColorSpace.HSBSpace:
                HSB hsb = ColorConverter.RGBtoHSB(gray, gray, gray);
                color = new ColorResult(hsb.Hue, hsb.Saturation, hsb.Brightness);
                break;

            case ColorSpace.HSLSpace:
                HSL hsl = ColorConverter.RGBtoHSL(gray, gray, gray);
                color = new ColorResult(hsl.Hue, hsl.Saturation, hsl.Luminance);
                break;

            case ColorSpace.LabSpace:
                Lab lab = ColorConverter.RGBtoLab(gray, gray, gray);
                color = ScaleCIELabOutputRange(lab);
                break;

            case ColorSpace.XYZSpace:
                XYZ xyz = ColorConverter.RGBtoXYZ(gray, gray, gray);
                color = new ColorResult(xyz.X, xyz.Y, xyz.Z);
                break;

            default:
                throw new InvalidEnumArgumentException(nameof(resultSpace), (int)resultSpace, typeof(ColorSpace));
            }

            return(color);
        }
Example #33
0
            public RGB(HSL hsl)
            {
                double C = (1 - Math.Abs(2 * hsl.L - 1)) * hsl.S;
                double X = C * (1 - Math.Abs((hsl.H / 60) % 2 - 1));
                double m = hsl.L - (C / 2);

                double r, g, b;

                switch ((int)hsl.H / 60)
                {
                case 0:
                    r = C; g = X; b = 0;
                    break;

                case 1:
                    r = X; g = C; b = 0;
                    break;

                case 2:
                    r = 0; g = C; b = X;
                    break;

                case 3:
                    r = 0; g = X; b = C;
                    break;

                case 4:
                    r = X; g = 0; b = C;
                    break;

                case 5:
                    r = C; g = 0; b = X;
                    break;

                default:
                    r = 0; g = 0; b = 0;
                    break;
                }

                R = (byte)((r + m) * 31);
                G = (byte)((g + m) * 31);
                B = (byte)((b + m) * 31);
            }
Example #34
0
        public unsafe Bitmap Apply(Bitmap srcImg)
        {
            if (srcImg.PixelFormat != PixelFormat.Format24bppRgb)
            {
                throw new ArgumentException();
            }
            int        width      = srcImg.Width;
            int        height     = srcImg.Height;
            BitmapData bitmapdata = srcImg.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            Bitmap     bitmap     = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            BitmapData data2      = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            RGB        rgb        = new RGB();
            HSL        hsl        = new HSL();
            int        num3       = bitmapdata.Stride - (width * 3);
            byte *     numPtr     = (byte *)bitmapdata.Scan0.ToPointer();
            byte *     numPtr2    = (byte *)data2.Scan0.ToPointer();

            for (int i = 0; i < height; i++)
            {
                int num5 = 0;
                while (num5 < width)
                {
                    rgb.Red   = numPtr[2];
                    rgb.Green = numPtr[1];
                    rgb.Blue  = numPtr[0];
                    GodLesZ.Library.Imaging.ColorConverter.RGB2HSL(rgb, hsl);
                    hsl.Hue = this.hue;
                    GodLesZ.Library.Imaging.ColorConverter.HSL2RGB(hsl, rgb);
                    numPtr2[2] = rgb.Red;
                    numPtr2[1] = rgb.Green;
                    numPtr2[0] = rgb.Blue;
                    num5++;
                    numPtr  += 3;
                    numPtr2 += 3;
                }
                numPtr  += num3;
                numPtr2 += num3;
            }
            bitmap.UnlockBits(data2);
            srcImg.UnlockBits(bitmapdata);
            return(bitmap);
        }
Example #35
0
        /// <summary> 
        /// Converts a colour from HSL to RGB 
        /// </summary> 
        /// <remarks>Adapted from the algoritm in Foley and Van-Dam</remarks> 
        /// <param scriptName="hsl">The HSL value</param> 
        /// <returns>A Color structure containing the equivalent RGB values</returns> 
        public static Color HSL_to_RGB(HSL hsl)
        {
            int Max, Mid, Min;
            double q;

            Max = (int)Math.Round(hsl.L * 255);
            Min = (int)Math.Round((1.0 - hsl.S) * (hsl.L / 1.0) * 255);
            q   = (double)(Max - Min)/255;

            if ( hsl.H >= 0 && hsl.H <= (double)1/6 )
            {
                Mid = (int)Math.Round(((hsl.H - 0) * q) * 1530 + Min);
                return Color.FromArgb(Max,Mid,Min);
            }
            else if ( hsl.H <= (double)1/3 )
            {
                Mid = (int)Math.Round(-((hsl.H - (double)1 / 6) * q) * 1530 + Max);
                return Color.FromArgb(Mid,Max,Min);
            }
            else if ( hsl.H <= 0.5 )
            {
                Mid = (int)Math.Round(((hsl.H - (double)1 / 3) * q) * 1530 + Min);
                return Color.FromArgb(Min,Max,Mid);
            }
            else if ( hsl.H <= (double)2/3 )
            {
                Mid = (int)Math.Round(-((hsl.H - 0.5) * q) * 1530 + Max);
                return Color.FromArgb(Min,Mid,Max);
            }
            else if ( hsl.H <= (double)5/6 )
            {
                Mid = (int)Math.Round(((hsl.H - (double)2 / 3) * q) * 1530 + Min);
                return Color.FromArgb(Mid,Min,Max);
            }
            else if ( hsl.H <= 1.0 )
            {
                Mid = (int)Math.Round(-((hsl.H - (double)5 / 6) * q) * 1530 + Max);
                return Color.FromArgb(Max,Min,Mid);
            }
            else	return Color.FromArgb(0,0,0);
        }
Example #36
0
        // Process the filter
        private unsafe void ProcessFilter( BitmapData data )
        {
            int width	= data.Width;
            int height	= data.Height;

            RGB		rgb = new RGB( );
            HSL		hsl = new HSL( );
            int		offset = data.Stride - width * 3;
            bool	updated;

            // do the job
            byte * ptr = (byte *) data.Scan0.ToPointer( );

            // for each row
            for ( int y = 0; y < height; y++ )
            {
                // for each pixel
                for ( int x = 0; x < width; x++, ptr += 3 )
                {
                    updated		= false;
                    rgb.Red		= ptr[RGB.R];
                    rgb.Green	= ptr[RGB.G];
                    rgb.Blue	= ptr[RGB.B];

                    // convert to HSL
                    AForge.Imaging.ColorConverter.RGB2HSL( rgb, hsl );

                    // check HSL values
                    if (
                        ( hsl.Saturation >= saturation.Min ) && ( hsl.Saturation <= saturation.Max ) &&
                        ( hsl.Luminance >= luminance.Min ) && ( hsl.Luminance <= luminance.Max ) &&
                        (
                        ( ( hue.Min < hue.Max ) && ( hsl.Hue >= hue.Min ) && ( hsl.Hue <= hue.Max ) ) ||
                        ( ( hue.Min > hue.Max ) && ( ( hsl.Hue >= hue.Min ) || ( hsl.Hue <= hue.Max ) ) )
                        )
                        )
                    {
                        if ( !fillOutsideRange )
                        {
                            if ( updateH ) hsl.Hue			= fillH;
                            if ( updateS ) hsl.Saturation	= fillS;
                            if ( updateL ) hsl.Luminance	= fillL;

                            updated = true;
                        }
                    }
                    else
                    {
                        if ( fillOutsideRange )
                        {
                            if ( updateH ) hsl.Hue			= fillH;
                            if ( updateS ) hsl.Saturation	= fillS;
                            if ( updateL ) hsl.Luminance	= fillL;

                            updated = true;
                        }
                    }

                    if ( updated )
                    {
                        // convert back to RGB
                        AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );

                        ptr[RGB.R] = rgb.Red;
                        ptr[RGB.G] = rgb.Green;
                        ptr[RGB.B] = rgb.Blue;
                    }
                }
                ptr += offset;
            }
        }
Example #37
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            int pixelSize = Bitmap.GetPixelFormatSize( image.PixelFormat ) / 8;

            int startX  = rect.Left;
            int startY  = rect.Top;
            int stopX   = startX + rect.Width;
            int stopY   = startY + rect.Height;
            int offset  = image.Stride - rect.Width * pixelSize;

            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            // do the job
            byte* ptr = (byte*) image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += ( startY * image.Stride + startX * pixelSize );

            // for each row
            for ( int y = startY; y < stopY; y++ )
            {
                // for each pixel
                for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                {
                    rgb.Red     = ptr[RGB.R];
                    rgb.Green   = ptr[RGB.G];
                    rgb.Blue    = ptr[RGB.B];

                    // convert to HSL
                     BestCS.Imaging.HSL.FromRGB( rgb, hsl );

                    // modify hue value
                    hsl.Hue = hue;

                    // convert back to RGB
                     BestCS.Imaging.HSL.ToRGB( hsl, rgb );

                    ptr[RGB.R] = rgb.Red;
                    ptr[RGB.G] = rgb.Green;
                    ptr[RGB.B] = rgb.Blue;
                }
                ptr += offset;
            }
        }
Example #38
0
 // http://en.wikipedia.org/wiki/Tints_and_shades
 public static IEnumerable<RGB> GetShades(RGB rgb, int count = 10)
 {
     HSL hsl = RGB2HSL(rgb);
     RGB[] result = new RGB[count];
     for (int i = 1; i <= count - 1; i++)
     {
         var H = hsl.Hue;
         var S = hsl.Saturation;
         var L = 255 * (float)(1 / i);
         HSL temp = new HSL(H, S, L);
         result[i] = HSL2RGB(temp);
     }
     return result;
 }
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            int pixelSize = Bitmap.GetPixelFormatSize( image.PixelFormat ) / 8;

            int startX  = rect.Left;
            int startY  = rect.Top;
            int stopX   = startX + rect.Width;
            int stopY   = startY + rect.Height;
            int offset  = image.Stride - rect.Width * pixelSize;

            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            float desaturationChangeFactor = 1.0f + adjustValue;

            // do the job
            byte* ptr = (byte*) image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += ( startY * image.Stride + startX * pixelSize );

            // for each row
            for ( int y = startY; y < stopY; y++ )
            {
                // for each pixel
                for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                {
                    rgb.Red   = ptr[RGB.R];
                    rgb.Green = ptr[RGB.G];
                    rgb.Blue  = ptr[RGB.B];

                    // convert to HSL
                    Accord.Imaging.HSL.FromRGB( rgb, hsl );

                    if ( adjustValue > 0 )
                    {
                        hsl.Saturation += ( 1.0f - hsl.Saturation ) * adjustValue * hsl.Saturation;
                    }
                    else if ( adjustValue < 0 )
                    {
                        hsl.Saturation *= desaturationChangeFactor;
                    }

                    // convert back to RGB
                    Accord.Imaging.HSL.ToRGB(hsl, rgb);

                    ptr[RGB.R] = rgb.Red;
                    ptr[RGB.G] = rgb.Green;
                    ptr[RGB.B] = rgb.Blue;
                }
                ptr += offset;
            }
        }
Example #40
0
        // Process the filter
        private unsafe void ProcessFilter( BitmapData data )
        {
            int width	= data.Width;
            int height	= data.Height;

            RGB		rgb = new RGB( );
            HSL		hsl = new HSL( );
            int		offset = data.Stride - width * 3;

            // do the job
            byte * ptr = (byte *) data.Scan0.ToPointer( );

            // for each row
            for ( int y = 0; y < height; y++ )
            {
                // for each pixel
                for ( int x = 0; x < width; x++, ptr += 3 )
                {
                    rgb.Red		= ptr[RGB.R];
                    rgb.Green	= ptr[RGB.G];
                    rgb.Blue	= ptr[RGB.B];

                    // convert to HSL
                    AForge.Imaging.ColorConverter.RGB2HSL( rgb, hsl );

                    // modify Hue value
                    hsl.Hue = hue;

                    // convert back to RGB
                    AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );

                    ptr[RGB.R] = rgb.Red;
                    ptr[RGB.G] = rgb.Green;
                    ptr[RGB.B] = rgb.Blue;
                }
                ptr += offset;
            }
        }
Example #41
0
        public void Reset()
        {
            Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GraphicsWindow.BackgroundColor));
            foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GraphicsWindow.PenColor));
            fontFamily = new FontFamily(GraphicsWindow.FontName);
            fontSize = GraphicsWindow.FontSize;
            fontWeight = GraphicsWindow.FontBold ? FontWeights.Bold : FontWeights.Normal;
            fontStyle = GraphicsWindow.FontItalic ? FontStyles.Italic : FontStyles.Normal;

            eStyle = Styles.PIE;
            Values = new List<double>();
            Labels = new List<string>();
            Count = 0;

            eLegend = Legends.LEGEND_PERCENT;
            bLegendBackground = false;
            legendScale = 1.0;
            scale = 1.0;

            hue = 0;
            saturation = 0.5;
            lightness = 0.5;
            startColor = 0;
            endColor = 1;
            centralColour = "";
            eHSL = HSL.HUE;

            Update();
        }
 /// <summary>
 /// Convert from RGB to HSL color space.
 /// </summary>
 /// 
 /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
 /// 
 /// <returns>Returns <see cref="HSL"/> instance, which represents converted color value.</returns>
 /// 
 public static HSL FromRGB(RGB rgb)
 {
     HSL hsl = new HSL();
     FromRGB(rgb, hsl);
     return hsl;
 }
 /// <summary>
 /// Sets the absolute saturation level
 /// </summary>
 /// <remarks>Accepted values 0-1</remarks>
 /// <param name="c">An original color</param>
 /// <param name="Saturation">The saturation value to impose</param>
 /// <returns>An adjusted color</returns>
 public static Color SetSaturation(Color c, double Saturation)
 {
     HSL hsl = new HSL(c);
     hsl.S = Saturation;
     return hsl.GetColor();
 }
 /// <summary>
 /// Sets the absolute Hue level
 /// </summary>
 /// <remarks>Accepted values 0-1</remarks>
 /// <param name="c">An original color</param>
 /// <param name="Hue">The Hue value to impose</param>
 /// <returns>An adjusted color</returns>
 public static Color SetHue(Color c, double Hue)
 {
     HSL hsl = new HSL(c);
     hsl.H = Hue;
     return hsl.GetColor();
 }
Example #45
0
 // http://en.wikipedia.org/wiki/Weber%E2%80%93Fechner_law#The_case_of_vision
 public static RGB GetContrast(RGB rgb)
 {
     var hsl = RGB2HSL(rgb);
     hsl = new HSL((hsl.Hue + 180) % 360, hsl.Saturation, hsl.Lightness);
     return HSL2RGB(hsl);
 }
            public static Color Convert(HSL hsl)
            {
                var rgb = new RGB();

                if (hsl.S == 0M)
                    rgb.R = rgb.G = rgb.B = hsl.L;
                else
                    rgb = GetRGBFromHSLWithChroma(hsl);

                return rgb.ToColor();
            }
            private static RGB GetRGBFromHSLWithChroma(HSL hsl)
            {
                decimal min, max, h;

                h = hsl.H / 360M;

                max = hsl.L < 0.5M ? hsl.L * (1 + hsl.S) : (hsl.L + hsl.S) - (hsl.L * hsl.S);
                min = (hsl.L * 2M) - max;

                var rgb = new RGB();
                rgb.R = ComponentFromHue(min, max, h + (1M / 3M));
                rgb.G = ComponentFromHue(min, max, h);
                rgb.B = ComponentFromHue(min, max, h - (1M / 3M));
                return rgb;
            }
Example #48
0
        // Process the filter
        private unsafe void ProcessFilter( BitmapData data )
        {
            int width	= data.Width;
            int height	= data.Height;

            RGB		rgb = new RGB( );
            HSL		hsl = new HSL( );
            int		offset = data.Stride - width * 3;
            double	kl = 0, bl = 0;
            double	ks = 0, bs = 0;

            // luminance line params
            if ( inLuminance.Max != inLuminance.Min )
            {
                kl = ( outLuminance.Max - outLuminance.Min ) / ( inLuminance.Max - inLuminance.Min );
                bl = outLuminance.Min - kl * inLuminance.Min;
            }
            // saturation line params
            if ( inSaturation.Max != inSaturation.Min )
            {
                ks = ( outSaturation.Max - outSaturation.Min ) / ( inSaturation.Max - inSaturation.Min );
                bs = outSaturation.Min - ks * inSaturation.Min;
            }

            // do the job
            byte * ptr = (byte *) data.Scan0.ToPointer( );

            // for each row
            for ( int y = 0; y < height; y++ )
            {
                // for each pixel
                for ( int x = 0; x < width; x++, ptr += 3 )
                {
                    rgb.Red		= ptr[RGB.R];
                    rgb.Green	= ptr[RGB.G];
                    rgb.Blue	= ptr[RGB.B];

                    // convert to HSL
                    AForge.Imaging.ColorConverter.RGB2HSL( rgb, hsl );

                    // correct luminance
                    if ( hsl.Luminance >= inLuminance.Max )
                        hsl.Luminance = outLuminance.Max;
                    else if ( hsl.Luminance <= inLuminance.Min )
                        hsl.Luminance = outLuminance.Min;
                    else
                        hsl.Luminance = kl * hsl.Luminance + bl;

                    // correct saturation
                    if ( hsl.Saturation >= inSaturation.Max )
                        hsl.Saturation = outSaturation.Max;
                    else if ( hsl.Saturation <= inSaturation.Min )
                        hsl.Saturation = outSaturation.Min;
                    else
                        hsl.Saturation = ks * hsl.Saturation + bs;

                    // convert back to RGB
                    AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );

                    ptr[RGB.R] = rgb.Red;
                    ptr[RGB.G] = rgb.Green;
                    ptr[RGB.B] = rgb.Blue;
                }
                ptr += offset;
            }
        }
    /// <summary>
    /// Convert from HSL to RGB color space.
    /// </summary>
    /// 
    /// <param name="hsl">Source color in <b>HSL</b> color space.</param>
    /// <param name="rgb">Destination color in <b>RGB</b> color space.</param>
    /// 
    public static void ToRGB(HSL hsl, RGB rgb)
    {
        if (hsl.Saturation == 0)
        {
            // gray values
            rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255);
        }
        else
        {
            float v1, v2;
            float hue = (float)hsl.Hue / 360;

            v2 = (hsl.Luminance < 0.5) ?
                (hsl.Luminance * (1 + hsl.Saturation)) :
                ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation));
            v1 = 2 * hsl.Luminance - v2;

            rgb.Red = (byte)(255 * Hue_2_RGB(v1, v2, hue + (1.0f / 3)));
            rgb.Green = (byte)(255 * Hue_2_RGB(v1, v2, hue));
            rgb.Blue = (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0f / 3)));
            rgb.Alpha = 255;
        }
    }
Example #50
0
    //Given HSL, returns RGB
    private Color HSLtoRGB( HSL _hsl )
    {
        //default value
        Color rgb = Color.green;

        //number wizardry
        //from http://www.rapidtables.com/convert/color/hsl-to-rgb.htm
        float c = (1 - (Mathf.Abs(2*_hsl.l) - 1)) * _hsl.s,
              x =  c * ( 1 - Mathf.Abs (((_hsl.h/60)%2) - 1)),
              m = _hsl.l - c/2;

        //default value
        Color rgbDelta = Color.black;

        //To find what rgbDelta is, we need
        if ( 0 <= _hsl.h  && _hsl.h < 60 )
        {
            rgbDelta.r = c;
            rgbDelta.g = x;
            rgbDelta.b = 0;
        }
        else if ( 60 <= _hsl.h  && _hsl.h < 120 )
        {
            rgbDelta.r = x;
            rgbDelta.g = c;
            rgbDelta.b = 0;
        }
        else if ( 120 <= _hsl.h  && _hsl.h < 180 )
        {
            rgbDelta.r = 0;
            rgbDelta.g = c;
            rgbDelta.b = x;
        }
        else if ( 180 <= _hsl.h  && _hsl.h < 240 )
        {
            rgbDelta.r = 0;
            rgbDelta.g = x;
            rgbDelta.b = c;
        }
        else if ( 240 <= _hsl.h  && _hsl.h < 300 )
        {
            rgbDelta.r = x;
            rgbDelta.g = 0;
            rgbDelta.b = c;
        }
        else //300 <= _hsl.h  && _hsl.h < 360
        {
            rgbDelta.r = c;
            rgbDelta.g = 0;
            rgbDelta.b = x;
        }

        rgb.r = rgbDelta.r + m;
        rgb.g = rgbDelta.g + m;
        rgb.b = rgbDelta.b + m;

        return rgb;
    }
    /// <summary>
    /// Convert from RGB to HSL color space.
    /// </summary>
    /// 
    /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
    /// <param name="hsl">Destination color in <b>HSL</b> color space.</param>
    /// 
    /// <remarks><para>See <a href="http://en.wikipedia.org/wiki/HSI_color_space#Conversion_from_RGB_to_HSL_or_HSV">HSL and HSV Wiki</a>
    /// for information about the algorithm to convert from RGB to HSL.</para></remarks>
    /// 
    public static void FromRGB(RGB rgb, HSL hsl)
    {
        float r = (rgb.Red / 255.0f);
        float g = (rgb.Green / 255.0f);
        float b = (rgb.Blue / 255.0f);

        float min = Math.Min(Math.Min(r, g), b);
        float max = Math.Max(Math.Max(r, g), b);
        float delta = max - min;

        // get luminance value
        hsl.Luminance = (max + min) / 2;

        if (delta == 0)
        {
            // gray color
            hsl.Hue = 0;
            hsl.Saturation = 0.0f;
        }
        else
        {
            // get saturation value
            hsl.Saturation = (hsl.Luminance <= 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));

            // get hue value
            float hue;

            if (r == max)
            {
                hue = ((g - b) / 6) / delta;
            }
            else if (g == max)
            {
                hue = (1.0f / 3) + ((b - r) / 6) / delta;
            }
            else
            {
                hue = (2.0f / 3) + ((r - g) / 6) / delta;
            }

            // correct hue if needed
            if (hue < 0)
                hue += 1;
            if (hue > 1)
                hue -= 1;

            hsl.Hue = (int)(hue * 360);
        }
    }
Example #52
0
        private void CustomDrawItem(ListBoxDrawItemEventArgs e)
        {
            double h = _customDrawingPosition / Convert.ToDouble(CustomDrawingPositionMaximum);
            if(e.IsItemSelected)
            {
                h = Math.Abs(1.0 - h);
            }

            HSL backColor = new HSL(h,
                                    1,
                                    e.ItemIndex % 2 == 0 ? 0.5 : 0.6);
            using(SolidBrush sb = new SolidBrush(backColor.ToRgb()))
            {
                e.Graphics.FillRectangle(sb, e.DrawingBounds);
            }

            HSL foreColor = new HSL(Math.Abs(1.0 - h),
                                    1,
                                    0.5);
            using(SolidBrush sb = new SolidBrush(foreColor.ToRgb()))
                e.Graphics.DrawString(DemoListBox.GetItemText(e.ItemIndex), DemoListBox.Font, sb, e.DrawingBounds);
        }
Example #53
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            int pixelSize = Image.GetPixelFormatSize( image.PixelFormat ) / 8;

            int startX  = rect.Left;
            int startY  = rect.Top;
            int stopX   = startX + rect.Width;
            int stopY   = startY + rect.Height;
            int offset  = image.Stride - rect.Width * pixelSize;

            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            float kl = 0, bl = 0;
            float ks = 0, bs = 0;

            // luminance line parameters
            if ( inLuminance.Max != inLuminance.Min )
            {
                kl = ( outLuminance.Max - outLuminance.Min ) / ( inLuminance.Max - inLuminance.Min );
                bl = outLuminance.Min - kl * inLuminance.Min;
            }
            // saturation line parameters
            if ( inSaturation.Max != inSaturation.Min )
            {
                ks = ( outSaturation.Max - outSaturation.Min ) / ( inSaturation.Max - inSaturation.Min );
                bs = outSaturation.Min - ks * inSaturation.Min;
            }

            // do the job
            byte* ptr = (byte*) image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += ( startY * image.Stride + startX * pixelSize );

            // for each row
            for ( int y = startY; y < stopY; y++ )
            {
                // for each pixel
                for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                {
                    rgb.Red   = ptr[RGB.R];
                    rgb.Green = ptr[RGB.G];
                    rgb.Blue  = ptr[RGB.B];

                    // convert to HSL
                    Accord.Imaging.HSL.FromRGB(rgb, hsl);

                    // do luminance correction
                    if ( hsl.Luminance >= inLuminance.Max )
                        hsl.Luminance = outLuminance.Max;
                    else if ( hsl.Luminance <= inLuminance.Min )
                        hsl.Luminance = outLuminance.Min;
                    else
                        hsl.Luminance = kl * hsl.Luminance + bl;

                    // do saturation correct correction
                    if ( hsl.Saturation >= inSaturation.Max )
                        hsl.Saturation = outSaturation.Max;
                    else if ( hsl.Saturation <= inSaturation.Min )
                        hsl.Saturation = outSaturation.Min;
                    else
                        hsl.Saturation = ks * hsl.Saturation + bs;

                    // convert back to RGB
                    Accord.Imaging.HSL.ToRGB( hsl, rgb );

                    ptr[RGB.R] = rgb.Red;
                    ptr[RGB.G] = rgb.Green;
                    ptr[RGB.B] = rgb.Blue;
                }
                ptr += offset;
            }
        }
Example #54
0
        public static RGB HSL2RGB(HSL hsl)
        {
            if (hsl.Saturation == 0)
            {
                return new RGB(hsl.Lightness, hsl.Lightness, hsl.Lightness);
            }
            else
            {
                double H = hsl.Hue / 360;
                double S = hsl.Saturation / 255;
                double L = hsl.Lightness / 255;

                double valueA;
                if (L < .5) { valueA = L * (1 + S); }
                else { valueA = (L + S) - (S * L); }
                double valueB = 2 * L - valueA;

                Func<double, double, double, float> hueToRGB = (valA, valB, Hue) =>
                {
                    if (Hue < 0)
                        Hue += 1;
                    if (Hue > 1)
                        Hue -= 1;
                    if (6 * Hue < 1)
                        return (float)(valA + (valB - valA) * 6 * Hue);
                    if (2 * Hue < 1)
                        return (float)valB;
                    if (3 * Hue < 2)
                        return (float)(valA + (valB - valA) * ((2 / 3) - Hue) * 6);
                    return (float)valA;
                };

                float R = 255f * hueToRGB(valueB, valueA, H + 1 / 3);
                float G = 255f * hueToRGB(valueB, valueA, H);
                float B = 255f * hueToRGB(valueB, valueA, H - 1 / 3);
                return new RGB(R, G, B);
            }
        }
 /// <summary>
 /// Modifies an existing Hue level
 /// </summary>
 /// <remarks>
 /// To reduce Hue use a number smaller than 1. To increase Hue use a number larger than 1
 /// </remarks>
 /// <param name="c">The original color</param>
 /// <param name="Hue">The Hue delta</param>
 /// <returns>An adjusted color</returns>
 public static Color ModifyHue(Color c, double Hue)
 {
     HSL hsl = new HSL(c);
     hsl.H *= Hue;
     return hsl.GetColor();
 }
 /// <summary>
 /// Sets the absolute brightness of a color
 /// </summary>
 /// <param name="c">Original color</param>
 /// <param name="brightness">The luminance level to impose</param>
 /// <returns>an adjusted color</returns>
 public static Color SetBrightness(Color c, double brightness)
 {
     HSL hsl = new HSL(c);
     hsl.L = brightness;
     return hsl.GetColor();
 }
Example #57
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            // get pixel size
            int pixelSize = ( image.PixelFormat == PixelFormat.Format24bppRgb ) ? 3 : 4;

            int startX  = rect.Left;
            int startY  = rect.Top;
            int stopX   = startX + rect.Width;
            int stopY   = startY + rect.Height;
            int offset  = image.Stride - rect.Width * pixelSize;

            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            bool updated;

            // do the job
            byte* ptr = (byte*) image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += ( startY * image.Stride + startX * pixelSize );

            // for each row
            for ( int y = startY; y < stopY; y++ )
            {
                // for each pixel
                for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                {
                    updated   = false;
                    rgb.Red   = ptr[RGB.R];
                    rgb.Green = ptr[RGB.G];
                    rgb.Blue  = ptr[RGB.B];

                    // convert to HSL
                    AForge.Imaging.HSL.FromRGB( rgb, hsl );

                    // check HSL values
                    if (
                        ( hsl.Saturation >= saturation.Min ) && ( hsl.Saturation <= saturation.Max ) &&
                        ( hsl.Luminance >= luminance.Min ) && ( hsl.Luminance <= luminance.Max ) &&
                        (
                        ( ( hue.Min < hue.Max ) && ( hsl.Hue >= hue.Min ) && ( hsl.Hue <= hue.Max ) ) ||
                        ( ( hue.Min > hue.Max ) && ( ( hsl.Hue >= hue.Min ) || ( hsl.Hue <= hue.Max ) ) )
                        )
                        )
                    {
                        if ( !fillOutsideRange )
                        {
                            if ( updateH ) hsl.Hue = fillH;
                            if ( updateS ) hsl.Saturation = fillS;
                            if ( updateL ) hsl.Luminance = fillL;

                            updated = true;
                        }
                    }
                    else
                    {
                        if ( fillOutsideRange )
                        {
                            if ( updateH ) hsl.Hue = fillH;
                            if ( updateS ) hsl.Saturation = fillS;
                            if ( updateL ) hsl.Luminance = fillL;

                            updated = true;
                        }
                    }

                    if ( updated )
                    {
                        // convert back to RGB
                        AForge.Imaging.HSL.ToRGB( hsl, rgb );

                        ptr[RGB.R] = rgb.Red;
                        ptr[RGB.G] = rgb.Green;
                        ptr[RGB.B] = rgb.Blue;
                    }
                }
                ptr += offset;
            }
        }
Example #58
0
 /// <summary> 
 /// Converts RGB to HSL 
 /// </summary> 
 /// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks> 
 /// <param name="c">A Color to convert</param> 
 /// <returns>An HSL value</returns> 
 public static HSL RGB_to_HSL(Color c)
 {
     HSL hsl = new HSL {H = (c.GetHue()/360.0), L = c.GetBrightness(), S = c.GetSaturation()};
     return hsl;
 }
        /// <summary> 
        /// Converts RGB to HSL 
        /// </summary> 
        /// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks> 
        /// <param name="c">A Color to convert</param> 
        /// <returns>An HSL value</returns> 
        public static HSL RGB_to_HSL(Color c)
        {
            HSL hsl =  new HSL();

            int Max, Min, Diff;

            //	Of our RGB values, assign the highest value to Max, and the Smallest 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;
            else if ( c.B < Min ) Min = c.B;

            Diff = Max - Min;

            //	Luminance - a.k.a. Brightness - Adobe photoshop uses the logic that the
            //	site VBspeed regards (regarded) as too primitive = superior decides the
            //	level of brightness.
            hsl.L = (double)Max/255;

            //	Saturation
            if ( Max == 0 ) hsl.S = 0;	//	Protecting from the impossible operation of division by zero.
            else hsl.S = (double)Diff/Max;	//	The logic of Adobe Photoshops is this simple.

            //	Hue		R is situated at the angel of 360 eller noll degrees;
            //			G vid 120 degrees
            //			B vid 240 degrees
            double q;
            if ( Diff == 0 ) q = 0; // Protecting from the impossible operation of division by zero.
            else q = (double)60/Diff;

            if ( Max == c.R )
            {
                    if ( c.G < c.B )	hsl.H = (double)(360 + q * (c.G - c.B))/360;
                else				hsl.H = (double)(q * (c.G - c.B))/360;
            }
            else if ( Max == c.G )	hsl.H = (double)(120 + q * (c.B - c.R))/360;
            else if ( Max == c.B )	hsl.H = (double)(240 + q * (c.R - c.G))/360;
            else					hsl.H = 0.0;

            return hsl;
        }
            public static HSL Convert(Color color)
            {
                var hsl = new HSL();
                var rgb = GetRGBFromColor(color);

                var max = Math.Max(Math.Max(rgb.R, rgb.G), rgb.B);
                var min = Math.Min(Math.Min(rgb.R, rgb.G), rgb.B);
                var chroma = max - min;

                hsl.L = GetL(max, min);

                if (chroma != 0)
                {
                    hsl.H = GetH(rgb, max, chroma);
                    hsl.S = GetS(hsl.L, chroma);
                }
                return hsl;
            }