66 posts
Posted 25 October 2012 - 01:02 AM
Hello i want to know if someone can make a function that you pass a word(s) and it will select a random color for each letter then write that letter in that colorer the move to the next letter. So it will print rainbow colored text. So if i passed the function "Test" it would print something like this "Test".
1688 posts
Location
'MURICA
Posted 25 October 2012 - 01:34 AM
I'm in a generous mood, so I'll go ahead and throw a small implementation your way.
local function rainbowText(text)
-- define the colors we want in the order we want them to appear.
-- in this case, roygbv
local _colors = {
colors.red,
colors.orange,
colors.yellow,
colors.green,
colors.blue,
colors.purple
}
-- go through all of the letters of text
for i=1, #text do
local char = text:sub(i,i)
-- set the text color to the first color in our _colors table
term.setTextColor(_colors[1])
-- then write the character
write(char)
-- send the first color of our _colors table to the end
-- we've already used it, so we move on to the next one.
table.insert(_colors, table.remove(_colors, 1))
end
end
521 posts
Location
Stockholm, Sweden
Posted 25 October 2012 - 10:40 AM
Or you know the simple way
-- Colors go from 0 to 15, but I dont want black and white
for i = 1, 14 do
term.setTextColor(2^i)
print("#")
end
And if you want to have it as a function
function rainbow(text)
local textTable = {}
local num = 1
for i = 1, #text do
table.insert(textTable, string.sub(text,i,i+1))
end
while #textTable > 0 do
if num == 14 then
num = 1
else
num = num + 1
end
write(textTable[1])
table.remove(textTable[1])
end
print()
end
I've not tested it, but it
should work.
1548 posts
Location
That dark shadow under your bed...
Posted 25 October 2012 - 12:20 PM
Here is a rough code
local function pRainbow(text)
local cols={colors.red;colors.orange;colors.yellow;colors.green;colors.blue;colors.purple}
for x=1,#text do
local colnum=x
while colnum>#cols do colnum=colnum-#cols end
term.setTextColor(cols[colnum])
write(string.sub(text,x,x))
end
end