-
Notifications
You must be signed in to change notification settings - Fork 11
/
colors.lua
81 lines (74 loc) · 1.96 KB
/
colors.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local function ColorToCMYK(col)
local K = math.max(col.r, col.g, col.b)
local k = 255 - K
return (K - col.r) / K, (K - col.g) / K, (K - col.b) / K, k
end
local color_formats = {
{
name = "rgb",
tostring = function(c)
return string.format("%s, %s, %s", c.r, c.g, c.b)
end,
tocolor = function(s)
local r, g, b = s:match("([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+)")
if r and g and b then
return Color(r, g, b)
end
end
},
{
name = "hex",
tostring = function(c)
return string.format("%x", (c.r * 0x10000) + (c.g * 0x100) + c.b):upper()
end,
tocolor = function(s)
s = s:gsub("#", "")
local r, g, b = tonumber("0x".. s:sub(1,2)), tonumber("0x".. s:sub(3,4)), tonumber("0x".. s:sub(5,6))
if r and g and b then
return Color(r, g, b)
end
end
},
{
name = "hsl",
tostring = function(c)
local h, s, l = ColorToHSL(c)
return string.format("%s, %s, %s", math.Round(h, 2), math.Round(s, 2), math.Round(l, 2))
end,
tocolor = function(s)
local h, s, l = s:match("([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+)")
if h and s and l then
return HSLToColor(h, s, l)
end
end
},
{
name = "hsv",
tostring = function(c)
local h, s, v = ColorToHSV(c)
return string.format("%s, %s, %s", math.Round(h, 2), math.Round(s, 2), math.Round(v, 2))
end,
tocolor = function(s)
local h, s, v = s:match("([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+)")
if h and s and v then
return HSVToColor(h, s, v)
end
end
},
{
name = "cmyk",
tostring = function(c)
local c, m, y, k = ColorToCMYK(c)
return string.format("%s, %s, %s, %s", math.Round(c, 1), math.Round(m, 1), math.Round(y, 1), math.Round(k, 1))
end,
tocolor = function(s)
local c, m, y, k = s:match("([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+)[ ,]+([-x.%x]+)")
if not (c and m and y and k) then return end
local mk = (1 - k)
local r = 255 * (1 - c) * mk
local g = 255 * (1 - m) * mk
local b = 255 * (1 - y) * mk
return Color(r, g, b)
end
}
}