Advertisement
ZeeDerp

LampControl

Jun 1st, 2021 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. mount = function (peripheral_type)
  2.     for _,location in pairs(peripheral.getNames()) do
  3.         if peripheral.getType(location) == peripheral_type then
  4.             return peripheral.wrap(location)
  5.         end
  6.     end
  7.     return false
  8. end
  9.  
  10. lamp = mount("modem")
  11. hue = 0.00
  12.  
  13. function toHex(dec)
  14.     return(string.format("%x",dec))
  15.  end
  16.  
  17. function toDec(hex)
  18.     return(tonumber(hex))
  19. end
  20. function hsvToRgb(h, s, v)
  21.     local r, g, b
  22.     local i = math.floor(h * 6);
  23.     local f = h * 6 - i;
  24.     local p = v * (1 - s);
  25.     local q = v * (1 - f * s);
  26.     local t = v * (1 - (1 - f) * s);
  27.     i = i % 6
  28.     if i == 0 then r, g, b = v, t, p
  29.     elseif i == 1 then r, g, b = q, v, p
  30.     elseif i == 2 then r, g, b = p, v, t
  31.     elseif i == 3 then r, g, b = p, q, v
  32.     elseif i == 4 then r, g, b = t, p, v
  33.     elseif i == 5 then r, g, b = v, p, q
  34.     end
  35.     rHex = toHex(math.floor(r*255))
  36.     gHex = toHex(math.floor(g*255))
  37.     bHex = toHex(math.floor(b*255))
  38.     if string.len(rHex) <= 1 then
  39.        rHex = rHex..0
  40.     end
  41.     if string.len(gHex) <= 1 then
  42.        gHex = gHex..0
  43.     end
  44.     if string.len(bHex) <= 1 then
  45.        bHex = bHex..0
  46.     end
  47.     return("0x"..rHex..gHex..bHex)
  48. end
  49. function centerText(text)
  50.     local x,y = term.getSize()
  51.     term.setCursorPos(math.floor((x / 2) - (text:len() / 2)), y)
  52.     write(text)
  53.     end
  54. while not stop do
  55.     term.clear()
  56.     centerText("Color in hex? ")
  57.     color = read()
  58.     term.clear()
  59.     if color == "stop" then stop = true end
  60.     if color == "rainbow" then
  61.         while not true do
  62.             for _,v in pairs(lamp.getNamesRemote()) do
  63.                 local hex = hsvToRgb(hue,1,1)
  64.                 local decimal = toDec(hex)
  65.                 lamp.callRemote(v,"setColor",decimal)
  66.             end
  67.             hue = hue+.01
  68.             sleep(.005)
  69.         end
  70.     elseif color == "something" then
  71.         print()
  72.     else
  73.         for _,v in pairs(lamp.getNamesRemote()) do
  74.             lamp.callRemote(v,"setColor",tonumber(color))
  75.         end
  76.     end
  77. end
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement