Advertisement
Guest User

Better Rainbows Script

a guest
Jan 8th, 2017
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. -- Take in a string to rainbow-ify, optionally rainbow type and frequency for type 1
  2. -- str : string to rainbow-ify
  3. -- type : type of rainbow-ification
  4. -- 0 - single cycle of colours, default option
  5. -- 1 - multiple cycles determined by frequency, possible less than a full cycle
  6. -- 2 - Old, ugly, technically rainbowed thing
  7. -- freq : color frequency of rainbow in degrees, 0 to 360, default 15
  8. -- higher freqs produce much shorter cycles of color, lower freqs longer cycles
  9. -- freq of 1 is solid color
  10. -- Return: returns a string suitable for plugging into hecho, colored in the rainbow
  11. -- style chosen
  12. function rainbow(str, type, freq)
  13. -- default settings, set to default if out-of-bounds
  14. type = type or 0
  15. if type > 2 or type < 0 then type = 0 end
  16.  
  17. freq = freq or 15
  18.  
  19. if freq < 0 or freq > 360 then freq = 15 end
  20.  
  21. local product = ""
  22. local phase = 2 * math.pi / 3
  23.  
  24. local rand = betterRand()
  25.  
  26.  
  27. if type == 0 then -- Non-repeating rainbow
  28. freq = 2*math.pi / #str
  29. for i = 1, #str do
  30. if string.sub(str, i, i) == " " then
  31. product = product..string.sub(str, i, i)
  32. else
  33. r = math.sin(freq*i + phase + rand*10) * 127 + 128
  34. g = math.sin(freq*i + 0 + rand*10) * 127 + 128
  35. b = math.sin(freq*i + 2*phase + rand*10) * 127 + 128
  36. r = string.format("%x", r)
  37. g = string.format("%x", g)
  38. b = string.format("%x", b)
  39.  
  40. if #r < 2 then r = "0"..r end
  41. if #g < 2 then g = "0"..g end
  42. if #b < 2 then b = "0"..b end
  43. product = product.."|c"..r..g..b..string.sub(str, i, i)
  44. end
  45. end
  46. elseif type == 1 then -- Repeating rainbow
  47.  
  48. freq = freq * math.pi / 180
  49. for i = 1, #str do
  50. if string.sub(str, i, i) == " " then
  51. product = product..string.sub(str, i, i)
  52. else
  53. r = math.sin(freq*i + phase + rand*10) * 127 + 128
  54. g = math.sin(freq*i + 0 + rand*10) * 127 + 128
  55. b = math.sin(freq*i + 2*phase + rand*10) * 127 + 128
  56. r = string.format("%x", r)
  57. g = string.format("%x", g)
  58. b = string.format("%x", b)
  59.  
  60. if #r < 2 then r = "0"..r end
  61. if #g < 2 then g = "0"..g end
  62. if #b < 2 then b = "0"..b end
  63. product = product.."|c"..r..g..b..string.sub(str, i, i)
  64. end
  65. end
  66. elseif type == 2 then -- Old, uglier rainbow
  67. local colours = { "|cFF0000", "|cFF6600", "|cFFEE00", "|c00FF00", "|c0099FF", "|c4400FF", "|c9900FF" }
  68. local pass = math.random(7)
  69.  
  70. for char in str:gmatch"." do
  71. if char == " " then
  72. product = product .. char
  73. else
  74. product = product .. colours[pass] .. char
  75. if pass == #colours then pass = 1 end
  76. pass = pass + 1
  77. end
  78. end
  79. end
  80. return product.."|r"
  81. end
  82.  
  83.  
  84.  
  85. function betterRand()
  86. randomtable = {}
  87. for i = 1, 97 do
  88. randomtable[i] = math.random()
  89. end
  90. local x = math.random()
  91. local i = 1 + math.floor(97*x)
  92. x, randomtable[i] = randomtable[i], x
  93. return x
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement