RISC OS programming

  Sprite colours & BBC BASIC

 
I routinely work with 16 million colours available on screen. This colour depth, unimaginable ten years ago, is now the norm. It's a major reason why videos, photographs, and websites look good on modern computers.
 
There are situations in which fewer colours are needed. Working with, say, 256 colours, can result in a palette that is far easier to manipulate and control.
 
Next issue, I'll show how to generate 'nice' 256 colour palettes from the 16 million colours using BBC BASIC. I'll also show the palettes being used to create spectacular images. But first, in this edition's Yellow Pages, I aim to clarify the relationship between the default colours of a 256 colour RISC OS sprite, and the default colours available in BBC BASIC's 256 colour modes.
 
In the Yellow Pages tradition, the segments of program code presented are designed to be easy to tinker with and provide useful building blocks for readers' own projects.
 
Let's start by recalling what the default 256 colour palette of a RISC OS sprite looks like. I'll refer later on to this as being a colour card :
 

 
This is not an easy palette to work with. The layout is confused and the content biased in favour of blues and greens. It's far from clear how to persuade BBC BASIC to render this colour card to the screen from within BASIC.
 
What follows is the 11 line procedure that the MathMagical Software Company uses to write a 256 colour palette to the screen, in colour card format, as displayed by Paint. Note that from RECTANGLE to 10 is one long line of code.
 

DEF PROCshow
LOCAL n%
n% = -1
FOR colour% = 0 TO 63
FOR tint% = 0 TO 192 STEP 64
n% += 1
GCOL COLOUR% TINT tint%
RECTANGLE FILL ((n%MOD8) + 8*(n%DIV64) - 16*(n%DIV128) + 8*(n%DIV912))*14 + 40, 264 - (n%DIV8)*14 + 8*(n%DIV64)*14 - (n%DIV128)*14*8, 10, 10
NEXT
NEXT
ENDPROC

 
The reason the key line of code is convoluted is to do with where the graphics colour numbers that BBC BASIC understands are located on the 256 colour card. The colour black is 0, upper left, and white is 255, lower right, both as you'd expect. But here's the in between:
 

000 ... 007   064 ... 071

008 ... 015   072 ... 079

     .             .     

048 ... 063   120 ... 127

                         

128 ... 135   192 ... 199

136 ... 143   200 ... 207

     .             .     

184 ... 191   248 ... 255

 
Once the four quadrant blocking is noticed it's easy to write code to pick a colour off the card. Number the columns from 0 to 15 running left to right. Number the rows 0 to 15 running top to bottom. The following routine will generate the number in column c% and row r%.
 

DEF PROCpick(c%,r%)
IF r%<8 AND c%<8 THEN p%=8*r%+c%
IF r%<8 AND c%>=8 THEN p%=8*r%+c%+64
IF r%>=8 AND c%<8 THEN p%=8*r%+c%+64
IF r%>=8 AND c%>=8 THEN p%=8*r%+c%+120
ENDPROC

 
Now to link the two procedures PROCshow and PROCpick together. I'll use MODE 21. If your monitor definition file (MDF) does not support this you'll need to use another 256 colour mode.
 
ColourMe01, displays the colour card, waits for a key press, then picks the colour from column 12 and row 14. This is the darker cyan in the bottom but one row from the last group of four shades. This it will display as a large rectangle. A final key press will exit the program.
 

REM >ColourMe01
REM by Martin Hansen
MODE 21
PROCshow
Q$=GET$
PROCpick(12,14)
GCOL (p%DIV4) TINT (p%MOD4)*64
RECTANGLE FILL 300, 200, 300, 400
Q$=GET$
END

 

 Non typists : download the program .

   
    
    
    
    
    
    
    
    
   
    
    
 

BBC BASIC is a computer programming language. Paint is a bit mapped image editor. Both have been included as a part of RISC OS since the late 1980s. Many RISC OS applications are written in BBC BASIC and many applications import and export Paint files, called Sprites. They are part of the glue that holds RISC OS together.