Allegro.cc Forums » Programming Questions » Gradient line

 ngiacomelli Member #5,114 October 2004 I'm looking to render a line with a gradient from one colour to the other. I'm looking to use vanilla Allegro. I assume the best way would be to use do_line and calculate the colour value each iteration but I'm not so sure from there.Anyone have any nice solutions?
 kentl Member #2,905 November 2002 Bresenhams's line drawing algorithm where you paint each pixel yourself in the gradient colors.
 Sirocco Member #88 April 2000 I've done it before using do_line, then stepping through the supplied coords and painting them accordingly. That's about as "Allegro friendly" as you can get. Makes for some very nice effects. -->Graphic file formats used to fascinate me, but now I find them rather satanic.
Mae Mutts
Member #3,766
August 2003

This is how I did it. Looks fine.

 1 Notes: Color is a typedef of int 2 : GradientType indicates a horizontal or vertical gradient 3 void fillGradient( BITMAP * bmp, int x1, int y1, int x2, int y2, Color startColor, Color endColor, GradientType direction) 4 { 5 int numOfRedDivisions = getr( startColor ) - getr( endColor ); 6 int numOfGreenDivisions = getg( startColor ) - getg( endColor ); 7 int numOfBlueDivisions = getb( startColor ) - getb( endColor ); 8 9 int divisions = 0; 10 switch( direction ) 11 { 12 case HORIZONTAL_GRADIENT: divisions = x2-x1 ; break; 13 case VERTICAL_GRADIENT: divisions = y2-y1 ; break; 14 } 15 16 for(int i = 0; i < divisions; i++ ) 17 { 18 int r = getr( startColor ) - (int)floor( i * numOfRedDivisions / divisions ); 19 int g = getg( startColor ) - (int)floor( i * numOfGreenDivisions / divisions ); 20 int b = getb( startColor ) - (int)floor( i * numOfBlueDivisions / divisions ); 21 22 23 switch( direction ) 24 { 25 case HORIZONTAL_GRADIENT: drawLine( bmp, x1+i, y1, x1+i, y2, makecol(r, g, b) ); break; 26 case VERTICAL_GRADIENT: drawLine( bmp, x1, y1+i, x2, y1+i, makecol(r, g, b) ); break; 27 } 28 } 29 } 30 31 void drawLine( BITMAP * bmp, int x1, int y1, int x2, int y2, Color color) 32 { 33 if( x1 == x2 ) 34 vline( bmp, x1, y1, y2, color ); 35 else 36 line( bmp, x1, y1, x2, y2, color ); 37 }

 Johan HalmÃ©n Member #1,550 September 2001 http://edu.loviisa.fi/~jhalmen/projects/mapgen/spectrline.gifA line drawn using a class of mineBasicly it's just my own version of do_line(), without a callback. You initiate an object with a start coordinate and an end coordinate or a start coordinate and a direction. Then you call a step function, which updates members x and y, which you can read. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.
 Neil Walker Member #210 April 2000 I'm still waiting for the OpenLayer plug wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie
Peter Hanratty
Member #7,191
May 2006

Greetings,

Heres a definable gradient class I did in Delphi a couple of years ago (you shouldnt have any problem understanding it since its not too different from C++). It lets you add points at a given position and define the colours.

------------------------------------------------------