Ejemplo n.º 1
0
        private static HslType HexToHsl(this string hexIn)
        {
            /*
             * the source text is taken from here (with minor modifications):
             * https://css-tricks.com/converting-color-spaces-in-javascript/
             */
            // Convert hex to RGB first. Ignore opacity
            RgbType rgb = hexIn.HexToRgb(1);

            // Then to HSL
            rgb.ToHsl();

            var cmin  = rgb.Min();
            var cmax  = rgb.Max();
            var delta = cmax - cmin;

            var hls = new HslType
            {
                Hue       = ComputeH(rgb, cmax, delta),
                Lightness = (cmax + cmin) / 2
            };

            hls.Saturation = delta == 0 ? 0 : delta / (1 - Math.Abs(2 * hls.Lightness - 1));
            hls.Saturation = Convert.ToDouble(String.Format("{0:0.0}", hls.Saturation * 100));
            hls.Lightness  = Convert.ToDouble(String.Format("{0:0.0}", hls.Lightness * 100));
            hls.Round();

            return(hls);
        }
Ejemplo n.º 2
0
        public HslType(HslType offset)
        {
            if (offset == null)
            {
                throw new ArgumentNullException(nameof(offset));
            }

            Hue        = offset.Hue;
            Saturation = offset.Saturation;
            Lightness  = offset.Lightness;
        }
Ejemplo n.º 3
0
        public static HslType ColorShiftHsl(this string main, HslType offset)
        {
            if (main == null)
            {
                throw new ArgumentNullException(nameof(main));
            }
            if (offset == null)
            {
                throw new ArgumentNullException(nameof(offset));
            }

            var offsetCopy = new HslType(offset);

            HslType color = main.HexToHsl();

            color.Hue        = NormalizeValue(color.Hue + offsetCopy.Hue, minValue: 0, maxValue: 360);
            color.Saturation = NormalizeValue(color.Saturation + offsetCopy.Saturation);
            color.Lightness  = NormalizeValue(color.Lightness + offsetCopy.Lightness);

            return(color);
        }
Ejemplo n.º 4
0
        public static (string, string) GetGradientColor(this string color)
        {
            if (color == null)
            {
                throw new ArgumentNullException(nameof(color));
            }

            HslType first  = color.HexToHsl();
            HslType second = color.HexToHsl();

            // hue circle degrees approximation
            bool isRed            = (first.Hue >= 0 && first.Hue < 45) || (first.Hue >= 285);
            bool isYellow         = first.Hue >= 45 && first.Hue < 85;
            bool isGreen          = first.Hue >= 85 && first.Hue < 165;
            bool isBlue           = first.Hue >= 165 && first.Hue < 285;
            bool isUndersaturated = first.Saturation < 30; // i.e. too pale, gray-ish, monotone

            if (isRed)
            {
                first.Hue        += 11;
                first.Saturation += 27;
                first.Lightness  += 8;
            }
            else if (isYellow)
            {
                first.Hue       += 3;
                first.Lightness += 9;

                second.Hue -= 2;
            }
            else if (isGreen)
            {
                first.Hue        += 13;
                first.Saturation -= 5;
                first.Lightness  += 7;

                second.Hue        -= 3;
                second.Saturation -= 1;
                second.Lightness  -= 6;
            }
            else if (isBlue)
            {
                first.Hue        -= 15;
                first.Saturation += 3;
                first.Lightness  += 2;
            }

            if (isUndersaturated)
            {
                first.Lightness  += 6;
                second.Lightness -= 2;
            }

            // restrictions and validations
            if (first.Hue < 0)
            {
                first.Hue += 360;
            }
            if (first.Hue > 0)
            {
                Math.Round(first.Hue = first.Hue % 360);
            }
            if (second.Hue < 0)
            {
                second.Hue += 360;
            }
            if (second.Hue > 0)
            {
                Math.Round(second.Hue = second.Hue % 360);
            }

            first.Saturation  = first.Saturation > 0 ? first.Saturation : 0;
            first.Saturation  = first.Saturation < 100 ? first.Saturation : 100;
            second.Saturation = second.Saturation > 0 ? second.Saturation : 0;
            second.Saturation = second.Saturation < 100 ? second.Saturation : 100;

            first.Lightness  = first.Lightness > 0 ? first.Lightness : 0;
            first.Lightness  = first.Lightness < 100 ? first.Lightness : 100;
            second.Lightness = second.Lightness > 0 ? second.Lightness : 0;
            second.Lightness = second.Lightness < 100 ? second.Lightness : 100;

            return(first.ToString(), second.ToString());
        }