diff --git a/src/Providers/Implementations/ColorNamesProvider.cs b/src/Providers/Implementations/ColorNamesProvider.cs index ff179cf..cde25b5 100644 --- a/src/Providers/Implementations/ColorNamesProvider.cs +++ b/src/Providers/Implementations/ColorNamesProvider.cs @@ -1,157 +1,164 @@ using System.Collections.Generic; -using System.Globalization; +using System.Linq; using System.Text.RegularExpressions; namespace Shifter.Providers { public class ColorNamesProvider : RegexProviderBase { - private readonly List _list = new() + private static readonly List _list = new() { - "pink", - "lightpink", - "hotpink", - "deeppink", - "palevioletred", - "mediumvioletred", - "lavender", - "thistle", - "plum", - "orchid", - "violet", - "fuchsia", - "magenta", - "mediumorchid", - "darkorchid", - "darkviolet", - "blueviolet", - "darkmagenta", - "purple", - "mediumpurple", - "mediumslateblue", - "slateblue", - "darkslateblue", - "rebeccapurple", - "indigo", - "lightsalmon", - "salmon", - "darksalmon", - "lightcoral", - "indianred", - "crimson", - "red", - "firebrick", - "darkred", - "orange", - "darkorange", - "coral", - "tomato", - "orangered", - "gold", - "yellow", - "lightyellow", - "lemonchiffon", - "lightgoldenrodyellow", - "papayawhip", - "moccasin", - "peachpuff", - "palegoldenrod", - "khaki", - "darkkhaki", - "greenyellow", - "chartreuse", - "lawngreen", - "lime", - "limegreen", - "palegreen", - "lightgreen", - "mediumspringgreen", - "springgreen", - "mediumseagreen", - "seagreen", - "forestgreen", - "green", - "darkgreen", - "yellowgreen", - "olivedrab", - "darkolivegreen", - "mediumaquamarine", - "darkseagreen", - "lightseagreen", - "darkcyan", - "teal", - "aqua", - "cyan", - "lightcyan", - "paleturquoise", - "aquamarine", - "turquoise", - "mediumturquoise", - "darkturquoise", - "cadetblue", - "steelblue", - "lightsteelblue", - "lightblue", - "powderblue", - "lightskyblue", - "skyblue", - "cornflowerblue", - "deepskyblue", - "dodgerblue", - "royalblue", - "blue", - "mediumblue", - "darkblue", - "navy", - "midnightblue", - "cornsilk", - "blanchedalmond", - "bisque", - "navajowhite", - "wheat", - "burlywood", - "tan", - "rosybrown", - "sandybrown", - "goldenrod", - "darkgoldenrod", - "peru", - "chocolate", - "olive", - "saddlebrown", - "sienna", - "brown", - "maroon", - "white", - "snow", - "honeydew", - "mintcream", - "azure", - "aliceblue", - "ghostwhite", - "whitesmoke", - "seashell", - "beige", - "oldlace", - "floralwhite", - "ivory", - "antiquewhite", - "linen", - "lavenderblush", - "mistyrose", - "gainsboro", - "lightgray", - "silver", - "darkgray", - "dimgray", - "gray", - "lightslategray", - "slategray", - "darkslategray", - "black", + "Pink", + "LightPink", + "HotPink", + "DeepPink", + "PaleVioletRed", + "MediumVioletRed", + "Lavender", + "Thistle", + "Plum", + "Orchid", + "Violet", + "Fuchsia", + "Magenta", + "MediumOrchid", + "DarkOrchid", + "DarkViolet", + "BlueViolet", + "DarkMagenta", + "Purple", + "MediumPurple", + "MediumSlateBlue", + "SlateBlue", + "DarkSlateBlue", + "RebeccaPurple", + "Indigo", + "LightSalmon", + "Salmon", + "DarkSalmon", + "LightCoral", + "IndianRed", + "Crimson", + "Red", + "Firebrick", + "DarkRed", + "Orange", + "DarkOrange", + "Coral", + "Tomato", + "OrangeRed", + "Gold", + "Yellow", + "LightYellow", + "LemonChiffon", + "LightGoldenrodYellow", + "PapayaWhip", + "Moccasin", + "PeachPuff", + "PaleGoldenrod", + "Khaki", + "DarkKhaki", + "GreenYellow", + "Chartreuse", + "LawnGreen", + "Lime", + "LimeGreen", + "PaleGreen", + "LightGreen", + "MediumSpringGreen", + "SpringGreen", + "MediumSeaGreen", + "SeaGreen", + "ForestGreen", + "Green", + "DarkGreen", + "YellowGreen", + "OliveDrab", + "DarkOliveGreen", + "MediumAquamarine", + "DarkSeaGreen", + "LightSeaGreen", + "DarkCyan", + "Teal", + "Aqua", + "Cyan", + "LightCyan", + "PaleTurquoise", + "Aquamarine", + "Turquoise", + "MediumTurquoise", + "DarkTurquoise", + "CadetBlue", + "SteelBlue", + "LightSteelBlue", + "LightBlue", + "PowderBlue", + "LightSkyBlue", + "SkyBlue", + "CornflowerBlue", + "DeepSkyBlue", + "DodgerBlue", + "RoyalBlue", + "Blue", + "MediumBlue", + "DarkBlue", + "Navy", + "MidnightBlue", + "Cornsilk", + "BlanchedAlmond", + "Bisque", + "NavajoWhite", + "Wheat", + "BurlyWood", + "Tan", + "RosyBrown", + "SandyBrown", + "Goldenrod", + "DarkGoldenrod", + "Peru", + "Chocolate", + "Olive", + "SaddleBrown", + "Sienna", + "Brown", + "Maroon", + "White", + "Snow", + "Honeydew", + "MintCream", + "Azure", + "AliceBlue", + "GhostWhite", + "WhiteSmoke", + "SeaShell", + "Beige", + "OldLace", + "FloralWhite", + "Ivory", + "AntiqueWhite", + "Linen", + "LavenderBlush", + "MistyRose", + "Gainsboro", + "LightGray", + "Silver", + "DarkGray", + "DimGray", + "Gray", + "LightSlateGray", + "SlateGray", + "DarkSlateGray", + "Black", }; + private static readonly List _listLower; + + static ColorNamesProvider() + { + _listLower = _list.Select(s => s.ToLowerInvariant()).ToList(); + } + private static readonly Regex _regex = new(@"\b[a-zA-Z]+\b", RegexOptions.Compiled); public override Regex Regex => _regex; @@ -159,22 +166,23 @@ public override bool TryShiftSelection(Match match, ShiftDirection direction, ou { result = null; - int index = _list.IndexOf(match.Value.ToLowerInvariant()); + int index = _listLower.IndexOf(match.Value.ToLowerInvariant()); + + if (index < 0 || index >= _list.Count) return false; - if (index > -1 && index < _list.Count - 1) - { - string shiftedText = direction == ShiftDirection.Down ? _list[index + 1] : _list[index - 1]; + // Get index with allowed rotation + index = GetShiftedIndex(index, direction == ShiftDirection.Down); + bool isUpper = char.IsUpper(match.Value[0]); - if (char.IsUpper(match.Value[0])) - { - shiftedText = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(shiftedText); - } + // If first letter was upper, assume the color should be from PascalCase list else use lowercase + string shiftedText = isUpper + ? _list[index] + : _listLower[index]; - result = new(match.Index, match.Length, shiftedText); - return true; - } + result = new(match.Index, match.Length, shiftedText); + return true; - return false; + static int GetShiftedIndex(int index, bool down) => (index + _list.Count + (down ? 1 : -1)) % _list.Count; } } } diff --git a/test/ColorNamesProviderTest.cs b/test/ColorNamesProviderTest.cs index 49618c1..0c28cab 100644 --- a/test/ColorNamesProviderTest.cs +++ b/test/ColorNamesProviderTest.cs @@ -16,7 +16,7 @@ public void Setup() [DataTestMethod] [DataRow("abc blue number", 6, "mediumblue", "royalblue")] - [DataRow("abc Azure number", 6, "Aliceblue", "Mintcream")] + [DataRow("abc Azure number", 6, "AliceBlue", "MintCream")] public void ColorNames(string textIn, int position, string down, string up) { bool successDown = _provider.TryShiftLine(textIn, position, ShiftDirection.Down, out ShiftResult resultDown);