Advertisement
Guest User

jupiter2

a guest
Sep 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. print("Let there be lights")
  2.  
  3. --# Choose a brightness between 1-10
  4. local brightness = 7
  5. --# Choose Fade Time (s)
  6. local fade = 0
  7. --# Choose delay (s)
  8. local delay = 10
  9.  
  10. ------------------- ignore ----------------
  11. fade = fade/10
  12. local tbl = {} -- Declare tbl as a table to store lamps in
  13. local rNext, gNext, bNext = 100, 250, 250 -- The next random color
  14. local rCur, gCur, bCur = 100, 250, 250 -- current color
  15.  
  16. --#Makes a decimal number hexdecimal
  17. function intToHex(num)
  18.     return string.format("%x", num)
  19. end
  20.  
  21. --#Keeps numbers from breaking everything
  22. function rgbreg(num)
  23.     if num > 255 then return 255
  24.     elseif num < 0 then return 0
  25.     else return num end
  26. end
  27.  
  28. --#Turns seperate rgb values into hexidecimal form
  29. function rgbToHex(r,g,b)
  30.     rhex = intToHex(r)
  31.     ghex = intToHex(g)
  32.     bhex = intToHex(b)
  33.     hexstring = rhex .. ghex .. bhex
  34.     return tonumber(hexstring, 16)
  35. end
  36.  
  37. --# returns a random rgb color
  38. function randomColor()
  39.     lum = rgbreg(math.floor(brightness*25.5))
  40.     r = math.random(lum,255)
  41.     g = math.random(lum,255)
  42.     b = math.random(lum,255)   
  43.     return rgbreg(r),rgbreg(g),rgbreg(b)
  44. end
  45.  
  46. --#Find and automatically wrap lights.
  47. for a,v in pairs(peripheral.getNames()) do -- Start loop
  48.   if (peripheral.getType(v) == "thermalexpansion_light" ) then -- If lights are detected then continue
  49.     tbl[#tbl + 1] = peripheral.wrap(v) -- Add lamps to table
  50.   end
  51. end
  52.  
  53. --#Initialize lights
  54. for i = 1,#tbl do
  55.   tbl[i].setColor(rgbToHex(rCur, gCur, bCur))  
  56. end
  57.  
  58. while true do -- Start loop
  59.     rs.setOutput("right",true)
  60.     -- Choose a new random color
  61.     rNext, gNext, bNext = randomColor()
  62.     sleep(delay)   
  63. -- fade the colors 'Cur' into colors 'Next'
  64.     rStep,gStep,bStep = (rNext-rCur)/20,(gNext-gCur)/20,(bNext-bCur)/20
  65.     -- Creates more precise version of current color
  66.     rCurP, gCurP, bCurP = rCur, gCur, bCur
  67.     for i = 1,20 do
  68.         rCurP = rCurP + rStep
  69.         gCurP = gCurP + gStep
  70.         bCurP = bCurP + bStep
  71.         rCur, gCur, bCur = math.floor(rCurP),math.floor(gCurP),math.floor(bCurP)
  72.         curColor = rgbToHex(rCur, gCur, bCur)
  73.         for i = 1,#tbl do
  74.             tbl[i].setColor(curColor)
  75.         end
  76.         sleep(fade)
  77.     end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement