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