Snusmumriken

Lua love2d 11 color lib

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