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

[Solved] is rainbow text posible?

Started by qwerty, 16 October 2018 - 09:53 AM
qwerty #1
Posted 16 October 2018 - 11:53 AM
I had thought of making a program whose title would always be written in randomised colors but I just can't get it to work…
I tried

local write = term.write
term.write = function(text)
  term.setTextColor(c[math.random(1, 15)])
  write(text)
end
where c is a table containing color values,
but this only changed the entire text - far from my vision of single character randomised color.
thus I've been drawn towards ask a pro for advice on randomising text colors per character.

Sincerely:
Qwerty.
Edited on 16 October 2018 - 10:42 AM
EveryOS #2
Posted 16 October 2018 - 12:26 PM

local owrite = write
write = function(text)
  for i=1, #text do
	term.setTextColor(c[math.random(1, 15)])
	owrite(string.sub(text, i, i))
  end
end
I modified it to be just write so that you have line wrapping BTW
Edited on 16 October 2018 - 10:27 AM
Jummit #3
Posted 16 October 2018 - 12:30 PM
There are two ways of doing this:
  1. Print it character by character, where everyone has randomized color
  2. Generate a randomized string and write it at once using term.blit (it takes three strings, one for the text, and two for the foreground an background color)
Way one:

function rainbowWrite(text)
	local cx, cy = term.getCursorPos()
	for x = 1, #text do
		term.setCursorPos(cx+x-1, cy)
		term.setTextColor(c[math.random(1, 15)])
		term.write(string.sub(text, x, x))
	end
end
Way 2:
Your c table has to be filled with paint color codes

function rainbowWrite(text)
   local textColorString = ""
   local backgroundColorString = ""
   for x = 1, #text do
	  colorString = colorString..c[math.random(1, 15)]
	  backgroundColorString = backgroundColorString.."0"
	  --# the background will always be 0
   end
   term.blit(text, textColorString, backgroundColorString)
end
Edited on 16 October 2018 - 10:36 AM
qwerty #4
Posted 16 October 2018 - 12:41 PM
There are two ways of doing this:
  1. Print it character by character, where everyone has randomized color
  2. Generate a randomized string and write it at once using term.blit (it takes three strings, one for the text, and two for the foreground an background color)
Way one:

function rainbowWrite(text)
	local cx, cy = term.getCursorPos()
	for x = 1, #text do
		term.setCursorPos(cx+x-1, cy)
		term.setTextColor(c[math.random(1, 15)])
		term.write(string.sub(text, x, x))
	end
end
Way 2:
Your c table has to be filled with paint color codes

function rainbowWrite(text)
   local textColorString = ""
   local backgroundColorString = ""
   for x = 1, #text do
	  colorString = colorString..c[math.random(1, 15)]
	  backgroundColorString = backgroundColorString.."0"
	  --# the background will always be 0
   end
   term.blit(text, textColorString, backgroundColorString)
end

Thank you very much! the response came way quicker than expected and you helped me out very much, kind Sir!
magiczocker #5
Posted 16 October 2018 - 02:10 PM
Way one:

function rainbowWrite(text)
	local cx, cy = term.getCursorPos()
	for x = 1, #text do
		term.setCursorPos(cx+x-1, cy)
		term.setTextColor(c[math.random(1, 15)])
		term.write(string.sub(text, x, x))
	end
end

You don't need to change the cursor pos, because term.write change it automatically.
Edited on 16 October 2018 - 12:10 PM
Jummit #6
Posted 16 October 2018 - 03:15 PM
Way one:

function rainbowWrite(text)
	local cx, cy = term.getCursorPos()
	for x = 1, #text do
		term.setCursorPos(cx+x-1, cy)
		term.setTextColor(c[math.random(1, 15)])
		term.write(string.sub(text, x, x))
	end
end

You don't need to change the cursor pos, because term.write change it automatically.
Good find!
Sewbacca #7
Posted 17 October 2018 - 05:38 PM
Way 2:
Your c table has to be filled with paint color codes

function rainbowWrite(text)
   local textColorString = ""
   local backgroundColorString = ""
   for x = 1, #text do
	  colorString = colorString..c[math.random(1, 15)]
	  backgroundColorString = backgroundColorString.."0"
	  --# the background will always be 0
   end
   term.blit(text, textColorString, backgroundColorString)
end




Well, I'm bored (more precisly, i have other stuff to do, but I'm lazy or procrastinating), and found another way, using gsub. It is maybe faster or shorter than your second way:

local cols = "0123456789abcdef"
local text = "Hello world!"
local function randomReplace(match)
	local i = math.random(1, 16)
	return cols:sub(i, i)
end
term.blit(text, text:gsub(".", randomReplace), text:gsub(".", randomReplace))
Edited on 17 October 2018 - 03:40 PM