Gradient examples

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I need to have a function that fills any specified area with a gradient. Do

you guys have any example code that implements this?

Thanks!


Eduardo B. Fonseca
ebf at aedsolucoes.com.br
10:54:11 up 5 days, 23:53, 1 user, load average: 0.25, 0.31, 0.36
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+mWzFAt7PF/UgYyYRAnFmAJ4obU6C9DnxrcSdFowmHuUSSCHL5gCeLOTf
r5dNwGhULyy9NtnzNE2uxQU=
=YkmT
-----END PGP SIGNATURE-----

What kind of ‘area’? A polygon? A mask? Just a rectangle?

I’ll assume the latter, since it’s the simplest.

What kind of gradient? Horizontal? Vertical? 45-degree diagonal?
Some arbitrary direction? Is it a linear gradient? A circle? Rectangular?

I’ll assume it’s vertical, since it’s one of the simplest, and most used.

How many colors? Just one color to another? Or a whole array of them?

I’ll assume the former, since it’s the simplest. (The latter could be done
by piecing together the former a number of times.)

Let’s see… Off the cuff and untested, as usual:

void vertgradient(SDL_Surface * surf, SDL_Color c1, SDL_Color c2)
{
int y, width, height;
Uint8 r, g, b;
SDL_Rect dest;
Uint32 pixelcolor;

width = surf->w;
height = surf->h;

for (y = 0; y < height; y++)
{
  r = (c2.r * y / height) + (c1.r * (height - y) / height);
  g = (c2.g * y / height) + (c1.g * (height - y) / height);
  b = (c2.b * y / height) + (c1.b * (height - y) / height);

  dest.x = 0;
  dest.y = y;
  dest.w = width;
  dest.h = 1;

  pixelcolor = SDL_MapRGB(surf->format, r, g, b);

  SDL_FillRect(surf, &dest, pixelcolor);
}

}

(Okay, I actually did test it, and it did work :slight_smile: )

Enjoy!

-bill!On Sun, Apr 13, 2003 at 10:57:23AM -0300, Eduardo B. Fonseca wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I need to have a function that fills any specified area with a gradient. Do
you guys have any example code that implements this?

Are you talking about rectangles, polygons or “flood fill”? Do you
need dithering? How critical is speed? What type of gradients?

Either way, all I’ve got that’s in C and using SDL is some simple
dithering code and a quick hack “plasma bar” effect, both found in
Kobo Deluxe. I’ve thought about hacking a polygon rasterizer, but so
far, I’ve been sticking to plain blits for 2D and OpenGL for heavier
stuff. I’ve pretty much lost all interest in real time s/w rendering
due to the performance issues it has on modern hardware, and
something like Audiality’s “Algorithmically Generated Waveforms” for
graphics is still on the “cool idea” stage.

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Sunday 13 April 2003 15.57, Eduardo B. Fonseca wrote:

Hello,

I need to have a function that fills any specified area with a
gradient. Do you guys have any example code that implements this?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill,

Thanks again for the help. That code was exactly what I needed :slight_smile:
Now, I just need to dither the gradient… I will try by myself, but if you
have any clues, I will surely thank you :slight_smile:

Thanks again!

Eduardo.On Sunday 13 April 2003 14:25, Bill Kendrick wrote:

On Sun, Apr 13, 2003 at 10:57:23AM -0300, Eduardo B. Fonseca wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I need to have a function that fills any specified area with a gradient.
Do you guys have any example code that implements this?

What kind of ‘area’? A polygon? A mask? Just a rectangle?

I’ll assume the latter, since it’s the simplest.

What kind of gradient? Horizontal? Vertical? 45-degree diagonal?
Some arbitrary direction? Is it a linear gradient? A circle?
Rectangular?

I’ll assume it’s vertical, since it’s one of the simplest, and most used.

How many colors? Just one color to another? Or a whole array of them?

I’ll assume the former, since it’s the simplest. (The latter could be done
by piecing together the former a number of times.)

Let’s see… Off the cuff and untested, as usual:

void vertgradient(SDL_Surface * surf, SDL_Color c1, SDL_Color c2)
{
int y, width, height;
Uint8 r, g, b;
SDL_Rect dest;
Uint32 pixelcolor;

width = surf->w;
height = surf->h;

for (y = 0; y < height; y++)
{
  r = (c2.r * y / height) + (c1.r * (height - y) / height);
  g = (c2.g * y / height) + (c1.g * (height - y) / height);
  b = (c2.b * y / height) + (c1.b * (height - y) / height);

  dest.x = 0;
  dest.y = y;
  dest.w = width;
  dest.h = 1;

  pixelcolor = SDL_MapRGB(surf->format, r, g, b);

  SDL_FillRect(surf, &dest, pixelcolor);
}

}

(Okay, I actually did test it, and it did work :slight_smile: )

Enjoy!

-bill!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Eduardo B. Fonseca
ebf at aedsolucoes.com.br
21:48:06 up 6 days, 10:47, 2 users, load average: 0.25, 1.11, 0.88
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+mgWVAt7PF/UgYyYRAu1fAJ43JDJgFlAHTVQnv4AV8QQc8FZ+8QCeO8fN
E9ADRIGpnLKDd/6M+2rS8yE=
=NzDM
-----END PGP SIGNATURE-----

Grab a book on computer graphics (like those used at Universities). :^)

I have one from when I took a graphics class back in 1997/1998, and have
found it somewhat useful, though the more I try to use it, the more
frustrating it gets. Example code you can find online (search Google and
Google Groups) can help quite a bit.

Heck, I’ve found some websites with articles by demosceners doing
programming on the Commodore 64 (of all things!) that’s been quite useful.
(Since they’re written much more readably than the book I have, and aren’t
just “here’s the code to do X in direct-x” crap that I’ve found elsewhere
online. :^) )

Good luck!

-bill!On Sun, Apr 13, 2003 at 09:49:22PM -0300, Eduardo B. Fonseca wrote:

Thanks again for the help. That code was exactly what I needed :slight_smile:
Now, I just need to dither the gradient… I will try by myself, but if you
have any clues, I will surely thank you :slight_smile:


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/