"""
Color support in limited to coloring an object at time of save or view.

Colors can be specifed by any of the following:
CSS color name: `"aqua"`
CSS style HEX rgb value: "#00FFFF"
A tuple of RGB values in as a 3 tuple: (0, 255, 255)

(All three examples above specify the color "aqua".)


For a list of color names see: [Color keywords](https://www.w3.org/wiki/CSS/Properties/color/keywords)
"""

from . import ValidationError


_cssColors = {}
# Basic color keywords
_cssColors["black"] = (0, 0, 0)
_cssColors["silver"] = (192, 192, 192)
_cssColors["gray"] = (128, 128, 128)
_cssColors["white"] = (255, 255, 255)
_cssColors["maroon"] = (128, 0, 0)
_cssColors["red"] = (255, 0, 0)
_cssColors["purple"] = (128, 0, 128)
_cssColors["fuchsia"] = (255, 0, 255)
_cssColors["green"] = (0, 128, 0)
_cssColors["lime"] = (0, 255, 0)
_cssColors["olive"] = (128, 128, 0)
_cssColors["yellow"] = (255, 255, 0)
_cssColors["navy"] = (0, 0, 128)
_cssColors["blue"] = (0, 0, 255)
_cssColors["teal"] = (0, 128, 128)
_cssColors["aqua"] = (0, 255, 255)
# Extended color keywords
_cssColors["aliceblue"] = (240, 248, 255)
_cssColors["antiquewhite"] = (250, 235, 215)
_cssColors["aquamarine"] = (127, 255, 212)
_cssColors["azure"] = (240, 255, 255)
_cssColors["beige"] = (245, 245, 220)
_cssColors["bisque"] = (255, 228, 196)
_cssColors["blanchedalmond"] = (255, 235, 205)
_cssColors["blueviolet"] = (138, 43, 226)
_cssColors["brown"] = (165, 42, 42)
_cssColors["burlywood"] = (222, 184, 135)
_cssColors["cadetblue"] = (95, 158, 160)
_cssColors["chartreuse"] = (127, 255, 0)
_cssColors["chocolate"] = (210, 105, 30)
_cssColors["coral"] = (255, 127, 80)
_cssColors["cornflowerblue"] = (100, 149, 237)
_cssColors["cornsilk"] = (255, 248, 220)
_cssColors["crimson"] = (220, 20, 60)
_cssColors["cyan"] = (0, 255, 255)
_cssColors["darkblue"] = (0, 0, 139)
_cssColors["darkcyan"] = (0, 139, 139)
_cssColors["darkgoldenrod"] = (184, 134, 11)
_cssColors["darkgray"] = (169, 169, 169)
_cssColors["darkgreen"] = (0, 100, 0)
_cssColors["darkgrey"] = (169, 169, 169)
_cssColors["darkkhaki"] = (189, 183, 107)
_cssColors["darkmagenta"] = (139, 0, 139)
_cssColors["darkolivegreen"] = (85, 107, 47)
_cssColors["darkorange"] = (255, 140, 0)
_cssColors["darkorchid"] = (153, 50, 204)
_cssColors["darkred"] = (139, 0, 0)
_cssColors["darksalmon"] = (233, 150, 122)
_cssColors["darkseagreen"] = (143, 188, 143)
_cssColors["darkslateblue"] = (72, 61, 139)
_cssColors["darkslategray"] = (47, 79, 79)
_cssColors["darkslategrey"] = (47, 79, 79)
_cssColors["darkturquoise"] = (0, 206, 209)
_cssColors["darkviolet"] = (148, 0, 211)
_cssColors["deeppink"] = (255, 20, 147)
_cssColors["deepskyblue"] = (0, 191, 255)
_cssColors["dimgray"] = (105, 105, 105)
_cssColors["dimgrey"] = (105, 105, 105)
_cssColors["dodgerblue"] = (30, 144, 255)
_cssColors["firebrick"] = (178, 34, 34)
_cssColors["floralwhite"] = (255, 250, 240)
_cssColors["forestgreen"] = (34, 139, 34)
_cssColors["gainsboro"] = (220, 220, 220)
_cssColors["ghostwhite"] = (248, 248, 255)
_cssColors["gold"] = (255, 215, 0)
_cssColors["goldenrod"] = (218, 165, 32)
_cssColors["greenyellow"] = (173, 255, 47)
_cssColors["grey"] = (128, 128, 128)
_cssColors["honeydew"] = (240, 255, 240)
_cssColors["hotpink"] = (255, 105, 180)
_cssColors["indianred"] = (205, 92, 92)
_cssColors["indigo"] = (75, 0, 130)
_cssColors["ivory"] = (255, 255, 240)
_cssColors["khaki"] = (240, 230, 140)
_cssColors["lavender"] = (230, 230, 250)
_cssColors["lavenderblush"] = (255, 240, 245)
_cssColors["lawngreen"] = (124, 252, 0)
_cssColors["lemonchiffon"] = (255, 250, 205)
_cssColors["lightblue"] = (173, 216, 230)
_cssColors["lightcoral"] = (240, 128, 128)
_cssColors["lightcyan"] = (224, 255, 255)
_cssColors["lightgoldenrodyellow"] = (250, 250, 210)
_cssColors["lightgray"] = (211, 211, 211)
_cssColors["lightgreen"] = (144, 238, 144)
_cssColors["lightgrey"] = (211, 211, 211)
_cssColors["lightpink"] = (255, 182, 193)
_cssColors["lightsalmon"] = (255, 160, 122)
_cssColors["lightseagreen"] = (32, 178, 170)
_cssColors["lightskyblue"] = (135, 206, 250)
_cssColors["lightslategray"] = (119, 136, 153)
_cssColors["lightslategrey"] = (119, 136, 153)
_cssColors["lightsteelblue"] = (176, 196, 222)
_cssColors["lightyellow"] = (255, 255, 224)
_cssColors["limegreen"] = (50, 205, 50)
_cssColors["linen"] = (250, 240, 230)
_cssColors["magenta"] = (255, 0, 255)
_cssColors["mediumaquamarine"] = (102, 205, 170)
_cssColors["mediumblue"] = (0, 0, 205)
_cssColors["mediumorchid"] = (186, 85, 211)
_cssColors["mediumpurple"] = (147, 112, 219)
_cssColors["mediumseagreen"] = (60, 179, 113)
_cssColors["mediumslateblue"] = (123, 104, 238)
_cssColors["mediumspringgreen"] = (0, 250, 154)
_cssColors["mediumturquoise"] = (72, 209, 204)
_cssColors["mediumvioletred"] = (199, 21, 133)
_cssColors["midnightblue"] = (25, 25, 112)
_cssColors["mintcream"] = (245, 255, 250)
_cssColors["mistyrose"] = (255, 228, 225)
_cssColors["moccasin"] = (255, 228, 181)
_cssColors["navajowhite"] = (255, 222, 173)
_cssColors["oldlace"] = (253, 245, 230)
_cssColors["olivedrab"] = (107, 142, 35)
_cssColors["orange"] = (255, 165, 0)
_cssColors["orangered"] = (255, 69, 0)
_cssColors["orchid"] = (218, 112, 214)
_cssColors["palegoldenrod"] = (238, 232, 170)
_cssColors["palegreen"] = (152, 251, 152)
_cssColors["paleturquoise"] = (175, 238, 238)
_cssColors["palevioletred"] = (219, 112, 147)
_cssColors["papayawhip"] = (255, 239, 213)
_cssColors["peachpuff"] = (255, 218, 185)
_cssColors["peru"] = (205, 133, 63)
_cssColors["pink"] = (255, 192, 203)
_cssColors["plum"] = (221, 160, 221)
_cssColors["powderblue"] = (176, 224, 230)
_cssColors["rosybrown"] = (188, 143, 143)
_cssColors["royalblue"] = (65, 105, 225)
_cssColors["saddlebrown"] = (139, 69, 19)
_cssColors["salmon"] = (250, 128, 114)
_cssColors["sandybrown"] = (244, 164, 96)
_cssColors["seagreen"] = (46, 139, 87)
_cssColors["seashell"] = (255, 245, 238)
_cssColors["sienna"] = (160, 82, 45)
_cssColors["skyblue"] = (135, 206, 235)
_cssColors["slateblue"] = (106, 90, 205)
_cssColors["slategray"] = (112, 128, 144)
_cssColors["slategrey"] = (112, 128, 144)
_cssColors["snow"] = (255, 250, 250)
_cssColors["springgreen"] = (0, 255, 127)
_cssColors["steelblue"] = (70, 130, 180)
_cssColors["tan"] = (210, 180, 140)
_cssColors["thistle"] = (216, 191, 216)
_cssColors["tomato"] = (255, 99, 71)
_cssColors["turquoise"] = (64, 224, 208)
_cssColors["violet"] = (238, 130, 238)
_cssColors["wheat"] = (245, 222, 179)
_cssColors["whitesmoke"] = (245, 245, 245)
_cssColors["yellowgreen"] = (154, 205, 50)


def _parse_color(c):
    def _chk_range(v):
        if v < 0 or v > 255:
            raise ValidationError("RGB values must be between 0 and 255")

    if type(c) == tuple:
        if len(c) != 3:
            raise ValidationError("Color tuples must have 3 items")
        r, g, b = c
        _chk_range(r)
        _chk_range(g)
        _chk_range(b)
        return c
    elif type(c) == str:
        if c[0] == "#":
            if len(c) != 7:
                raise ValidationError(
                    "A hex color specification must have 6 hex digits"
                )
            try:
                r = int(c[1:3], 16)
                g = int(c[3:5], 16)
                b = int(c[5:7], 16)
            except:
                raise ValidationError("Bad hex digit in color")
            return (r, g, b)
        if c in _cssColors:
            return _cssColors[c]
        raise ValidationError(f"Color '{c}' is not a known color.")
    else:
        raise ValidationError(
            "A color is specifed by a string or a tuple of RGB values"
        )
