User Tools

Site Tools


reading_20the_20palette_20contents

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

reading_20the_20palette_20contents [2018/03/31 13:19]
127.0.0.1 external edit
reading_20the_20palette_20contents [2018/04/13 15:56] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Reading the palette contents===== =====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:\\ +//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: 
 + 
 +<code bb4w>
         DIM palette{(15)r&,​g&,​b&​}         DIM palette{(15)r&,​g&,​b&​}
-Then each time you modify the palette you update the local copy as well:\\ +</​code>​ 
 + 
 +Then each time you modify the palette you update the local copy as well: 
 + 
 +<code bb4w>
         COLOUR i, r, g, b         COLOUR i, r, g, b
         palette{(i)}.r&​ = r         palette{(i)}.r&​ = r
         palette{(i)}.g&​ = g         palette{(i)}.g&​ = g
         palette{(i)}.b&​ = b         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:\\ +</​code>​ 
 + 
 +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: 
 + 
 +<code bb4w>
         SYS "​GetPaletteEntries",​ @hpal%, index%, 1, ^rgb%         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:\\ +</​code>​ 
 + 
 +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: 
 + 
 +<code bb4w>
         DIM rgb%(15)         DIM rgb%(15)
         SYS "​GetPaletteEntries",​ @hpal%, 0, 16, ^rgb%(0)         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:\\ +</​code>​ 
 + 
 +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: 
 + 
 +<code bb4w>
         rgb% = rgb%(index%) : REM Needed only in the second case         rgb% = rgb%(index%) : REM Needed only in the second case
         r = rgb% AND &FF         r = rgb% AND &FF
         g = (rgb% >> 8) AND &FF         g = (rgb% >> 8) AND &FF
         b = (rgb% >> 16) AND &FF         b = (rgb% >> 16) AND &FF
 +</​code>​
reading_20the_20palette_20contents.txt · Last modified: 2018/04/13 15:56 by richardrussell