Advertisement
Guest User

acid

a guest
Apr 25th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. coord = {x = 0, y = 0}
  2. size = {x = 0, y = 0}
  3. time = 0
  4.  
  5. function main()
  6.   mon = peripheral.wrap("left")
  7.   size.x, size.y = mon.getSize()
  8.   local col = 0
  9.   while true do
  10.     sleep(0.05)
  11.     time = time + 0.1
  12.     for x = 1, size.x do
  13.       for y = 1, size.y do
  14.         coord.x = x
  15.         coord.y = y
  16.         col = pixel()
  17.         mon.setCursorPos(x, y)
  18.         mon.setBackgroundColor(getCol(col.r * 255, col.g * 255, col.b * 255))
  19.         mon.write(' ')
  20.       end
  21.     end
  22.   end
  23. end
  24.  
  25. function pixel()
  26.   local col = {}
  27.   col.r = coord.x / size.x
  28.   col.g = coord.y / size.y
  29.   col.b = 0.5 + 0.5 * math.sin(time)
  30.   return col  
  31. end
  32.  
  33. function sqr(x)
  34.   return x*x
  35. end
  36.  
  37. cols = {{240, 240, 240, colors.white},
  38. {242, 178, 51, colors.orange},
  39. {229, 127, 216, colors.magenta},
  40. {153, 178, 242, colors.lightBlue},
  41. {222, 222, 108, colors.yellow},
  42. {127, 204, 25, colors.lime},
  43. {242, 178, 204, colors.pink},
  44. {76, 76, 76, colors.gray},
  45. {153, 153, 153, colors.lightGray},
  46. {76, 153, 178, colors.cyan},
  47. {178, 102, 229, colors.purple},
  48. {51, 102, 204, colors.blue},
  49. {127, 102, 76, colors.brown},
  50. {87, 166, 78, colors.green},
  51. {204, 76, 76, colors.red},
  52. {0, 0, 0, colors.black}}
  53.  
  54. function getCol(r, g, b)
  55.   local dst = 0
  56.   local best = 2147483647
  57.   local col = 0
  58.   for i = 1, #cols do
  59.     dst = sqr(r - cols[i][1]) +
  60.           sqr(g - cols[i][2]) +
  61.           sqr(b - cols[i][3])
  62.     if dst < best then
  63.       best = dst
  64.       col = cols[i][4]
  65.     end
  66.   end
  67.   return col
  68. end
  69.  
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement