Gradient code

Dear SDL dudez & Sammy:

I would like to know if any of you know how to write gradient code that
starts with any RGB value and ends with any RGB value. Currently I only
know how to create gradients that use only red, or only green, or
combinations of red and green, etc. green and blue…etc…

For example, if you want it to grade from red to black use:

Horiz_Gradient(SDL_Surface *screen, 0, 0, 100, 100, 1, 0, 0,
LIGHT_TO_DARK)

Or you can use both red and green and it will grade using yellow to
black. I would like to know if anyone here knows how to grade from to . Not just to black. This would totally rox ma
bong.

I’ve thought about it…seems like I would somehow have to calculate the
difference between the two values, then proportionally move from one
color to the other depending on the width of the gradient.

Horiz_Gradient(SDL_Surface *screen, Uint16 x1, Uint16 y1, Uint16 x2,
Uint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 spec) {

Uint16 line, color;
Uint8 count;
Uint8 null = 0;
Uint8 *red, *green, *blue;
Uint16 width, height;

width = x2 - x1;
height = y2 - y1;

if® red = &count;
else red = &null;
if(g) green = &count;
else green = &null;
if(b) blue = &count;
else blue = &null;

#ifdef DEBUG
printf(“Gradient: X: %u Y: %u Width: %u Height: %u\n”, x1, y1, width,
height);
#endif

for(line = 0; line < width; line++) {
count = line * 255 / width;

PIXEL_FROM_RGB(color, screen->format, *red, *green, *blue);

if(spec == DARK_TO_LIGHT)
Verti_Line(screen, x1 + line, y1, y2, color);

if(spec == LIGHT_TO_DARK)
Verti_Line(screen, x2 - line, y1, y2, color);

} /* end of line for */

} /* end of function */

Thanx,

Paul Lowe
spazz at ulink.net

I would like to know if any of you know how to write gradient code that
starts with any RGB value and ends with any RGB value. Currently I only
know how to create gradients that use only red, or only green, or
combinations of red and green, etc. green and blue…etc…

[snipped]

I’ve thought about it…seems like I would somehow have to calculate the
difference between the two values, then proportionally move from one
color to the other depending on the width of the gradient.

I believe that’s correct. Check out the fade code in the “testwin” test
program in the SDL 0.9.x archive. It fades to red, before fading to black.
Using this concept you could conceivably fade to any color, which is
similar to your problem.

See ya!
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Dear SDL dudez & Sammy:

I would like to know if any of you know how to write gradient code that
starts with any RGB value and ends with any RGB value. Currently I only
know how to create gradients that use only red, or only green, or
combinations of red and green, etc. green and blue…etc…

linear interpolation(takes a value from one to another based on a mixing
value)

out = (endt) + (start(1-t))
where t is the mixing value(0 = start colour, 1 = end colour)

just do that for all three colour channels.

njhOn Tue, 9 Feb 1999, Paul Lowe wrote:

Nathan Hurst wrote:

Dear SDL dudez & Sammy:

I would like to know if any of you know how to write gradient code that
starts with any RGB value and ends with any RGB value. Currently I only
know how to create gradients that use only red, or only green, or
combinations of red and green, etc. green and blue…etc…

linear interpolation(takes a value from one to another based on a mixing
value)

out = (endt) + (start(1-t))
where t is the mixing value(0 = start colour, 1 = end colour)

Ok…
r = (end_r_value * t) + (start_r_value * (1 - t))

t is the mixing value? (0 = start color? 1 = end color?)

Could you explain this. It seems you are using some context I am unaware of.
What is t? What is a mixing value?

so…

a code example would be nice? (with comments)

whoah man. I’m only in Algebra 2 and a amateur graphix dOOd.

just do that for all three colour channels.

njh

I will.

Thanx,

Paul Lowe
spazz at ulink.net> On Tue, 9 Feb 1999, Paul Lowe wrote:

t is the mixing value? (0 = start color? 1 = end color?)

exactly!

Could you explain this. It seems you are using some context I am unaware of.
What is t? What is a mixing value?

Ok, you want to grade from cyan to magenta, via dark blue(through RGB
space):

For each pixel along the way you compute the fraction of the total
distance covered, so half way from cyan to magenta t would be 0.5, 3
quarters of the way, t = 0.75. So if you want to know t at a point, x, on
a line starting at ‘a’ and ending at 'b’
t = (double)(x - a) / (double)(b - a);
Next, now you know t’s value, you want to compute the rgb value:
or = (brt) + (ar(1-t));
og = (bgt) + (ag(1-t));
ob = (bbt) + (ab(1-t));
Where o* is the out component(the value to write at the particular pixel)
a* is the component of the first pixel
b* is the component of the second pixel

So, poking the values in for 0.75 of the way through the cyan(rgb = a =
(0, 1, 1)) to magenta(b = (1, 0, 1)) case:
0.75 = (10.75) + (0(1-0.75));
0.25 = (00.75) + (1(1-0.75));
1 = (10.75) + (1(1-0.75));

which is (i think) dull purple…

a code example would be nice? (with comments)

You can work it out - you’re smart! There are a couple of optimisations,
but you think about them!

whoah man. I’m only in Algebra 2 and a amateur graphix dOOd.

I was once too… and I got told about as much as I am telling you… and
hopefully, you will continue this pointfull tradition! :slight_smile:

njhOn Tue, 9 Feb 1999, Paul Lowe wrote: