Incremental Color Change Over Long Periods

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: heatherlaine, Klaus, FourthWorld, kevinmiller

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Incremental Color Change Over Long Periods

Post by theotherbassist » Thu Mar 06, 2014 9:58 am

Hi, I'm a graduate psychology student in California, working on my thesis design--Livecode was recommended to me by a professor who now doesn't seem to have much time to help out. Basically, my task requires me to be able to do this:

Fade an object (the whole computer screen) from black to brightest representable white incrementally over the course of exactly 23 hours. I wrote something before (though the file got corrupted and I can't remember how I did it) where each of the R,G,B values increased by 1 every so many seconds... but I'm thinking now that it would be better to use system time rather than repeat hundreds of "wait" functions so as not to rely on CPU and preserve timing accuracy. I will also need to have separate functions that vary in rate--that is, separate functions that fade from 0% to 50% white in 11.5 hours, to 25% in 11.5 hours, and to 100% in 11.5 hours. Preferably, I'd also like to link each fading event to a certain hotkey.

I'm a total newbie, but I catch on pretty quickly. I just haven't been able to come up with any relevant threads so far. Any ideas as to how I should approach this?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Thu Mar 06, 2014 11:43 am

Hi theotherbassist,
I'm a graduate dummy in California

Here is your start;

Code: Select all

on mouseUp
   repeat with x = 1 to 255
      set the backgroundcolor of this stack to x,x,x
      add 1 to  x
--you can add wait here
   end repeat
end mouseUp
You can use "the seconds" with a "wait" to get you 11 something hours.

Do you know how to make the stack go full screen?

Please don't post the same request twice.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Thu Mar 06, 2014 11:59 am

Sorry about the duplicate post...

Thanks a lot for the snippet there. I believe that was pretty much the tactic I was using before. Is there a way to mark the initiation of the "fade" according to the system clock and use that as the reference for passing time? I noticed complaints on other posts about CPU lag creating timing issues with "wait."

Klaus
Posts: 10327
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Incremental Color Change Over Long Periods

Post by Klaus » Thu Mar 06, 2014 1:30 pm

Hi all,

1. I deleted the duplicate and will move this thread to the beginner section after I posted°

2. "wait" is SO 2000s :D

Better use SEND here like in this example:

Code: Select all

on mouseup
  ## "Initiate" the process:
  put 1 into x
  
  ## Set for the first time:
  set the backgroundcolor of this stack to x,x,x

  ## Now deliver the next setting of the bagcolor in some time in the future
  ## I will use 15 minutes here
  put 15*60 into tNextDelivery

  send "do_nexsetting x" to me in tNextDelivery secs
end mouseup

command do_nexsetting tValue
  ## We did not reach the last possible value yet, so:
  if tValue < 255 then
    add 1 to tValue
    put 15*60 into tNextDelivery
    send "do_nexsetting x" to me in tNextDelivery secs
  end if
end do_nexsetting
You get the picture! :D


Best

Klaus

P.S.
Are you a bass player? Or is it just a silly nickname? :lol:
I am a bass player, see my webpage :D

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4615
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Thu Mar 06, 2014 5:54 pm

Klaus is right, you really don't want to use a repeat loop in a handler that is going to run over the course of a whole day. Repeat loops are blocking, and will stop everything else on the computer from working. Nothing will respond until the loop is done, background tasks will hang, clicks won't register. Eventually the OS will put up a notice that the app is not responding and you'll need to force-quit. You can add a "wait with messages" command that alleviates some of that, but in this case the "send" structure is far more friendly.

In general, avoid repeat loops that will take longer than one second or so.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Thu Mar 06, 2014 7:11 pm

Thanks for the great informed replies, guys. I'm going to try to get this working later today.

And, yes, I am a bass player--though it's been some time since I've been playing with a band. That was years ago back in Michigan... I'll check out your site, Klaus.

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 2:05 am

I used your code, Klaus. However, I'm not seeing any changes in the background color over time. I tried editing the 15*60 to much lower numbers in order to see changes faster too... am I missing something?--Livecode syntax is so simple that I must simply be failing to think about it in the correct way. I apologize for my lack of competence here; I'm not really able to distinguish whether I was supposed to have declared some variables/functions prior to this or where I should be placing the code (I tried the stack).

The background color sets correctly... then no changes. Does anyone mind indulging me a bit by going into a little more detail about how the "send," tNextDelivery," and "do_nextsetting" work and how I should be using them, and what the "tValue" is representing? Thank you much--I really appreciate it.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 2:27 am

Hi theotherbassist,
This is getting a bit messy but

Code: Select all

command do_nexsetting tValue
   set the backgroundcolor of this stack to tValue,tValue,tValue
  ## We did not reach the last possible value yet, so:
  if tValue < 255 then
    add 1 to tValue
    put 15*60 into tNextDelivery
    send "do_nexsetting tValue" to me in tNextDelivery secs
  end if
end do_nexsetting
Do you see how that "send" will loop 254 times?
Needs a clean up.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 2:47 am

Simon, that's really close to what I'm going for--thanks. I assume I need to swap that in at the "command do_nex..." bit of the code Klaus provided? That's what I did, and it seems to work except that it is pulsating back and forth... it goes up in brightness then back down, though not quite as far down. So it is gradually working its way up to full 255,255,255 but in a strange way. Does that make sense?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 2:58 am

Hi theotherbassist,
It really shouldn't be going back down to black.
Throw this into the if/then
if tValue > 253 then breakpoint
While stepping through that number should never go down.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 3:13 am

That seems to do what I need. However, if I click again by accident at any point, the screen runs another function at the same time and you get the weird oscillations. How can I make the loop ignore subsequent clicks (and, also, how can I throw in a hotkey that pauses the loop?)?

Here's what I have:

Code: Select all

on mouseup
  ## "Initiate" the process:
  put 1 into x
  
  ## Set for the first time:
  set the backgroundcolor of this stack to x,x,x

  ## Now deliver the next setting of the bagcolor in some time in the future
  ## I will use 15 minutes here
  put 1 into tNextDelivery

  send "do_nexsetting x" to me in tNextDelivery secs
end mouseup

command do_nexsetting tValue
   set the backgroundcolor of this stack to tValue,tValue,tValue
  ## We did not reach the last possible value yet, so:
   if tValue < 255 then
      if tValue > 253 then breakpoint
    add 1 to tValue
    put 1 into tNextDelivery
    send "do_nexsetting tValue" to me in tNextDelivery secs
  end if
end do_nexsetting
I replaced the 15*60 instances in Klaus's code with 1 values so the changes would occur faster for testing purposes. My version seems like a cobbled-together mess--is it?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 3:54 am

Yeah this is getting messy and could be cleaned up a bit.
Here quick and dirty

Code: Select all

global tContinue
on mouseup
   If tContinue <> true then
      put true into tContinue
      send "do_nexsetting 1" to me in 1 secs
   end if
end mouseup

command do_nexsetting tValue
   if tContinue = true then
      set the backgroundcolor of this stack to tValue,tValue,tValue
      ## We did not reach the last possible value yet, so:
      if tValue < 255 then
         if tValue > 253 then breakpoint
         add 1 to tValue
         put 1 into tNextDelivery
         send "do_nexsetting tValue" to me in tNextDelivery secs
      end if
   end if
end do_nexsetting
And in the card script

Code: Select all

on optionKeyDown tkey
   global tContinue
   put false into tContinue
end optionKeyDown
This situation shows up all the time and I know there must be cleaner ways of doing it.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 5:12 am

I'm really amazed by the great timely responses on these forums. Simon: you might call it quick and dirty, but I would say it's pretty elegant compared to what I had cooked up. Kudos.

Why does the optionkeydown initiate the "false" even when I only press Alt alone? I thought that it was supposed to be alt+another key that produces a character.

I was hoping to write in a specific optionkeydown key to put false into tContinue and a separate one to put true back into tContinue, in case I decided to resume where I left off. If that works, how do I then continue without resetting the background color to the starting value?

Further, (and I know all of my questions showcase just how much I know nothing about this) how can I make the stack/card run fullscreen but ONLY when it is transitioning from black to white?

And, finally, the finished product will need to be a bit trickier--how can I have multiple cards like this one that function exactly the same but run at different rates and for different times?--I know how I would change the variables, I just need to be able to have the program open up with a screen housing a MENU that has 4 options leading to those cards--clicking option 1 results in the fullscreen color change over 23 hours--options 2, 3, & 4 all function the same but over 11.5 hours at different rates. Each should be pausable and have the ability at that point to either continue or send the user back to the options menu.

Sorry about all the questions, but that is really all I'm trying to accomplish with the final product. It will be used as part of a stimulus training package in a psychological study.

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 5:37 am

Or I suppose I could actually just call different loops--using different hotkeys or buttons--that are placed within the same card, each having a different rate and maximum time/RGB value value attached to it. But I'm open to any suggestions... I think I'm catching on, albeit slowly.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 5:38 am

Hi
I was hoping to write in a specific optionkeydown key to put false into tContinue
See that tKey in there?
Put a breakpoint in and see what tKey holds. You can build an if/then from there.
how do I then continue without resetting the background color to the starting value?
I hate to do it but make tValue a global. Hitting the button will reset to black but using the option keys will act like a pause. Watch out, you can't just put "true" back into tContinue from the optionkeydown.
how can I make the stack/card run fullscreen but ONLY when it
Homework. Look up fullscreen in the dictionary also screenRect and rect.

Moving from card to card using options is too easy I'll let you figure that out. Edit: or using different loops...

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”