This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dcleondc's profile picture

[Request] rainbow colored text

Started by dcleondc, 24 October 2012 - 11:02 PM
dcleondc #1
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".
Kingdaro #2
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
jag #3
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.
KaoS #4
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