We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Eliminating magic numbers
Page Index Toggle Pages: 1
Eliminating magic numbers (Read 1021 times)
Eliminating magic numbers
Dec 14th, 2009, 6:52am
 
Hi
I've mashed together the sequencing method by PhiLho and the slideshow method by John Gilbertson to make a slideshow with a fade effect. The code in the two tabs is below.

It works fine!

.. except when I try to eliminate a magic number. I have the slides fading to a pale blue. If I spell out this pale blue in the fill command

Code:
fill(201, 208, 222, _alphaValue); 



it works fine, but if I define the pale blue at the top of the code as blueFade and use this in the fill command instead

Code:
fill(blueFade, _alphaValue); 



the image fades away as it's overwritten by the pale blue rectangle, but then shows at full brightness for a couple of frames before vanishing.

I would prefer to define the fade colour at the top, and can't figure out why it misbehaves when I do so. Any ideas?
string



** slideShow **
Code:

/* sequencing method by PhiLho
/  slideshow method by John Gilbertson
/  combination and fading by string Nov 2009
*/
String[]_images;

ArrayList _boxOfDisplayItems = new ArrayList();
Renderer _currentDisplay;
int _currentDisplayIndex;
int _currentDisplayDuration;

interface Renderer
{
 public void Show();
 public int GetDisplayDuration();
 public void SetDisplayDuration(int displayDuration);
 public void Reset();  
}

//Here's a sample Renderer class for the
//SlideDisplayAndEffects class to extend
abstract class DefaultRenderer implements Renderer
{
 //default display duration
 protected int duration = 10;
 
 //extending class must contain a function called Show()
 public abstract void Show();
 
 //find out the duration of this display
 public int GetDisplayDuration()
 {
   return duration;
 }
 
 //set the duration of this display
 public void SetDisplayDuration(int displayDuration)
 {
   duration = displayDuration;
 }
}

void setup()
{
 size(300, 300);
 frameRate(10);
 
 //list the images to display
 _images = new String[3];
 _images[0] = "red.jpg";
 _images[1] = "green.jpg";
 _images[2] = "blue.jpg";
 
 //add the image wrapped in its effect to pass to the sequencer
 _boxOfDisplayItems.add(new SlideDisplayAndEffects(_images[0], 10));
 _boxOfDisplayItems.add(new SlideDisplayAndEffects(_images[1], 10));
 _boxOfDisplayItems.add(new SlideDisplayAndEffects(_images[2], 10));
}

void draw()
{
 //As the arrayList _boxOfDisplayItems may contain anything,
 //(Renderer) in brackets tells the program that this
 //particular item is an instance of the Renderer class
 _currentDisplay = (Renderer) _boxOfDisplayItems.get(_currentDisplayIndex);
 //Now that the program knows that this is a Renderer
 //it can call the Renderer function Show()
 _currentDisplay.Show();

 //start the stopwatch
 int runningTime = millis() / 1000;
 //when the runningTime - _currentDisplayDuration (starts = 0)
 //is more than the duration set for this particular display
 //move on
 if(runningTime - _currentDisplayDuration > _currentDisplay.GetDisplayDuration())
 {
   //update the index
   _currentDisplayIndex++;
   //set the _currentDisplayDuration to the runningTime so far
   _currentDisplayDuration = runningTime;
   //reset the effect values to initial values
   _currentDisplay.Reset();
   //if on last index, start again
   if(_currentDisplayIndex == _boxOfDisplayItems.size())
   {
     _currentDisplayIndex = 0;
   }
 }  
}


** SlideDisplayAndEffects **
Code:

class SlideDisplayAndEffects extends DefaultRenderer
{
 PImage _img, logo;
 String _slideName;
 int _slideDuration;
 
 int _fadeDirection = -1;
 int _maxAlpha = 250;
 int _alphaValue = _maxAlpha;
 float _alphaIncrement = 10;
 float _fadeTime = _alphaValue / _alphaIncrement;
 
 color blueFade = color(201, 208, 222);

 
 SlideDisplayAndEffects(String _slideName, int _slideDuration)
 {
   this._slideName = _slideName;
   this._slideDuration = _slideDuration;
   logo = loadImage("headerLogo.gif");
   Reset();
 }
 
 void Reset()
 {
   _fadeDirection = -1;
   _maxAlpha = 250;
   _alphaValue = _maxAlpha;
   _alphaIncrement = 10;
   _fadeTime = _alphaValue / _alphaIncrement;
 }

 void Show()
 {
//    println(_slideName);
//    println("fade " + _fadeDirection + "  alpha " + _alphaValue);  
   //load the current slide
   _img = loadImage(_slideName);
   image(_img, 0, 0);
   
   //fade out a screen-sized rectangle
   //to fade in the image
   //_fadeTime is how long this fading takes
   if(_fadeDirection == -1)
   {
//      fill(201, 208, 222, _alphaValue);
     fill(blueFade, _alphaValue);
     rect(0, 0, width, height);
     _alphaValue += _alphaIncrement * _fadeDirection;
   }
   
   //show image at full brightness for
   //_slideDuration - twice the fading time
   //to allow time for fade out
   if(_alphaValue <= 0)
   {
     delay(int((_slideDuration - 0.2 * _fadeTime) * 1000));
     _fadeDirection = 1;
   }
   
   //fade in a screen-sized rectangle as above
   //to fade out the image    
   if(_fadeDirection == 1)
   {
//      fill(201, 208, 222, _alphaValue);
     fill(blueFade, _alphaValue);      
     rect(0, 0, width, height);
     _alphaValue += _alphaIncrement * _fadeDirection;
   }
 }
}
Re: Eliminating magic numbers
Reply #1 - Dec 14th, 2009, 8:01am
 
given that color is just an int, how does it differentiate between

fill(gray, alpha)

and

fill(color, alpha) ?

perhaps this is the problem. you could try replacing lightBlue with a hex string ("#c9d0de") so that it uses this form:

fill(hex, alpha).

can your alphaValue go above 255 in some circumstances? or below 0?

and if this all works what will all the people who've been set it as a homework problem do then? 8)
Re: Eliminating magic numbers
Reply #2 - Dec 15th, 2009, 4:53am
 
koogy wrote on Dec 14th, 2009, 8:01am:
given that color is just an int, how does it differentiate between

fill(gray, alpha)

and

fill(color, alpha)

Good question, I asked it to myself earlier.
I found the answer in the source code, as usual.
(colorCalc routine) "The problem with this code is that it has to detect between these two situations automatically. This is done by checking to see if the high bits (the alpha for 0xAA000000) is set, and if not, whether the color value that follows is less than colorModeX (first param passed to colorMode)."
and gives examples where the heuristic can break... :)
Re: Eliminating magic numbers
Reply #3 - Dec 17th, 2009, 12:44pm
 
Hi ...
... and thank you both for answering.

I had no idea that people might take classes in Processing, nor that they would get homework Wink, but don't most of us learn by adapting other people's code?

Though disappointed not to be able to remove a magic number, I'm relieved that it wasn't a silly mistake.
Thanks
string
Re: Eliminating magic numbers
Reply #4 - Dec 17th, 2009, 1:28pm
 
If you ask me, "color" being a number (rather than a colour type), and there being some non-obvious interpretation of the numeric value passed to fill(), is a silly mistake, just not your silly mistake.

You can still eliminate your magic numbers by using something like
Code:
fill(red(blueFade), green(blueFade), blue(blueFade), _alphaValue);  


That is, supposing you're using RGB and not HSV colour mode, else try using hue(), saturation(), brightness()

I might also suggest renaming your "blueFade" variable to not mention "blue", since you might decide later to use some other colour.

-spxl
Re: Eliminating magic numbers
Reply #5 - Dec 21st, 2009, 5:55am
 
Hi
Thanks

     fill(red(fadeColour), green(fadeColour), blue(fadeColour), _alphaValue);      

works fine. And see that I've taken your advice to rename blueFade to fadeColour.
string
Page Index Toggle Pages: 1