Esempio n. 1
0
        /// <summary>
        /// Attempts to parse the specified <paramref name="str"/> into an instance of <see cref="RgbColor"/>.
        /// </summary>
        /// <param name="str">The input string to be parsed.</param>
        /// <param name="color">An instance of <see cref="RgbColor"/>.</param>
        /// <returns><c>true</c> if <paramref name="str"/> was converted successfully; otherwise, <c>false</c>.</returns>
        public static bool TryParse(string str, out RgbColor color)
        {
            color = null;

            // Attempt to parse the input string
            if (!ColorHelpers.TryParse(str, out IColor result))
            {
                return(false);
            }

            // Convert the color to RGB
            color = result as RgbColor ?? result.ToRgb();
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Attempts to parse the specified <paramref name="str"/> into an instance of <see cref="IColor"/>.
        /// </summary>
        /// <param name="str">The input string to be parsed.</param>
        /// <param name="color">An instance of <see cref="IColor"/>.</param>
        /// <returns><c>true</c> if <paramref name="str"/> was converted successfully; otherwise, <c>false</c>.</returns>
        public static bool TryParse(string str, out IColor color)
        {
            color = null;

            // Return "false" if the string is empty
            if (String.IsNullOrWhiteSpace(str))
            {
                return(false);
            }

            // Strip a leading hashtag and convert to lowercase
            str = str.TrimStart('#').ToLower();

            // Time for some regex :D
            Match m1 = Regex.Match(str, "^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$");
            Match m2 = Regex.Match(str, "^([0-9a-f]{1})([0-9a-f]{1})([0-9a-f]{1})$");

            Match m3 = Regex.Match(str, "^hsl\\(([0-9]+), ([0-9]+)%, ([0-9]+)%\\)$");

            if (m1.Success)
            {
                Byte.TryParse(m1.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out byte r);
                Byte.TryParse(m1.Groups[2].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out byte g);
                Byte.TryParse(m1.Groups[3].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out byte b);
                color = new RgbColor(r, g, b);
                return(true);
            }

            if (m2.Success)
            {
                Byte.TryParse(m2.Groups[1].Value + m2.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out byte r);
                Byte.TryParse(m2.Groups[2].Value + m2.Groups[2].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out byte g);
                Byte.TryParse(m2.Groups[3].Value + m2.Groups[3].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out byte b);
                color = new RgbColor(r, g, b);
                return(true);
            }

            if (m3.Success)
            {
                float h = Int32.Parse(m3.Groups[1].Value) / 360f;
                float s = Int32.Parse(m3.Groups[2].Value) / 100f;
                float l = Int32.Parse(m3.Groups[3].Value) / 100f;
                color = new HslColor(h, s, l);
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the inverted color.
        /// </summary>
        /// <param name="color">The input color.</param>
        /// <returns>An instance of <see cref="IColor"/> representing the output color.</returns>
        public static IColor Invert(this IColor color)
        {
            switch (color)
            {
            case IAlphaColor alpha:

                RgbaColor rgba = alpha as RgbaColor ?? alpha.ToRgba();

                return(new RgbaColor(~rgba.R, ~rgba.G, ~rgba.B, rgba.Alpha));

            default:

                RgbColor rgb = color as RgbColor ?? color.ToRgb();

                return(new RgbColor(~rgb.Red, ~rgb.Green, ~rgb.Blue));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the inverted color.
        /// </summary>
        /// <param name="color">The input color.</param>
        /// <returns>An instance of <see cref="IColor"/> representing the output color.</returns>
        public static IColor Invert(this IColor color)
        {
            RgbColor rgb = color as RgbColor ?? color.ToRgb();

            return(new RgbColor(~rgb.Red, ~rgb.Green, ~rgb.Blue, rgb.Alpha));
        }