private List <RegexReplacement> GetMatches(string text, IReadOnlyList <Color> colors)
        {
            // Encapsulate filtered colors and regex matches within list of RegexReplacements
            var matches = _matches ?? Regex.Matches(text, RegexPattern);
            int i       = 0;
            List <RegexReplacement> colorMatches = new List <RegexReplacement>();

            foreach (Match match in matches)
            {
                string rgbString         = match.Groups[MatchGroupName].ToString();
                string filteredRgbString = HexRgbUtil.ColorToHexString(colors[i], OutputHexFormat);

                var sourceColor = PaddableHexFormat.PaddableHexStringToColor(rgbString, InputHexFormats);
                var resultColor = colors[i];
                var minmax      = new StringBuilder();
                if (sourceColor.CompareValue().AboutEqual(sourceMax))
                {
                    minmax.Append($"<Source Max {sourceColor.CompareValue():F3}> ");
                }
                if (sourceColor.CompareValue().AboutEqual(sourceMin))
                {
                    minmax.Append($"<Source Min {sourceColor.CompareValue():F3}> ");
                }
                if (resultColor.CompareValue().AboutEqual(resultMax))
                {
                    minmax.Append($"<Result Max {resultColor.CompareValue():F3}> ");
                }
                if (resultColor.CompareValue().AboutEqual(resultMin))
                {
                    minmax.Append($"<Result Min {resultColor.CompareValue():F3}> ");
                }

                if (match.Groups["attr"] != null && match.Groups["attr"].Value != "")
                {
                    Console.WriteLine(match.Groups["attr"].Value);
                }

                if (i == 0)
                {
                    Console.WriteLine();
                }
                string subAttr = match.Groups["attr2"]?.Value ?? "";
                Console.WriteLine($"  {subAttr,-30} #{rgbString} -> #{filteredRgbString} {minmax}");

                colorMatches.Add(new RegexReplacement()
                {
                    Index             = match.Groups[MatchGroupName].Index,
                    Length            = match.Groups[MatchGroupName].Length,
                    MatchingString    = rgbString,
                    ReplacementString = filteredRgbString
                });

                i++;
            }

            Console.WriteLine($"\n{i} colors affected\n");
            Console.WriteLine($"SOURCE: Min: {sourceMin:F3} Max: {sourceMax:F3}");
            Console.WriteLine($"RESULT: Min: {resultMin:F3} Max: {resultMax:F3}");
            return(colorMatches);
        }
        /// <summary>
        /// Sets builtin parent scheme to Default for light background schemes, and Darcula for dark
        /// background schemes.
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private static string SetParentScheme(string xml)
        {
            var root       = XElement.Parse(xml);
            var textOption = root.Elements("attributes").Descendants("option")
                             .First(x => x.Attribute("name")?.Value == "TEXT");
            string textFG = textOption.Descendants("value").Descendants("option")
                            .First(x => x.Attribute("name")?.Value == "FOREGROUND").Attribute("value")?.Value;
            string textBG = textOption.Descendants("value").Descendants("option")
                            .First(x => x.Attribute("name")?.Value == "BACKGROUND").Attribute("value")?.Value;

            if (!string.IsNullOrEmpty(textFG) && !string.IsNullOrEmpty(textBG) && HexRgbUtil.IsValidHexString(textFG) &&
                HexRgbUtil.IsValidHexString(textBG))
            {
                var fg = HexRgbUtil.RGBStringToColor(textFG);
                var bg = HexRgbUtil.RGBStringToColor(textBG);
                Console.WriteLine(
                    $"Background #{textBG} ({bg.CompareValue():F3}), Foreground #{textFG} ({fg.CompareValue():F3})");
                var parentScheme = "Default";
                if (fg.GetBrightness() > bg.GetBrightness())
                {
                    parentScheme = "Darcula";
                }
                root.Attribute("parent_scheme")?.SetValue(parentScheme);
                return(root.ToString());
            }

            return(xml);
        }
        public void Color_ClampingManuallyOverSaturatedColor_ReturnsUnchangedHue()
        {
            var sourceColor = HexRgbUtil.HexStringToColor("2b6ca2", "rrggbb");
            var color       = new Color(sourceColor);

            color.Saturation *= 2;
            Console.WriteLine(color.ToString());
            color.ClampExceedingColors();
            Console.WriteLine(color.ToString());
            Assert.That(color.Hue, Is.EqualTo(sourceColor.Hue).Within(0.000001));
        }
        public void ClampedColor_MultiplySaturationBy2ToOverSaturateThenDivideBy2_ReturnsOriginalColor()
        {
            var sourceColor = HexRgbUtil.HexStringToColor("2b6ca2", "rrggbb");
            var color       = new ClampedColor(sourceColor);

            color.Saturation *= 2;
            Console.WriteLine(color.ToString());
            color.Saturation /= 2;
            Console.WriteLine(color.ToString());
            Assert.That(color.Saturation, Is.EqualTo(0.5).Within(0.0000001));
        }
        public void Color_MultiplySaturationBy2ToOverSaturateThenDivideBy2_ReturnsOriginalColor()
        {
            var sourceColor = HexRgbUtil.HexStringToColor("2b6ca2", "rrggbb");
            var color       = new Color(sourceColor);

            color.Saturation *= 2;
            Console.WriteLine(color.ToString());
            color.Saturation /= 2;
            Console.WriteLine(color.ToString());
            Assert.True(color.Equals(sourceColor));
        }
Esempio n. 6
0
        /// <summary>
        /// Converts RGB hex string to Color. Applies padding to short strings by the color scheme padding rules.
        /// </summary>
        /// <param name="rgbString"></param>
        /// <param name="formats"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static Color PaddableHexStringToColor(string rgbString, IEnumerable <PaddableHexFormat> formats)
        {
            foreach (var format in formats)
            {
                string rgbHexFormat = format.HexFormat;
                if (format.Padding != null && format.Padding.Length != format.HexFormat.Length)
                {
                    throw new Exception("RGB hex string misconfiguration: " + format.HexFormat + " with padding " +
                                        format.Padding);
                }

                if (HexRgbUtil.IsValidHexString(rgbString) && rgbString.Length <= rgbHexFormat.Length)
                {
                    if (rgbString.Length < rgbHexFormat.Length)
                    {
                        if (format.PaddingDirection == PaddingDirection.Left ||
                            format.PaddingDirection == PaddingDirection.Right)
                        {
                            switch (format.PaddingDirection)
                            {
                            case PaddingDirection.Left:
                                rgbString = rgbString.PadLeft(format.Padding);
                                break;

                            case PaddingDirection.Right:
                                rgbString = rgbString.PadRight(format.Padding);
                                break;
                            }
                        }
                    }

                    if (rgbString.Length == rgbHexFormat.Length)
                    {
                        return(HexRgbUtil.HexStringToColor(rgbString, rgbHexFormat));
                    }
                }
            }

            throw new Exception("Invalid color string: " + rgbString);
        }