Advertisement
Guest User

Untitled

a guest
May 28th, 2017
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. --Make Gradient library for TIC-80. Useful for effects such as changing color
  2. --on scanline or palette animation, all in just over 1600 bytes (except this info).
  3. --Two functions you need to be aware of: make_gradient and make gradient_direct.
  4. --Both take the same parameters (r1,g1,b1,r2,g2,b2,steps) where first six are two
  5. --sets of rgb values of opposite colors and the final is how long the gradient will
  6. --be (in pixels) and both return an array of rgb values (as records, usable by
  7. --out[index].r,etc.). However make_gradient also first converts color to HSV, which is
  8. --usable when you want a rainbow-like pattern.
  9.  
  10. function lerp(t,a,b)
  11.     return a+(b-a)*t
  12. end
  13.  
  14. function rgbToHsv(r, g, b)
  15.   r,g,b=r/255,g/255,b/255
  16.   local max,min=math.max(r,g,b),math.min(r,g,b)
  17.   local h,s,v
  18.   v=max
  19.   local d=max-min
  20.   if max==0 then s=0 else s=d/max end
  21.   if max==min then
  22.     h=0
  23.   else
  24.     if max==r then
  25.     h=(g-b)/d
  26.     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
  27.     h=h/6
  28.   end
  29.   return h,s,v
  30. end
  31.  
  32. function hsvToRgb(h,s,v)
  33.   local r,g,b
  34.   local i=math.floor(h*6);
  35.   local f=h*6-i;
  36.   local p=v*(1-s);
  37.   local q=v*(1-f*s);
  38.   local t=v*(1-(1-f)*s);
  39.         i=i%6
  40.         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
  41.   return math.floor(r*255),math.floor(g*255),math.floor(b*255)
  42. end
  43.  
  44. function make_gradient(r1,g1,b1,r2,g2,b2,steps)
  45. steps = math.abs(steps)
  46. local out = {}
  47. local h1=0
  48. local s1=0
  49. local v1=0
  50. h1,s1,v1=rgbToHsv(r1,g1,b1)
  51. local h2=0
  52. local s2=0
  53. local v2=0
  54. h2,s2,v2=rgbToHsv(r2,g2,b2)
  55. local stepamount = 1/steps
  56. local prog =0
  57. local i=0
  58.     for i=1,steps,1 do
  59.         local temph=lerp(prog,h1,h2)
  60.         local temps=lerp(prog,s1,s2)
  61.         local tempv=lerp(prog,v1,v2)
  62.         out[i]={}
  63.         out[i].r,out[i].g,out[i].b=hsvToRgb(temph,temps,tempv)
  64.         prog=prog+stepamount
  65.     end
  66. return out 
  67. end
  68. function make_gradient_direct(r1,g1,b1,r2,g2,b2,steps)
  69. steps = math.abs(steps)
  70. local out = {}
  71. local stepamount = 1/steps
  72. local prog =0
  73. local i=0
  74.     for i=1,steps,1 do
  75.         out[i]={}
  76.         out[i].r=lerp(prog,r1,r2)
  77.         out[i].g=lerp(prog,g1,g2)
  78.         out[i].b=lerp(prog,b1,b2)
  79.         prog=prog+stepamount
  80.     end
  81. return out 
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement