The Beeb Bodybuilding Course Exercise 78 THE CHAMELEON part 2 By Mike Cook Last month we looked at the hardware of the Chameleon and saw how it worked, well this month let's see how to use it and what effects it can produce. If you remember the Chameleon colour palette allows you to re-define the colours produced by your computer in terms of analogue red, green and blue components. The great beauty of the design of the Chameleon is that once it is initialised with a palette it needs no further software support. This means that it is compatible with every program ever written because the program never gets to see the hardware, it goes on working just as before and only when the video signal leaves your computer does the Chameleon intercept and alter it. The Chameleon can be operated in two basic ways, one where the program knows nothing about it, and the other where the program interacts with it. Let's look at the first way to begin with. Take any program such as a snooker game, now the colours available on the standard computer do not exactly match the ones in real life. The programmer has had to make some compromises, take the green ball for example. As there is only one shade of green it is a little difficult to spot on a green felt table. What you can do is to re-assign the colours before you run the game program and the Chameleon will give you a more realistic view. It's not restricted to games either, the subtle hues available for both foreground and background allow you to tailor the colours given by, say a word processor, to be restful to your eyes. Not everyone finds the same colours restful so you can personalise your system. The simplest way of setting up a palette of colours from the Chameleon is to load it in from a file, however that file has to be created first and that is where the program in Listing 1 comes in. This will allow you to specify each colour and save the results to a disc file, as well as reloading files previously created. Before we look at the program in detail let's look at how we can specify a colour. The simplest way to define a colour is to give it three numbers which specify the red, green and blue components that are sent to the colour guns in the monitor. This system matches what the hardware does. If a colour has three components, red, green and blue, it can be thought of as a point in three dimensional cubic space. This is known as the colour cube and is shown in figure I. The origin of the cube, bottom corner, represents black (no red, green or blue), as you move out along one axis you increase just one of the colour components. When you have moved out to the maximum extent on all three axes you reach the cornor opposite the origin which represents white. A line drawn across the cube from black to white is known as the grey line, all grey shades lie along this line. The point of having a model of the colour definition like this is that it allows you to manipulate colours. For example suppose you have two colours, these are two points inside the cube. Now if you want a range of colours that will blend from one to the other all you have to do is to draw a line between the two colours and distribute the additional colours along this line. In that way you will always get a range of blending colours. While the colour cube model is quite good for some applications if you are trying to produce an exact match to a colour it is not exactly intuitive which colour axis you need to move along. If we look at how an artist selects colours for painting we will see a different approach. What he does is to select a basic pigment, or hue, and then add white to lighten it or reduce its saturation. If he wants a darker colour then black paint is used to reduce the brightness of the colour. This is why in any set of artist's paints the white tube is always the biggest, simply more of that colour gets used. The HVS colour model tries to replicate this process by having three controls over the colour: H the hue, V the value or brightness and S the saturation. In geometrical terms this can be thought of as a hexagonal cone as shown in figure II. The H co-ordinate is around the cone, the S co-ordinate is into the centre and the V co-ordinate is along the length. In hexcone space the line running up the centre of the cube is the GREY line. The program in listing 1 will allow you to set up any colour using either cubic or hexcone colour models. The program first sets up the palette to match the standard computer colours. As I mentioned last month there is an alternate palette stored in the chameleon and this is initialised to a grey scale, in order to exchange the main and alternate palettes simply press the E key at any time. To change any individual colour you must first type its number. The display then shows the working colour as the one you have selected. This colour can be altered by pressing the R, G or B keys to add more of the primary colours Red, Green or Blue into the working colour. If the shift key is held down at the same time then the primary colour will be subtracted from the working colour. The amount of any primary colour in a displayed colour is changed on a scale of 0 (none of that primary colour) to 15 (maximum amount of primary colour). In addition you can specify the colour in HVS components, these keys change more than one primary colour to give easier control over the exact shade. The key A increases the saturation of a colour (shift and A reduces it). The saturation is how strong or pure the colour is. The key V increases the value or brightness of the colour (shift and V reduces it). Finally the H key alters the the hue of the colour. This gives a colour composed of the same proportions of the three primary colours but distributed among them differently. It should be noted in the program that the colours are kept in R,G,B cubic co-ordinates and, whenever the keys H,V,S are pressed, they are translated into HVS co-ordinates, altered and then converted back into RGB co-ordinates. This inevitably leads to some rounding errors such that adding one unit of H and then subtracting one unit of H does not necessarily bring you back to the same colour. Also if you get stuck on the GREY line S ceases to have meaning. When this happens S is moved towards the blue if requested to increase. If you wish you could write a program that keeps its value in HVS space, only converting when the hardware has to be accessed. The co-ordinate conversion procedures are easily culled and inserted into your own program. If you want to have a range of blending colours, ones that harmonise with each other, the "I" key generates a whole range of colours that are interpolated between any two colours you choose. You can view the results of this and then decide to keep them or revert to what you had before. This also allows you to select colours for shading an object to give the effect of light falling on it from various angles. You can change the background colour by selecting colour 0 as the working colour. Note that this affects the appearance of the other colours. This is a trick played by the eye and not by the Chameleon. As mentioned last month some types of monitor might "object to this" and produce some distortion. Once you have set up a palette (and alternative palette if required) the values used can be saved on a file. The key "S" allows you to write a file of the colours. Of course you can save as many files of colour palettes as you want. A previously written file of colours can be loaded in using the L key. This can then be modified or used as it is. When you have set up your palette as desired, the initialisation program may be terminated by pressing the ESCAPE key. Note that you will then be in MODE 2 graphics display. Alternatively, by pressing Control and Q at the same time you will have a cleared screen and be in Mode 7. Your own software can now be loaded or typed in. If you are typing it in be sure to type NEW to remove the initialisation program. Remember that the Chameleon changes the display colours on the BBC Micro, and so the display colours as defined by the VDU 19 statement act in the same way as the logical colours did. Note also that you can use flashing colours with the Chameleon. As each colour can be precisely defined you are not restricted to flashing a colour and its complement, they can be any two colours that you have defined. The program in listing 1 uses a small machine code program to reprogram the Chameleon. This is to reduce the slight flickering on the screen that accompanies reprogramming. This is necessary as the initialisation program accesses the Chameleon many times in order to change the colour to exactly the one desired. Note it simply fires the numbers in the three registers to the user port, these are set up in the procedure DCOL and passed to the machine code in A%, X% and Y%. The other major way of using the Chameleon is when you are writing your own programs. In these you can use it to dynamically change the colours. For example a graphic program of a sunset, as the sun goes down the colours darken and the sun reddens, then you can have the moon rising and the colours turning more pale blue. The effect can be quite stunning. Then you can use the Chameleon to produce palette switched animation. This is where various frames are drawn in different colours, all of these except one is set to black. Then you simply have to reprogram the colours to get an animated effect. You can also use the ability of the chameleon to help you in graphic design, allowing you to select colours that match. For example you could write a program to design needle work and alter the colours for the best effect. Another example of its use was that a student of mine produced a package to teach the fundamentals of colour addition and filtering. One of the most effective uses of a palette is when it comes to shading, you can give graphics a more 3-D look by having darker shades towards the back of the object. There is not space for examples of all these techniques but I hope you have enough here to, in best Body Build fashion, to allow you to explore the world of colour yourself. Well that's all for this month, next month we will look at something to help physically handicapped people use the computer more easily, see you then.