I have to developed some fade algorithms to get more user friendly applications.
I used this algorithms to create nice backgrounds and menus.
I will post some articles that describe how to use it, this is the first one.
|
|
First I wrote a function to fade a color to white. We have to set the following parameters to this function:
The dimension of the area we want to fade.
The color.
A boolean value to indicate if the fade area start with the white color or not.
Then this function will return an int array that we will use to create an Image Object.
This is the function:
public static int [] MakeFadeRegionToWhite(int iWidth, int iHeight, int iColor, boolean bStartWithWhite)
{
int []data = new int[iWidth * iHeight];
int newColor;
if (bStarWithWhite)
newColor = 0x00FFFFFF;
else
newColor = iColor;
double r = (iColor & 0x00FF0000)>>16;
double g = (iColor & 0x0000FF00)>>8;
double b = (iColor & 0x000000FF);
double offsetR = (256 - r) / iHeight;
double offsetG = (256 - g) / iHeight;
double offsetB = (256 - b) / iHeight;
double rNew = (newColor & 0x00FF0000)>>16;
double gNew = (newColor & 0x0000FF00)>>8;
double bNew = (newColor & 0x000000FF);
for (int i=0; i < data.length; i++)
{
data[i] = newColor;
if(i % iWidth == 0)
{ // substract each color on every row
if (bStarWithWhite)
{
if (rNew > r )
rNew -= offsetR;
if(gNew > g)
gNew -= offsetG;
if(bNew > b)
bNew -= offsetB;
} else
{
if(rNew < 256)
rNew += offsetR;
if(gNew < 256)
gNew += offsetG;
if(bNew < 256)
bNew += offsetB;
}
newColor = 0xFF000000; // make the pixel opaque.
newColor += ((int)rNew<<16);
newColor += ((int)gNew<<8);
newColor += (int)bNew;
}
}
return data;
}
We can use this function to create a fade background in a Canvas Object. For example:
public class MyCanvas extends GameCanvas
{
private Image objImage=null;
public void paint (Graphics g)
{
int iViewW = this.getWidth();
int iViewH = this.getHeight();
int iColor = 0x00a35ca0;
// if the Image object doesn't exist create it
if (objImage == null)
{
int [] imageArray = MakeFadeRegionToWhite(iViewW, iViewH, iColor, false);
objImage = Image.createRGBImage (imageArray, iViewW, iViewH, false);
}
// Draw the fade background
g.drawImage(objImage, 0, 0, Graphics.LEFT | Graphics.TOP);
}
}




[...] will use the algorithm MakeFadeRegionToWhite(…) introduced on the post How to – Fade Algorithms (1) to create a new fade effect. This effect can be used, for example, on an initial screen where we [...]
[...] will use the algorithm MakeFadeRegionToWhite(…) introduced on the post How to – Fade Algorithms (1) to create a new fade effect. This effect can be used, for example, on an initial screen where we [...]