LoginLogin

Gradient tutorial

Root / Submissions / [.]

DevinF06Created:
VAR X = 5,    Y = 5    'X and Y can be almost any value above 0
VAR W = 50-1, H = 50-1 'Width and height of the gradient
VAR R1, R2, G1, G2, B1, B2, N, F, I 'Defining other variables
'Get the RGB value of the colors for use in the gradient
RGBREAD #LIME  OUT R1, G1, B1 'Primary color
RGBREAD #BLACK OUT R2, G2, B2 'Secondary color

FOR I=0 TO H
 N = I/H 'Get the level of the secondary color
 F = 1-N 'Get the level of the primary color
 'Using N and F with the RGB values of the colors
 R = ( R1*F )+( R2*N )
 G = ( G1*F )+( G2*N )
 B = ( B1*F )+( B2*N )
 
 'Insert gradient code/snippet here
NEXT
Gradient snippets:
GLINE X+I, Y, X+I, Y+H, RGB( R, G, B ) 'Width gradient (Across)
GLINE X, Y+I, X+W, Y+I, RGB( R, G, B ) 'Height gradient (Down)
'Remember to swap H with W in FOR and N (FOR I=0 TO W ; N = I/W)

Nice, somehow this is faster than my ARYOP version... Anyway there are a few minor mistakes: - FOR I=0 TO H should be FOR I=0 TO W-1 - F = ABS(N-1) can be F = 1-N (Results are the same, but it's simpler) - Y+H and X+W should be Y+H-1 and X+H-1 And you should make this into a function:
DEF GRADIENT X,Y,W,H,C1,C2,VERTICAL
 IF VERTICAL THEN SWAP W,H
 VAR R1,G1,B1,R2,G2,B2
 RGBREAD C1 OUT R1,G1,B1
 RGBREAD C2 OUT R2,G2,B2
 VAR I
 FOR I=0 TO W-1
  VAR N=I/(W-1)
  VAR F=1-N
  VAR R=R1*F+R2*N
  VAR G=G1*F+G2*N
  VAR B=B1*F+B2*N
  IF VERTICAL THEN
   GLINE X,Y+I,X+H-1,Y+I,RGB(R,G,B)
  ELSE
   GLINE X+I,Y,X+I,Y+H-1,RGB(R,G,B)
  ENDIF
END

DEF GRADIENT X,Y,W,H,C1,C2,VERTICAL
 IF VERTICAL THEN SWAP W,H
 VAR R1,G1,B1,R2,G2,B2
 RGBREAD C1 OUT R1,G1,B1
 RGBREAD C2 OUT R2,G2,B2
 VAR I
 FOR I=0 TO W-1
  VAR N=I/(W-1)
  VAR F=1-N
  VAR R=R1*F+R2*N
  VAR G=G1*F+G2*N
  VAR B=B1*F+B2*N
  IF VERTICAL THEN
   GLINE X,Y+I,X+H-1,Y+I,RGB(R,G,B)
  ELSE
   GLINE X+I,Y,X+I,Y+H-1,RGB(R,G,B)
  ENDIF
END
should be
DEF GRADIENT X%,Y%,W%,H%,C1%,C2%,VERTICAL%
'VERTICAL is really a boolean, so make it use the least amount of bits posible, making it an int
 IF VERTICAL% THEN SWAP W%,H%'you can't put a pixel at a non - int position, so W and H should be ints
 VAR R1%,G1%,B1%,R2%,G2%,B2%'RGB is always an int
 RGBREAD C1% OUT R1%,G1%,B1%
 RGBREAD C2% OUT R2%,G2%,B2%
 VAR I%'Should be an int because it is always a whole number
 FOR I=W-1 TO 0 STEP -1'12Me21
  VAR N%=I%/(W%-1)
  VAR F%=1-N%
  VAR R%=R1%*F%+R2%*N%
  VAR G%=G1%*F%+G2%*N%
  VAR B%=B1%*F%+B2%*N%
  IF VERTICAL% THEN
   GLINE X%,Y%+I%,X%+H%-1,Y%+I%,RGB(R%,G%,B%)
  ELSE
   GLINE X%+I%,Y%,X%+I%,Y%+H%-1,RGB(R%,G%,B%)
  ENDIF
END
should be a tad faster...

Replying to:DFrost
DEF GRADIENT X,Y,W,H,C1,C2,VERTICAL
 IF VERTICAL THEN SWAP W,H
 VAR R1,G1,B1,R2,G2,B2
 RGBREAD C1 OUT R1,G1,B1
 RGBREAD C2 OUT R2,G2,B2
 VAR I
 FOR I=0 TO W-1
  VAR N=I/(W-1)
  VAR F=1-N
  VAR R=R1*F+R2*N
  VAR G=G1*F+G2*N
  VAR B=B1*F+B2*N
  IF VERTICAL THEN
   GLINE X,Y+I,X+H-1,Y+I,RGB(R,G,B)
  ELSE
   GLINE X+I,Y,X+I,Y+H-1,RGB(R,G,B)
  ENDIF
END
should be
DEF GRADIENT X%,Y%,W%,H%,C1%,C2%,VERTICAL%
'VERTICAL is really a boolean, so make it use the least amount of bits posible, making it an int
 IF VERTICAL% THEN SWAP W%,H%'you can't put a pixel at a non - int position, so W and H should be ints
 VAR R1%,G1%,B1%,R2%,G2%,B2%'RGB is always an int
 RGBREAD C1% OUT R1%,G1%,B1%
 RGBREAD C2% OUT R2%,G2%,B2%
 VAR I%'Should be an int because it is always a whole number
 FOR I=W-1 TO 0 STEP -1'12Me21
  VAR N%=I%/(W%-1)
  VAR F%=1-N%
  VAR R%=R1%*F%+R2%*N%
  VAR G%=G1%*F%+G2%*N%
  VAR B%=B1%*F%+B2%*N%
  IF VERTICAL% THEN
   GLINE X%,Y%+I%,X%+H%-1,Y%+I%,RGB(R%,G%,B%)
  ELSE
   GLINE X%+I%,Y%,X%+I%,Y%+H%-1,RGB(R%,G%,B%)
  ENDIF
END
should be a tad faster...
Yes, but be careful, because the types of the input variables depend on the value that is passed to them (type suffixes don't actually do anything there):
DEF TEST A#
 A#=7.5
 PRINT A#
END

TEST 10 'prints 7, because 10 is an integer, so A# becomes an integer variable.
TEST 10.0 'prints 7.5

Replying to:DFrost
DEF GRADIENT X,Y,W,H,C1,C2,VERTICAL
 IF VERTICAL THEN SWAP W,H
 VAR R1,G1,B1,R2,G2,B2
 RGBREAD C1 OUT R1,G1,B1
 RGBREAD C2 OUT R2,G2,B2
 VAR I
 FOR I=0 TO W-1
  VAR N=I/(W-1)
  VAR F=1-N
  VAR R=R1*F+R2*N
  VAR G=G1*F+G2*N
  VAR B=B1*F+B2*N
  IF VERTICAL THEN
   GLINE X,Y+I,X+H-1,Y+I,RGB(R,G,B)
  ELSE
   GLINE X+I,Y,X+I,Y+H-1,RGB(R,G,B)
  ENDIF
END
should be
DEF GRADIENT X%,Y%,W%,H%,C1%,C2%,VERTICAL%
'VERTICAL is really a boolean, so make it use the least amount of bits posible, making it an int
 IF VERTICAL% THEN SWAP W%,H%'you can't put a pixel at a non - int position, so W and H should be ints
 VAR R1%,G1%,B1%,R2%,G2%,B2%'RGB is always an int
 RGBREAD C1% OUT R1%,G1%,B1%
 RGBREAD C2% OUT R2%,G2%,B2%
 VAR I%'Should be an int because it is always a whole number
 FOR I=W-1 TO 0 STEP -1'12Me21
  VAR N%=I%/(W%-1)
  VAR F%=1-N%
  VAR R%=R1%*F%+R2%*N%
  VAR G%=G1%*F%+G2%*N%
  VAR B%=B1%*F%+B2%*N%
  IF VERTICAL% THEN
   GLINE X%,Y%+I%,X%+H%-1,Y%+I%,RGB(R%,G%,B%)
  ELSE
   GLINE X%+I%,Y%,X%+I%,Y%+H%-1,RGB(R%,G%,B%)
  ENDIF
END
should be a tad faster...
lol didn't think of that