Advertisement
HertzDevil

bbcode sprite

Jun 24th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.59 KB | None | 0 0
  1. print("Create bitmap here: (space denotes transparent cell, empty row ends bitmap)")
  2. local bitmap = {}
  3. local height, width
  4. while true do
  5.   local str = io.read()
  6.   if #str == 0 then break end
  7.   local i = #bitmap + 1
  8.   bitmap[i] = {}
  9.   string.gsub(str, ".", function (s) table.insert(bitmap[i], s) end)
  10.   width = width and math.max(width, #str) or #str
  11. end
  12. height = #bitmap
  13.  
  14. local palette = {}
  15. print("Create palette entries here: (character, red, green, blue)")
  16. print("e.g.: $ 216 192 240")
  17. while true do
  18.   local ch, r, g, b = string.match(io.read(), "^(.) ([0-9]+) ([0-9]+) ([0-9]+)")
  19.   if not ch or ch == " " then break end
  20.   r = math.min(255, math.max(0, tonumber(r)))
  21.   g = math.min(255, math.max(0, tonumber(g)))
  22.   b = math.min(255, math.max(0, tonumber(b)))
  23.   palette[ch] = string.format("#%02X%02X%02X", r, g, b)
  24. end
  25.  
  26. print("Save the BBcode sprite to:")
  27. local f = io.open(io.read(), "w")
  28. if not f then
  29.   print("Invalid filename!")
  30. else
  31.   f:write("[size=150]")
  32.   for _, row in ipairs(bitmap) do
  33.     local str = ""
  34.     local ch = row[1]
  35.     local len = 0
  36.     for i, v in ipairs(row) do
  37.       if v == ch then
  38.         len = len + 1
  39.       else
  40.         str = str .. string.format("[color=%s]%s[/color]", palette[ch] or "transparent", string.rep("\xE2\x96\x88", len * 2))
  41.         ch = v
  42.         len = 1
  43.       end
  44.     end
  45.     if palette[ch] then
  46.       str = str .. string.format("[color=%s]%s[/color]", palette[ch] or "transparent", string.rep("\xE2\x96\x88", len * 2))
  47.     end
  48.     f:write(str, "\n")
  49.   end
  50.   f:write("[/size]")
  51.   f:close()
  52.   print("Saved.")
  53. end
  54. os.execute("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement