Advertisement
Snusmumriken

color library

Jul 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. color = {}
  2. color.__index = color
  3. function color:new(r, g, b, a)
  4.     self = setmetatable({}, self)
  5.    
  6.     if type(r) == 'string' then
  7.         self:fromHex(r)
  8.     else
  9.         self:setRGB(r, g, b, a)
  10.     end
  11.    
  12.     return self
  13. end
  14.  
  15. function color:white()
  16.     for i = 1, 4 do self[i] = 1 end
  17.     return self
  18. end
  19.  
  20. function color:black()
  21.     for i = 1, 3 do self[i] = 0 end
  22.     self[4] = 1
  23.     return self
  24. end
  25.  
  26. function color:setRGB(r, g, b, a)
  27.     self[1] = r and r/255 or self[1] or 0
  28.     self[2] = g and g/255 or self[2] or 0
  29.     self[3] = b and b/255 or self[3] or 0
  30.     self[4] = a and a/255 or self[4] or 1
  31. end
  32.  
  33. function color:getRGB()
  34.     return unpack(self)
  35. end
  36.  
  37. function color:fromHex(hex)
  38.     self:black()
  39.     hex = hex:gsub('%X', '') -- remove non-hex chars
  40.  
  41.     local l = #hex
  42.  
  43.     -- Short with/without alpha (FFF/FFFF)
  44.     -- Long with/without alpha (FFFFFF/FFFFFFFF)
  45.     assert( l == 3 or l == 4 or l == 6 or l == 8, 'Invalid hex format')
  46.  
  47.     local mul = l > 4 and 1/255 or 16/255
  48.  
  49.     local i = 0
  50.     for c in hex:gmatch(l > 4 and '%x%x' or '%x') do
  51.             i = i + 1
  52.             self[i] = tonumber(c, 16) * mul
  53.     end
  54.  
  55.     return self
  56. end
  57.  
  58. function color:fromHSV(h, s, v, a)
  59.     local r, g, b
  60.     local i = math.floor(h * 6);
  61.     local f = h * 6 - i;
  62.     local p = v * (1 - s);
  63.     local q = v * (1 - f * s);
  64.     local t = v * (1 - (1 - f) * s);
  65.  
  66.     i = i % 6
  67.     if i == 0 then
  68.         r, g, b = v, t, p
  69.     elseif i == 1 then
  70.         r, g, b = q, v, p
  71.     elseif i == 2 then
  72.         r, g, b = p, v, t
  73.     elseif i == 3 then
  74.         r, g, b = p, q, v
  75.     elseif i == 4 then
  76.         r, g, b = t, p, v
  77.     elseif i == 5 then
  78.         r, g, b = v, p, q
  79.     end
  80.     self[1], self[2], self[3] = r, g, b
  81.     self[4] = a or self[4] or 1
  82.     return self
  83. end
  84.  
  85. function color:toHSV()
  86.     local r, g, b, a = self[1], self[2], self[3], self[4]
  87.    
  88.     local max, min = math.max(r, g, b), math.min(r, g, b)
  89.     local h, s, v
  90.     v = max
  91.  
  92.     local d = max - min
  93.     if max == 0 then s = 0 else s = d / max end
  94.  
  95.     if max == min then
  96.         h = 0 -- achromatic
  97.     else
  98.         if max == r then
  99.             h = (g - b) / d
  100.             if g < b then h = h + 6 end
  101.         elseif max == g then
  102.             h = (b - r) / d + 2
  103.         elseif max == b then
  104.             h = (r - g) / d + 4
  105.         end
  106.         h = h / 6
  107.     end
  108.    
  109.     return h, s, v, a
  110. end
  111.  
  112.  
  113. local function hue2rgb(p, q, t)
  114.     if t < 0     then t = t + 1 end
  115.     if t > 1     then t = t - 1 end
  116.     if t < 1/6 then return p + (q - p) * 6 * t end
  117.     if t < 1/2 then return q end
  118.     if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
  119.     return p
  120. end
  121.  
  122. function color:fromHSL(h, s, l, a)
  123.     local r, g, b
  124.    
  125.     if s == 0 then
  126.         r, g, b = l, l, l -- achromatic
  127.     else
  128.         local q
  129.         if l < 0.5 then
  130.             q = l * (1 + s)
  131.         else
  132.             q = l + s - l * s
  133.         end
  134.        
  135.         local p = 2 * l - q
  136.         r = hue2rgb(p, q, h + 1/3)
  137.         g = hue2rgb(p, q, h)
  138.         b = hue2rgb(p, q, h - 1/3)
  139.     end
  140.    
  141.     self[1] = r
  142.     self[2] = g
  143.     self[3] = b
  144.     self[4] = a and a or self[4]
  145. end
  146.  
  147. function color:toHSL()
  148.     local r, g, b
  149.    
  150.     if s == 0 then
  151.         r, g, b = l, l, l -- achromatic
  152.     else
  153.         local q
  154.         if l < 0.5 then
  155.             q = l * (1 + s)
  156.         else
  157.             q = l + s - l * s
  158.         end
  159.        
  160.         local p = 2 * l - q
  161.         r = hue2rgb(p, q, h + 1/3)
  162.         g = hue2rgb(p, q, h)
  163.         b = hue2rgb(p, q, h - 1/3)
  164.     end
  165.    
  166.     self[1] = r
  167.     self[2] = g
  168.     self[3] = b
  169.     self[4] = a and a or self[4]
  170. end
  171.  
  172. function color:setHue(value)
  173.     local h, s, v, a = self:toHSV(); h = value
  174.     self:fromHSV(h, s, v, a)
  175. end
  176.  
  177. function color:setSat(value)
  178.     local h, s, v, a = self:toHSV(); s = value
  179.     self:fromHSV(h, s, v, a)
  180. end
  181.  
  182. function color:setVal(value)
  183.     local h, s, v, a = self:toHSV(); v = value
  184.     self:fromHSV(h, s, v, a)
  185. end
  186.  
  187. return color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement