-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.lua
287 lines (253 loc) · 9.8 KB
/
color.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
-----------------------------------------------------------------------------
-- Provides support for color manipulation in HSL color space.
--
-- http://sputnik.freewisdom.org/lib/colors/
--
-- License: MIT/X
--
-- (c) 2008 Yuri Takhteyev ([email protected]) *
--
-- * rgb_to_hsl() implementation was contributed by Markus Fleck-Graffe.
-----------------------------------------------------------------------------
-- module(..., package.seeall)
local Color = {}
local Color_mt = {__metatable = {}, __index = Color}
-----------------------------------------------------------------------------
-- Instantiates a new "color".
--
-- @param H hue (0-360) _or_ an RGB string ("#930219")
-- @param S saturation (0.0-1.0)
-- @param L lightness (0.0-1.0)
-- @return an instance of Color
-----------------------------------------------------------------------------
function Color:new(H, S, L)
if type(H) == "string" and H:sub(1,1)=="#" and H:len() == 7 then
H, S, L = rgb_string_to_hsl(H)
end
assert(Color_mt)
return setmetatable({H = H, S = S, L = L}, Color_mt)
end
-----------------------------------------------------------------------------
-- Converts an HSL triplet to RGB
-- (see http://homepages.cwi.nl/~steven/css/hsl.html).
--
-- @param h hue (0-360)
-- @param s saturation (0.0-1.0)
-- @param L lightness (0.0-1.0)
-- @return an R, G, and B component of RGB
-----------------------------------------------------------------------------
local function hsl_to_rgb(h, s, L)
h = h/360
local m1, m2
if L<=0.5 then
m2 = L*(s+1)
else
m2 = L+s-L*s
end
m1 = L*2-m2
local function _h2rgb(m1, m2, h)
if h<0 then h = h+1 end
if h>1 then h = h-1 end
if h*6<1 then
return m1+(m2-m1)*h*6
elseif h*2<1 then
return m2
elseif h*3<2 then
return m1+(m2-m1)*(2/3-h)*6
else
return m1
end
end
return _h2rgb(m1, m2, h+1/3), _h2rgb(m1, m2, h), _h2rgb(m1, m2, h-1/3)
end
-----------------------------------------------------------------------------
-- Converts an RGB triplet to HSL.
-- (see http://easyrgb.com)
--
-- @param r red (0.0-1.0)
-- @param g green (0.0-1.0)
-- @param b blue (0.0-1.0)
-- @return corresponding H, S and L components
-----------------------------------------------------------------------------
local function rgb_to_hsl(r, g, b)
--r, g, b = r/255, g/255, b/255
local min = math.min(r, g, b)
local max = math.max(r, g, b)
local delta = max - min
local h, s, l = 0, 0, ((min+max)/2)
if l > 0 and l < 0.5 then s = delta/(max+min) end
if l >= 0.5 and l < 1 then s = delta/(2-max-min) end
if delta > 0 then
if max == r and max ~= g then h = h + (g-b)/delta end
if max == g and max ~= b then h = h + 2 + (b-r)/delta end
if max == b and max ~= r then h = h + 4 + (r-g)/delta end
h = h / 6;
end
if h < 0 then h = h + 1 end
if h > 1 then h = h - 1 end
return h * 360, s, l
end
local function rgb_string_to_hsl(rgb)
return rgb_to_hsl(tonumber(rgb:sub(2,3), 16)/256,
tonumber(rgb:sub(4,5), 16)/256,
tonumber(rgb:sub(6,7), 16)/256)
end
-----------------------------------------------------------------------------
-- Converts the color to an RGB string.
--
-- @return a 6-digit RGB representation of the color prefixed
-- with "#" (suitable for inclusion in HTML)
-----------------------------------------------------------------------------
function Color:to_rgb()
local r, g, b = hsl_to_rgb(self.H, self.S, self.L)
local rgb = {hsl_to_rgb(self.H, self.S, self.L)}
local buffer = "#"
for i,v in ipairs(rgb) do
buffer = buffer..string.format("%02x",math.floor(v*256+0.5))
end
return buffer
end
-----------------------------------------------------------------------------
-- Creates a new color with hue different by delta.
--
-- @param delta a delta for hue.
-- @return a new instance of Color.
-----------------------------------------------------------------------------
function Color:hue_offset(delta)
return new((self.H + delta) % 360, self.S, self.L)
end
-----------------------------------------------------------------------------
-- Creates a complementary color.
--
-- @return a new instance of Color
-----------------------------------------------------------------------------
function Color:complementary()
return self:hue_offset(180)
end
-----------------------------------------------------------------------------
-- Creates two neighboring colors (by hue), offset by "angle".
--
-- @param angle the difference in hue between this color and the
-- neighbors
-- @return two new instances of Color
-----------------------------------------------------------------------------
function Color:neighbors(angle)
local angle = angle or 30
return self:hue_offset(angle), self:hue_offset(360-angle)
end
-----------------------------------------------------------------------------
-- Creates two new colors to make a triadic color scheme.
--
-- @return two new instances of Color
-----------------------------------------------------------------------------
function Color:triadic()
return self:neighbors(120)
end
-----------------------------------------------------------------------------
-- Creates two new colors, offset by angle from this colors complementary.
--
-- @param angle the difference in hue between the complementary and
-- the returned colors
-- @return two new instances of Color
-----------------------------------------------------------------------------
function Color:split_complementary(angle)
return self:neighbors(180-(angle or 30))
end
-----------------------------------------------------------------------------
-- Creates a new color with saturation set to a new value.
--
-- @param saturation the new saturation value (0.0 - 1.0)
-- @return a new instance of Color
-----------------------------------------------------------------------------
function Color:desaturate_to(saturation)
return new(self.H, saturation, self.L)
end
-----------------------------------------------------------------------------
-- Creates a new color with saturation set to a old saturation times r.
--
-- @param r the multiplier for the new saturation
-- @return a new instance of Color
-----------------------------------------------------------------------------
function Color:desaturate_by(r)
return new(self.H, self.S*r, self.L)
end
-----------------------------------------------------------------------------
-- Creates a new color with lightness set to a new value.
--
-- @param lightness the new lightness value (0.0 - 1.0)
-- @return a new instance of Color
-----------------------------------------------------------------------------
function Color:lighten_to(lightness)
return new(self.H, self.S, lightness)
end
-----------------------------------------------------------------------------
-- Creates a new color with lightness set to a old lightness times r.
--
-- @param r the multiplier for the new lightness
-- @return a new instance of Color
-----------------------------------------------------------------------------
function Color:lighten_by(r)
return new(self.H, self.S, self.L*r)
end
-----------------------------------------------------------------------------
-- Creates n variations of this color using supplied function and returns
-- them as a table.
--
-- @param f the function to create variations
-- @param n the number of variations
-- @return a table with n values containing the new colors
-----------------------------------------------------------------------------
function Color:variations(f, n)
n = n or 5
local results = {}
for i=1,n do
table.insert(results, f(self, i, n))
end
return results
end
-----------------------------------------------------------------------------
-- Creates n tints of this color and returns them as a table
--
-- @param n the number of tints
-- @return a table with n values containing the new colors
-----------------------------------------------------------------------------
function Color:tints(n)
local f = function (color, i, n)
return color:lighten_to(color.L + (1-color.L)/n*i)
end
return self:variations(f, n)
end
-----------------------------------------------------------------------------
-- Creates n shades of this color and returns them as a table
--
-- @param n the number of shades
-- @return a table with n values containing the new colors
-----------------------------------------------------------------------------
function Color:shades(n)
local f = function (color, i, n)
return color:lighten_to(color.L - (color.L)/n*i)
end
return self:variations(f, n)
end
function Color:tint(r)
return self:lighten_to(self.L + (1-self.L)*r)
end
function Color:shade(r)
return self:lighten_to(self.L - self.L*r)
end
-- function Color:get_rgb()
-- local r, g, b = hsl_to_rgb(self.H, self.S, self.L)
-- r = math.floor(math.max(0, math.min(255, r * 255)) + 0.5)
-- g = math.floor(math.max(0, math.min(255, g * 255)) + 0.5)
-- b = math.floor(math.max(0, math.min(255, b * 255)) + 0.5)
-- return r, g, b
-- end
function Color:get_rgb()
local r, g, b = hsl_to_rgb(self.H, self.S, self.L)
r = math.max(0, math.min(1, r))
g = math.max(0, math.min(1, g))
b = math.max(0, math.min(1, b))
return r, g, b
end
Color_mt.__tostring = Color.to_rgb
return Color