Jump to content




How to make a word with two changing colors


  • You cannot reply to this topic
31 replies to this topic

#1 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03: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.

#2 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 02 April 2013 - 03:25 AM

Neat!

#3 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03:33 AM

View PostDlcruz129, on 02 April 2013 - 03:25 AM, said:

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

Attached Thumbnails

  • Attached Image: Capture.PNG


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 03: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

#5 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03:36 AM

View Posttheoriginalbit, on 02 April 2013 - 03:34 AM, said:

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

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 03:39 AM

View PostSuicidalSTDz, on 02 April 2013 - 03:36 AM, said:

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...n-green-and-red

View PostSuicidalSTDz, on 02 April 2013 - 03:36 AM, said:

I will update the main post to be more 'novice' friendly :P
good idea. :P

#7 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03:43 AM

View Posttheoriginalbit, on 02 April 2013 - 03:39 AM, said:

View PostSuicidalSTDz, on 02 April 2013 - 03:36 AM, said:

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.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 03:44 AM

View PostSuicidalSTDz, on 02 April 2013 - 03:43 AM, said:

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


#9 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03:46 AM

View Posttheoriginalbit, on 02 April 2013 - 03:44 AM, said:

View PostSuicidalSTDz, on 02 April 2013 - 03:43 AM, said:

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.

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 03:48 AM

View PostSuicidalSTDz, on 02 April 2013 - 03:46 AM, said:

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

#11 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03:50 AM

View Posttheoriginalbit, on 02 April 2013 - 03:48 AM, said:

View PostSuicidalSTDz, on 02 April 2013 - 03:46 AM, said:

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 ;)

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 03:52 AM

View PostSuicidalSTDz, on 02 April 2013 - 03:50 AM, said:

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 :)

#13 BigSHinyToys

  • Members
  • 1,001 posts

Posted 02 April 2013 - 04:48 AM

I think line 6 is redundant and I personally would use a for i = 1,# do term.write(sub) end
Example
Spoiler
Other than that nice code.

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 April 2013 - 04:53 AM

View PostBigSHinyToys, on 02 April 2013 - 04:48 AM, said:

I think line 6 is redundant
good pickup... I didn't notice that :P

#15 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 05: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 :)

#16 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 02 April 2013 - 02:31 PM

View PostSuicidalSTDz, on 02 April 2013 - 03:33 AM, said:

EDIT: Hmm, nice Title. Congrats on the big 1,000 btw :P/>

Thanks!

#17 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 03:23 PM

No, thank you ^_^ (No idea why i'm thanking you, but i'm doing it any way!)

#18 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 02 April 2013 - 11:41 PM

This is pretty cool... as long as we don't start seeing rainbow text everywhere :P

#19 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 April 2013 - 11:55 PM

View Postoeed, on 02 April 2013 - 11:41 PM, said:

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*

#20 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 03 April 2013 - 01: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 )






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users