local colors = {}

local unpack = unpack or table.unpack

local _c = {
    ["red"] = {
        ["50"]   = {0xff,0xeb,0xee,0xff},
        ["100"]  = {0xff,0xcd,0xd2,0xff},
        ["200"]  = {0xef,0x9a,0x9a,0xff},
        ["300"]  = {0xe5,0x73,0x73,0xff},
        ["400"]  = {0xef,0x53,0x50,0xff},
        ["500"]  = {0xf4,0x43,0x36,0xff},
        ["600"]  = {0xe5,0x39,0x35,0xff},
        ["700"]  = {0xd3,0x2f,0x2f,0xff},
        ["800"]  = {0xc6,0x28,0x28,0xff},
        ["900"]  = {0xb7,0x1c,0x1c,0xff},
        ["A100"] = {0xff,0x8a,0x80,0xff},
        ["A200"] = {0xff,0x52,0x52,0xff},
        ["A400"] = {0xff,0x17,0x44,0xff},
        ["A700"] = {0xd5,0x00,0x00,0xff},
    },

    ["pink"] = {
        ["50"]   = {0xfc,0xe4,0xec,0xff},
        ["100"]  = {0xf8,0xbb,0xd0,0xff},
        ["200"]  = {0xf4,0x8f,0xb1,0xff},
        ["300"]  = {0xf0,0x62,0x92,0xff},
        ["400"]  = {0xec,0x40,0x7a,0xff},
        ["500"]  = {0xe9,0x1e,0x63,0xff},
        ["600"]  = {0xd8,0x1b,0x60,0xff},
        ["700"]  = {0xc2,0x18,0x5b,0xff},
        ["800"]  = {0xad,0x14,0x57,0xff},
        ["900"]  = {0x88,0x0e,0x4f,0xff},
        ["A100"] = {0xff,0x80,0xab,0xff},
        ["A200"] = {0xff,0x40,0x81,0xff},
        ["A400"] = {0xf5,0x00,0x57,0xff},
        ["A700"] = {0xc5,0x11,0x62,0xff},
    },

    ["purple"] = {
        ["50"]   = {0xf3,0xe5,0xf5,0xff},
        ["100"]  = {0xe1,0xbe,0xe7,0xff},
        ["200"]  = {0xce,0x93,0xd8,0xff},
        ["300"]  = {0xba,0x68,0xc8,0xff},
        ["400"]  = {0xab,0x47,0xbc,0xff},
        ["500"]  = {0x9c,0x27,0xb0,0xff},
        ["600"]  = {0x8e,0x24,0xaa,0xff},
        ["700"]  = {0x7b,0x1f,0xa2,0xff},
        ["800"]  = {0x6a,0x1b,0x9a,0xff},
        ["900"]  = {0x4a,0x14,0x8c,0xff},
        ["A100"] = {0xea,0x80,0xfc,0xff},
        ["A200"] = {0xe0,0x40,0xfb,0xff},
        ["A400"] = {0xd5,0x00,0xf9,0xff},
        ["A700"] = {0xaa,0x00,0xff,0xff},
    },

    ["deep-purple"] = {
        ["50"]   = {0xed,0xe7,0xf6,0xff},
        ["100"]  = {0xd1,0xc4,0xe9,0xff},
        ["200"]  = {0xb3,0x9d,0xdb,0xff},
        ["300"]  = {0x95,0x75,0xcd,0xff},
        ["400"]  = {0x7e,0x57,0xc2,0xff},
        ["500"]  = {0x67,0x3a,0xb7,0xff},
        ["600"]  = {0x5e,0x35,0xb1,0xff},
        ["700"]  = {0x51,0x2d,0xa8,0xff},
        ["800"]  = {0x45,0x27,0xa0,0xff},
        ["900"]  = {0x31,0x1b,0x92,0xff},
        ["A100"] = {0xb3,0x88,0xff,0xff},
        ["A200"] = {0x7c,0x4d,0xff,0xff},
        ["A400"] = {0x65,0x1f,0xff,0xff},
        ["A700"] = {0x62,0x00,0xea,0xff},
    },

    ["indigo"] = {
        ["50"]   = {0xe8,0xea,0xf6,0xff},
        ["100"]  = {0xc5,0xca,0xe9,0xff},
        ["200"]  = {0x9f,0xa8,0xda,0xff},
        ["300"]  = {0x79,0x86,0xcb,0xff},
        ["400"]  = {0x5c,0x6b,0xc0,0xff},
        ["500"]  = {0x3f,0x51,0xb5,0xff},
        ["600"]  = {0x39,0x49,0xab,0xff},
        ["700"]  = {0x30,0x3f,0x9f,0xff},
        ["800"]  = {0x28,0x35,0x93,0xff},
        ["900"]  = {0x1a,0x23,0x7e,0xff},
        ["A100"] = {0x8c,0x9e,0xff,0xff},
        ["A200"] = {0x53,0x6d,0xfe,0xff},
        ["A400"] = {0x3d,0x5a,0xfe,0xff},
        ["A700"] = {0x30,0x4f,0xfe,0xff},
    },

    ["blue"] = {
        ["50"]   = {0xe3,0xf2,0xfd,0xff},
        ["100"]  = {0xbb,0xde,0xfb,0xff},
        ["200"]  = {0x90,0xca,0xf9,0xff},
        ["300"]  = {0x64,0xb5,0xf6,0xff},
        ["400"]  = {0x42,0xa5,0xf5,0xff},
        ["500"]  = {0x21,0x96,0xf3,0xff},
        ["600"]  = {0x1e,0x88,0xe5,0xff},
        ["700"]  = {0x19,0x76,0xd2,0xff},
        ["800"]  = {0x15,0x65,0xc0,0xff},
        ["900"]  = {0x0d,0x47,0xa1,0xff},
        ["A100"] = {0x82,0xb1,0xff,0xff},
        ["A200"] = {0x44,0x8a,0xff,0xff},
        ["A400"] = {0x29,0x79,0xff,0xff},
        ["A700"] = {0x29,0x62,0xff,0xff},
    },

    ["light-blue"] = {
        ["50"]   = {0xe1,0xf5,0xfe,0xff},
        ["100"]  = {0xb3,0xe5,0xfc,0xff},
        ["200"]  = {0x81,0xd4,0xfa,0xff},
        ["300"]  = {0x4f,0xc4,0xf7,0xff},
        ["400"]  = {0x29,0xb6,0xf6,0xff},
        ["500"]  = {0x03,0xa9,0xf4,0xff},
        ["600"]  = {0x03,0x9b,0xe5,0xff},
        ["700"]  = {0x02,0x88,0xd1,0xff},
        ["800"]  = {0x02,0x77,0xbd,0xff},
        ["900"]  = {0x01,0x57,0x9b,0xff},
        ["A100"] = {0x80,0xd8,0xff,0xff},
        ["A200"] = {0x40,0xc4,0xff,0xff},
        ["A400"] = {0x00,0xb0,0xff,0xff},
        ["A700"] = {0x00,0x91,0xea,0xff},
    },

    ["cyan"] = {
        ["50"]   = {0xe0,0xf7,0xfa,0xff},
        ["100"]  = {0xb2,0xeb,0xf2,0xff},
        ["200"]  = {0x80,0xde,0xea,0xff},
        ["300"]  = {0x4d,0xd0,0xe1,0xff},
        ["400"]  = {0x26,0xc6,0xda,0xff},
        ["500"]  = {0x00,0xbc,0xd4,0xff},
        ["600"]  = {0x00,0xac,0xc1,0xff},
        ["700"]  = {0x00,0x97,0xa7,0xff},
        ["800"]  = {0x00,0x83,0x8f,0xff},
        ["900"]  = {0x00,0x60,0x64,0xff},
        ["A100"] = {0x84,0xff,0xff,0xff},
        ["A200"] = {0x18,0xff,0xff,0xff},
        ["A400"] = {0x00,0xe5,0xff,0xff},
        ["A700"] = {0x00,0xb8,0xd4,0xff},
    },

    ["teal"] = {
        ["50"]   = {0xe0,0xf2,0xf1,0xff},
        ["100"]  = {0xb2,0xdf,0xdb,0xff},
        ["200"]  = {0x80,0xcb,0xc4,0xff},
        ["300"]  = {0x4d,0xb6,0xac,0xff},
        ["400"]  = {0x26,0xa6,0x9a,0xff},
        ["500"]  = {0x00,0x96,0x88,0xff},
        ["600"]  = {0x00,0x89,0x7b,0xff},
        ["700"]  = {0x00,0x79,0x6b,0xff},
        ["800"]  = {0x00,0x69,0x5c,0xff},
        ["900"]  = {0x00,0x4d,0x40,0xff},
        ["A100"] = {0xa7,0xff,0xeb,0xff},
        ["A200"] = {0x64,0xff,0xda,0xff},
        ["A400"] = {0x1d,0xe9,0xb6,0xff},
        ["A700"] = {0x00,0xbf,0xa5,0xff},
    },

    ["green"] = {
        ["50"]   = {0xe8,0xf5,0xe9,0xff},
        ["100"]  = {0xc8,0xe6,0xc9,0xff},
        ["200"]  = {0xa5,0xd6,0xa7,0xff},
        ["300"]  = {0x81,0xc7,0x84,0xff},
        ["400"]  = {0x66,0xbb,0x6a,0xff},
        ["500"]  = {0x4c,0xaf,0x50,0xff},
        ["600"]  = {0x43,0xa0,0x47,0xff},
        ["700"]  = {0x38,0x8e,0x3c,0xff},
        ["800"]  = {0x2e,0x7d,0x32,0xff},
        ["900"]  = {0x1b,0x5e,0x20,0xff},
        ["A100"] = {0xb9,0xf6,0xca,0xff},
        ["A200"] = {0x69,0xf0,0xae,0xff},
        ["A400"] = {0x00,0xe6,0x76,0xff},
        ["A700"] = {0x00,0xc8,0x53,0xff},
    },

    ["light-green"] = {
        ["50"]   = {0xf1,0xf8,0xe9,0xff},
        ["100"]  = {0xdc,0xed,0xc8,0xff},
        ["200"]  = {0xc5,0xe1,0xa5,0xff},
        ["300"]  = {0xae,0xd5,0x81,0xff},
        ["400"]  = {0x9c,0xcc,0x65,0xff},
        ["500"]  = {0x8b,0xc3,0x4a,0xff},
        ["600"]  = {0x7c,0xb3,0x42,0xff},
        ["700"]  = {0x68,0x9f,0x38,0xff},
        ["800"]  = {0x55,0x8b,0x2f,0xff},
        ["900"]  = {0x33,0x69,0x1e,0xff},
        ["A100"] = {0xcc,0xff,0x90,0xff},
        ["A200"] = {0xb2,0xff,0x59,0xff},
        ["A400"] = {0x76,0xff,0x03,0xff},
        ["A700"] = {0x64,0xdd,0x17,0xff},
    },

    ["lime"] = {
        ["50"]   = {0xf9,0xfb,0xe7,0xff},
        ["100"]  = {0xf0,0xf4,0xc3,0xff},
        ["200"]  = {0xe6,0xee,0x9c,0xff},
        ["300"]  = {0xdc,0xe7,0x75,0xff},
        ["400"]  = {0xd4,0xe1,0x57,0xff},
        ["500"]  = {0xcd,0xdc,0x39,0xff},
        ["600"]  = {0xc0,0xca,0x33,0xff},
        ["700"]  = {0xaf,0xb4,0x2b,0xff},
        ["800"]  = {0x9e,0x9d,0x24,0xff},
        ["900"]  = {0x82,0x77,0x17,0xff},
        ["A100"] = {0xf4,0xff,0x81,0xff},
        ["A200"] = {0xee,0xff,0x41,0xff},
        ["A400"] = {0xc6,0xff,0x00,0xff},
        ["A700"] = {0xae,0xea,0x00,0xff},
    },

    ["yellow"] = {
        ["50"]   = {0xFF,0xFD,0xE7,0xff},
        ["100"]  = {0xFF,0xF9,0xC4,0xff},
        ["200"]  = {0xFF,0xF5,0x9D,0xff},
        ["300"]  = {0xFF,0xF1,0x76,0xff},
        ["400"]  = {0xFF,0xEE,0x58,0xff},
        ["500"]  = {0xFF,0xEB,0x3B,0xff},
        ["600"]  = {0xFD,0xD8,0x35,0xff},
        ["700"]  = {0xFB,0xC0,0x2D,0xff},
        ["800"]  = {0xF9,0xA8,0x25,0xff},
        ["900"]  = {0xF5,0x7F,0x17,0xff},
        ["A100"] = {0xFF,0xFF,0x8D,0xff},
        ["A200"] = {0xFF,0xFF,0x00,0xff},
        ["A400"] = {0xFF,0xEA,0x00,0xff},
        ["A700"] = {0xFF,0xD6,0x00,0xff}
    },

    ["amber"] = {
        ["50"]   = {0xFF,0xF8,0xE1,0xff},
        ["100"]  = {0xFF,0xEC,0xB3,0xff},
        ["200"]  = {0xFF,0xE0,0x82,0xff},
        ["300"]  = {0xFF,0xD5,0x4F,0xff},
        ["400"]  = {0xFF,0xCA,0x28,0xff},
        ["500"]  = {0xFF,0xC1,0x07,0xff},
        ["600"]  = {0xFF,0xB3,0x00,0xff},
        ["700"]  = {0xFF,0xA0,0x00,0xff},
        ["800"]  = {0xFF,0x8F,0x00,0xff},
        ["900"]  = {0xFF,0x6F,0x00,0xff},
        ["A100"] = {0xFF,0xE5,0x7F,0xff},
        ["A200"] = {0xFF,0xD7,0x40,0xff},
        ["A400"] = {0xFF,0xC4,0x00,0xff},
        ["A700"] = {0xFF,0xAB,0x00,0xff}
    },

    ["orange"] = {
        ["50"]   = {0xFF,0xF3,0xE0,0xff},
        ["100"]  = {0xFF,0xE0,0xB2,0xff},
        ["200"]  = {0xFF,0xCC,0x80,0xff},
        ["300"]  = {0xFF,0xB7,0x4D,0xff},
        ["400"]  = {0xFF,0xA7,0x26,0xff},
        ["500"]  = {0xFF,0x98,0x00,0xff},
        ["600"]  = {0xFB,0x8C,0x00,0xff},
        ["700"]  = {0xF5,0x7C,0x00,0xff},
        ["800"]  = {0xEF,0x6C,0x00,0xff},
        ["900"]  = {0xE6,0x51,0x00,0xff},
        ["A100"] = {0xFF,0xD1,0x80,0xff},
        ["A200"] = {0xFF,0xAB,0x40,0xff},
        ["A400"] = {0xFF,0x91,0x00,0xff},
        ["A700"] = {0xFF,0x6D,0x00,0xff}
    },

    ["deep-orange"] = {
        ["50"]   = {0xFB,0xE9,0xE7,0xff},
        ["100"]  = {0xFF,0xCC,0xBC,0xff},
        ["200"]  = {0xFF,0xAB,0x91,0xff},
        ["300"]  = {0xFF,0x8A,0x65,0xff},
        ["400"]  = {0xFF,0x70,0x43,0xff},
        ["500"]  = {0xFF,0x57,0x22,0xff},
        ["600"]  = {0xF4,0x51,0x1E,0xff},
        ["700"]  = {0xE6,0x4A,0x19,0xff},
        ["800"]  = {0xD8,0x43,0x15,0xff},
        ["900"]  = {0xBF,0x36,0x0C,0xff},
        ["A100"] = {0xFF,0x9E,0x80,0xff},
        ["A200"] = {0xFF,0x6E,0x40,0xff},
        ["A400"] = {0xFF,0x3D,0x00,0xff},
        ["A700"] = {0xDD,0x2C,0x00,0xff}
    },

    ["brown"] = {
        ["50"]   = {0xEF,0xEB,0xE9,0xff},
        ["100"]  = {0xD7,0xCC,0xC8,0xff},
        ["200"]  = {0xBC,0xAA,0xA4,0xff},
        ["300"]  = {0xA1,0x88,0x7F,0xff},
        ["400"]  = {0x8D,0x6E,0x63,0xff},
        ["500"]  = {0x79,0x55,0x48,0xff},
        ["600"]  = {0x6D,0x4C,0x41,0xff},
        ["700"]  = {0x5D,0x40,0x37,0xff},
        ["800"]  = {0x4E,0x34,0x2E,0xff},
        ["900"]  = {0x3E,0x27,0x23,0xff}
    },

    ["grey"] = {
        ["50"]   = {0xFA,0xFA,0xFA,0xff},
        ["100"]  = {0xF5,0xF5,0xF5,0xff},
        ["200"]  = {0xEE,0xEE,0xEE,0xff},
        ["300"]  = {0xE0,0xE0,0xE0,0xff},
        ["400"]  = {0xBD,0xBD,0xBD,0xff},
        ["500"]  = {0x9E,0x9E,0x9E,0xff},
        ["600"]  = {0x75,0x75,0x75,0xff},
        ["700"]  = {0x61,0x61,0x61,0xff},
        ["800"]  = {0x42,0x42,0x42,0xff},
        ["900"]  = {0x21,0x21,0x21,0xff}
    },

    ["blue-grey"] = {
        ["50"]   = {0xEC,0xEF,0xF1,0xff},
        ["100"]  = {0xCF,0xD8,0xDC,0xff},
        ["200"]  = {0xB0,0xBE,0xC5,0xff},
        ["300"]  = {0x90,0xA4,0xAE,0xff},
        ["400"]  = {0x78,0x90,0x9C,0xff},
        ["500"]  = {0x60,0x7D,0x8B,0xff},
        ["600"]  = {0x54,0x6E,0x7A,0xff},
        ["700"]  = {0x45,0x5A,0x64,0xff},
        ["800"]  = {0x37,0x47,0x4F,0xff},
        ["900"]  = {0x26,0x32,0x38,0xff}
    },

    ["black"] = {
        ["full"]            = {0x00,0x00,0x00,0xff}, --100
        ["divider"]         = {0x00,0x00,0x00,0x1f}, --12
        ["disabled"]        = {0x00,0x00,0x00,0x42}, --26
        ["inactive-icon"]   = {0x00,0x00,0x00,0x42}, --26
        ["hint-text"]       = {0x00,0x00,0x00,0x42}, --26
        ["secondary-text"]  = {0x00,0x00,0x00,0x8f}, --54
        ["icon"]            = {0x00,0x00,0x00,0x8f}, --54
        ["text"]            = {0x00,0x00,0x00,0xde}, --87

        ["display"]         = {0x00,0x00,0x00,0x8f}, --54
        ["headline"]        = {0x00,0x00,0x00,0xde}, --87
        ["title"]           = {0x00,0x00,0x00,0xde}, --87
        ["subhead"]         = {0x00,0x00,0x00,0xde}, --87
        ["body"]            = {0x00,0x00,0x00,0xde}, --87
        ["caption"]         = {0x00,0x00,0x00,0x8f}, --54
        ["menu"]            = {0x00,0x00,0x00,0xde}, --87
        ["button"]          = {0x00,0x00,0x00,0xde}, --87
    },

    ["white"] = {
        ["full"]            = {0xff,0xff,0xff,0xff}, --100
        ["divider"]         = {0xff,0xff,0xff,0x1f}, --12
        ["disabled"]        = {0xff,0xff,0xff,0x4d}, --30
        ["inactive-icon"]   = {0xff,0xff,0xff,0x4d}, --30
        ["hint-text"]       = {0xff,0xff,0xff,0x4d}, --30
        ["secondary-text"]  = {0xff,0xff,0xff,0xb3}, --70
        ["icon"]            = {0xff,0xff,0xff,0xff}, --100
        ["text"]            = {0xff,0xff,0xff,0xff}, --100

        ["display"]         = {0xff,0xff,0xff,0xb3}, --70
        ["headline"]        = {0xff,0xff,0xff,0xff}, --100
        ["title"]           = {0xff,0xff,0xff,0xff}, --100
        ["subhead"]         = {0xff,0xff,0xff,0xff}, --100
        ["body"]            = {0xff,0xff,0xff,0xff}, --100
        ["caption"]         = {0xff,0xff,0xff,0xb3}, --70
        ["menu"]            = {0xff,0xff,0xff,0xff}, --100
        ["button"]          = {0xff,0xff,0xff,0xff}, --100
    }
}

local isSRGB = love.graphics.isGammaCorrect and love.graphics.isGammaCorrect() or function ()
    local _, _, flags = love.window.getMode()
    return flags.srgb
end

local convert = function (color)
    if isSRGB() then
        return love.math.linearToGamma(color)
    else
        return unpack(color)
    end
end

function colors.main(name, variation)
    if (name == "black" or name == "white") then
        if variation then
            error ("The colors black and white dont support variations, use colors.monochrome instead", 2)
        end

        return convert(_c[name].full)
    end

    variation = variation or "500"

    if type(variation) == "number" then
        variation = tostring(variation)
    elseif not type(variation) == "string" then
        error("Argument #2 to colors, number or string expected got "..type(variation), 2)
    end

    if not _c[name] then
        error ("Argument #1 to colors, "..name.." is not a valid color", 2)
    end

    if not _c[name][variation] then
        error ("Argument #2 to colors, "..variation.." is not a valid color variation for the color: "..name, 2)
    end

    return convert(_c[name][variation])
end

function colors.mono(name, variation)
    if not (name == "black" or name == "white") then
        error ('The color "'..name..'" passed to monochrome is not black nor white', 2)
    end

    variation = variation or "full"

    if not _c[name][variation] then
        error ("Argument #2 to monochrome, "..variation.." is not a valid color variation", 2)
    end

    return convert(_c[name][variation])
end

function colors.list()
    local t = {}

    for k, _ in pairs(_c) do
        t[#t+1] = k
    end

    return t
end

function colors.variations(name)
    if not _c[name] then
        error("Argument #1 to variations, the color is not a valid color", 2)
    end

    local t = {}

    for k, _ in pairs(_c[name]) do
        t[#t + 1] = k
    end

    return t
end

function colors.background (l)
    l = l or "light"

    if l ~= "light" then
        if l == "dark" then
            return convert(_c["blue-grey"]["800"])
        else
            error ("Argument #1 to background, is not light nor black", 2)
        end
    else
        return convert(_c["grey"]["50"])
    end
end

setmetatable(colors, {__call = function (self, ...) return self.main(...) end})

return colors
