Advertisement
MagmaLP

colorsAPI

May 26th, 2024
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. -- colorAPI.lua
  2.  
  3. local colors = {
  4.     ["0"] = colors.black,
  5.     ["1"] = colors.blue,
  6.     ["2"] = colors.green,
  7.     ["3"] = colors.cyan,
  8.     ["4"] = colors.red,
  9.     ["5"] = colors.purple,
  10.     ["6"] = colors.orange,
  11.     ["7"] = colors.lightGray,
  12.     ["8"] = colors.gray,
  13.     ["9"] = colors.lightBlue,
  14.     ["a"] = colors.lime,
  15.     ["b"] = colors.lightBlue,
  16.     ["c"] = colors.red,
  17.     ["d"] = colors.magenta,
  18.     ["e"] = colors.yellow,
  19.     ["f"] = colors.white,
  20. }
  21.  
  22. local function msg(text, monitor)
  23.     local x, y = monitor.getCursorPos()
  24.     local originalBgColor = monitor.getBackgroundColor()
  25.     local originalTextColor = monitor.getTextColor()
  26.    
  27.     local i = 1
  28.     while i <= #text do
  29.         local char = text:sub(i, i)
  30.         if char == "&" then
  31.             local nextChar = text:sub(i + 1, i + 1)
  32.             if nextChar == "&" then
  33.                 i = i + 2
  34.                 local colorCode = text:sub(i + 1, i + 1)
  35.                 if colors[colorCode] then
  36.                     monitor.setBackgroundColor(colors[colorCode])
  37.                 end
  38.                 i = i + 1
  39.             else
  40.                 i = i + 1
  41.                 local colorCode = text:sub(i, i)
  42.                 if colors[colorCode] then
  43.                     monitor.setTextColor(colors[colorCode])
  44.                 end
  45.             end
  46.         else
  47.             monitor.write(char)
  48.         end
  49.         i = i + 1
  50.     end
  51.  
  52.     monitor.setTextColor(originalTextColor)
  53.     monitor.setBackgroundColor(originalBgColor)
  54. end
  55.  
  56. return {
  57.     msg = msg
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement