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); }
public virtual IEnumerable <Color> GetColors(string text) { sourceMin = double.MaxValue; sourceMax = double.MinValue; var matches = Regex.Matches(text, RegexPattern); _matches = matches; foreach (Match match in matches) { string rgbString = match.Groups[MatchGroupName].ToString(); var color = PaddableHexFormat.PaddableHexStringToColor(rgbString, InputHexFormats); sourceMin = color.CompareValue() < sourceMin?color.CompareValue() : sourceMin; sourceMax = color.CompareValue() > sourceMax?color.CompareValue() : sourceMax; yield return(color); } }