' DrawingOpacity.sdlblib
' Paulo Silva (GPL) - 040504 
' writed over a template coded from Vroby 

'---------------------
'--- Bar

sub BarAlpha(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255
      if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
      if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if
   for y=y1 to y2: for x=x1 to x2
      r0=(int(point(x,y)/0x10000))mod 256
      g0=(int(point(x,y)/0x100))mod 256
      b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
      g1=(int(c/0x100))mod 256
      b1=c mod 256
      r2=((r1*a)+(r0*(255-a)))/255
      g2=((g1*a)+(g0*(255-a)))/255
      b2=((b1*a)+(b0*(255-a)))/255
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (x,y)
    next:next
 end sub

sub BarOverprint(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255
      if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
      if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if
   for y=y1 to y2: for x=x1 to x2
      r0=(int(point(x,y)/0x10000))mod 256
      g0=(int(point(x,y)/0x100))mod 256
      b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
      g1=(int(c/0x100))mod 256
      b1=c mod 256
      r2=255-((255-r0)+((255-r1)*(a/255))):if r2<0 then:r2=0:end if
      g2=255-((255-g0)+((255-g1)*(a/255))):if g2<0 then:g2=0:end if
      b2=255-((255-b0)+((255-b1)*(a/255))):if b2<0 then:b2=0:end if
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (x,y)
    next:next
 end sub

sub BarSpotlight(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255
      if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
      if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if
   for y=y1 to y2: for x=x1 to x2
      r0=(int(point(x,y)/0x10000))mod 256
       g0=(int(point(x,y)/0x100))mod 256
       b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
       g1=(int(c/0x100))mod 256
       b1=c mod 256
      r2=(r0+((r1)*(a/255))):if r2>255 then:r2=255:end if
       g2=(g0+((g1)*(a/255))):if g2>255 then:g2=255:end if
       b2=(b0+((b1)*(a/255))):if b2>255 then:b2=255:end if
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (x,y)
    next:next
 end sub

'---------------------
'--- Box

sub BoxAlpha(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255
      if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
      if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if
   for y=y1 to y2
         stv=1
       if not ((y=y1) or (y=y2)) then
         stv=x2-x1: end if
     for x=x1 to x2 step stv
      r0=(int(point(x,y)/0x10000))mod 256
      g0=(int(point(x,y)/0x100))mod 256
      b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
      g1=(int(c/0x100))mod 256
      b1=c mod 256
      r2=((r1*a)+(r0*(255-a)))/255
      g2=((g1*a)+(g0*(255-a)))/255
      b2=((b1*a)+(b0*(255-a)))/255
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (x,y)
    next:next
 end sub

sub BoxOverprint(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255
 '     if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
 '     if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if
   for y=y1 to y2 step sgn(y2-y1)
         stv=1
       if not ((y=y1) or (y=y2)) then
         stv=x2-x1: end if
     for x=x1 to x2 step stv*sgn(x2-x1)
      r0=(int(point(x,y)/0x10000))mod 256
      g0=(int(point(x,y)/0x100))mod 256
      b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
      g1=(int(c/0x100))mod 256
      b1=c mod 256
      r2=255-((255-r0)+((255-r1)*(a/255))):if r2<0 then:r2=0:end if
      g2=255-((255-g0)+((255-g1)*(a/255))):if g2<0 then:g2=0:end if
      b2=255-((255-b0)+((255-b1)*(a/255))):if b2<0 then:b2=0:end if
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (x,y)
    next:next
 end sub

sub BoxSpotlight(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255
      if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
      if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if
  for y=y1 to y2: stv=1
       if not ((y=y1) or (y=y2)) then:stv=x2-x1: end if
    for x=x1 to x2 step stv
      r0=(int(point(x,y)/0x10000))mod 256
       g0=(int(point(x,y)/0x100))mod 256
       b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
       g1=(int(c/0x100))mod 256
       b1=c mod 256
      r2=(r0+((r1)*(a/255))):if r2>255 then:r2=255:end if
       g2=(g0+((g1)*(a/255))):if g2>255 then:g2=255:end if
       b2=(b0+((b1)*(a/255))):if b2>255 then:b2=255:end if
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (x,y)
    next:next
 end sub

'---------------------
'--- Fillcircle

sub FillcircleAlpha(x,y,r,c,a)
' alpha is from 0 to 255
'--- bug: fillcircle from here is 1 pixel radius smaller
'---     than default fullcircle
'---    (issue related to sin(acos()) formula)
    for yd=-r to r
        xl=(sin(acos(yd/r)))*r
      for xd=-xl to xl
        xp=x+xd:yp=y+yd
      r0=(int(point(xp,yp)/0x10000))mod 256
       g0=(int(point(xp,yp)/0x100))mod 256
       b0=(point(xp,yp))mod 256
      r1=(int(c/0x10000))mod 256
       g1=(int(c/0x100))mod 256
       b1=c mod 256
      r2=((r1*a)+(r0*(255-a)))/255
      g2=((g1*a)+(g0*(255-a)))/255
      b2=((b1*a)+(b0*(255-a)))/255
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (xp,yp)
    next: next
 end sub

sub FillcircleOverprint(x,y,r,c,a)
'--- alpha is from 0 to 255
'--- bug: fillcircle from here is 1 pixel radius smaller
'---     than default fullcircle
'---    (issue related to sin(acos()) formula)
    for yd=-r to r
        xl=(sin(acos(yd/r)))*r
      for xd=-xl to xl
        xp=x+xd:yp=y+yd
      r0=(int(point(xp,yp)/0x10000))mod 256
       g0=(int(point(xp,yp)/0x100))mod 256
       b0=(point(xp,yp))mod 256
      r1=(int(c/0x10000))mod 256
       g1=(int(c/0x100))mod 256
       b1=c mod 256
      r2=255-((255-r0)+((255-r1)*(a/255))):if r2<0 then:r2=0:end if
       g2=255-((255-g0)+((255-g1)*(a/255))):if g2<0 then:g2=0:end if
       b2=255-((255-b0)+((255-b1)*(a/255))):if b2<0 then:b2=0:end if
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (xp,yp)
    next: next
 end sub

sub FillcircleSpotlight(x,y,r,c,a)
'--- alpha is from 0 to 255
'--- bug: fillcircle from here is 1 pixel radius smaller
'---     than default fullcircle
'---    (issue related to sin(acos()) formula)
    for yd=-r to r
        xl=(sin(acos(yd/r)))*r
      for xd=-xl to xl
        xp=x+xd:yp=y+yd
      r0=(int(point(xp,yp)/0x10000))mod 256
       g0=(int(point(xp,yp)/0x100))mod 256
       b0=(point(xp,yp))mod 256
      r1=(int(c/0x10000))mod 256
       g1=(int(c/0x100))mod 256
       b1=c mod 256
      r2=(r0+((r1)*(a/255))):if r2>255 then:r2=255:end if
       g2=(g0+((g1)*(a/255))):if g2>255 then:g2=255:end if
       b2=(b0+((b1)*(a/255))):if b2>255 then:b2=255:end if
        c2=(r2*0x10000)+(g2*0x100)+b2
        ink(c2):dot (xp,yp)
    next: next
 end sub


'---------------------
'--- Line
'--- bug: division by zero error
sub LineOverprint(x1,y1,x2,y2,c,a)
' alpha is from 0 to 255

'      if x2<x1 then: tmpr=x1:x1=x2:x2=tmpr: end if
'      if y2<y1 then: tmpr=y1:y1=y2:y2=tmpr: end if


'   for y=y1 to y2
'         stv=1
'       if not ((y=y1) or (y=y2)) then
'         stv=x2-x1: end if
'     for x=x1 to x2 step stv

dx=x2-x1:dy=y2-y1
adx=abs(dx):ady=abs(dy)

if dx>=dy then
   b1=x1:b2=x2:db=dx:s1=y1:s2=y2:ds=dy
 else
   b1=y1:b2=y2:db=dy:s1=x1:s2=x2:ds=dx
 end if


for ct=b1 to b2 step sgn(b2-b1)
  pr=s1+((ds/db)*(ct-b1))
  
      r0=(int(point(x,y)/0x10000))mod 256
      g0=(int(point(x,y)/0x100))mod 256
      b0=(point(x,y))mod 256
      r1=(int(c/0x10000))mod 256
      g1=(int(c/0x100))mod 256
      b1=c mod 256
      r2=255-((255-r0)+((255-r1)*(a/255))):if r2<0 then:r2=0:end if
      g2=255-((255-g0)+((255-g1)*(a/255))):if g2<0 then:g2=0:end if
      b2=255-((255-b0)+((255-b1)*(a/255))):if b2<0 then:b2=0:end if
        c2=(r2*0x10000)+(g2*0x100)+b2

       if dx>=dy then
         x=ct:y=pr:else
         y=ct:x=pr:end if

        ink(c2):dot (x,y)

    next

 end sub




