Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- color = {}
- color.__index = color
- function color:new(r, g, b, a)
- self = setmetatable({}, self)
- if type(r) == 'string' then
- self:fromHex(r)
- else
- self:setRGB(r, g, b, a)
- end
- return self
- end
- function color:white()
- for i = 1, 4 do self[i] = 1 end
- return self
- end
- function color:black()
- for i = 1, 3 do self[i] = 0 end
- self[4] = 1
- return self
- end
- function color:setRGB(r, g, b, a)
- self[1] = r and r/255 or self[1] or 0
- self[2] = g and g/255 or self[2] or 0
- self[3] = b and b/255 or self[3] or 0
- self[4] = a and a/255 or self[4] or 1
- end
- function color:getRGB()
- return unpack(self)
- end
- function color:fromHex(hex)
- self:black()
- hex = hex:gsub('%X', '') -- remove non-hex chars
- local l = #hex
- -- Short with/without alpha (FFF/FFFF)
- -- Long with/without alpha (FFFFFF/FFFFFFFF)
- assert( l == 3 or l == 4 or l == 6 or l == 8, 'Invalid hex format')
- local mul = l > 4 and 1/255 or 16/255
- local i = 0
- for c in hex:gmatch(l > 4 and '%x%x' or '%x') do
- i = i + 1
- self[i] = tonumber(c, 16) * mul
- end
- return self
- end
- function color:fromHSV(h, s, v, a)
- local r, g, b
- local i = math.floor(h * 6);
- local f = h * 6 - i;
- local p = v * (1 - s);
- local q = v * (1 - f * s);
- local t = v * (1 - (1 - f) * s);
- i = i % 6
- if i == 0 then
- r, g, b = v, t, p
- elseif i == 1 then
- r, g, b = q, v, p
- elseif i == 2 then
- r, g, b = p, v, t
- elseif i == 3 then
- r, g, b = p, q, v
- elseif i == 4 then
- r, g, b = t, p, v
- elseif i == 5 then
- r, g, b = v, p, q
- end
- self[1], self[2], self[3] = r, g, b
- self[4] = a or self[4] or 1
- return self
- end
- function color:toHSV()
- local r, g, b, a = self[1], self[2], self[3], self[4]
- local max, min = math.max(r, g, b), math.min(r, g, b)
- local h, s, v
- v = max
- local d = max - min
- if max == 0 then s = 0 else s = d / max end
- if max == min then
- h = 0 -- achromatic
- else
- if max == r then
- h = (g - b) / d
- if g < b then h = h + 6 end
- elseif max == g then
- h = (b - r) / d + 2
- elseif max == b then
- h = (r - g) / d + 4
- end
- h = h / 6
- end
- return h, s, v, a
- end
- local function hue2rgb(p, q, t)
- if t < 0 then t = t + 1 end
- if t > 1 then t = t - 1 end
- if t < 1/6 then return p + (q - p) * 6 * t end
- if t < 1/2 then return q end
- if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
- return p
- end
- function color:fromHSL(h, s, l, a)
- local r, g, b
- if s == 0 then
- r, g, b = l, l, l -- achromatic
- else
- local q
- if l < 0.5 then
- q = l * (1 + s)
- else
- q = l + s - l * s
- end
- local p = 2 * l - q
- r = hue2rgb(p, q, h + 1/3)
- g = hue2rgb(p, q, h)
- b = hue2rgb(p, q, h - 1/3)
- end
- self[1] = r
- self[2] = g
- self[3] = b
- self[4] = a and a or self[4]
- end
- function color:toHSL()
- local r, g, b
- if s == 0 then
- r, g, b = l, l, l -- achromatic
- else
- local q
- if l < 0.5 then
- q = l * (1 + s)
- else
- q = l + s - l * s
- end
- local p = 2 * l - q
- r = hue2rgb(p, q, h + 1/3)
- g = hue2rgb(p, q, h)
- b = hue2rgb(p, q, h - 1/3)
- end
- self[1] = r
- self[2] = g
- self[3] = b
- self[4] = a and a or self[4]
- end
- function color:setHue(value)
- local h, s, v, a = self:toHSV(); h = value
- self:fromHSV(h, s, v, a)
- end
- function color:setSat(value)
- local h, s, v, a = self:toHSV(); s = value
- self:fromHSV(h, s, v, a)
- end
- function color:setVal(value)
- local h, s, v, a = self:toHSV(); v = value
- self:fromHSV(h, s, v, a)
- end
- return color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement