#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# colors.py

import re

_name_map = {
    "black": (0, 0, 0),
    "brown4": (139, 35, 35),
    "ghostgray": (248, 248, 255),
    "navy": (0x00, 0x00, 0x80),
    "darkblue": (0x00, 0x00, 0x8B),
    "mediumblue": (0x00, 0x00, 0xCD),
    "blue": (0x00, 0x00, 0xFF),
    "darkgreen": (0x00, 0x64, 0x00),
    "green": (0x00, 0x80, 0x00),
    "teal": (0x00, 0x80, 0x80),
    "darkcyan": (0x00, 0x8B, 0x8B),
    "deepskyblue": (0x00, 0xBF, 0xFF),
    "darkturquoise": (0x00, 0xCE, 0xD1),
    "mediumspringgreen": (0x00, 0xFA, 0x9A),
    "lime": (0x00, 0xFF, 0x00),
    "springgreen": (0x00, 0xFF, 0x7F),
    "aqua": (0x00, 0xFF, 0xFF),
    "cyan": (0x00, 0xFF, 0xFF),
    "midnightblue": (0x19, 0x19, 0x70),
    "dodgerblue": (0x1E, 0x90, 0xFF),
    "lightseagreen": (0x20, 0xB2, 0xAA),
    "forestgreen": (0x22, 0x8B, 0x22),
    "seagreen": (0x2E, 0x8B, 0x57),
    "darkslategray": (0x2F, 0x4F, 0x4F),
    "limegreen": (0x32, 0xCD, 0x32),
    "mediumseagreen": (0x3C, 0xB3, 0x71),
    "turquoise": (0x40, 0xE0, 0xD0),
    "royalblue": (0x41, 0x69, 0xE1),
    "steelblue": (0x46, 0x82, 0xB4),
    "darkslateblue": (0x48, 0x3D, 0x8B),
    "mediumturquoise": (0x48, 0xD1, 0xCC),
    "indigo  ": (0x4B, 0x00, 0x82),
    "darkolivegreen": (0x55, 0x6B, 0x2F),
    "cadetblue": (0x5F, 0x9E, 0xA0),
    "cornflowerblue": (0x64, 0x95, 0xED),
    "rebeccapurple": (0x66, 0x33, 0x99),
    "mediumaquamarine": (0x66, 0xCD, 0xAA),
    "dimgray": (0x69, 0x69, 0x69),
    "slateblue": (0x6A, 0x5A, 0xCD),
    "olivedrab": (0x6B, 0x8E, 0x23),
    "slategray": (0x70, 0x80, 0x90),
    "lightslategray": (0x77, 0x88, 0x99),
    "mediumslateblue": (0x7B, 0x68, 0xEE),
    "lawngreen": (0x7C, 0xFC, 0x00),
    "chartreuse": (0x7F, 0xFF, 0x00),
    "aquamarine": (0x7F, 0xFF, 0xD4),
    "maroon": (0x80, 0x00, 0x00),
    "purple": (0x80, 0x00, 0x80),
    "olive": (0x80, 0x80, 0x00),
    "gray": (0x80, 0x80, 0x80),
    "skyblue": (0x87, 0xCE, 0xEB),
    "lightskyblue": (0x87, 0xCE, 0xFA),
    "blueviolet": (0x8A, 0x2B, 0xE2),
    "darkred": (0x8B, 0x00, 0x00),
    "darkmagenta": (0x8B, 0x00, 0x8B),
    "saddlebrown": (0x8B, 0x45, 0x13),
    "darkseagreen": (0x8F, 0xBC, 0x8F),
    "lightgreen": (0x90, 0xEE, 0x90),
    "mediumpurple": (0x93, 0x70, 0xDB),
    "darkviolet": (0x94, 0x00, 0xD3),
    "palegreen": (0x98, 0xFB, 0x98),
    "darkorchid": (0x99, 0x32, 0xCC),
    "yellowgreen": (0x9A, 0xCD, 0x32),
    "sienna": (0xA0, 0x52, 0x2D),
    "brown": (0xA5, 0x2A, 0x2A),
    "darkgray": (0xA9, 0xA9, 0xA9),
    "lightblue": (0xAD, 0xD8, 0xE6),
    "greenyellow": (0xAD, 0xFF, 0x2F),
    "paleturquoise": (0xAF, 0xEE, 0xEE),
    "lightsteelblue": (0xB0, 0xC4, 0xDE),
    "powderblue": (0xB0, 0xE0, 0xE6),
    "firebrick": (0xB2, 0x22, 0x22),
    "darkgoldenrod": (0xB8, 0x86, 0x0B),
    "mediumorchid": (0xBA, 0x55, 0xD3),
    "rosybrown": (0xBC, 0x8F, 0x8F),
    "darkkhaki": (0xBD, 0xB7, 0x6B),
    "silver": (0xC0, 0xC0, 0xC0),
    "mediumvioletred": (0xC7, 0x15, 0x85),
    "indianred ": (0xCD, 0x5C, 0x5C),
    "peru": (0xCD, 0x85, 0x3F),
    "chocolate": (0xD2, 0x69, 0x1E),
    "tan": (0xD2, 0xB4, 0x8C),
    "lightgray": (0xD3, 0xD3, 0xD3),
    "thistle": (0xD8, 0xBF, 0xD8),
    "orchid": (0xDA, 0x70, 0xD6),
    "goldenrod": (0xDA, 0xA5, 0x20),
    "palevioletred": (0xDB, 0x70, 0x93),
    "crimson": (0xDC, 0x14, 0x3C),
    "gainsboro": (0xDC, 0xDC, 0xDC),
    "plum": (0xDD, 0xA0, 0xDD),
    "burlywood": (0xDE, 0xB8, 0x87),
    "lightcyan": (0xE0, 0xFF, 0xFF),
    "lavender": (0xE6, 0xE6, 0xFA),
    "darksalmon": (0xE9, 0x96, 0x7A),
    "violet": (0xEE, 0x82, 0xEE),
    "palegoldenrod": (0xEE, 0xE8, 0xAA),
    "lightcoral": (0xF0, 0x80, 0x80),
    "khaki": (0xF0, 0xE6, 0x8C),
    "aliceblue": (0xF0, 0xF8, 0xFF),
    "honeydew": (0xF0, 0xFF, 0xF0),
    "azure": (0xF0, 0xFF, 0xFF),
    "sandybrown": (0xF4, 0xA4, 0x60),
    "wheat": (0xF5, 0xDE, 0xB3),
    "beige": (0xF5, 0xF5, 0xDC),
    "whitesmoke": (0xF5, 0xF5, 0xF5),
    "mintcream": (0xF5, 0xFF, 0xFA),
    "ghostwhite": (0xF8, 0xF8, 0xFF),
    "salmon": (0xFA, 0x80, 0x72),
    "antiquewhite": (0xFA, 0xEB, 0xD7),
    "linen": (0xFA, 0xF0, 0xE6),
    "lightgoldenrodyellow": (0xFA, 0xFA, 0xD2),
    "oldlace": (0xFD, 0xF5, 0xE6),
    "red": (0xFF, 0x00, 0x00),
    "fuchsia": (0xFF, 0x00, 0xFF),
    "magenta": (0xFF, 0x00, 0xFF),
    "deeppink": (0xFF, 0x14, 0x93),
    "orangered": (0xFF, 0x45, 0x00),
    "tomato": (0xFF, 0x63, 0x47),
    "hotpink": (0xFF, 0x69, 0xB4),
    "coral": (0xFF, 0x7F, 0x50),
    "darkorange": (0xFF, 0x8C, 0x00),
    "lightsalmon": (0xFF, 0xA0, 0x7A),
    "orange": (0xFF, 0xA5, 0x00),
    "lightpink": (0xFF, 0xB6, 0xC1),
    "pink": (0xFF, 0xC0, 0xCB),
    "gold": (0xFF, 0xD7, 0x00),
    "peachpuff": (0xFF, 0xDA, 0xB9),
    "navajowhite": (0xFF, 0xDE, 0xAD),
    "moccasin": (0xFF, 0xE4, 0xB5),
    "bisque": (0xFF, 0xE4, 0xC4),
    "mistyrose": (0xFF, 0xE4, 0xE1),
    "blanchedalmond": (0xFF, 0xEB, 0xCD),
    "papayawhip": (0xFF, 0xEF, 0xD5),
    "lavenderblush": (0xFF, 0xF0, 0xF5),
    "seashell": (0xFF, 0xF5, 0xEE),
    "cornsilk": (0xFF, 0xF8, 0xDC),
    "lemonchiffon": (0xFF, 0xFA, 0xCD),
    "floralwhite": (0xFF, 0xFA, 0xF0),
    "snow": (0xFF, 0xFA, 0xFA),
    "yellow": (0xFF, 0xFF, 0x00),
    "lightyellow": (0xFF, 0xFF, 0xE0),
    "ivory": (0xFF, 0xFF, 0xF0),
    "white": (0xFF, 0xFF, 0xFF),
}


class Color:

    pat_hex_color = re.compile(
        "#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", re.IGNORECASE
    )

    def __init__(self, *args):
        global _name_map
        argc = len(args)
        if argc == 1:
            name = args[0].lower()
            if name in _name_map:
                self.name = name
                self.r, self.g, self.b = _name_map[self.name]
            else:
                m = Color.pat_hex_color.match(name)
                if m:
                    self.r = int(m.group(1), 16)
                    self.g = int(m.group(2), 16)
                    self.b = int(m.group(3), 16)
                else:
                    raise ValueError("No se ha especificado correctamente el color")
        elif argc == 3:
            self.r = args[0]
            self.g = args[1]
            self.b = args[2]
        else:
            raise ValueError("No se ha especificado correctamente el color")

    def as_rgb(self):
        return (self.r, self.g, self.b)

    def as_hex(self):
        return "#{:02x}{:02x}{:02x}".format(self.r, self.g, self.b)

    def as_svg(self):
        return "rgb({},{},{})".format(self.r, self.g, self.b)

    def __eq__(self, op2):
        if isinstance(op2, str):
            op2 = Color(op2)
        return self.r == op2.r and self.g == op2.g and self.b == op2.b

    def inverse(self):
        r = 255 - self.r
        g = 255 - self.g
        b = 255 - self.b
        return Color(r, g, b)

    def __str__(self):
        if hasattr(self, "name"):
            return self.name
        return self.as_hex()

    def lower(self):
        return str(self).lower()

    def __repr__(self):
        if hasattr(self, "name"):
            return 'Color("{}")'.format(self.name)
        return 'Color("{}")'.format(self.as_hex())

    def get_red(self):
        return self.r

    def set_red(self, new_red):
        self.r = new_red

    red = property(get_red, set_red)

    def get_green(self):
        return self.g

    def set_green(self, new_green):
        self.g = new_green

    green = property(get_green, set_green)

    def get_blue(self):
        return self.b

    def set_blue(self, new_blue):
        self.b = new_blue

    blue = property(get_blue, set_blue)


def random_color():
    import random

    return Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))


black = Color("black")
white = Color("white")
red = Color("red")
blue = Color("blue")
green = Color("green")
yellow = Color("yellow")
khaki = Color("khaki")
maroon = Color("maroon")
