Author Topic: Colour cycling script.  (Read 3736 times)

Luke_Nukem

  • Sr. Member
  • ****
  • Posts: 135
    • View Profile
    • Blogging about Rust lang
Colour cycling script.
« on: June 06, 2014, 06:58:27 PM »
Hi guys. Thought you might like to have a nosey at my script for cycling through colours for an item.
Pretty simple I guess, uses a tick timer and a counter. Set up as 3 individual elements for each colour.

Code: [Select]
fe.add_ticks_callback( "tick" );
//
local swR = 0;
local swG = 0;
local swB = 0;
local romListRed = 0;
local romListGreen = 0;
local romListBlue = 0;
local redTime = 0;
local greenTime = 0;
local blueTime = 0;

function tick( ttime ) {

if (redTime == 0)
redTime = ttime;
if (greenTime == 0)
greenTime = ttime;
if (blueTime == 0)
blueTime = ttime;
/////////////////////////////////////////////////////////
if (ttime - redTime > 0.15){
if (swR==0){
if (romListRed < 254)
romListRed += 3;
if (romListRed >= 254){
swR = 1;
}
}
else
if (swR==1){
if (romListRed > 100)
romListRed  -= 3;
if (romListRed <= 100){
swR = 0;
}
}
redTime = 0;
}
/////////////////////////////////////////////////////////
if (ttime - greenTime > 0.1){
if (swG==0){
if (romListGreen < 254)
romListGreen += 1;
if (romListGreen >= 254){
swG = 1;
}
}
else
if (swR==1){
if (romListGreen > 100)
romListGreen  -= 1;
if (romListGreen <= 100){
swG = 0;
}
}
greenTime = 0;
}
/////////////////////////////////////////////////////////
if (ttime - blueTime > 0.05){
if (swR==0){
if (romListBlue < 254)
romListBlue += 1;
if (romListBlue >= 254){
swR = 1;
}
}
else
if (swR==1){
if (romListBlue > 100)
romListBlue  -= 1;
if (romListBlue <= 100){
swR = 0;
}
}
blueTime = 0;
}
set_bright( romListRed, romListGreen, romListBlue, romList );
}

The set_bright function is from one of the other scripts available, though i did modify it to set each r,g,b colour.

To change how long each colour takes to cycle up and down, edit;
Code: [Select]
if (ttime - redTime > 0.15){
This could be adapted to all sorts of applications i bet..

Cheers!

Luke_Nukem

  • Sr. Member
  • ****
  • Posts: 135
    • View Profile
    • Blogging about Rust lang
Re: Colour cycling script.
« Reply #1 on: June 07, 2014, 08:07:47 PM »
Refactored the code quite a bit. The joy of learning.

Code: [Select]
if (ttime - rgbTime > 2){
if (swR==0){
if (romListRed < 254)
romListRed += 1;
if (romListRed >= 254)
swR = 1;
}
else if (swR==1){
if (romListRed > 100)
romListRed  -= 2;
if (romListRed <= 100)
swR = 0;
}
///////////////////////////
if (swG==0){
if (romListGreen < 254)
romListGreen += 2;
if (romListGreen >= 254)
swG = 1;
}
else if (swG==1){
if (romListGreen > 100)
romListGreen  -= 1;
if (romListGreen <= 100)
swG = 0;
}
///////////////////////////
if (swB==0){
if (romListBlue < 254)
romListBlue += 1.5;
if (romListBlue >= 254)
swG = 1;
}
else if (swB==1){
if (romListBlue > 100)
romListBlue  -= 1.5;
if (romListBlue <= 100)
swG = 0;
}
rgbTime = 0;
}

You can use this for other things such as;
Code: [Select]
if (ttime - pinchTime > 0.5){
if (swPinch==0){
romListPinch += 1;
if (romListPinch >= 75){
swPinch = 1;
}
}
else
if (swPinch==1){
romListPinch  -= 1;
if (romListPinch <= 25)
swPinch = 0;
}
pinchTime = 0;
}
romListSurf.pinch_x = -(romListPinch);

Luke_Nukem

  • Sr. Member
  • ****
  • Posts: 135
    • View Profile
    • Blogging about Rust lang
Re: Colour cycling script.
« Reply #2 on: June 09, 2014, 02:44:37 AM »
updated code, again...

Code: [Select]
cycleVTable <-{
"cnListPinch" : 0, "swListPinch" : 0, "wkListPinch" : 0,
"cnListRed" : 0, "swListRed" : 0, "wkListRed" : 0,
"cnListGreen" : 0, "swListGreen" : 0, "wkListGreen" : 0,
"cnListBlue" : 0, "swListBlue" : 0, "wkListBlue" : 0,
}

function cycleValue( ttime, counter, switcher, workValue, minV, maxV, BY, speed ) {
if (cycleVTable[counter] == 0)
cycleVTable[counter] = ttime;
////////////// 1000 = 1 second //////////////
if (ttime - cycleVTable[counter] > speed){
if (cycleVTable[switcher]==0){
cycleVTable[workValue] += BY;
if (cycleVTable[workValue] >= maxV)
cycleVTable[switcher] = 1;
}
else
if (cycleVTable[switcher]==1){
cycleVTable[workValue]  -= BY;
if (cycleVTable[workValue] <= minV)
cycleVTable[switcher] = 0;
}
cycleVTable[counter] = 0;
}
return cycleVTable[workValue];
}

fe.add_ticks_callback( "textTickles" );
function textTickles( ttime ) {
//To get a negative, need to use a temporary var and - it.
local temp = cycleValue(ttime, "cnListPinch", "swListPinch", "wkListPinch", 10, 25, 1, 20);
romListSurf.pinch_x = -temp;
local RED = cycleValue(ttime,"cnListRed","swListRed","wkListRed",100,254,1,20);
local GREEN = cycleValue(ttime,"cnListGreen","swListGreen","wkListGreen",100,254,1.5,20);
local BLUE = cycleValue(ttime,"cnListBlue","swListBlue","wkListBlue",100,254,2,20);
romList.set_rgb(RED, GREEN, BLUE);
gameTitle.set_rgb(RED, GREEN, BLUE);
listPosition.set_rgb(RED, GREEN, BLUE);
}

And the comment to help explain it;
Code: [Select]
// This function should be pretty self explanatory;
// cycleValue( ttime, this is the tick time.
// counter, this keeps track of time passed since last call.
// switcher, this keeps track of wether to increment or decrement.
// workValue, this is the value that is passed to the function, worked on and returned.
// minV and maxV are the min/max to decrement or increment up to.
// BY, is the amount to increase or decrease workVakue by.
// speed is the length of time to wait to run again. 500 = half second.
////
// This function does require the table to work correctly.

I hope this comes in useful for all sorts of things for everyone...
« Last Edit: June 09, 2014, 02:58:15 AM by Luke_Nukem »

raygun

  • Administrator
  • Sr. Member
  • *****
  • Posts: 390
    • View Profile
Re: Colour cycling script.
« Reply #3 on: July 29, 2014, 08:39:54 PM »
Very cool subtle effect Luke.

I was testing some stuff out with your swapper layout the other night and when I first noticed the colours shifting I thought I was finally losing it...  ;D

cools

  • Full Member
  • ***
  • Posts: 76
  • Arcade Otaku Sysadmin
    • View Profile
    • Arcade Otaku
Re: Colour cycling script.
« Reply #4 on: July 30, 2014, 03:15:38 PM »
Pretty cool script :) Behaves strangely when I try and use it on object sizes - they always seem to start from zero rather than the minimum value?

Luke_Nukem

  • Sr. Member
  • ****
  • Posts: 135
    • View Profile
    • Blogging about Rust lang
Re: Colour cycling script.
« Reply #5 on: August 15, 2014, 01:18:55 AM »
Yeah that sounds right. I guess you could initialise the object to the max on min size to start with.

cools

  • Full Member
  • ***
  • Posts: 76
  • Arcade Otaku Sysadmin
    • View Profile
    • Arcade Otaku
Re: Colour cycling script.
« Reply #6 on: August 15, 2014, 02:58:21 PM »
It does it with colours as well, only they aren't as noticeable since it works like a fade in.

Luke_Nukem

  • Sr. Member
  • ****
  • Posts: 135
    • View Profile
    • Blogging about Rust lang
Re: Colour cycling script.
« Reply #7 on: August 15, 2014, 03:13:15 PM »
It does it with colours as well, only they aren't as noticeable since it works like a fade in.

Yep, it is because the function takes the initialised value and increases or decreases it until the high/low max is reached, then flips.
To make it look like it starts from the min or max value, the object that the function is being run on just needs to be initialised to either the highest or lowest cycle value.