# Color + Alpha is killing me.... (Language Help)

Well, I more or less cracked it after reading about two's complement encoding for signed integers.
I have now pretty convoluted code that will decompose a numeric color (given by the pixel-command) into its composite color elements ARGB...
I first turn the absolute number (so no negative sign) into a binary string, flip the bits (1 becomes 0 and 0 becomes 1), add 1 to it and voila!, a binary string containing the ARGB. Then it is only an issue of cutting it up in byte-sized pieces, manipulate them (by eg changing the alpha) and reconstituting the numeral (negative or not) with the rgb()-command
The following program takes the colors stored in the col array, prints out a square with it, decomposes it into the ARGB elements and reconstructs the original number to draw it a second time.

#color value decomposition and recreation
#*****************************************
clg
cls
graphsize 300,300
fastgraphics
dim col(20)
col={-16777216,-1,-65536,-8388608,-16711936,-16744448,-16776961,-16777088, -16711681,-16744320,-65281, -8388480,-256,-8355840,-39424, -5623040, -5987164,-8355712}

for i = 0 to 16 step 2
colorid=col[i]
color colorid
rect (i*10),20,20,20
newcolor=twocomp(colorid)/2
coloridb = blauw(newcolor)
coloridg = groen(newcolor)
coloridr = rood(newcolor)
colorida= alpha(newcolor)
color rgb(coloridr, coloridg, coloridb, colorida)
rect ((i)*10),100,20,20
refresh
input z
next i
for i = 1 to 17 step 2
colorid=col[i]
cls
newcolor=twocomp(colorid)/2
coloridb = blauw(newcolor)
coloridg = groen(newcolor)
coloridr = rood(newcolor)
colorida= alpha(newcolor)
color colorid
rect ((i-1)*10),40,20,20
color rgb(coloridr, coloridg, coloridb, colorida)
rect ((i-1)*10),120,20,20
refresh
input z
refresh
next i
end

function twocomp(hexcol)
s\$=""
hexcol=abs(hexcol)
for i = 0 to 31
r = hexcol % 2
r\$ = string(r)
s\$ = r\$ + s\$
hexcol = int(hexcol \ 2)
next i
t\$=""
for i = 1 to 32
if mid(s\$,i,1)="0" then
t\$=t\$+1
else
t\$=t\$+0
endif
next i
carry\$="1"
u\$=""
for i=32 to 1 step -1
if mid(t\$,i,1)="0" and carry\$="1" then
u\$="1"+u\$
carry\$="0"
else
if mid(t\$,i,1)="1" and carry\$="1" then
u\$="0"+u\$
carry\$="1"
else
if mid(t\$,i,1)="0" and carry\$="0" then
u\$="0"+u\$
else
if mid(t\$,i,1)="1" and carry\$="0" then
u\$="1"+u\$
endif
endif
endif
endif
next i
cls
print "binary reversed plus 1 ="+ u\$
print "press 'enter' for the next color"
total=0
for i = 32 to 1 step -1
if mid(u\$,i,1)="1" then total= total+2^(32-i+1)
next i
twocomp= total
end function

function alpha(hexcol)
if hexcol > 0 then
alpha = int(((hexcol/256)/256)/256)%256
else
alpha=0
endif
end function

function rood(hexcol)
if hexcol > 0 then
rood = int((hexcol/256)/256)%256
else
rood=0
endif
end function

function groen(hexcol)
if hexcol > 0 then
groen = int(hexcol/256)%256
else
groen=0
endif
end function

function blauw(hexco)
blauw = (hexco/256 - int(hexco/256))*256
# this should simply be hexco%256 but that gives 0....
end function