<commit_before>﻿using System;
using System.Windows.Media;

namespace Digimezzo.Utilities.ColorSpace
{
    public class HSLColor
    {
        #region Property
        private double hue;
        private double saturation;
        private double luminosity;

        public int Hue
        {
            get
            {
                var t = (int)(hue * 60d);
                if (t > 360)
                    t -= 360;
                return t;
            }
            set
            {
                var t = value;
                if (t > 0)
                    while (t > 360)
                        t -= 360;
                else
                    while (t < 0)
                        t += 360;
                hue = t / 60d;
            }
        }

        public int Saturation
        {
            get { return (int)(saturation * 100d); }
            set
            {
                if (value < 0 || value > 100)
                    throw new ArgumentOutOfRangeException("Saturation", "Saturation only can be set between 0 and 100");
                else
                    saturation = value / 100d;
            }
        }

        public int Luminosity
        {
            get { return (int)(luminosity * 100d); }
            set
            {
                if (value < 0 || value > 100)
                    throw new ArgumentOutOfRangeException("Luminosity", "Luminosity only can be set between 0 and 100");
                else
                    luminosity = value / 100d;
            }
        }
        #endregion

        #region Constructor
        public HSLColor(int H, int S, int L)
        {
            this.Hue = H;
            this.Saturation = S;
            this.Luminosity = L;
        }

        private HSLColor(double h, double s, double l)
        {
            this.hue = h;
            this.saturation = s;
            this.luminosity = l;
        }
        #endregion

        #region Public
        public Color ToRgb()
        {
            byte r, g, b;
            if (Saturation == 0)
            {
                r = (byte)Math.Round(luminosity * 255d);
                g = (byte)Math.Round(luminosity * 255d);
                b = (byte)Math.Round(luminosity * 255d);
            }
            else
            {
                double t1, t2;
                double th = hue / 6.0d;

                if (luminosity < 0.5d)
                {
                    t2 = luminosity * (1d + saturation);
                }
                else
                {
                    t2 = (luminosity + saturation) - (luminosity * saturation);
                }
                t1 = 2d * luminosity - t2;

                var tr = th + (1.0d / 3.0d);
                var tg = th;
                var tb = th - (1.0d / 3.0d);
                tr = ColorCalc(tr, t1, t2);
                tg = ColorCalc(tg, t1, t2);
                tb = ColorCalc(tb, t1, t2);

                r = (byte)Math.Round(tr * 255d);
                g = (byte)Math.Round(tg * 255d);
                b = (byte)Math.Round(tb * 255d);
            }

            return Color.FromRgb(r, g, b);
        }

        public HSLColor MoveNext(int step)
        {
            Hue += step;
            return this;
        }

        public static HSLColor GetFromRgb(Color color)
        {
            return GetFromRgb(color.R, color.G, color.B);
        }

        public static HSLColor GetFromRgb(byte R, byte G, byte B)
        {
            double r = R / 255d, g = G / 255d, b = B / 255d;
            double max = Math.Max(Math.Max(r, g), b), min = Math.Min(Math.Min(r, g), b);
            var delta = max - min;

            double h = 0, s = 0, l = (max + min) / 2d;
            if (delta != 0)
            {
                if (l < 0.5d)
                    s = delta / (max + min);
                else
                    s = delta / (2d - max - min);

                if (r == max)
                {
                    h = (g - b) / delta;
                }
                else if (g == max)
                {
                    h = 2d + (b - r) / delta;
                }
                else if (b == max)
                {
                    h = 4d + (r - g) / delta;
                }
            }

            return new HSLColor(h, s, l);
        }

        public static Color TrimLuminosity(Color color, int trimStart, int trimEnd)
        {
            HSLColor hslColor = HSLColor.GetFromRgb(color);

            if (hslColor.Luminosity < trimStart)
            {
                hslColor.Luminosity = trimStart;
            }
            else if (hslColor.Luminosity > 100 - trimEnd)
            {
                hslColor.Luminosity = 100 - trimEnd;
            }

            return hslColor.ToRgb();
        }
        #endregion

        #region Private
        private static double ColorCalc(double c, double t1, double t2)
        {
            if (c < 0) c += 1d;
            if (c > 1) c -= 1d;
            if (6.0d * c < 1.0d) return t1 + (t2 - t1) * 6.0d * c;
            if (2.0d * c < 1.0d) return t2;
            if (3.0d * c < 2.0d) return t1 + (t2 - t1) * (2.0d / 3.0d - c) * 6.0d;
            return t1;
        }
        #endregion
    }<commit_msg>Renamed TrimLuminosity to Normalize<commit_after>﻿using System;
using System.Windows.Media;

namespace Digimezzo.Utilities.ColorSpace
{
    public class HSLColor
    {
        #region Property
        private double hue;
        private double saturation;
        private double luminosity;

        public int Hue
        {
            get
            {
                var t = (int)(hue * 60d);
                if (t > 360)
                    t -= 360;
                return t;
            }
            set
            {
                var t = value;
                if (t > 0)
                    while (t > 360)
                        t -= 360;
                else
                    while (t < 0)
                        t += 360;
                hue = t / 60d;
            }
        }

        public int Saturation
        {
            get { return (int)(saturation * 100d); }
            set
            {
                if (value < 0 || value > 100)
                    throw new ArgumentOutOfRangeException("Saturation", "Saturation only can be set between 0 and 100");
                else
                    saturation = value / 100d;
            }
        }

        public int Luminosity
        {
            get { return (int)(luminosity * 100d); }
            set
            {
                if (value < 0 || value > 100)
                    throw new ArgumentOutOfRangeException("Luminosity", "Luminosity only can be set between 0 and 100");
                else
                    luminosity = value / 100d;
            }
        }
        #endregion

        #region Constructor
        public HSLColor(int H, int S, int L)
        {
            this.Hue = H;
            this.Saturation = S;
            this.Luminosity = L;
        }

        private HSLColor(double h, double s, double l)
        {
            this.hue = h;
            this.saturation = s;
            this.luminosity = l;
        }
        #endregion

        #region Public
        public Color ToRgb()
        {
            byte r, g, b;
            if (Saturation == 0)
            {
                r = (byte)Math.Round(luminosity * 255d);
                g = (byte)Math.Round(luminosity * 255d);
                b = (byte)Math.Round(luminosity * 255d);
            }
            else
            {
                double t1, t2;
                double th = hue / 6.0d;

                if (luminosity < 0.5d)
                {
                    t2 = luminosity * (1d + saturation);
                }
                else
                {
                    t2 = (luminosity + saturation) - (luminosity * saturation);
                }
                t1 = 2d * luminosity - t2;

                var tr = th + (1.0d / 3.0d);
                var tg = th;
                var tb = th - (1.0d / 3.0d);
                tr = ColorCalc(tr, t1, t2);
                tg = ColorCalc(tg, t1, t2);
                tb = ColorCalc(tb, t1, t2);

                r = (byte)Math.Round(tr * 255d);
                g = (byte)Math.Round(tg * 255d);
                b = (byte)Math.Round(tb * 255d);
            }

            return Color.FromRgb(r, g, b);
        }

        public HSLColor MoveNext(int step)
        {
            Hue += step;
            return this;
        }

        public static HSLColor GetFromRgb(Color color)
        {
            return GetFromRgb(color.R, color.G, color.B);
        }

        public static HSLColor GetFromRgb(byte R, byte G, byte B)
        {
            double r = R / 255d, g = G / 255d, b = B / 255d;
            double max = Math.Max(Math.Max(r, g), b), min = Math.Min(Math.Min(r, g), b);
            var delta = max - min;

            double h = 0, s = 0, l = (max + min) / 2d;
            if (delta != 0)
            {
                if (l < 0.5d)
                    s = delta / (max + min);
                else
                    s = delta / (2d - max - min);

                if (r == max)
                {
                    h = (g - b) / delta;
                }
                else if (g == max)
                {
                    h = 2d + (b - r) / delta;
                }
                else if (b == max)
                {
                    h = 4d + (r - g) / delta;
                }
            }

            return new HSLColor(h, s, l);
        }

        public static Color Normalize(Color color, int value)
        {
            if (value < 0 | value > 100) return color;

            HSLColor hslColor = HSLColor.GetFromRgb(color);

            if (hslColor.Luminosity < value)
            {
                hslColor.Luminosity = value;
            }
            else if (hslColor.Luminosity > 100 - value)
            {
                hslColor.Luminosity = 100 - value;
            }

            return hslColor.ToRgb();
        }
        #endregion

        #region Private
        private static double ColorCalc(double c, double t1, double t2)
        {
            if (c < 0) c += 1d;
            if (c > 1) c -= 1d;
            if (6.0d * c < 1.0d) return t1 + (t2 - t1) * 6.0d * c;
            if (2.0d * c < 1.0d) return t2;
            if (3.0d * c < 2.0d) return t1 + (t2 - t1) * (2.0d / 3.0d - c) * 6.0d;
            return t1;
        }
        #endregion
    }
{'fraction_non_alphanumeric': 0.09965602904643608, 'fraction_numerical': 0.029428626027135488, 'mean_word_length': 0.8951656708310701, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6634768', 'n_tokens_mistral': 3415, 'n_tokens_neox': 2983, 'n_words': 832}