toggle the text color in a field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

toggle the text color in a field

Post by keram » Fri Jan 23, 2015 3:38 am

Hi,

Here is a very basic task that I want to accomplish:
Toggle the color of the text line in a field. Trying with the code below but it doesn't work.

Code: Select all

on mouseUp
   if the textColor of (clickLine())  = "blue"  then 
      set the textColor of  (clickLine()) to "red"
   else 
      set the textColor of  (clickLine())  to "blue"
   end if
end mouseUp
What's wrong with that code?

keram
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: toggle the text color in a field

Post by FourthWorld » Fri Jan 23, 2015 5:35 am

If you check the value of the textColor you'll see that although LC allows you to use a variety of constants to assign colors, the actually stored value for colors is RGB. In the case of blue that's 0,0,255, so this change makes it work:

Code: Select all

   if the textColor of (clickLine())  = "0,0,255"  then
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: toggle the text color in a field

Post by keram » Fri Jan 23, 2015 10:50 am

Thanks Richard,

It works OK now. Still it's surprising that I only had to change the first line, the other two with the color names remained the same and the toggling works. Also if I put this line: set textColor of fld "d" to blue into a button it will change the text color in field d. So it seems that sometimes it works with just names of colors and sometimes not.
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm

Re: toggle the text color in a field

Post by paul_gr » Fri Jan 23, 2015 11:02 am

Watch out for empty lines -- If you click on an empty line you will get an error message.
That is because the clickline will not return a value.
I've added another line that will exit to top if you click on an empty line.

------------------------------------------------------------------
on mouseUp
if the value of the clickline is empty then exit to top

if the textColor of (clickLine()) = "0,0,255" then
set the textColor of (clickLine()) to "red"
else
set the textColor of (clickLine()) to "blue"
end if
end mouseUp
------------------------------------------------------------------
attached example.
Try commenting out the line I inserted and click below the bottom text on an empty line.

Paul
Attachments
toggletextcolor.zip
(1.32 KiB) Downloaded 232 times

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: toggle the text color in a field

Post by keram » Fri Jan 23, 2015 11:23 am

Thanks Paul,

It's good to know what you added.
I added 1 empty line between these 5 lines but not after the last one (yz). Then commented out your added line and still when clicking on the empty lines between the lines with the 'words' there is no error message. But clicking below the last one does create an error message (see the stack).

An additional comment to Richard's post:
If I create a field with a text in it and put this code into the field's script:

Code: Select all

on mouseUp
     if the textColor of me  = "blue"  then 
      set the textColor of  me to "red"
   else 
      set the textColor of  me  to "blue"
   end if
   end mouseUp
then the toggling works OK even though I did not use the numerical values for colors. There is some inconsistency there...

keram
Attachments
toggletextcolor2.zip
(1.34 KiB) Downloaded 220 times
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: toggle the text color in a field

Post by FourthWorld » Tue Jan 27, 2015 6:46 pm

Good find, Keram. Bug report filed:
http://quality.runrev.com/show_bug.cgi?id=14459
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply