Open SpeednetGroup opened 2 years ago
If you've already written this code in your hand rolled version, care to share it to save it being rewritten here
I do have some code that I can rip out, but be warned it is written in VB.NET. The code establishes a static Dictionary that holds color replacement regular expressions (for performance reasons) and then loops through them to check each possible color value.
The other thing I do is to be sure that all colors (both hex and names) are lower-case. There was a study a while back showing a slight performance increase and better network transmission times if you do this.
I'll try to pull out everything relevant from my code and paste below. It might be 12-15 years since I've looked at this code, so no guarantees. It has been in production all that time however.
Public Class CodeTransformation
Private Shared ReadOnly _CssColors As Dictionary(Of String, CssColorReplacement) = LoadColors()
Private Shared ReadOnly _Regex_CssRGBColors As New Regex("(?<=\W)rgb\((-?(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.\d*)?%?),(-?(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.\d*)?%?),(-?(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.\d*)?%?)\)(?=[^{}]*})", RegexOptions.IgnoreCase)
Private Shared ReadOnly _Regex_Css6HexTo3Hex As New Regex("(?<!filter:[^;]*)#([\da-f])\1([\da-f])\2([\da-f])\3(?=[^{}]*})", RegexOptions.IgnoreCase)
Private Shared ReadOnly _Regex_CssAllHex As New Regex("#([\da-f]{6}|[\da-f]{3})(?![\da-f])(?=[^{}]*})", RegexOptions.IgnoreCase)
Private Shared ReadOnly _Regex_CssToLowerCase As New Regex("(?<!(?:url|expression)\([^)]*|[.#][-\w]*|(?:font(?:-family)?|content):[^;}]*|\[[^\]]*)(?-i:[A-Z]+)", RegexOptions.IgnoreCase)
Private Shared ReadOnly _Regex_CssColorNamesToLower As New Regex("(?<=color:|background:|border(?:-top|-right|-bottom|-left)?:[^{}:;]*|outline:[^{}:;]*)(azure|beige|bisque|brown|coral|gold|gray|green|indigo|ivory|khaki|linen|maroon|navy|olive|orange|orchid|peru|pink|plum|purple|red|salmon|sienna|silver|snow|tan|teal|tomato|violet|wheat|aqua|blue|crimson|cyan|darkred|dimgray|hotpink|lime|oldlace|skyblue|thistle)(?![a-z])(?=[^{}]*})", RegexOptions.IgnoreCase)
Public Shared Function DeflateCss(ByVal source As String) As String
If (String.IsNullOrEmpty(source)) Then
Return String.Empty
End If
'***Only including relevant code here
_CSS_ShortCuts(source)
Return source
End Function
Private Shared Sub _CSS_ShortCuts(ByRef n As String)
'***Only including relevant code here
'Change RGB() colors to hex codes
If (_Regex_CssRGBColors.IsMatch(n)) Then
n = _Regex_CssRGBColors.Replace(n, AddressOf ConvertRGB)
End If
n = _Regex_Css6HexTo3Hex.Replace(n, "#$1$2$3") 'Change 6-digit colors to 3-digit (where possible)
For Each color As KeyValuePair(Of String, CssColorReplacement) In _CssColors
If (n.IndexOf(color.Key, StringComparison.OrdinalIgnoreCase) >= 0) Then
n = color.Value.RegexFind.Replace(n, color.Value.Replacement)
End If
Next
n = _Regex_CssAllHex.Replace(n, AddressOf ConvertLowerCase) 'Change all hex colors to lower case
'Convert everything that is not a URL, class name, id name, or font family to lower case
n = _Regex_CssToLowerCase.Replace(n, AddressOf ConvertLowerCase)
'Change color names to lower case. Must do this because leading # signs are ignored
'in global lower-case conversion to avoid converting id names, which can be case-sensitive
n = _Regex_CssColorNamesToLower.Replace(n, AddressOf ConvertLowerCase)
End Sub
Private Shared Function ConvertRGB(ByVal M As Match) As String
Dim sb As New StringBuilder("#", 7)
Dim s As String
Dim i As Integer
For x As Integer = 1 To 3
s = M.Groups(x).Value
If (s.EndsWith("%")) Then
i = Convert.ToInt32(Math.Round(255.0R * (Math.Min(100.0R, Math.Max(0.0R, Convert.ToDouble(s.LeftExcept(1)))) * 0.01R)))
Else
i = Convert.ToInt32(Math.Round(Math.Min(255.0R, Math.Max(0.0R, Convert.ToDouble(s)))))
End If
sb.Append(Conversion.Hex(i).PadLeft(2, "0"c).ToLower())
Next
Return sb.ToString()
End Function
Private Shared Function ConvertLowerCase(ByVal M As Match) As String
Return M.Value.ToLower()
End Function
Private Class CssColorReplacement
Public RegexFind As Regex
Public Replacement As String
Public Sub New(ByVal pRegexFind As Regex, ByVal pReplacement As String)
RegexFind = pRegexFind
Replacement = pReplacement
End Sub
End Class
Private Shared Function LoadColors() As Dictionary(Of String, CssColorReplacement)
Dim colors As New Dictionary(Of String, CssColorReplacement)
'Dim pre As String = "(?<=[ :(),])"
Dim pre As String = "(?<=color:|background:|border(?:-top|-right|-bottom|-left)?:[^{}:;]*|outline:[^{}:;]*)"
'Colors that are shorter as hex strings
colors.Add("aliceblue", New CssColorReplacement(New Regex(String.Concat(pre, "aliceblue"), RegexOptions.IgnoreCase), "#f0f8ff"))
colors.Add("antiquewhite", New CssColorReplacement(New Regex(String.Concat(pre, "antiquewhite"), RegexOptions.IgnoreCase), "#faebd7"))
colors.Add("aquamarine", New CssColorReplacement(New Regex(String.Concat(pre, "aquamarine"), RegexOptions.IgnoreCase), "#7fffd4"))
colors.Add("black", New CssColorReplacement(New Regex(String.Concat(pre, "black"), RegexOptions.IgnoreCase), "#000"))
colors.Add("blanchedalmond", New CssColorReplacement(New Regex(String.Concat(pre, "blanchedalmond"), RegexOptions.IgnoreCase), "#ffebcd"))
colors.Add("blueviolet", New CssColorReplacement(New Regex(String.Concat(pre, "blueviolet"), RegexOptions.IgnoreCase), "#8a2be2"))
colors.Add("burlywood", New CssColorReplacement(New Regex(String.Concat(pre, "burlywood"), RegexOptions.IgnoreCase), "#deb887"))
colors.Add("cadetblue", New CssColorReplacement(New Regex(String.Concat(pre, "cadetblue"), RegexOptions.IgnoreCase), "#5f9ea0"))
colors.Add("chartreuse", New CssColorReplacement(New Regex(String.Concat(pre, "chartreuse"), RegexOptions.IgnoreCase), "#7fff00"))
colors.Add("chocolate", New CssColorReplacement(New Regex(String.Concat(pre, "chocolate"), RegexOptions.IgnoreCase), "#d2691e"))
colors.Add("cornflowerblue", New CssColorReplacement(New Regex(String.Concat(pre, "cornflowerblue"), RegexOptions.IgnoreCase), "#6495ed"))
colors.Add("cornsilk", New CssColorReplacement(New Regex(String.Concat(pre, "cornsilk"), RegexOptions.IgnoreCase), "#fff8dc"))
colors.Add("darkblue", New CssColorReplacement(New Regex(String.Concat(pre, "darkblue"), RegexOptions.IgnoreCase), "#00008b"))
colors.Add("darkcyan", New CssColorReplacement(New Regex(String.Concat(pre, "darkcyan"), RegexOptions.IgnoreCase), "#008b8b"))
colors.Add("darkgoldenrod", New CssColorReplacement(New Regex(String.Concat(pre, "darkgoldenrod"), RegexOptions.IgnoreCase), "#b8860b"))
colors.Add("darkgray", New CssColorReplacement(New Regex(String.Concat(pre, "darkgray"), RegexOptions.IgnoreCase), "#a9a9a9"))
colors.Add("darkgreen", New CssColorReplacement(New Regex(String.Concat(pre, "darkgreen"), RegexOptions.IgnoreCase), "#006400"))
colors.Add("darkkhaki", New CssColorReplacement(New Regex(String.Concat(pre, "darkkhaki"), RegexOptions.IgnoreCase), "#bdb76b"))
colors.Add("darkmagenta", New CssColorReplacement(New Regex(String.Concat(pre, "darkmagenta"), RegexOptions.IgnoreCase), "#8b008b"))
colors.Add("darkolivegreen", New CssColorReplacement(New Regex(String.Concat(pre, "darkolivegreen"), RegexOptions.IgnoreCase), "#556b2f"))
colors.Add("darkorange", New CssColorReplacement(New Regex(String.Concat(pre, "darkorange"), RegexOptions.IgnoreCase), "#ff8c00"))
colors.Add("darkorchid", New CssColorReplacement(New Regex(String.Concat(pre, "darkorchid"), RegexOptions.IgnoreCase), "#9932cc"))
colors.Add("darksalmon", New CssColorReplacement(New Regex(String.Concat(pre, "darksalmon"), RegexOptions.IgnoreCase), "#e9967a"))
colors.Add("darkseagreen", New CssColorReplacement(New Regex(String.Concat(pre, "darkseagreen"), RegexOptions.IgnoreCase), "#8fbc8f"))
colors.Add("darkslateblue", New CssColorReplacement(New Regex(String.Concat(pre, "darkslateblue"), RegexOptions.IgnoreCase), "#483d8b"))
colors.Add("darkslategray", New CssColorReplacement(New Regex(String.Concat(pre, "darkslategray"), RegexOptions.IgnoreCase), "#2f4f4f"))
colors.Add("darkturquoise", New CssColorReplacement(New Regex(String.Concat(pre, "darkturquoise"), RegexOptions.IgnoreCase), "#00ced1"))
colors.Add("darkviolet", New CssColorReplacement(New Regex(String.Concat(pre, "darkviolet"), RegexOptions.IgnoreCase), "#9400d3"))
colors.Add("deeppink", New CssColorReplacement(New Regex(String.Concat(pre, "deeppink"), RegexOptions.IgnoreCase), "#ff1493"))
colors.Add("deepskyblue", New CssColorReplacement(New Regex(String.Concat(pre, "deepskyblue"), RegexOptions.IgnoreCase), "#00bfff"))
colors.Add("dodgerblue", New CssColorReplacement(New Regex(String.Concat(pre, "dodgerblue"), RegexOptions.IgnoreCase), "#1e90ff"))
colors.Add("firebrick", New CssColorReplacement(New Regex(String.Concat(pre, "firebrick"), RegexOptions.IgnoreCase), "#b22222"))
colors.Add("floralwhite", New CssColorReplacement(New Regex(String.Concat(pre, "floralwhite"), RegexOptions.IgnoreCase), "#fffaf0"))
colors.Add("forestgreen", New CssColorReplacement(New Regex(String.Concat(pre, "forestgreen"), RegexOptions.IgnoreCase), "#228b22"))
colors.Add("fuchsia", New CssColorReplacement(New Regex(String.Concat(pre, "fuchsia"), RegexOptions.IgnoreCase), "#f0f"))
colors.Add("gainsboro", New CssColorReplacement(New Regex(String.Concat(pre, "gainsboro"), RegexOptions.IgnoreCase), "#dcdcdc"))
colors.Add("ghostwhite", New CssColorReplacement(New Regex(String.Concat(pre, "ghostwhite"), RegexOptions.IgnoreCase), "#f8f8ff"))
colors.Add("greenyellow", New CssColorReplacement(New Regex(String.Concat(pre, "greenyellow"), RegexOptions.IgnoreCase), "#adff2f"))
colors.Add("honeydew", New CssColorReplacement(New Regex(String.Concat(pre, "honeydew"), RegexOptions.IgnoreCase), "#f0fff0"))
colors.Add("indianred", New CssColorReplacement(New Regex(String.Concat(pre, "indianred"), RegexOptions.IgnoreCase), "#cd5c5c"))
colors.Add("lavenderblush", New CssColorReplacement(New Regex(String.Concat(pre, "lavenderblush"), RegexOptions.IgnoreCase), "#fff0f5"))
colors.Add("lawngreen", New CssColorReplacement(New Regex(String.Concat(pre, "lawngreen"), RegexOptions.IgnoreCase), "#7cfc00"))
colors.Add("lemonchiffon", New CssColorReplacement(New Regex(String.Concat(pre, "lemonchiffon"), RegexOptions.IgnoreCase), "#fffacd"))
colors.Add("lightblue", New CssColorReplacement(New Regex(String.Concat(pre, "lightblue"), RegexOptions.IgnoreCase), "#add8e6"))
colors.Add("lightcoral", New CssColorReplacement(New Regex(String.Concat(pre, "lightcoral"), RegexOptions.IgnoreCase), "#f08080"))
colors.Add("lightcyan", New CssColorReplacement(New Regex(String.Concat(pre, "lightcyan"), RegexOptions.IgnoreCase), "#e0ffff"))
colors.Add("lightgoldenrodyellow", New CssColorReplacement(New Regex(String.Concat(pre, "lightgoldenrodyellow"), RegexOptions.IgnoreCase), "#fafad2"))
colors.Add("lightgreen", New CssColorReplacement(New Regex(String.Concat(pre, "lightgreen"), RegexOptions.IgnoreCase), "#90ee90"))
colors.Add("lightgrey", New CssColorReplacement(New Regex(String.Concat(pre, "lightgrey"), RegexOptions.IgnoreCase), "#d3d3d3"))
colors.Add("lightpink", New CssColorReplacement(New Regex(String.Concat(pre, "lightpink"), RegexOptions.IgnoreCase), "#ffb6c1"))
colors.Add("lightsalmon", New CssColorReplacement(New Regex(String.Concat(pre, "lightsalmon"), RegexOptions.IgnoreCase), "#ffa07a"))
colors.Add("lightseagreen", New CssColorReplacement(New Regex(String.Concat(pre, "lightseagreen"), RegexOptions.IgnoreCase), "#20b2aa"))
colors.Add("lightskyblue", New CssColorReplacement(New Regex(String.Concat(pre, "lightskyblue"), RegexOptions.IgnoreCase), "#87cefa"))
colors.Add("lightslategray", New CssColorReplacement(New Regex(String.Concat(pre, "lightslategray"), RegexOptions.IgnoreCase), "#789"))
colors.Add("lightsteelblue", New CssColorReplacement(New Regex(String.Concat(pre, "lightsteelblue"), RegexOptions.IgnoreCase), "#b0c4de"))
colors.Add("lightyellow", New CssColorReplacement(New Regex(String.Concat(pre, "lightyellow"), RegexOptions.IgnoreCase), "#ffffe0"))
colors.Add("limegreen", New CssColorReplacement(New Regex(String.Concat(pre, "limegreen"), RegexOptions.IgnoreCase), "#32cd32"))
colors.Add("mediumaquamarine", New CssColorReplacement(New Regex(String.Concat(pre, "mediumaquamarine"), RegexOptions.IgnoreCase), "#66cdaa"))
colors.Add("mediumblue", New CssColorReplacement(New Regex(String.Concat(pre, "mediumblue"), RegexOptions.IgnoreCase), "#0000cd"))
colors.Add("mediumorchid", New CssColorReplacement(New Regex(String.Concat(pre, "mediumorchid"), RegexOptions.IgnoreCase), "#ba55d3"))
colors.Add("mediumpurple", New CssColorReplacement(New Regex(String.Concat(pre, "mediumpurple"), RegexOptions.IgnoreCase), "#9370db"))
colors.Add("mediumseagreen", New CssColorReplacement(New Regex(String.Concat(pre, "mediumseagreen"), RegexOptions.IgnoreCase), "#3cb371"))
colors.Add("mediumslateblue", New CssColorReplacement(New Regex(String.Concat(pre, "mediumslateblue"), RegexOptions.IgnoreCase), "#7b68ee"))
colors.Add("mediumspringgreen", New CssColorReplacement(New Regex(String.Concat(pre, "mediumspringgreen"), RegexOptions.IgnoreCase), "#00fa9a"))
colors.Add("mediumturquoise", New CssColorReplacement(New Regex(String.Concat(pre, "mediumturquoise"), RegexOptions.IgnoreCase), "#48d1cc"))
colors.Add("mediumvioletred", New CssColorReplacement(New Regex(String.Concat(pre, "mediumvioletred"), RegexOptions.IgnoreCase), "#c71585"))
colors.Add("midnightblue", New CssColorReplacement(New Regex(String.Concat(pre, "midnightblue"), RegexOptions.IgnoreCase), "#191970"))
colors.Add("mintcream", New CssColorReplacement(New Regex(String.Concat(pre, "mintcream"), RegexOptions.IgnoreCase), "#f5fffa"))
colors.Add("mistyrose", New CssColorReplacement(New Regex(String.Concat(pre, "mistyrose"), RegexOptions.IgnoreCase), "#ffe4e1"))
colors.Add("moccasin", New CssColorReplacement(New Regex(String.Concat(pre, "moccasin"), RegexOptions.IgnoreCase), "#ffe4b5"))
colors.Add("navajowhite", New CssColorReplacement(New Regex(String.Concat(pre, "navajowhite"), RegexOptions.IgnoreCase), "#ffdead"))
colors.Add("navyblue", New CssColorReplacement(New Regex(String.Concat(pre, "navyblue"), RegexOptions.IgnoreCase), "#9fafdf"))
colors.Add("olivedrab", New CssColorReplacement(New Regex(String.Concat(pre, "olivedrab"), RegexOptions.IgnoreCase), "#6b8e23"))
colors.Add("orangered", New CssColorReplacement(New Regex(String.Concat(pre, "orangered"), RegexOptions.IgnoreCase), "#ff4500"))
colors.Add("palegoldenrod", New CssColorReplacement(New Regex(String.Concat(pre, "palegoldenrod"), RegexOptions.IgnoreCase), "#eee8aa"))
colors.Add("palegreen", New CssColorReplacement(New Regex(String.Concat(pre, "palegreen"), RegexOptions.IgnoreCase), "#98fb98"))
colors.Add("paleturquoise", New CssColorReplacement(New Regex(String.Concat(pre, "paleturquoise"), RegexOptions.IgnoreCase), "#afeeee"))
colors.Add("palevioletred", New CssColorReplacement(New Regex(String.Concat(pre, "palevioletred"), RegexOptions.IgnoreCase), "#db7093"))
colors.Add("papayawhip", New CssColorReplacement(New Regex(String.Concat(pre, "papayawhip"), RegexOptions.IgnoreCase), "#ffefd5"))
colors.Add("peachpuff", New CssColorReplacement(New Regex(String.Concat(pre, "peachpuff"), RegexOptions.IgnoreCase), "#ffdab9"))
colors.Add("powderblue", New CssColorReplacement(New Regex(String.Concat(pre, "powderblue"), RegexOptions.IgnoreCase), "#b0e0e6"))
colors.Add("rosybrown", New CssColorReplacement(New Regex(String.Concat(pre, "rosybrown"), RegexOptions.IgnoreCase), "#bc8f8f"))
colors.Add("royalblue", New CssColorReplacement(New Regex(String.Concat(pre, "royalblue"), RegexOptions.IgnoreCase), "#4169e1"))
colors.Add("saddlebrown", New CssColorReplacement(New Regex(String.Concat(pre, "saddlebrown"), RegexOptions.IgnoreCase), "#8b4513"))
colors.Add("sandybrown", New CssColorReplacement(New Regex(String.Concat(pre, "sandybrown"), RegexOptions.IgnoreCase), "#f4a460"))
colors.Add("seashell", New CssColorReplacement(New Regex(String.Concat(pre, "seashell"), RegexOptions.IgnoreCase), "#fff5ee"))
colors.Add("whitesmoke", New CssColorReplacement(New Regex(String.Concat(pre, "whitesmoke"), RegexOptions.IgnoreCase), "#f5f5f5"))
colors.Add("yellowgreen", New CssColorReplacement(New Regex(String.Concat(pre, "yellowgreen"), RegexOptions.IgnoreCase), "#9acd32"))
'Colors that are shorter as hex strings, but need to be substituted last because they are partial macthes for other color names
colors.Add("goldenrod", New CssColorReplacement(New Regex(String.Concat(pre, "goldenrod"), RegexOptions.IgnoreCase), "#daa520"))
colors.Add("lavender", New CssColorReplacement(New Regex(String.Concat(pre, "lavender"), RegexOptions.IgnoreCase), "#e6e6fa"))
colors.Add("magenta", New CssColorReplacement(New Regex(String.Concat(pre, "magenta"), RegexOptions.IgnoreCase), "#f0f"))
colors.Add("seagreen", New CssColorReplacement(New Regex(String.Concat(pre, "seagreen"), RegexOptions.IgnoreCase), "#2e8b57"))
colors.Add("slateblue", New CssColorReplacement(New Regex(String.Concat(pre, "slateblue"), RegexOptions.IgnoreCase), "#6a5acd"))
colors.Add("slategray", New CssColorReplacement(New Regex(String.Concat(pre, "slategray"), RegexOptions.IgnoreCase), "#708090"))
colors.Add("springgreen", New CssColorReplacement(New Regex(String.Concat(pre, "springgreen"), RegexOptions.IgnoreCase), "#00ff7f"))
colors.Add("steelblue", New CssColorReplacement(New Regex(String.Concat(pre, "steelblue"), RegexOptions.IgnoreCase), "#4682b4"))
colors.Add("turquoise", New CssColorReplacement(New Regex(String.Concat(pre, "turquoise"), RegexOptions.IgnoreCase), "#40e0d0"))
colors.Add("white", New CssColorReplacement(New Regex(String.Concat(pre, "white"), RegexOptions.IgnoreCase), "#fff"))
colors.Add("yellow", New CssColorReplacement(New Regex(String.Concat(pre, "yellow"), RegexOptions.IgnoreCase), "#ff0"))
'Colors that are shorter as CSS names
colors.Add("#f0ffff", New CssColorReplacement(New Regex(String.Concat(pre, "#f0ffff"), RegexOptions.IgnoreCase), "azure"))
colors.Add("#f5f5dc", New CssColorReplacement(New Regex(String.Concat(pre, "#f5f5dc"), RegexOptions.IgnoreCase), "beige"))
colors.Add("#ffe4c4", New CssColorReplacement(New Regex(String.Concat(pre, "#ffe4c4"), RegexOptions.IgnoreCase), "bisque"))
colors.Add("#a52a2a", New CssColorReplacement(New Regex(String.Concat(pre, "#a52a2a"), RegexOptions.IgnoreCase), "brown"))
colors.Add("#ff7f50", New CssColorReplacement(New Regex(String.Concat(pre, "#ff7f50"), RegexOptions.IgnoreCase), "coral"))
colors.Add("#ffd700", New CssColorReplacement(New Regex(String.Concat(pre, "#ffd700"), RegexOptions.IgnoreCase), "gold"))
colors.Add("#808080", New CssColorReplacement(New Regex(String.Concat(pre, "#808080"), RegexOptions.IgnoreCase), "gray"))
colors.Add("#008000", New CssColorReplacement(New Regex(String.Concat(pre, "#008000"), RegexOptions.IgnoreCase), "green"))
colors.Add("#4b0082", New CssColorReplacement(New Regex(String.Concat(pre, "#4b0082"), RegexOptions.IgnoreCase), "indigo"))
colors.Add("#fffff0", New CssColorReplacement(New Regex(String.Concat(pre, "#fffff0"), RegexOptions.IgnoreCase), "ivory"))
colors.Add("#f0e68c", New CssColorReplacement(New Regex(String.Concat(pre, "#f0e68c"), RegexOptions.IgnoreCase), "khaki"))
colors.Add("#faf0e6", New CssColorReplacement(New Regex(String.Concat(pre, "#faf0e6"), RegexOptions.IgnoreCase), "linen"))
colors.Add("#800000", New CssColorReplacement(New Regex(String.Concat(pre, "#800000"), RegexOptions.IgnoreCase), "maroon"))
colors.Add("#000080", New CssColorReplacement(New Regex(String.Concat(pre, "#000080"), RegexOptions.IgnoreCase), "navy"))
colors.Add("#808000", New CssColorReplacement(New Regex(String.Concat(pre, "#808000"), RegexOptions.IgnoreCase), "olive"))
colors.Add("#ffa500", New CssColorReplacement(New Regex(String.Concat(pre, "#ffa500"), RegexOptions.IgnoreCase), "orange"))
colors.Add("#da70d6", New CssColorReplacement(New Regex(String.Concat(pre, "#da70d6"), RegexOptions.IgnoreCase), "orchid"))
colors.Add("#cd853f", New CssColorReplacement(New Regex(String.Concat(pre, "#cd853f"), RegexOptions.IgnoreCase), "peru"))
colors.Add("#ffc0cb", New CssColorReplacement(New Regex(String.Concat(pre, "#ffc0cb"), RegexOptions.IgnoreCase), "pink"))
colors.Add("#dda0dd", New CssColorReplacement(New Regex(String.Concat(pre, "#dda0dd"), RegexOptions.IgnoreCase), "plum"))
colors.Add("#800080", New CssColorReplacement(New Regex(String.Concat(pre, "#800080"), RegexOptions.IgnoreCase), "purple"))
colors.Add("#f00", New CssColorReplacement(New Regex(String.Concat(pre, "#f00"), RegexOptions.IgnoreCase), "red"))
colors.Add("#fa8072", New CssColorReplacement(New Regex(String.Concat(pre, "#fa8072"), RegexOptions.IgnoreCase), "salmon"))
colors.Add("#a0522d", New CssColorReplacement(New Regex(String.Concat(pre, "#a0522d"), RegexOptions.IgnoreCase), "sienna"))
colors.Add("#c0c0c0", New CssColorReplacement(New Regex(String.Concat(pre, "#c0c0c0"), RegexOptions.IgnoreCase), "silver"))
colors.Add("#fffafa", New CssColorReplacement(New Regex(String.Concat(pre, "#fffafa"), RegexOptions.IgnoreCase), "snow"))
colors.Add("#d2b48c", New CssColorReplacement(New Regex(String.Concat(pre, "#d2b48c"), RegexOptions.IgnoreCase), "tan"))
colors.Add("#008080", New CssColorReplacement(New Regex(String.Concat(pre, "#008080"), RegexOptions.IgnoreCase), "teal"))
colors.Add("#ff6347", New CssColorReplacement(New Regex(String.Concat(pre, "#ff6347"), RegexOptions.IgnoreCase), "tomato"))
colors.Add("#ee82ee", New CssColorReplacement(New Regex(String.Concat(pre, "#ee82ee"), RegexOptions.IgnoreCase), "violet"))
colors.Add("#f5deb3", New CssColorReplacement(New Regex(String.Concat(pre, "#f5deb3"), RegexOptions.IgnoreCase), "wheat"))
'Colors that are only case-corrected
'aqua, blue, crimson, cyan, darkred, dimgray, hotpink, lime, oldlace, skyblue, thistle
Return colors
End Function
End Class
Please update the CSS optimizations in NUglify to optimize colors.
The shortest methods of including a color in CSS are either with the color name (
red
,blue
, etc.) or with a 6- or 3-digit hex string (#f0f0f0
,#e00
, etc.).All CSS colors, except those that include an opacity, should be converted to the shortest color represented either by name or 6- or 3-digit hex string. That would include colors defined by RBG notation (using specific numbers or percentages) and possibly HSL.
RGB is pretty simple to convert to a hex string, which can then be evaluated for possible shrinking to 3-digits or converted to the corresponding name.
Colors that are shorter as hex strings
aliceblue => #f0f8ff antiquewhite => #faebd7 aquamarine => #7fffd4 black => #000 blanchedalmond => #ffebcd blueviolet => #8a2be2 burlywood => #deb887 cadetblue => #5f9ea0 chartreuse => #7fff00 chocolate => #d2691e cornflowerblue => #6495ed cornsilk => #fff8dc darkblue => #00008b darkcyan => #008b8b darkgoldenrod => #b8860b darkgray => #a9a9a9 darkgreen => #006400 darkkhaki => #bdb76b darkmagenta => #8b008b darkolivegreen => #556b2f darkorange => #ff8c00 darkorchid => #9932cc darksalmon => #e9967a darkseagreen => #8fbc8f darkslateblue => #483d8b darkslategray => #2f4f4f darkturquoise => #00ced1 darkviolet => #9400d3 deeppink => #ff1493 deepskyblue => #00bfff dodgerblue => #1e90ff firebrick => #b22222 floralwhite => #fffaf0 forestgreen => #228b22 fuchsia => #f0f gainsboro => #dcdcdc ghostwhite => #f8f8ff greenyellow => #adff2f honeydew => #f0fff0 indianred => #cd5c5c lavenderblush => #fff0f5 lawngreen => #7cfc00 lemonchiffon => #fffacd lightblue => #add8e6 lightcoral => #f08080 lightcyan => #e0ffff lightgoldenrodyellow => #fafad2 lightgreen => #90ee90 lightgrey => #d3d3d3 lightpink => #ffb6c1 lightsalmon => #ffa07a lightseagreen => #20b2aa lightskyblue => #87cefa lightslategray => #789 lightsteelblue => #b0c4de lightyellow => #ffffe0 limegreen => #32cd32 mediumaquamarine => #66cdaa mediumblue => #0000cd mediumorchid => #ba55d3 mediumpurple => #9370db mediumseagreen => #3cb371 mediumslateblue => #7b68ee mediumspringgreen => #00fa9a mediumturquoise => #48d1cc mediumvioletred => #c71585 midnightblue => #191970 mintcream => #f5fffa mistyrose => #ffe4e1 moccasin => #ffe4b5 navajowhite => #ffdead navyblue => #9fafdf olivedrab => #6b8e23 orangered => #ff4500 palegoldenrod => #eee8aa palegreen => #98fb98 paleturquoise => #afeeee palevioletred => #db7093 papayawhip => #ffefd5 peachpuff => #ffdab9 powderblue => #b0e0e6 rosybrown => #bc8f8f royalblue => #4169e1 saddlebrown => #8b4513 sandybrown => #f4a460 seashell => #fff5ee whitesmoke => #f5f5f5 yellowgreen => #9acd32
Colors that are shorter as hex strings, but need to be substituted last because they are partial macthes for other color names
goldenrod => #daa520 lavender => #e6e6fa magenta => #f0f seagreen => #2e8b57 slateblue => #6a5acd slategray => #708090 springgreen => #00ff7f steelblue => #4682b4 turquoise => #40e0d0 white => #fff yellow => #ff0
Colors that are shorter as CSS names
f0ffff => azure
f5f5dc => beige
ffe4c4 => bisque
a52a2a => brown
ff7f50 => coral
ffd700 => gold
808080 => gray
008000 => green
4b0082 => indigo
fffff0 => ivory
f0e68c => khaki
faf0e6 => linen
800000 => maroon
# 000080 => navy
808000 => olive
ffa500 => orange
da70d6 => orchid
cd853f => peru
ffc0cb => pink
dda0dd => plum
800080 => purple
f00 => red
fa8072 => salmon
a0522d => sienna
c0c0c0 => silver
fffafa => snow
d2b48c => tan
008080 => teal
ff6347 => tomato
ee82ee => violet
f5deb3 => wheat