68 posts
Location
Land of the Pharos
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
749 posts
Location
BOO!!
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
327 posts
Location
Julfander Squad Studio
Posted 16 October 2018 - 12:30 PM
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 on 16 October 2018 - 10:36 AM
68 posts
Location
Land of the Pharos
Posted 16 October 2018 - 12:41 PM
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!
42 posts
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
327 posts
Location
Julfander Squad Studio
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!
463 posts
Location
Star Wars
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