%begin

%include "level1:graphinc.imp"
%include "inc:util.imp"
%halfarray cm(0:255)
%integer r, g, b, i, ix, iy, wid, ht, mb, msec, lht

%routine Mix Colour (%short Col, Red, Green, Blue)
   CM(Col)=Red+Green<<5+Blue<<10
%end

%routine drawbox(%integer x0,y0,x1,y1)
   hline(x0,x1,y0)
   vline(x0,y0,y1)
   hline(x0,x1,y1)
   vline(x1,y0,y1)
%end

%routine bar(%integer col, val)
   colour(col)
   fill(col*ix+1, iy+1, (col+1)*ix-1, iy+4*val+1)
   colour(0)
   fill(col*ix+1, iy+4*val+1, (col+1)*ix-1, iy+lht-1)
   fill(col*ix+1, iy+lht+1, (col+1)*ix-1, iy+ht)
   colour(255)
   textat(col*ix+1, iy+lht+6); showi(val,2)
%end


clear
%for i=0,1,255 %cycle; cm(i)=0; %repeat
mix colour(1, 31, 0, 0)
mix colour(2, 0, 31, 0)
mix colour(3, 0, 0, 31)
mix colour(255, 31, 31, 31)

ix=30; iy=30; ht=150; lht=126; wid=120
colour(255)
Draw box(ix,iy,4*ix,iy+ht+1)
colour(0)
fill(ix+1, iy+1, 4*ix-1, iy+ht)

colour(255)
draw box(344, iy, 688-ix, 512-iy)
hline(ix, 4*ix, iy+lht)
vline(2*ix, iy+1, iy+ht+1)
vline(3*ix, iy+1, iy+ht+1)
bar(1, 0)
bar(2, 0)
bar(3, 0)

colour(4)
fill(345, iy+1, 687-ix, 511-iy)
update colour map(cm(0))

r=0; g=0; b=0
%cycle
   mb = mouse buttons
   %if mb=mouse left %start
      r=(r+1)&31
      bar(1, r)
   %elseif mb = mouse middle ! mouse right
      r=(r-1)&31
      bar(1,r)
   %elseif mb=mouse middle
      g=(g+1)&31
      bar(2, g)
   %elseif mb = mouse left ! mouse right
      g=(g-1)&31
      bar(2,g)
   %elseif mb=mouse right
      b=(b+1)&31
      bar(3, b)
   %elseif mb = mouse left !mouse middle
      b=(b-1)&31
      bar(3, b)
   %finish
   %if mb#0 %start
      mix colour(4, r, g, b)
      update colour map(cm(0))
      msec=cputime + 50
      %cycle; %repeatuntil cputime = msec
   %finish
%repeat

%endofprogram
