=====Reading the palette contents=====
//by Richard Russell, October 2011//\\ \\ In BBC BASIC you can change the contents of the colour palette using either [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin8.html#vdu19|VDU 19]] or a variant of the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin4.html#colour|COLOUR]] statement. However, no standard method of reading back the contents of the palette is provided, other than by plotting the colour and sampling it using [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#tint|TINT]].\\ \\ Reading the contents of the palette can be avoided by keeping a local copy, and updating it every time you change the palette. For example the copy could be in the form of a structure array declared as follows:
DIM palette{(15)r&,g&,b&}
Then each time you modify the palette you update the local copy as well:
COLOUR i, r, g, b
palette{(i)}.r& = r
palette{(i)}.g& = g
palette{(i)}.b& = b
If the palette is modified from many places in your program you would probably want to put this code in a procedure.\\ \\ However you may consider the overhead of keeping a local copy unattractive. In that case you can alternatively read the palette contents using the Windows API. To read a single palette entry use code similar to the following:
SYS "GetPaletteEntries", @hpal%, index%, 1, ^rgb%
Here **index%** is the palette index (0-15) and the current value is returned in the variable **rgb%**. \\ \\ Alternatively you could read the entire 16 entries at once, for example into an array:
DIM rgb%(15)
SYS "GetPaletteEntries", @hpal%, 0, 16, ^rgb%(0)
In both cases the returned value contains the red component in bits 0 to 7, the green in bits 8 to 15 and the blue in bits 16 to 23. You could separate them out as follows:
rgb% = rgb%(index%) : REM Needed only in the second case
r = rgb% AND &FF
g = (rgb% >> 8) AND &FF
b = (rgb% >> 16) AND &FF