Reading current colours

From BeebWiki
Revision as of 13:31, 28 February 2016 by Jgharston (talk | contribs) (Added code to include 16-colour MODEs.)
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.

See also

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