Reading current colours

From BeebWiki
Jump to: navigation, search

OSBYTE 160 reads the VDU variables, and OSBYTE 160,87 to OSBYTE 160,90 read the current text and graphics colours:

OSBYTE 160,87:   read foreground text colour
OSBYTE 160,88:   read background text colour
OSBYTE 160,89:   read foreground graphics colour
OSBYTE 160,90:   read background graphics colour

Unfortunately, they don't return the actual logical colour supplied as the parameter to COLOUR or GCOL. They return the bitmap that that colour stores in the screen memory:

2 colours:   &00, &FF
4 colours:   &00, &0F, &F0, &FF
16 colours:   &00, &03, &0C, &0F, &30, &33, &3C, &3F, &C0, &C3, &CC, &CF, &F0, &F3, &FC, &FF

The values returned in 2-colour and 4-colour MODEs can be converted to logical colour numbers quite easily:

  colour%   =FNosbyte(160,whatever,0)
  maxColour%=FNosbyte(160,96,0)
  colour%=(colour% DIV 8) AND maxColour%

or:

  LDX #whatever:LDA #160:JSR osbyte   :\ read colour mask
  TXA:PHA                             :\ save colour mask
  LDX #96:LDA #160:JSR osbyte         :\ read maximum colour
  STX temp                            :\ save it for later
  PLA:LSR A:LSR A:LSR A               :\ divide colour mask by 8
  AND temp                            :\ AND with max. colour

Converting 16-colour colour masks is more complicated. The following code will convert colour masks in all MODEs:

  col%=FNosbyte(160,whatever,0)
  FOR loop%=1 TO 3-INTSQR((FNosbyte(160,96,0)+1)/4)
  col%=4*INTSQR((col%DIV16)+1)+SQR((col%AND15)+1)-5
  NEXT loop%

or:

  LDA #0:STA col              :\ Clear result
  LDX #87:LDA #160:JSR osbyte :\ Get foreground text colour
  STX mask                    :\ Save colour bitmap
  LDX #96:LDA #160:JSR osbyte :\ Get maximum colour
  STX max                     :\ Save maximum colour
  INX:TXA:ORA #8              :\ Use bitmap from max as counter
  .lp
  ROL mask:ROL mask:ROL col   :\ Rotate every second bit into result
  ROR A:BCC lp                :\ Loop until set bit rotated out
  LDA col:AND max             :\ Get result, AND with max colour

Note that on the Master the graphics colour is stored elsewhere.

Examples

  10 REM > RdColours
  20 REM Demo reading current colours
  30 :
  40 PRINT"Foregnd: COLOUR ";FNvdu_colour(0)
  50 PRINT"Backgnd: COLOUR ";128+FNvdu_colour(1)
  60 PRINT"Foregnd: GCOL ";FNvdu_colour(4);",";FNvdu_colour(2)
  70 PRINT"Backgnd: GCOL ";FNvdu_colour(5);",";128+FNvdu_colour(3)
  80 END
  90 :
 100 :
 110 REM Graphics colours stored differently on Master
 120 DEFFNvdu_colour(A%)
 130 LOCAL col%,loop%
 140 col%=FNosbyte(160,&57+A%,0):IF A%>3:=col%
 150 FOR loop%=1 TO 3-INTSQR((FNosbyte(160,96,0)+1)/4)
 160   col%=4*INTSQR((col%DIV16)+1)+SQR((col%AND15)+1)-5
 170 NEXT loop%
 180 =col%
 190 :
 200 DEFFNosbyte(A%,X%,Y%)=((USR&FFF4)AND&FF00)DIV256

See also

Jgharston (talk) 17:03, 31 October 2015 (UTC) Jgharston (talk) 12:31, 28 February 2016 (UTC)