-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from lordfanger/master
ColorNames - allow rotation + fix upper casing
- Loading branch information
Showing
2 changed files
with
164 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters