Skip to content

Commit

Permalink
Merge pull request #10 from lordfanger/master
Browse files Browse the repository at this point in the history
ColorNames - allow rotation + fix upper casing
  • Loading branch information
lordfanger authored Dec 17, 2022
2 parents 8d8033a + 646750a commit 204a2bf
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 156 deletions.
318 changes: 163 additions & 155 deletions src/Providers/Implementations/ColorNamesProvider.cs
Original file line number Diff line number Diff line change
@@ -1,180 +1,188 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

namespace Shifter.Providers
{
public class ColorNamesProvider : RegexProviderBase
{
private readonly List<string> _list = new()
private static readonly List<string> _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<string> _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;

public override bool TryShiftSelection(Match match, ShiftDirection direction, out ShiftResult result)
{
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;
}
}
}
2 changes: 1 addition & 1 deletion test/ColorNamesProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 204a2bf

Please sign in to comment.