%begin
   %include ":l:level1:graphinc.imp"
   %include ":l:src:util.imp"
   %integer c, cc, cy, i, t, r, g, b, or, og, ob, x, y, ox, oy, xx, yy
   %integer z=0

   %routine set cursor colour(%integer x)
      %integer i

      %if 56<=x<=87 %and cc#red %start
         cc = red
         colour map(i) = 31 %for i = 8, 2, 14
         colour map(i) = z %for i = 9, 2, 15
      %else %if 88<=x<=119 %and cc#green
         cc = green
         colour map(i) = 31<<5 %for i = 8, 4, 12
         colour map(i) = 31<<5 %for i = 9, 4, 13
         colour map(i) = z %for i = 10, 4, 14
         colour map(i) = z %for i = 11, 4, 15
      %else %if 120<=x<=151 %and cc#blue
         cc = blue
         colour map(i) = 31<<10 %for i = 8, 1, 11
         colour map(i) = z %for i = 12, 1, 15
      %finish
   %end

   %routine get mouse(%integer %name x, %short %name reg, %integer min, max)
      %integer xx

      xx = reg
      xx = xx//4
      reg = z %if xx#0
      x = x+xx
      %if x<min %start
         x = min
      %else %if x>max
         x = max
      %finish
   %end

   %routine meter(%integer c, x, y, %integer %name oy)

      y = y+1
      %if y<oy %start
         colour(0)
         fill(x, y<<4, x+16, oy<<4-1)
      %else %if y>oy
         colour(c)
         fill(x, oy<<4, x+16, y<<4-1)
      %finish
      oy = y
   %end

   %routine cursor(%integer x, y)
      %integer xl, xr, yb, yt

      x = 0 %if x<0
      x = 687 %if x>687
      y = 0 %if y<0
      y = 511 %if y>511
      xl = x-8
      xl = 0 %if xl<0
      xr = x+8
      xr = 687 %if xr>687
      yb = y-8
      yb = 0 %if yb<0
      yt = y+8
      yt = 511 %if yt>511
      line(x, yb, x, yt)
      line(xl, y, xr, y)
   %end
   %on %event 0 %start
      print symbol(27)
      print symbol('R')
      %stop
   %finish

   clear
   colour map(7) = z
   colour(7)
   fill(176, 0, 687, 511)
   i = 0
   r = 0
   g = 0
   b = 0
   or = 0
   og = 0
   ob = 0
   ox = 0
   oy = 0
   cc = black
   meter(red, 64, r, or)
   meter(green, 96, g, og)
   meter(blue, 128, b, ob)
   select output(0)
   colour(1)
   line(32, 0, 32, 511)
   %for i = 0, 1, 31 %cycle
       text at(40,i<<4+2)
       %if i<10 %start
          show symbol(' ')
       %else
          show symbol(i//10+'0')
       %finish
       show symbol(rem(i, 10)+'0')
       line(24, i<<4+15, 40, i<<4+15)
    %repeat
   mousex = z
   mousey = z
   x = 104
   y = 8
   %cycle
      set cursor colour(x)
      %cycle
         get mouse(x, mousex, 56, 151)
         get mouse(y, mousey, 0, 511)

         %if x#ox %or y#oy %start
            set cursor colour(x)
            colour(0)
            enable(8)
            cursor(ox, oy)
            ox = x
            oy = y
            colour(8)
            cursor(ox, oy)
            enable(7)
         %finish
      %repeat %until mouse buttons&2#0
      c = x
      cy = -1
      %while mouse buttons&2#0 %cycle
         get mouse(x, mousex, 56, 151)
         get mouse(y, mousey, 0, 511)
         %if y#oy %or x#ox %start
            colour(0)
            enable(8)
            cursor(ox, oy)
            ox = x
            oy = y
            colour(8)
            cursor(ox, oy)
            enable(7)
         %finish
         %if y#cy %start
            cy = y
            %if 56<=c<=87 %start
               r = y>>4
               meter(red, 64, r, or)
            %else %if 88<=c<=119
               g = y>>4
               meter(green, 96, g, og)
            %else %if 120<=c<=151
               b = y>>4
               meter(blue, 128, b, ob)
            %finish
            colour map(7) = b<<10+g<<5+r
         %finish
      %repeat
   %repeatuntil testsymbol>0

%end %of %program
