Jump to content




[Solved] is rainbow text posible?


6 replies to this topic

#1 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 16 October 2018 - 10: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 by qwerty, 16 October 2018 - 11:42 AM.


#2 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 16 October 2018 - 11:26 AM

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 by EveryOS, 16 October 2018 - 11:27 AM.


#3 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 16 October 2018 - 11:30 AM

There are two ways of doing this:
  • Print it character by character, where everyone has randomized color
  • 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 by Jummit, 16 October 2018 - 11:36 AM.


#4 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 16 October 2018 - 11:41 AM

View PostJummit, on 16 October 2018 - 11:30 AM, said:

There are two ways of doing this:
  • Print it character by character, where everyone has randomized color
  • 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!

#5 magiczocker

  • Members
  • 46 posts

Posted 16 October 2018 - 01:10 PM

View PostJummit, on 16 October 2018 - 11:30 AM, said:

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 by magiczocker, 16 October 2018 - 01:10 PM.


#6 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 16 October 2018 - 02:15 PM

View Postmagiczocker, on 16 October 2018 - 01:10 PM, said:

View PostJummit, on 16 October 2018 - 11:30 AM, said:

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!

#7 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 17 October 2018 - 04:38 PM

View PostJummit, on 16 October 2018 - 11:30 AM, said:

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 by Sewbacca, 17 October 2018 - 04:40 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users