"""Color definitions and manipulation."""
# pylint: disable=too-many-lines

import re
from typing import List

from vpe import wrappers

# Regular expression to split a colour name into up to 5 words.
_R_LUMPY = re.compile('''(?x)
    ([A-Za-z][a-z0-9]+)
    ([A-Z][a-z0-9]+)?
    ([A-Z][a-z0-9]+)?
    ([A-Z][a-z0-9]+)?
    ([A-Z][a-z0-9]+)?
''')

# A lookup table of the color names to tuples of RGB string, number. The
# RGB values can be used directly to define GUI colors and the number values
# are suitable for 256 color terminals.
#
# I have tried to find an official list of 256 unique color names that
# correspond to the 256 xterm colors, but everything I find contains a lot of
# duplicate names. So a lot of the names below are of my own choosing,
# using Vim's own set of colors to replace duplicates.
_xterm_color_table = {
    'Black':             ('#000000', 0),
    'Maroon':            ('#800000', 1),
    'Green':             ('#008000', 2),
    'Olive':             ('#808000', 3),
    'Navy':              ('#000080', 4),
    'Purple':            ('#800080', 5),
    'Teal':              ('#008080', 6),
    'Silver':            ('#c0c0c0', 7),
    'Grey':              ('#808080', 8),
    'Red':               ('#ff0000', 9),
    'Lime':              ('#00ff00', 10),
    'Yellow':            ('#ffff00', 11),
    'Blue':              ('#0000ff', 12),
    'Fuchsia':           ('#ff00ff', 13),
    'Aqua':              ('#00ffff', 14),
    'White':             ('#ffffff', 15),
    'Grey0':             ('#000000', 16),
    'NavyBlue':          ('#00005f', 17),
    'DarkBlue':          ('#000087', 18),
    'MediumBlue':        ('#0000af', 19),
    'Blue2':             ('#0000d7', 20),
    'Blue1':             ('#0000ff', 21),
    'DarkGreen':         ('#005f00', 22),
    'DeepSkyBlue4':      ('#005f5f', 23),
    'DodgerBlue4':       ('#005f87', 24),
    'RoyalBlue3':        ('#005faf', 25),
    'DodgerBlue3':       ('#005fd7', 26),
    'DodgerBlue2':       ('#005fff', 27),
    'Green4':            ('#008700', 28),
    'SpringGreen4':      ('#00875f', 29),
    'Turquoise4':        ('#008787', 30),
    'Cyan4':             ('#0087af', 31),
    'DeepSkyBlue3':      ('#0087d7', 32),
    'DodgerBlue1':       ('#0087ff', 33),
    'Green3':            ('#00af00', 34),
    'SpringGreen3':      ('#00af5f', 35),
    'DarkCyan':          ('#00af87', 36),
    'LightSeaGreen':     ('#00afaf', 37),
    'DeepSkyBlue2':      ('#00afd7', 38),
    'DeepSkyBlue1':      ('#00afff', 39),
    'Green2':            ('#00d700', 40),
    'SpringGreen2':      ('#00d75f', 41),
    'SpringGreen':       ('#00d787', 42),
    'Cyan3':             ('#00d7af', 43),
    'DarkTurquoise':     ('#00d7d7', 44),
    'Turquoise2':        ('#00d7ff', 45),
    'Green1':            ('#00ff00', 46),
    'LimeGreen':         ('#00ff5f', 47),
    'SpringGreen1':      ('#00ff87', 48),
    'MediumSpringGreen': ('#00ffaf', 49),
    'Cyan2':             ('#00ffd7', 50),
    'Cyan1':             ('#00ffff', 51),
    'WebMaroon':         ('#5f0000', 52),
    'Indigo':            ('#5f005f', 53),
    'Purple4':           ('#5f0087', 54),
    'Magenta4':          ('#5f00af', 55),
    'Purple3':           ('#5f00d7', 56),
    'BlueViolet':        ('#5f00ff', 57),
    'Orange4':           ('#5f5f00', 58),
    'Grey37':            ('#5f5f5f', 59),
    'MediumPurple4':     ('#5f5f87', 60),
    'SlateBlue3':        ('#5f5faf', 61),
    'SlateBlue':         ('#5f5fd7', 62),
    'RoyalBlue1':        ('#5f5fff', 63),
    'Chartreuse4':       ('#5f8700', 64),
    'DarkSeaGreen4':     ('#5f875f', 65),
    'PaleTurquoise4':    ('#5f8787', 66),
    'SteelBlue':         ('#5f87af', 67),
    'SteelBlue3':        ('#5f87d7', 68),
    'CornflowerBlue':    ('#5f87ff', 69),
    'Chartreuse3':       ('#5faf00', 70),
    'MediumSeaGreen':    ('#5faf5f', 71),
    'CadetBlue':         ('#5faf87', 72),
    'MediumAquamarine':  ('#5fafaf', 73),
    'SkyBlue3':          ('#5fafd7', 74),
    'SteelBlue1':        ('#5fafff', 75),
    'Chartreuse2':       ('#5fd700', 76),
    'PaleGreen3':        ('#5fd75f', 77),
    'SeaGreen3':         ('#5fd787', 78),
    'Aquamarine3':       ('#5fd7af', 79),
    'MediumTurquoise':   ('#5fd7d7', 80),
    'LightSkyBlue':      ('#5fd7ff', 81),
    'LawnGreen':         ('#5fff00', 82),
    'SeaGreen2':         ('#5fff5f', 83),
    'SeaGreen1':         ('#5fff87', 84),
    'Aquamarine2':       ('#5fffaf', 85),
    'Aquamarine':        ('#5fffd7', 86),
    'DarkSlateGray2':    ('#5fffff', 87),
    'Red4':              ('#870000', 88),
    'DeepPink4':         ('#87005f', 89),
    'Maroon4':           ('#870087', 90),
    'DarkViolet':        ('#8700af', 91),
    'LightPink4':        ('#875f5f', 95),
    'Plum4':             ('#875f87', 96),
    'SlateBlue1':        ('#875fff', 99),
    'Wheat4':            ('#87875f', 101),
    'Grey53':            ('#878787', 102),
    'LightSlateGrey':    ('#8787af', 103),
    'MediumPurple':      ('#8787d7', 104),
    'LightSlateBlue':    ('#8787ff', 105),
    'DarkSeaGreen':      ('#87af87', 108),
    'SkyBlue2':          ('#87afff', 111),
    'DarkSlateGray3':    ('#87d7d7', 116),
    'SkyBlue1':          ('#87d7ff', 117),
    'Chartreuse1':       ('#87ff00', 118),
    'DarkSlateGray1':    ('#87ffff', 123),
    'MediumVioletRed':   ('#af0087', 126),
    'MediumOrchid3':     ('#af5faf', 133),
    'MediumOrchid':      ('#af5fd7', 134),
    'DarkGoldenrod':     ('#af8700', 136),
    'RosyBrown':         ('#af8787', 138),
    'Grey63':            ('#af87af', 139),
    'MediumPurple1':     ('#af87ff', 141),
    'DarkKhaki':         ('#afaf5f', 143),
    'NavajoWhite3':      ('#afaf87', 144),
    'Grey69':            ('#afafaf', 145),
    'LightSteelBlue3':   ('#afafd7', 146),
    'LightSteelBlue':    ('#afafff', 147),
    'LightCyan3':        ('#afd7d7', 152),
    'LightSkyBlue1':     ('#afd7ff', 153),
    'GreenYellow':       ('#afff00', 154),
    'DarkOliveGreen2':   ('#afff5f', 155),
    'PaleTurquoise1':    ('#afffff', 159),
    'HotPink2':          ('#d75faf', 169),
    'Orchid':            ('#d75fd7', 170),
    'Orange3':           ('#d78700', 172),
    'LightPink3':        ('#d78787', 174),
    'Pink3':             ('#d787af', 175),
    'Plum3':             ('#d787d7', 176),
    'Violet':            ('#d787ff', 177),
    'LightGoldenrod3':   ('#d7af5f', 179),
    'Tan':               ('#d7af87', 180),
    'MistyRose3':        ('#d7afaf', 181),
    'Thistle3':          ('#d7afd7', 182),
    'Plum2':             ('#d7afff', 183),
    'Khaki3':            ('#d7d75f', 185),
    'LightYellow3':      ('#d7d7af', 187),
    'Grey84':            ('#d7d7d7', 188),
    'LightSteelBlue1':   ('#d7d7ff', 189),
    'Yellow2':           ('#d7ff00', 190),
    'Honeydew2':         ('#d7ffd7', 194),
    'LightCyan1':        ('#d7ffff', 195),
    'Red1':              ('#ff0000', 196),
    'DeepPink2':         ('#ff005f', 197),
    'Magenta1':          ('#ff00ff', 201),
    'OrangeRed1':        ('#ff5f00', 202),
    'DarkOrange':        ('#ff8700', 208),
    'Salmon1':           ('#ff875f', 209),
    'LightCoral':        ('#ff8787', 210),
    'PaleVioletRed1':    ('#ff87af', 211),
    'Orchid2':           ('#ff87d7', 212),
    'Orchid1':           ('#ff87ff', 213),
    'Orange1':           ('#ffaf00', 214),
    'SandyBrown':        ('#ffaf5f', 215),
    'LightSalmon1':      ('#ffaf87', 216),
    'LightPink1':        ('#ffafaf', 217),
    'Pink1':             ('#ffafd7', 218),
    'Plum1':             ('#ffafff', 219),
    'Gold1':             ('#ffd700', 220),
    'NavajoWhite1':      ('#ffd7af', 223),
    'MistyRose1':        ('#ffd7d7', 224),
    'Thistle1':          ('#ffd7ff', 225),
    'Yellow1':           ('#ffff00', 226),
    'LightGoldenrod1':   ('#ffff5f', 227),
    'Khaki1':            ('#ffff87', 228),
    'Wheat1':            ('#ffffaf', 229),
    'Cornsilk1':         ('#ffffd7', 230),
    'Grey100':           ('#ffffff', 231),
    'Grey3':             ('#080808', 232),
    'Grey7':             ('#121212', 233),
    'Grey11':            ('#1c1c1c', 234),
    'Grey15':            ('#262626', 235),
    'Grey19':            ('#303030', 236),
    'Grey23':            ('#3a3a3a', 237),
    'Grey27':            ('#444444', 238),
    'Grey30':            ('#4e4e4e', 239),
    'Grey35':            ('#585858', 240),
    'Grey39':            ('#626262', 241),
    'Grey42':            ('#6c6c6c', 242),
    'Grey46':            ('#767676', 243),
    'Grey50':            ('#808080', 244),
    'Grey54':            ('#8a8a8a', 245),
    'Grey58':            ('#949494', 246),
    'Grey62':            ('#9e9e9e', 247),
    'Grey66':            ('#a8a8a8', 248),
    'Grey70':            ('#b2b2b2', 249),
    'Grey74':            ('#bcbcbc', 250),
    'Grey78':            ('#c6c6c6', 251),
    'Grey82':            ('#d0d0d0', 252),
    'Grey85':            ('#dadada', 253),
    'Grey89':            ('#e4e4e4', 254),
    'Grey93':            ('#eeeeee', 255),
}

# Additional colour supported by Vim in GUI mode or when termguicolors is set.
# Each entry contains the hex color code and a fallback name in the 256 color
# palette.
_extra_color_table = {
    'AliceBlue':            ('#f0f8ff', 'Grey100'),
    'AntiqueWhite':         ('#faebd7', 'Grey93'),
    'AntiqueWhite1':        ('#ffefdb', 'Cornsilk1'),
    'AntiqueWhite2':        ('#eedfcc', 'MistyRose1'),
    'AntiqueWhite3':        ('#cdc0b0', 'Silver'),
    'AntiqueWhite4':        ('#8b8378', 'Grey50'),
    'Aquamarine4':          ('#458b74', 'PaleTurquoise4'),
    'Azure':                ('#f0ffff', 'Grey100'),
    'Azure2':               ('#e0eeee', 'Grey93'),
    'Azure3':               ('#c1cdcd', 'Grey78'),
    'Azure4':               ('#838b8b', 'Grey54'),
    'Beige':                ('#f5f5dc', 'Cornsilk1'),
    'Bisque':               ('#ffe4c4', 'MistyRose1'),
    'Bisque2':              ('#eed5b7', 'NavajoWhite1'),
    'Bisque3':              ('#cdb79e', 'MistyRose3'),
    'Bisque4':              ('#8b7d6b', 'Wheat4'),
    'BlanchedAlmond':       ('#ffebcd', 'MistyRose1'),
    'Blue4':                ('#00008b', 'DarkBlue'),
    'Brown':                ('#a52a2a', 'Red4'),
    'Brown1':               ('#ff4040', 'OrangeRed1'),
    'Brown2':               ('#ee3b3b', 'OrangeRed1'),
    'Brown3':               ('#cd3333', 'OrangeRed1'),
    'Brown4':               ('#8b2323', 'Red4'),
    'Burlywood':            ('#deb887', 'Tan'),
    'Burlywood1':           ('#ffd39b', 'NavajoWhite1'),
    'Burlywood2':           ('#eec591', 'LightSalmon1'),
    'Burlywood3':           ('#cdaa7d', 'Tan'),
    'Burlywood4':           ('#8b7355', 'LightPink4'),
    'CadetBlue1':           ('#98f5ff', 'DarkSlateGray1'),
    'CadetBlue2':           ('#8ee5ee', 'SkyBlue1'),
    'CadetBlue3':           ('#7ac5cd', 'DarkSlateGray3'),
    'CadetBlue4':           ('#53868b', 'PaleTurquoise4'),
    'Chartreuse':           ('#7fff00', 'Chartreuse1'),
    'Chocolate':            ('#d2691e', 'Orange3'),
    'Chocolate1':           ('#ff7f24', 'DarkOrange'),
    'Chocolate2':           ('#ee7621', 'DarkOrange'),
    'Chocolate3':           ('#cd661d', 'Orange3'),
    'Coral':                ('#ff7f50', 'Salmon1'),
    'Coral1':               ('#ff7256', 'Salmon1'),
    'Coral2':               ('#ee6a50', 'Salmon1'),
    'Coral3':               ('#cd5b45', 'LightPink4'),
    'Coral4':               ('#8b3e2f', 'LightPink4'),
    'Cornsilk':             ('#fff8dc', 'Cornsilk1'),
    'Cornsilk2':            ('#eee8cd', 'Grey89'),
    'Cornsilk3':            ('#cdc8b1', 'Grey78'),
    'Cornsilk4':            ('#8b8878', 'Grey53'),
    'Crimson':              ('#dc143c', 'DeepPink2'),
    'Cyan':                 ('#00ffff', 'Cyan1'),
    'DarkGoldenrod1':       ('#ffb90f', 'Orange1'),
    'DarkGoldenrod2':       ('#eead0e', 'Orange1'),
    'DarkGoldenrod3':       ('#cd950c', 'Orange3'),
    'DarkGoldenrod4':       ('#8b6508', 'Olive'),
    'DarkGrey':             ('#a9a9a9', 'Grey66'),
    'DarkOliveGreen':       ('#556b2f', 'Grey35'),
    'DarkOliveGreen1':      ('#caff70', 'DarkOliveGreen2'),
    'DarkOliveGreen3':      ('#a2cd5a', 'DarkKhaki'),
    'DarkOliveGreen4':      ('#6e8b3d', 'DarkSeaGreen4'),
    'DarkOrange1':          ('#ff7f00', 'DarkOrange'),
    'DarkOrange2':          ('#ee7600', 'DarkOrange'),
    'DarkOrange3':          ('#cd6600', 'Orange3'),
    'DarkOrange4':          ('#8b4500', 'Orange4'),
    'DarkOrchid':           ('#9932cc', 'MediumOrchid'),
    'DarkOrchid1':          ('#bf3eff', 'MediumOrchid'),
    'DarkOrchid2':          ('#b23aee', 'MediumOrchid'),
    'DarkOrchid3':          ('#9a32cd', 'MediumOrchid'),
    'DarkOrchid4':          ('#68228b', 'Purple4'),
    'DarkSalmon':           ('#e9967a', 'LightPink3'),
    'DarkSeaGreen1':        ('#c1ffc1', 'Honeydew2'),
    'DarkSeaGreen2':        ('#b4eeb4', 'LightCyan3'),
    'DarkSeaGreen3':        ('#9bcd9b', 'Grey70'),
    'DarkSlateBlue':        ('#483d8b', 'MediumPurple4'),
    'DarkSlateGray':        ('#2f4f4f', 'Grey27'),
    'DarkSlateGray4':       ('#528b8b', 'PaleTurquoise4'),
    'DeepPink':             ('#ff1493', 'DeepPink2'),
    'DeepPink3':            ('#cd1076', 'MediumVioletRed'),
    'DeepSkyBlue':          ('#00bfff', 'DeepSkyBlue1'),
    'DimGray':              ('#696969', 'Grey42'),
    'DodgerBlue':           ('#1e90ff', 'DodgerBlue1'),
    'Firebrick':            ('#b22222', 'Red4'),
    'Firebrick1':           ('#ff3030', 'OrangeRed1'),
    'Firebrick2':           ('#ee2c2c', 'Red1'),
    'Firebrick3':           ('#cd2626', 'Red1'),
    'Firebrick4':           ('#8b1a1a', 'Red4'),
    'FloralWhite':          ('#fffaf0', 'Grey100'),
    'ForestGreen':          ('#228b22', 'Green4'),
    'Gainsboro':            ('#dcdcdc', 'Grey85'),
    'GhostWhite':           ('#f8f8ff', 'Grey100'),
    'Gold':                 ('#ffd700', 'Gold1'),
    'Gold2':                ('#eec900', 'Gold1'),
    'Gold3':                ('#cdad00', 'Orange3'),
    'Gold4':                ('#8b7500', 'Olive'),
    'Goldenrod':            ('#daa520', 'Orange3'),
    'Goldenrod1':           ('#ffc125', 'Orange1'),
    'Goldenrod2':           ('#eeb422', 'Orange1'),
    'Goldenrod3':           ('#cd9b1d', 'Orange3'),
    'Goldenrod4':           ('#8b6914', 'Olive'),
    'Gray':                 ('#bebebe', 'Grey74'),
    'Gray0':                ('#000000', 'Black'),
    'Gray1':                ('#030303', 'Grey0'),
    'Gray10':               ('#1a1a1a', 'Grey11'),
    'Gray100':              ('#ffffff', 'Grey100'),
    'Gray11':               ('#1c1c1c', 'Grey11'),
    'Gray12':               ('#1f1f1f', 'Grey11'),
    'Gray13':               ('#212121', 'Grey11'),
    'Gray14':               ('#242424', 'Grey15'),
    'Gray15':               ('#262626', 'Grey15'),
    'Gray16':               ('#292929', 'Grey15'),
    'Gray17':               ('#2b2b2b', 'Grey15'),
    'Gray18':               ('#2e2e2e', 'Grey19'),
    'Gray19':               ('#303030', 'Grey19'),
    'Gray2':                ('#050505', 'Grey3'),
    'Gray20':               ('#333333', 'Grey19'),
    'Gray21':               ('#363636', 'Grey23'),
    'Gray22':               ('#383838', 'Grey23'),
    'Gray23':               ('#3b3b3b', 'Grey23'),
    'Gray24':               ('#3d3d3d', 'Grey23'),
    'Gray25':               ('#404040', 'Grey27'),
    'Gray26':               ('#424242', 'Grey27'),
    'Gray27':               ('#454545', 'Grey27'),
    'Gray28':               ('#474747', 'Grey27'),
    'Gray29':               ('#4a4a4a', 'Grey30'),
    'Gray3':                ('#080808', 'Grey3'),
    'Gray30':               ('#4d4d4d', 'Grey30'),
    'Gray31':               ('#4f4f4f', 'Grey30'),
    'Gray32':               ('#525252', 'Grey30'),
    'Gray33':               ('#545454', 'Grey35'),
    'Gray34':               ('#575757', 'Grey35'),
    'Gray35':               ('#595959', 'Grey35'),
    'Gray36':               ('#5c5c5c', 'Grey37'),
    'Gray37':               ('#5e5e5e', 'Grey37'),
    'Gray38':               ('#616161', 'Grey39'),
    'Gray39':               ('#636363', 'Grey39'),
    'Gray4':                ('#0a0a0a', 'Grey3'),
    'Gray40':               ('#666666', 'Grey39'),
    'Gray42':               ('#6b6b6b', 'Grey42'),
    'Gray43':               ('#6e6e6e', 'Grey42'),
    'Gray44':               ('#707070', 'Grey42'),
    'Gray45':               ('#737373', 'Grey46'),
    'Gray46':               ('#757575', 'Grey46'),
    'Gray47':               ('#787878', 'Grey46'),
    'Gray48':               ('#7a7a7a', 'Grey46'),
    'Gray49':               ('#7d7d7d', 'Grey50'),
    'Gray5':                ('#0d0d0d', 'Grey3'),
    'Gray50':               ('#7f7f7f', 'Grey50'),
    'Gray51':               ('#828282', 'Grey50'),
    'Gray52':               ('#858585', 'Grey53'),
    'Gray53':               ('#878787', 'Grey53'),
    'Gray54':               ('#8a8a8a', 'Grey54'),
    'Gray55':               ('#8c8c8c', 'Grey54'),
    'Gray56':               ('#8f8f8f', 'Grey54'),
    'Gray57':               ('#919191', 'Grey58'),
    'Gray58':               ('#949494', 'Grey58'),
    'Gray59':               ('#969696', 'Grey58'),
    'Gray6':                ('#0f0f0f', 'Grey7'),
    'Gray60':               ('#999999', 'Grey58'),
    'Gray61':               ('#9c9c9c', 'Grey62'),
    'Gray62':               ('#9e9e9e', 'Grey62'),
    'Gray63':               ('#a1a1a1', 'Grey62'),
    'Gray64':               ('#a3a3a3', 'Grey62'),
    'Gray65':               ('#a6a6a6', 'Grey66'),
    'Gray66':               ('#a8a8a8', 'Grey66'),
    'Gray67':               ('#ababab', 'Grey66'),
    'Gray68':               ('#adadad', 'Grey69'),
    'Gray69':               ('#b0b0b0', 'Grey69'),
    'Gray7':                ('#121212', 'Grey7'),
    'Gray70':               ('#b3b3b3', 'Grey70'),
    'Gray71':               ('#b5b5b5', 'Grey70'),
    'Gray72':               ('#b8b8b8', 'Grey74'),
    'Gray73':               ('#bababa', 'Grey74'),
    'Gray74':               ('#bdbdbd', 'Grey74'),
    'Gray75':               ('#bfbfbf', 'Silver'),
    'Gray76':               ('#c2c2c2', 'Silver'),
    'Gray77':               ('#c4c4c4', 'Grey78'),
    'Gray78':               ('#c7c7c7', 'Grey78'),
    'Gray79':               ('#c9c9c9', 'Grey78'),
    'Gray8':                ('#141414', 'Grey7'),
    'Gray80':               ('#cccccc', 'Grey82'),
    'Gray81':               ('#cfcfcf', 'Grey82'),
    'Gray82':               ('#d1d1d1', 'Grey82'),
    'Gray83':               ('#d4d4d4', 'Grey84'),
    'Gray84':               ('#d6d6d6', 'Grey84'),
    'Gray85':               ('#d9d9d9', 'Grey85'),
    'Gray86':               ('#dbdbdb', 'Grey85'),
    'Gray87':               ('#dedede', 'Grey85'),
    'Gray88':               ('#e0e0e0', 'Grey89'),
    'Gray89':               ('#e3e3e3', 'Grey89'),
    'Gray9':                ('#171717', 'Grey7'),
    'Gray90':               ('#e5e5e5', 'Grey89'),
    'Gray91':               ('#e8e8e8', 'Grey89'),
    'Gray92':               ('#ebebeb', 'Grey93'),
    'Gray93':               ('#ededed', 'Grey93'),
    'Gray94':               ('#f0f0f0', 'Grey93'),
    'Gray95':               ('#f2f2f2', 'Grey93'),
    'Gray97':               ('#f7f7f7', 'Grey100'),
    'Gray98':               ('#fafafa', 'Grey100'),
    'Gray99':               ('#fcfcfc', 'Grey100'),
    'Honeydew':             ('#f0fff0', 'Grey100'),
    'Honeydew3':            ('#c1cdc1', 'Grey78'),
    'Honeydew4':            ('#838b83', 'Grey53'),
    'HotPink':              ('#ff69b4', 'PaleVioletRed1'),
    'HotPink1':             ('#ff6eb4', 'PaleVioletRed1'),
    'HotPink3':             ('#cd6090', 'HotPink2'),
    'HotPink4':             ('#8b3a62', 'LightPink4'),
    'IndianRed':            ('#cd5c5c', 'LightPink3'),
    'IndianRed1':           ('#ff6a6a', 'Salmon1'),
    'IndianRed2':           ('#ee6363', 'Salmon1'),
    'IndianRed3':           ('#cd5555', 'LightPink4'),
    'IndianRed4':           ('#8b3a3a', 'LightPink4'),
    'Ivory':                ('#fffff0', 'Grey100'),
    'Ivory2':               ('#eeeee0', 'Grey93'),
    'Ivory3':               ('#cdcdc1', 'Grey78'),
    'Ivory4':               ('#8b8b83', 'Grey54'),
    'Khaki':                ('#f0e68c', 'Khaki1'),
    'Khaki2':               ('#eee685', 'Khaki1'),
    'Khaki4':               ('#8b864e', 'Wheat4'),
    'Lavender':             ('#e6e6fa', 'Grey93'),
    'LavenderBlush':        ('#fff0f5', 'Grey93'),
    'LavenderBlush2':       ('#eee0e5', 'Grey89'),
    'LavenderBlush3':       ('#cdc1c5', 'Grey78'),
    'LavenderBlush4':       ('#8b8386', 'Grey53'),
    'LemonChiffon':         ('#fffacd', 'Cornsilk1'),
    'LemonChiffon2':        ('#eee9bf', 'NavajoWhite1'),
    'LemonChiffon3':        ('#cdc9a5', 'LightYellow3'),
    'LemonChiffon4':        ('#8b8970', 'Wheat4'),
    'LightBlue':            ('#add8e6', 'LightCyan3'),
    'LightBlue1':           ('#bfefff', 'PaleTurquoise1'),
    'LightBlue2':           ('#b2dfee', 'LightSkyBlue1'),
    'LightBlue3':           ('#9ac0cd', 'LightSteelBlue3'),
    'LightBlue4':           ('#68838b', 'PaleTurquoise4'),
    'LightCyan':            ('#e0ffff', 'LightCyan1'),
    'LightCyan2':           ('#d1eeee', 'Grey89'),
    'LightCyan4':           ('#7a8b8b', 'Grey53'),
    'LightGoldenrod':       ('#eedd82', 'Khaki3'),
    'LightGoldenrod2':      ('#eedc82', 'Khaki3'),
    'LightGoldenrod4':      ('#8b814c', 'Wheat4'),
    'LightGoldenrodYellow': ('#fafad2', 'Cornsilk1'),
    'LightGrey':            ('#d3d3d3', 'Grey82'),
    'LightPink':            ('#ffb6c1', 'LightPink1'),
    'LightPink2':           ('#eea2ad', 'LightPink1'),
    'LightSalmon':          ('#ffa07a', 'LightSalmon1'),
    'LightSalmon2':         ('#ee9572', 'Salmon1'),
    'LightSalmon3':         ('#cd8162', 'LightPink3'),
    'LightSalmon4':         ('#8b5742', 'LightPink4'),
    'LightSkyBlue2':        ('#a4d3ee', 'LightSkyBlue1'),
    'LightSkyBlue3':        ('#8db6cd', 'LightSteelBlue3'),
    'LightSkyBlue4':        ('#607b8b', 'PaleTurquoise4'),
    'LightSlateGray':       ('#778899', 'Grey53'),
    'LightSteelBlue2':      ('#bcd2ee', 'LightSkyBlue1'),
    'LightSteelBlue4':      ('#6e7b8b', 'PaleTurquoise4'),
    'LightYellow':          ('#ffffe0', 'Cornsilk1'),
    'LightYellow2':         ('#eeeed1', 'Grey89'),
    'LightYellow4':         ('#8b8b7a', 'Grey53'),
    'Linen':                ('#faf0e6', 'Grey93'),
    'Magenta':              ('#ff00ff', 'Magenta1'),
    'Magenta2':             ('#ee00ee', 'Magenta1'),
    'Magenta3':             ('#cd00cd', 'Magenta1'),
    'Maroon1':              ('#ff34b3', 'HotPink2'),
    'Maroon2':              ('#ee30a7', 'HotPink2'),
    'Maroon3':              ('#cd2990', 'MediumVioletRed'),
    'MediumOrchid1':        ('#e066ff', 'Orchid'),
    'MediumOrchid2':        ('#d15fee', 'Orchid'),
    'MediumOrchid4':        ('#7a378b', 'Plum4'),
    'MediumPurple2':        ('#9f79ee', 'MediumPurple1'),
    'MediumPurple3':        ('#8968cd', 'MediumPurple'),
    'MediumSlateBlue':      ('#7b68ee', 'SlateBlue1'),
    'MidnightBlue':         ('#191970', 'Navy'),
    'MintCream':            ('#f5fffa', 'Grey100'),
    'MistyRose':            ('#ffe4e1', 'MistyRose1'),
    'MistyRose2':           ('#eed5d2', 'MistyRose1'),
    'MistyRose4':           ('#8b7d7b', 'Grey50'),
    'Moccasin':             ('#ffe4b5', 'NavajoWhite1'),
    'NavajoWhite':          ('#ffdead', 'NavajoWhite1'),
    'NavajoWhite2':         ('#eecfa1', 'NavajoWhite1'),
    'NavajoWhite4':         ('#8b795e', 'Wheat4'),
    'OldLace':              ('#fdf5e6', 'Cornsilk1'),
    'OliveDrab':            ('#6b8e23', 'Chartreuse4'),
    'OliveDrab1':           ('#c0ff3e', 'DarkOliveGreen2'),
    'OliveDrab2':           ('#b3ee3a', 'DarkOliveGreen2'),
    'OliveDrab4':           ('#698b22', 'Chartreuse4'),
    'Orange':               ('#ffa500', 'Orange1'),
    'Orange2':              ('#ee9a00', 'DarkOrange'),
    'OrangeRed':            ('#ff4500', 'OrangeRed1'),
    'OrangeRed2':           ('#ee4000', 'OrangeRed1'),
    'OrangeRed3':           ('#cd3700', 'OrangeRed1'),
    'OrangeRed4':           ('#8b2500', 'Red4'),
    'Orchid3':              ('#cd69c9', 'Orchid'),
    'Orchid4':              ('#8b4789', 'Plum4'),
    'PaleGoldenrod':        ('#eee8aa', 'NavajoWhite1'),
    'PaleGreen':            ('#98fb98', 'SeaGreen1'),
    'PaleGreen1':           ('#9aff9a', 'DarkOliveGreen2'),
    'PaleGreen2':           ('#90ee90', 'SeaGreen1'),
    'PaleGreen4':           ('#548b54', 'DarkSeaGreen4'),
    'PaleTurquoise':        ('#afeeee', 'PaleTurquoise1'),
    'PaleTurquoise2':       ('#aeeeee', 'PaleTurquoise1'),
    'PaleTurquoise3':       ('#96cdcd', 'DarkSlateGray3'),
    'PaleVioletRed':        ('#db7093', 'LightPink3'),
    'PaleVioletRed2':       ('#ee799f', 'PaleVioletRed1'),
    'PaleVioletRed3':       ('#cd6889', 'HotPink2'),
    'PaleVioletRed4':       ('#8b475d', 'LightPink4'),
    'PapayaWhip':           ('#ffefd5', 'Cornsilk1'),
    'PeachPuff':            ('#ffdab9', 'NavajoWhite1'),
    'PeachPuff2':           ('#eecbad', 'NavajoWhite1'),
    'PeachPuff3':           ('#cdaf95', 'Tan'),
    'PeachPuff4':           ('#8b7765', 'Wheat4'),
    'Peru':                 ('#cd853f', 'Orange3'),
    'Pink':                 ('#ffc0cb', 'Pink1'),
    'Pink2':                ('#eea9b8', 'LightPink1'),
    'Pink4':                ('#8b636c', 'LightPink4'),
    'Plum':                 ('#dda0dd', 'Thistle3'),
    'PowderBlue':           ('#b0e0e6', 'LightCyan3'),
    'Purple':               ('#a020f0', 'BlueViolet'),
    'Purple1':              ('#9b30ff', 'SlateBlue1'),
    'Purple2':              ('#912cee', 'SlateBlue1'),
    'RebeccaPurple':        ('#663399', 'MediumPurple4'),
    'Red2':                 ('#ee0000', 'Red1'),
    'Red3':                 ('#cd0000', 'Red1'),
    'RosyBrown1':           ('#ffc1c1', 'LightPink1'),
    'RosyBrown2':           ('#eeb4b4', 'LightPink1'),
    'RosyBrown3':           ('#cd9b9b', 'LightPink3'),
    'RosyBrown4':           ('#8b6969', 'LightPink4'),
    'RoyalBlue':            ('#4169e1', 'SlateBlue'),
    'RoyalBlue2':           ('#436eee', 'RoyalBlue1'),
    'RoyalBlue4':           ('#27408b', 'DodgerBlue4'),
    'SaddleBrown':          ('#8b4513', 'Orange4'),
    'Salmon':               ('#fa8072', 'Salmon1'),
    'Salmon2':              ('#ee8262', 'Salmon1'),
    'Salmon3':              ('#cd7054', 'LightPink3'),
    'Salmon4':              ('#8b4c39', 'LightPink4'),
    'SeaGreen':             ('#2e8b57', 'SpringGreen4'),
    'Seashell':             ('#fff5ee', 'Grey100'),
    'Seashell2':            ('#eee5de', 'Grey89'),
    'Seashell3':            ('#cdc5bf', 'Grey78'),
    'Seashell4':            ('#8b8682', 'Grey53'),
    'Sienna':               ('#a0522d', 'LightPink4'),
    'Sienna1':              ('#ff8247', 'Salmon1'),
    'Sienna2':              ('#ee7942', 'Salmon1'),
    'Sienna3':              ('#cd6839', 'Orange3'),
    'Sienna4':              ('#8b4726', 'LightPink4'),
    'SkyBlue':              ('#87ceeb', 'DarkSlateGray3'),
    'SkyBlue4':             ('#4a708b', 'MediumPurple4'),
    'SlateBlue2':           ('#7a67ee', 'SlateBlue1'),
    'SlateBlue4':           ('#473c8b', 'MediumPurple4'),
    'SlateGray':            ('#708090', 'PaleTurquoise4'),
    'SlateGray1':           ('#c6e2ff', 'LightSteelBlue1'),
    'SlateGray2':           ('#b9d3ee', 'LightSkyBlue1'),
    'SlateGray3':           ('#9fb6cd', 'LightSteelBlue3'),
    'SlateGray4':           ('#6c7b8b', 'PaleTurquoise4'),
    'Snow':                 ('#fffafa', 'Grey100'),
    'Snow2':                ('#eee9e9', 'Grey93'),
    'Snow3':                ('#cdc9c9', 'Grey78'),
    'Snow4':                ('#8b8989', 'Grey54'),
    'SteelBlue2':           ('#5cacee', 'SteelBlue1'),
    'SteelBlue4':           ('#36648b', 'MediumPurple4'),
    'Tan1':                 ('#ffa54f', 'SandyBrown'),
    'Tan2':                 ('#ee9a49', 'Salmon1'),
    'Tan4':                 ('#8b5a2b', 'LightPink4'),
    'Thistle':              ('#d8bfd8', 'Thistle3'),
    'Thistle2':             ('#eed2ee', 'Thistle1'),
    'Thistle4':             ('#8b7b8b', 'Grey53'),
    'Tomato':               ('#ff6347', 'Salmon1'),
    'Tomato2':              ('#ee5c42', 'OrangeRed1'),
    'Tomato3':              ('#cd4f39', 'OrangeRed1'),
    'Tomato4':              ('#8b3626', 'LightPink4'),
    'Turquoise':            ('#40e0d0', 'MediumTurquoise'),
    'Turquoise1':           ('#00f5ff', 'Cyan1'),
    'Turquoise3':           ('#00c5cd', 'DarkTurquoise'),
    'VioletRed':            ('#d02090', 'MediumVioletRed'),
    'VioletRed1':           ('#ff3e96', 'HotPink2'),
    'VioletRed2':           ('#ee3a8c', 'HotPink2'),
    'VioletRed3':           ('#cd3278', 'MediumVioletRed'),
    'VioletRed4':           ('#8b2252', 'DeepPink4'),
    'WebGray':              ('#808080', 'Grey50'),
    'WebGreen':             ('#008000', 'Green'),
    'Wheat':                ('#f5deb3', 'NavajoWhite1'),
    'Wheat2':               ('#eed8ae', 'NavajoWhite1'),
    'Wheat3':               ('#cdba96', 'Tan'),
    'WhiteSmoke':           ('#f5f5f5', 'Grey93'),
    'X11Green':             ('#00ff00', 'Green1'),
    'X11Maroon':            ('#b03060', 'MediumVioletRed'),
    'Yellow3':              ('#cdcd00', 'Gold1'),
    'Yellow4':              ('#8b8b00', 'Olive'),
    'YellowGreen':          ('#9acd32', 'DarkKhaki'),
}

# A mapping from any defined color name to the one of the standard 16 colors.
_to_standard = {
    'AliceBlue':            'White',
    'AntiqueWhite':         'White',
    'AntiqueWhite1':        'White',
    'AntiqueWhite2':        'Silver',
    'AntiqueWhite3':        'Silver',
    'AntiqueWhite4':        'Grey',
    'Aqua':                 'Aqua',
    'Aquamarine':           'Aqua',
    'Aquamarine2':          'Aqua',
    'Aquamarine3':          'Silver',
    'Aquamarine4':          'Grey',
    'Azure':                'White',
    'Azure2':               'White',
    'Azure3':               'Silver',
    'Azure4':               'Grey',
    'Beige':                'White',
    'Bisque':               'White',
    'Bisque2':              'Silver',
    'Bisque3':              'Silver',
    'Bisque4':              'Grey',
    'Black':                'Black',
    'BlanchedAlmond':       'White',
    'Blue':                 'Blue',
    'Blue1':                'Blue',
    'Blue2':                'Blue',
    'Blue4':                'Navy',
    'BlueViolet':           'Blue',
    'Brown':                'Maroon',
    'Brown1':               'Red',
    'Brown2':               'Red',
    'Brown3':               'Red',
    'Brown4':               'Maroon',
    'Burlywood':            'Silver',
    'Burlywood1':           'Silver',
    'Burlywood2':           'Silver',
    'Burlywood3':           'Silver',
    'Burlywood4':           'Grey',
    'CadetBlue':            'Grey',
    'CadetBlue1':           'Silver',
    'CadetBlue2':           'Silver',
    'CadetBlue3':           'Silver',
    'CadetBlue4':           'Grey',
    'Chartreuse':           'Lime',
    'Chartreuse1':          'Yellow',
    'Chartreuse2':          'Lime',
    'Chartreuse3':          'Olive',
    'Chartreuse4':          'Olive',
    'Chocolate':            'Olive',
    'Chocolate1':           'Olive',
    'Chocolate2':           'Olive',
    'Chocolate3':           'Olive',
    'Coral':                'Grey',
    'Coral1':               'Grey',
    'Coral2':               'Grey',
    'Coral3':               'Grey',
    'Coral4':               'Maroon',
    'CornflowerBlue':       'Grey',
    'Cornsilk':             'White',
    'Cornsilk1':            'White',
    'Cornsilk2':            'White',
    'Cornsilk3':            'Silver',
    'Cornsilk4':            'Grey',
    'Crimson':              'Red',
    'Cyan':                 'Aqua',
    'Cyan1':                'Aqua',
    'Cyan2':                'Aqua',
    'Cyan3':                'Aqua',
    'Cyan4':                'Teal',
    'DarkBlue':             'Navy',
    'DarkCyan':             'Teal',
    'DarkGoldenrod':        'Olive',
    'DarkGoldenrod1':       'Yellow',
    'DarkGoldenrod2':       'Yellow',
    'DarkGoldenrod3':       'Olive',
    'DarkGoldenrod4':       'Olive',
    'DarkGreen':            'Green',
    'DarkGrey':             'Silver',
    'DarkKhaki':            'Grey',
    'DarkOliveGreen':       'Olive',
    'DarkOliveGreen1':      'Silver',
    'DarkOliveGreen2':      'Yellow',
    'DarkOliveGreen3':      'Silver',
    'DarkOliveGreen4':      'Olive',
    'DarkOrange':           'Olive',
    'DarkOrange1':          'Olive',
    'DarkOrange2':          'Olive',
    'DarkOrange3':          'Olive',
    'DarkOrange4':          'Olive',
    'DarkOrchid':           'Purple',
    'DarkOrchid1':          'Fuchsia',
    'DarkOrchid2':          'Fuchsia',
    'DarkOrchid3':          'Purple',
    'DarkOrchid4':          'Purple',
    'DarkSalmon':           'Silver',
    'DarkSeaGreen':         'Grey',
    'DarkSeaGreen1':        'Silver',
    'DarkSeaGreen2':        'Silver',
    'DarkSeaGreen3':        'Silver',
    'DarkSeaGreen4':        'Grey',
    'DarkSlateBlue':        'Purple',
    'DarkSlateGray':        'Teal',
    'DarkSlateGray1':       'Silver',
    'DarkSlateGray2':       'Aqua',
    'DarkSlateGray3':       'Silver',
    'DarkSlateGray4':       'Grey',
    'DarkTurquoise':        'Aqua',
    'DarkViolet':           'Purple',
    'DeepPink':             'Fuchsia',
    'DeepPink2':            'Red',
    'DeepPink3':            'Purple',
    'DeepPink4':            'Purple',
    'DeepSkyBlue':          'Aqua',
    'DeepSkyBlue1':         'Aqua',
    'DeepSkyBlue2':         'Aqua',
    'DeepSkyBlue3':         'Teal',
    'DeepSkyBlue4':         'Teal',
    'DimGray':              'Grey',
    'DodgerBlue':           'Teal',
    'DodgerBlue1':          'Teal',
    'DodgerBlue2':          'Blue',
    'DodgerBlue3':          'Teal',
    'DodgerBlue4':          'Teal',
    'Firebrick':            'Maroon',
    'Firebrick1':           'Red',
    'Firebrick2':           'Red',
    'Firebrick3':           'Red',
    'Firebrick4':           'Maroon',
    'FloralWhite':          'White',
    'ForestGreen':          'Green',
    'Fuchsia':              'Fuchsia',
    'Gainsboro':            'Silver',
    'GhostWhite':           'White',
    'Gold':                 'Yellow',
    'Gold1':                'Yellow',
    'Gold2':                'Yellow',
    'Gold3':                'Olive',
    'Gold4':                'Olive',
    'Goldenrod':            'Olive',
    'Goldenrod1':           'Yellow',
    'Goldenrod2':           'Yellow',
    'Goldenrod3':           'Olive',
    'Goldenrod4':           'Olive',
    'Gray':                 'Silver',
    'Gray0':                'Black',
    'Gray1':                'Black',
    'Gray10':               'Black',
    'Gray100':              'White',
    'Gray11':               'Black',
    'Gray12':               'Black',
    'Gray13':               'Black',
    'Gray14':               'Black',
    'Gray15':               'Black',
    'Gray16':               'Black',
    'Gray17':               'Black',
    'Gray18':               'Black',
    'Gray19':               'Black',
    'Gray2':                'Black',
    'Gray20':               'Black',
    'Gray21':               'Black',
    'Gray22':               'Black',
    'Gray23':               'Black',
    'Gray24':               'Black',
    'Gray25':               'Black',
    'Gray26':               'Grey',
    'Gray27':               'Grey',
    'Gray28':               'Grey',
    'Gray29':               'Grey',
    'Gray3':                'Black',
    'Gray30':               'Grey',
    'Gray31':               'Grey',
    'Gray32':               'Grey',
    'Gray33':               'Grey',
    'Gray34':               'Grey',
    'Gray35':               'Grey',
    'Gray36':               'Grey',
    'Gray37':               'Grey',
    'Gray38':               'Grey',
    'Gray39':               'Grey',
    'Gray4':                'Black',
    'Gray40':               'Grey',
    'Gray42':               'Grey',
    'Gray43':               'Grey',
    'Gray44':               'Grey',
    'Gray45':               'Grey',
    'Gray46':               'Grey',
    'Gray47':               'Grey',
    'Gray48':               'Grey',
    'Gray49':               'Grey',
    'Gray5':                'Black',
    'Gray50':               'Grey',
    'Gray51':               'Grey',
    'Gray52':               'Grey',
    'Gray53':               'Grey',
    'Gray54':               'Grey',
    'Gray55':               'Grey',
    'Gray56':               'Grey',
    'Gray57':               'Grey',
    'Gray58':               'Grey',
    'Gray59':               'Grey',
    'Gray6':                'Black',
    'Gray60':               'Grey',
    'Gray61':               'Grey',
    'Gray62':               'Grey',
    'Gray63':               'Silver',
    'Gray64':               'Silver',
    'Gray65':               'Silver',
    'Gray66':               'Silver',
    'Gray67':               'Silver',
    'Gray68':               'Silver',
    'Gray69':               'Silver',
    'Gray7':                'Black',
    'Gray70':               'Silver',
    'Gray71':               'Silver',
    'Gray72':               'Silver',
    'Gray73':               'Silver',
    'Gray74':               'Silver',
    'Gray75':               'Silver',
    'Gray76':               'Silver',
    'Gray77':               'Silver',
    'Gray78':               'Silver',
    'Gray79':               'Silver',
    'Gray8':                'Black',
    'Gray80':               'Silver',
    'Gray81':               'Silver',
    'Gray82':               'Silver',
    'Gray83':               'Silver',
    'Gray84':               'Silver',
    'Gray85':               'Silver',
    'Gray86':               'Silver',
    'Gray87':               'Silver',
    'Gray88':               'White',
    'Gray89':               'White',
    'Gray9':                'Black',
    'Gray90':               'White',
    'Gray91':               'White',
    'Gray92':               'White',
    'Gray93':               'White',
    'Gray94':               'White',
    'Gray95':               'White',
    'Gray97':               'White',
    'Gray98':               'White',
    'Gray99':               'White',
    'Green':                'Green',
    'Green1':               'Lime',
    'Green2':               'Lime',
    'Green3':               'Green',
    'Green4':               'Green',
    'GreenYellow':          'Yellow',
    'Grey':                 'Grey',
    'Grey0':                'Black',
    'Grey100':              'White',
    'Grey11':               'Black',
    'Grey15':               'Black',
    'Grey19':               'Black',
    'Grey23':               'Black',
    'Grey27':               'Grey',
    'Grey3':                'Black',
    'Grey30':               'Grey',
    'Grey35':               'Grey',
    'Grey37':               'Grey',
    'Grey39':               'Grey',
    'Grey42':               'Grey',
    'Grey46':               'Grey',
    'Grey50':               'Grey',
    'Grey53':               'Grey',
    'Grey54':               'Grey',
    'Grey58':               'Grey',
    'Grey62':               'Grey',
    'Grey63':               'Grey',
    'Grey66':               'Silver',
    'Grey69':               'Silver',
    'Grey7':                'Black',
    'Grey70':               'Silver',
    'Grey74':               'Silver',
    'Grey78':               'Silver',
    'Grey82':               'Silver',
    'Grey84':               'Silver',
    'Grey85':               'Silver',
    'Grey89':               'White',
    'Grey93':               'White',
    'Honeydew':             'White',
    'Honeydew2':            'White',
    'Honeydew3':            'Silver',
    'Honeydew4':            'Grey',
    'HotPink':              'Silver',
    'HotPink1':             'Silver',
    'HotPink2':             'Grey',
    'HotPink3':             'Grey',
    'HotPink4':             'Purple',
    'IndianRed':            'Grey',
    'IndianRed1':           'Grey',
    'IndianRed2':           'Grey',
    'IndianRed3':           'Grey',
    'IndianRed4':           'Maroon',
    'Indigo':               'Purple',
    'Ivory':                'White',
    'Ivory2':               'White',
    'Ivory3':               'Silver',
    'Ivory4':               'Grey',
    'Khaki':                'Silver',
    'Khaki1':               'White',
    'Khaki2':               'Silver',
    'Khaki3':               'Silver',
    'Khaki4':               'Grey',
    'Lavender':             'White',
    'LavenderBlush':        'White',
    'LavenderBlush2':       'White',
    'LavenderBlush3':       'Silver',
    'LavenderBlush4':       'Grey',
    'LawnGreen':            'Lime',
    'LemonChiffon':         'White',
    'LemonChiffon2':        'White',
    'LemonChiffon3':        'Silver',
    'LemonChiffon4':        'Grey',
    'LightBlue':            'Silver',
    'LightBlue1':           'White',
    'LightBlue2':           'Silver',
    'LightBlue3':           'Silver',
    'LightBlue4':           'Grey',
    'LightCoral':           'Silver',
    'LightCyan':            'White',
    'LightCyan1':           'White',
    'LightCyan2':           'White',
    'LightCyan3':           'Silver',
    'LightCyan4':           'Grey',
    'LightGoldenrod':       'Silver',
    'LightGoldenrod1':      'Yellow',
    'LightGoldenrod2':      'Silver',
    'LightGoldenrod3':      'Silver',
    'LightGoldenrod4':      'Grey',
    'LightGoldenrodYellow': 'White',
    'LightGrey':            'Silver',
    'LightPink':            'Silver',
    'LightPink1':           'Silver',
    'LightPink2':           'Silver',
    'LightPink3':           'Grey',
    'LightPink4':           'Grey',
    'LightSalmon':          'Silver',
    'LightSalmon1':         'Silver',
    'LightSalmon2':         'Silver',
    'LightSalmon3':         'Grey',
    'LightSalmon4':         'Grey',
    'LightSeaGreen':        'Teal',
    'LightSkyBlue':         'Aqua',
    'LightSkyBlue1':        'Silver',
    'LightSkyBlue2':        'Silver',
    'LightSkyBlue3':        'Silver',
    'LightSkyBlue4':        'Grey',
    'LightSlateBlue':       'Silver',
    'LightSlateGray':       'Grey',
    'LightSlateGrey':       'Grey',
    'LightSteelBlue':       'Silver',
    'LightSteelBlue1':      'White',
    'LightSteelBlue2':      'Silver',
    'LightSteelBlue3':      'Silver',
    'LightSteelBlue4':      'Grey',
    'LightYellow':          'White',
    'LightYellow2':         'White',
    'LightYellow3':         'Silver',
    'LightYellow4':         'Grey',
    'Lime':                 'Lime',
    'LimeGreen':            'Lime',
    'Linen':                'White',
    'Magenta':              'Fuchsia',
    'Magenta1':             'Fuchsia',
    'Magenta2':             'Fuchsia',
    'Magenta3':             'Fuchsia',
    'Magenta4':             'Purple',
    'Maroon':               'Maroon',
    'Maroon1':              'Fuchsia',
    'Maroon2':              'Fuchsia',
    'Maroon3':              'Purple',
    'Maroon4':              'Purple',
    'MediumAquamarine':     'Grey',
    'MediumBlue':           'Navy',
    'MediumOrchid':         'Grey',
    'MediumOrchid1':        'Silver',
    'MediumOrchid2':        'Fuchsia',
    'MediumOrchid3':        'Grey',
    'MediumOrchid4':        'Purple',
    'MediumPurple':         'Grey',
    'MediumPurple1':        'Silver',
    'MediumPurple2':        'Silver',
    'MediumPurple3':        'Grey',
    'MediumPurple4':        'Grey',
    'MediumSeaGreen':       'Grey',
    'MediumSlateBlue':      'Grey',
    'MediumSpringGreen':    'Aqua',
    'MediumTurquoise':      'Silver',
    'MediumVioletRed':      'Purple',
    'MidnightBlue':         'Navy',
    'MintCream':            'White',
    'MistyRose':            'White',
    'MistyRose1':           'White',
    'MistyRose2':           'Silver',
    'MistyRose3':           'Silver',
    'MistyRose4':           'Grey',
    'Moccasin':             'White',
    'NavajoWhite':          'Silver',
    'NavajoWhite1':         'Silver',
    'NavajoWhite2':         'Silver',
    'NavajoWhite3':         'Silver',
    'NavajoWhite4':         'Grey',
    'Navy':                 'Navy',
    'NavyBlue':             'Navy',
    'OldLace':              'White',
    'Olive':                'Olive',
    'OliveDrab':            'Olive',
    'OliveDrab1':           'Yellow',
    'OliveDrab2':           'Yellow',
    'OliveDrab4':           'Olive',
    'Orange':               'Yellow',
    'Orange1':              'Yellow',
    'Orange2':              'Olive',
    'Orange3':              'Olive',
    'Orange4':              'Olive',
    'OrangeRed':            'Red',
    'OrangeRed1':           'Red',
    'OrangeRed2':           'Red',
    'OrangeRed3':           'Red',
    'OrangeRed4':           'Maroon',
    'Orchid':               'Silver',
    'Orchid1':              'Silver',
    'Orchid2':              'Silver',
    'Orchid3':              'Grey',
    'Orchid4':              'Grey',
    'PaleGoldenrod':        'Silver',
    'PaleGreen':            'Silver',
    'PaleGreen1':           'Silver',
    'PaleGreen2':           'Silver',
    'PaleGreen3':           'Grey',
    'PaleGreen4':           'Grey',
    'PaleTurquoise':        'Silver',
    'PaleTurquoise1':       'White',
    'PaleTurquoise2':       'Silver',
    'PaleTurquoise3':       'Silver',
    'PaleTurquoise4':       'Grey',
    'PaleVioletRed':        'Grey',
    'PaleVioletRed1':       'Silver',
    'PaleVioletRed2':       'Silver',
    'PaleVioletRed3':       'Grey',
    'PaleVioletRed4':       'Grey',
    'PapayaWhip':           'White',
    'PeachPuff':            'Silver',
    'PeachPuff2':           'Silver',
    'PeachPuff3':           'Silver',
    'PeachPuff4':           'Grey',
    'Peru':                 'Olive',
    'Pink':                 'Silver',
    'Pink1':                'Silver',
    'Pink2':                'Silver',
    'Pink3':                'Silver',
    'Pink4':                'Grey',
    'Plum':                 'Silver',
    'Plum1':                'Silver',
    'Plum2':                'Silver',
    'Plum3':                'Silver',
    'Plum4':                'Grey',
    'PowderBlue':           'Silver',
    'Purple':               'Fuchsia',
    'Purple1':              'Fuchsia',
    'Purple2':              'Purple',
    'Purple3':              'Purple',
    'Purple4':              'Purple',
    'RebeccaPurple':        'Purple',
    'Red':                  'Red',
    'Red1':                 'Red',
    'Red2':                 'Red',
    'Red3':                 'Red',
    'Red4':                 'Maroon',
    'RosyBrown':            'Grey',
    'RosyBrown1':           'Silver',
    'RosyBrown2':           'Silver',
    'RosyBrown3':           'Silver',
    'RosyBrown4':           'Grey',
    'RoyalBlue':            'Grey',
    'RoyalBlue1':           'Grey',
    'RoyalBlue2':           'Grey',
    'RoyalBlue3':           'Teal',
    'RoyalBlue4':           'Navy',
    'SaddleBrown':          'Olive',
    'Salmon':               'Silver',
    'Salmon1':              'Silver',
    'Salmon2':              'Grey',
    'Salmon3':              'Grey',
    'Salmon4':              'Olive',
    'SandyBrown':           'Silver',
    'SeaGreen':             'Teal',
    'SeaGreen1':            'Silver',
    'SeaGreen2':            'Lime',
    'SeaGreen3':            'Grey',
    'Seashell':             'White',
    'Seashell2':            'White',
    'Seashell3':            'Silver',
    'Seashell4':            'Grey',
    'Sienna':               'Olive',
    'Sienna1':              'Grey',
    'Sienna2':              'Grey',
    'Sienna3':              'Olive',
    'Sienna4':              'Olive',
    'Silver':               'Silver',
    'SkyBlue':              'Silver',
    'SkyBlue1':             'Silver',
    'SkyBlue2':             'Silver',
    'SkyBlue3':             'Silver',
    'SkyBlue4':             'Grey',
    'SlateBlue':            'Grey',
    'SlateBlue1':           'Grey',
    'SlateBlue2':           'Grey',
    'SlateBlue3':           'Grey',
    'SlateBlue4':           'Purple',
    'SlateGray':            'Grey',
    'SlateGray1':           'White',
    'SlateGray2':           'Silver',
    'SlateGray3':           'Silver',
    'SlateGray4':           'Grey',
    'Snow':                 'White',
    'Snow2':                'White',
    'Snow3':                'Silver',
    'Snow4':                'Grey',
    'SpringGreen':          'Teal',
    'SpringGreen1':         'Aqua',
    'SpringGreen2':         'Lime',
    'SpringGreen3':         'Teal',
    'SpringGreen4':         'Teal',
    'SteelBlue':            'Grey',
    'SteelBlue1':           'Silver',
    'SteelBlue2':           'Silver',
    'SteelBlue3':           'Grey',
    'SteelBlue4':           'Teal',
    'Tan':                  'Silver',
    'Tan1':                 'Silver',
    'Tan2':                 'Silver',
    'Tan4':                 'Olive',
    'Teal':                 'Teal',
    'Thistle':              'Silver',
    'Thistle1':             'White',
    'Thistle2':             'White',
    'Thistle3':             'Silver',
    'Thistle4':             'Grey',
    'Tomato':               'Red',
    'Tomato2':              'Red',
    'Tomato3':              'Olive',
    'Tomato4':              'Maroon',
    'Turquoise':            'Aqua',
    'Turquoise1':           'Aqua',
    'Turquoise2':           'Aqua',
    'Turquoise3':           'Aqua',
    'Turquoise4':           'Teal',
    'Violet':               'Silver',
    'VioletRed':            'Purple',
    'VioletRed1':           'Fuchsia',
    'VioletRed2':           'Fuchsia',
    'VioletRed3':           'Purple',
    'VioletRed4':           'Purple',
    'WebGray':              'Grey',
    'WebGreen':             'Green',
    'WebMaroon':            'Maroon',
    'Wheat':                'Silver',
    'Wheat1':               'White',
    'Wheat2':               'Silver',
    'Wheat3':               'Silver',
    'Wheat4':               'Grey',
    'White':                'White',
    'WhiteSmoke':           'White',
    'X11Green':             'Lime',
    'X11Maroon':            'Purple',
    'Yellow':               'Yellow',
    'Yellow1':              'Yellow',
    'Yellow2':              'Yellow',
    'Yellow3':              'Yellow',
    'Yellow4':              'Olive',
    'YellowGreen':          'Olive',
}


def _alt_names(name: str) -> List[str]:
    """Work out possible alternative names for a given color.

    :name:   The color name.
    :return: A list of alternative names, which will typically include the
             suppied name. The returned list is sorted.
    """
    if ' ' in name:
        words = name.split()
    else:
        m = _R_LUMPY.match(name)
        if m:
            words = [g for g in m.groups() if g]
        else:                                                # pragma: no cover
            words = [name]

    words_1 = [w.lower() for w in words]
    words_2 = [w.replace('gray', 'grey') for w in words]
    words_3 = [w.replace('grey', 'gray') for w in words]

    alternatives = set()
    for words in (words_1, words_2, words_3):
        alternatives.add(' '.join(words))
        alternatives.add(''.join(w.capitalize() for w in words))
    return sorted(alternatives)


def well_defined_name(name: str) -> str:
    """Convert a color name to a well defined form.

    The well defined form is the same as the first entry, without spaces,
    obtained using `alt_name'. This can be used to present consistent color
    names to a user, but is not based on any 'standard'.

    :name:   The color name.
    :return: The name converted to a well defined form. If the name is unknown
             then 'Black' is returned.
    """
    #-for alt_name in _alt_names(name):
    #-    if ' ' not in alt_name:
    #-        if (alt_name in _xterm_color_table
    #-                or alt_name in _extra_color_table):
    #-            return alt_name
    for alt_name in _alt_names(name):
        if ' ' not in alt_name:
            return alt_name

    return 'Black'                                           # pragma: no cover


def to_256_num(name: str):
    """Convert a colour name to a best match 256 color terminal number.

    The provided name is converted to a well defined name and then the 'best'
    match within the 256 color pallette is chosen.

    :name: The color to convert.
    """
    name = well_defined_name(name)
    if name not in _xterm_color_table:
        _, name = _extra_color_table.get(name, (None, name))

    if name not in _xterm_color_table:
        # A naughty fallback in the case of a bug!
        return 0                                             # pragma: no cover

    return _xterm_color_table[name][1]


def _to_std_num(name: str):
    """Convert a colour name to a best standard color numebr (0 to 15).

    :name: The color to convert. It should exist and be well defined.
    """
    name = _to_standard.get(well_defined_name(name), name)
    if name not in _xterm_color_table:
        return 0
    return _xterm_color_table[name][1]


# TODO:
#   I have a much more full-featured class in vpe_syntax. This should be
#   abandonded.
class HighlightSpec:                   # pylint: disable=too-few-public-methods
    """A full specification for text highlighting.

    An experiment in handling highlight specifications for GUI and 256 color
    terminals.

    :fg:      The preferred foreground color.
    :bg:      The preferred background color.
    :sp:      The preferred special color.
    """
    def __init__(self, *, fg, bg=None, sp=None):
        self.fg = well_defined_name(fg) if fg is not None else fg
        self.bg = well_defined_name(bg) if bg is not None else bg
        self.sp = well_defined_name(sp) if sp is not None else sp

    @property
    def args(self):
        """Get the most appropriate highlight arguments based on Vim's mode.

        :return:
            A dictionary suitable as arguments to `highlight`. Either GUI of
            terminal arguments are returned.
        """
        args = {}
        vim = wrappers.vim
        if vim.has('gui_running') or vim.options.termguicolors:
            if self.fg is not None:
                args['guifg'] = self.fg
            if self.bg is not None:
                args['guibg'] = self.bg
            if self.sp is not None:
                args['guisp'] = self.sp
        elif int(vim.options.t_Co) == 256:
            if self.fg is not None:
                args['ctermfg'] = to_256_num(self.fg)
            if self.bg is not None:
                args['ctermbg'] = to_256_num(self.bg)
        else:
            if self.fg is not None:
                args['ctermfg'] = _to_std_num(self.fg)
            if self.bg is not None:
                args['ctermbg'] = _to_std_num(self.bg)

        return args
