1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:23 AM
Did you ever want a word with two colors every other letter but could never figure out how to do it?
Well here you go!
local word = "Insert your word here " --Leave one space so the cursor doesnt obscure the word
local firstCol = colours.red
local secondCol = colours.blue
for len = 1,#word do
if bit.band(len,1) == 0 then --This line will detect if the current char is on an odd or even column. Odd will be red and Even will be blue in this case
term.setTextColour(firstCol)
else
term.setTextColour(secondCol)
end
term.write(string.sub(word,len,len)) --Sub the string to the current character
end
This is a very simple script but I thought some users might benefit from this.
1619 posts
Posted 02 April 2013 - 04:25 AM
Neat!
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:33 AM
Neat!
I got the idea when I was doing this for my OS. Thanks for the feedback :)/>
EDIT: Hmm, nice Title. Congrats on the big 1,000 btw :P/>
7508 posts
Location
Australia
Posted 02 April 2013 - 04:34 AM
I bit more of a description of how the code works would be nice, so that its more of a tutorial.
interesting take on it though… what if I wanted more changes? :P/> I did actually work on a version of this with a similar style to C's printf where a string would be formatted like so "\\FThis is one colour, black I think \\Ethis is another colour read I think\\2 this is another again, orange this time"
so you use the \\ and HEX combo to change the text colour. you could also do \\FE to set text and background colour. but I ended up losing the code and couldn't be bothered doing it again :P/>
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:36 AM
I bit more of a description of how the code works would be nice, so that its more of a tutorial.
interesting take on it though… what if I wanted more changes? :P/> I did actually work on a version of this with a similar style to C's printf where a string would be formatted like so "\\FThis is one colour, black I think \\Ethis is another colour read I think\\2 this is another again, orange this time"
so you use the \\ and HEX combo to change the text colour. you could also do \\FE to set text and background colour. but I ended up losing the code and couldn't be bothered doing it again :P/>
Lol, nice. It is an odd way of doing interchanging colors using the bit operator. Works like a charm though :)/> I will update the main post to be more 'novice' friendly :P/>
EDIT: If you wanted more colors.. I would have to add in some new code. Not sure right now how to go about that. I'm sure it is quite easy though :P/>
7508 posts
Location
Australia
Posted 02 April 2013 - 04:39 AM
Lol, nice. It is an odd way of doing interchanging colors using the bit operator. Works like a charm though :)/>
Bit operator? o.O I used HEX as it allowed all 16 colours in 1 letter as opposed to all the longer numbers.
Have a look at answer #2… it is how you change colour and emphasis in C for the text (can be used on terminals) and it is what I kinda based it off
http://stackoverflow.com/questions/1961209/making-some-text-in-printf-appear-in-green-and-redI will update the main post to be more 'novice' friendly :P/>
good idea. :P/>
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:43 AM
Lol, nice. It is an odd way of doing interchanging colors using the bit operator. Works like a charm though :)/>
Bit operator?
Lol, my code. I know you're using HEX. I'm using the bit operator to detect if the current char in the word is in an even or odd column.
RE TO LINK: Things are much more compactable in C than in Lua :P/>
I'd like to see if someone could compact this code actually.
7508 posts
Location
Australia
Posted 02 April 2013 - 04:44 AM
Lol, my code. I know you're using HEX. I'm using the bit operator to detect if the current char in the word is in an even or odd column.
Ahh. just curious why didn't you use %?
function isEven(num)
return num % 2 == 0
end
function isOdd(num)
return not isEven(num)
end
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:46 AM
Lol, my code. I know you're using HEX. I'm using the bit operator to detect if the current char in the word is in an even or odd column.
Ahh. just curious why didn't you use %?
function isEven(num)
return num % 2 == 0
end
function isOdd(num)
return not isEven(num)
end
The modulo operator? Idk, just like using the bitwise operator better. Even though the modulo is faster :P/>
EDIT: They both take one line up so compaction doesn't matter, just the speed. Even then, the bitwise is still pretty fast and the speed difference would only be seen with large words.
7508 posts
Location
Australia
Posted 02 April 2013 - 04:48 AM
The modulo operator? Idk, just like using the bitwise operator better. Even though the modulo is faster :P/>
Lol… I think the % might use the bitwise operator meaning doing the bitwise would be quicker… not too sure on the impl of it tho :P/>
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:50 AM
The modulo operator? Idk, just like using the bitwise operator better. Even though the modulo is faster :P/>
Lol… I think the % might use the bitwise operator meaning doing the bitwise would be quicker… not too sure on the impl of it tho :P/>
Meh :P/> The point is it works :D/> An odd way of doing it. I'm sure clueless members will love this since they would just do this:
term.setTextColour(colours.red)
term.write("T")
term.setTextColour(colours.blue)
term.write("E")
Well, you get the point ;)/>
7508 posts
Location
Australia
Posted 02 April 2013 - 04:52 AM
Meh :P/> The point is it works :D/> An odd way of doing it. I'm sure clueless members will love this since they would just do this:
term.setTextColour(colours.red)
term.write("T")
term.setTextColour(colours.blue)
term.write("E")
Well, you get the point ;)/>
Yeh its much better :)/>
992 posts
Posted 02 April 2013 - 05:48 AM
I think line 6 is redundant and I personally would use a for i = 1,# do term.write(sub) end
Example
Spoiler
local word = "Insert your word here " --Leave one space so the cursor doesnt obscure the word
local firstCol = colours.red
local secondCol = colours.blue
for i = 1,#word do
if bit.band(i,1) == 0 then --This line will detect if the current char is on an odd or even column. Odd will be red and Even will be blue in this case
term.setTextColour(firstCol)
else
term.setTextColour(secondCol)
end
term.write(string.sub(word,i,i)) --Sub the string to the current character using the len variable defined earlier
end
Other than that nice code.
7508 posts
Location
Australia
Posted 02 April 2013 - 05:53 AM
I think line 6 is redundant
good pickup… I didn't notice that :P/>
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 06:15 AM
I didn't notice that either. Good eye :P/> Changing that now. (I must've pasted my first attempt at it using the repeat loop, I further realized it was not necessary)
Thanks for the feedback btw :)/>
1619 posts
Posted 02 April 2013 - 03:31 PM
EDIT: Hmm, nice Title. Congrats on the big 1,000 btw :P/>/>
Thanks!
1511 posts
Location
Pennsylvania
Posted 02 April 2013 - 04:23 PM
No, thank you ^_^/> (No idea why i'm thanking you, but i'm doing it any way!)
2151 posts
Location
Auckland, New Zealand
Posted 03 April 2013 - 12:41 AM
This is pretty cool… as long as we don't start seeing rainbow text everywhere :P/>
1511 posts
Location
Pennsylvania
Posted 03 April 2013 - 12:55 AM
This is pretty cool… as long as we don't start seeing rainbow text everywhere :P/>
Hehe
*SuicidalSTDz quickly codes in rainbow text as it is very possible*
2088 posts
Location
South Africa
Posted 03 April 2013 - 02:03 AM
Why not rainbow text!
local function rainbowText( word, x, y, ... )
local colSel = 1
term.setCursorPos( x, y )
for i = 1, #word do
term.setTextColour( arg[colSel] )
write( word:sub( i, i ) )
colSel = word:sub( i, i ) ~= ' ' and ( colSel >= #arg and 1 or colSel + 1 ) or colSel
end
end
term.clear()
rainbowText( 'Your word/phrase here\n', 3, 2, colours.lime, colours.blue, colours.yellow, colours.purple )
1511 posts
Location
Pennsylvania
Posted 12 April 2013 - 02:58 PM
Why not rainbow text!
I had recently made a program to do this in the programs section. Go check it out :)/> It can also be a substitution for this code as it can handle any amount of colours. Didn't realize you posted this, i've been very busy :P/>
47 posts
Posted 14 April 2013 - 10:35 AM
I have an Error: "Color:8: Colour not supported"
My Code:
[color=#000088]local[/color][color=#000000] word [/color][color=#666600]=[/color][color=#000000] [/color][color=#008800]"Insert your word here "[/color][color=#000000] [/color][color=#666600]--[/color][color=#660066]Leave[/color][color=#000000] one space so the cursor doesnt obscure the word
[/color][color=#000088]local[/color][color=#000000] firstCol [/color][color=#666600]=[/color][color=#000000] colours[/color][color=#666600].[/color][color=#000000]red
[/color][color=#000088]local[/color][color=#000000] secondCol [/color][color=#666600]=[/color][color=#000000] colours[/color][color=#666600].[/color][color=#000000]blue
[/color][color=#000088]
for[/color][color=#000000] len [/color][color=#666600]=[/color][color=#000000] [/color][color=#006666]1[/color][color=#666600],[/color][color=#880000]#word do[/color]
[color=#000000] [/color][color=#000088]if[/color][color=#000000] bit[/color][color=#666600].[/color][color=#000000]band[/color][color=#666600]([/color][color=#000000]len[/color][color=#666600],[/color][color=#006666]1[/color][color=#666600])[/color][color=#000000] [/color][color=#666600]==[/color][color=#000000] [/color][color=#006666]0[/color][color=#000000] [/color][color=#000088]then[/color][color=#000000] [/color][color=#666600]--[/color][color=#660066]This[/color][color=#000000] line will detect [/color][color=#000088]if[/color][color=#000000] the current [/color][color=#000088]char[/color][color=#000000] [/color][color=#000088]is[/color][color=#000000] on an odd [/color][color=#000088]or[/color][color=#000000] even column[/color][color=#666600].[/color][color=#000000] [/color][color=#660066]Odd[/color][color=#000000] will be red [/color][color=#000088]and[/color][color=#000000] [/color][color=#660066]Even[/color][color=#000000] will be blue [/color][color=#000088]in[/color][color=#000000] [/color][color=#000088]this[/color][color=#000000] [/color][color=#000088]case[/color]
[color=#000000] term[/color][color=#666600].[/color][color=#000000]setTextColour[/color][color=#666600]([/color][color=#000000]firstCol[/color][color=#666600])[/color]
[color=#000000] [/color][color=#000088]else[/color]
[color=#000000] term[/color][color=#666600].[/color][color=#000000]setTextColour[/color][color=#666600]([/color][color=#000000]secondCol[/color][color=#666600])[/color]
[color=#000000] [/color][color=#000088]end[/color]
[color=#000000] term[/color][color=#666600].[/color][color=#000000]write[/color][color=#666600]([/color][color=#000088]string[/color][color=#666600].[/color][color=#000088]sub[/color][color=#666600]([/color][color=#000000]word[/color][color=#666600],[/color][color=#000000]len[/color][color=#666600],[/color][color=#000000]len[/color][color=#666600]))[/color][color=#000000] [/color][color=#666600]--[/color][color=#660066]Sub[/color][color=#000000] the [/color][color=#000088]string[/color][color=#000000] to the current character
[/color][color=#000088]end[/color]
Could anyone Help?
1511 posts
Location
Pennsylvania
Posted 14 April 2013 - 12:31 PM
Are you using a non-advanced computer?
645 posts
Location
'Merica
Posted 14 April 2013 - 04:14 PM
^
He has to be, since that is the only way he can get that error, but nice tutorial!
1511 posts
Location
Pennsylvania
Posted 14 April 2013 - 04:26 PM
-snippity snip snip-
nice tutorial!
Thanks ^_^/>
47 posts
Posted 15 April 2013 - 07:23 AM
I was using a Normal Computer. Also awesome Tutorial!
47 posts
Posted 15 April 2013 - 07:38 AM
Are you Supposed to enter your own Word?
This is what happen's for Me:
1511 posts
Location
Pennsylvania
Posted 15 April 2013 - 07:43 AM
You must change the string in the word variable to your own word:
local word = "Test " --Leave one space so the cursor doesnt obscure the word
47 posts
Posted 15 April 2013 - 07:49 AM
Ah okay. Thank's!
645 posts
Location
'Merica
Posted 15 April 2013 - 06:11 PM
Are you Supposed to enter your own Word?
This is what happen's for Me:
Looks like you shovel and sword are about to break… you might want to repair them. :P/>
1511 posts
Location
Pennsylvania
Posted 15 April 2013 - 06:19 PM
Looks like you shovel and sword are about to break… you might want to repair them. :P/>
local function foo()
if yourAttentionToDetails > myAttentionSpan then
return true
else
return false
end
end
local ok,err = pcall(foo)
if not ok then
print(err)
end
>attempt to compare number to nil
See what I did there :P/>
645 posts
Location
'Merica
Posted 15 April 2013 - 06:23 PM
^
Lol