Issue #2
March 10th, 2001

Ahh yes the palette. Your best friend when making a game. Well, it can be anyway. The palette in SCREEN 13 can be quite important, mostly because the default one is really quite crap unless you're adventurous. So your first order of business should be to change it around to what you want.

Most people find that when making a palette it helps to use gradients. A gradient is of course a series of colours which go from either a dark shade to a light or vice-versa. Anyway, how does one change the palette? Well I'm here to answer that question and some. =)

The first thing we'll cover is changing the RGB values for colours. RGB stands for red, green, blue. If you don't already know every colour on your screen is made up of different values of red green and blue. By mixing different amounts of these three colours you can get basically any colour you want. Now I'm not going to go over "rules" for palette creation, that's for another article. So anyway, we can change the value by first OUTing to port 3C8h the colour we want to change. Then we OUT to 3C9h the red, green, and blue values of the new colour. An example:

SCREEN 13			'We'll use mode 13h for this
COLOR 1				'Colour blue for the text
OUT &H3C8, 0			'Let's change the background colour
OUT &H3C9, 63			'Change the red value
OUT &H3C9, 63			'...and the green
OUT &H3C9, 63			'...and the blue
PRINT "Colour 0 is now white!"

In case you didn't read the last line, this example changes colour zero to white. Colour zero is the background colour so you'll notice something different. =) Now, white is made up of 63 for R, G and B. Black is 0 for R, G and B. Bright green would be R=0, G=63, and B=0. I think now you understand.

OK so what if you wanted to get the RGB values of a certain colour? Just what do you do? Well first you OUT the colour whoes values you want to read to port 3C7h. Then you INP from port 3C9h the red, green, then blue values. Simple? Yes I thought so too. Here's an example:

OUT &H3C7, 100			'Get the colours for colour 100
r = INP(&H3C9)  		'Get the red value
g = INP(&H3C9)  		'... and the green
b = INP(&H3C9)  		'... and the blue
PRINT "Red:", r 		'Now show the values of that colour
PRINT "Green:", g
PRINT "Blue:", b

That will display, on screen, the RGB values of colour 100. So what can we do with this knowledge? Well we can do interesting stuff like fades. Or we could make a fully featured palette editor. I think the best example is something which modifies the palette in some way. So lets make a fading routine. This will work in SCREEN 13. Here's the commented code:

change = 50               'We're gonna darken the palette by 50 shades
FOR c = 0 TO 255          'Go thru all the colours
 OUT &H3C7, c
 r = INP(&H3C9)           'Get the red value
 g = INP(&H3C9)           'green
 b = INP(&H3C9)           'blue
 FOR i = 1 TO change      'Loop through as many times as shades we want to darken
  IF r > 0 THEN r = r - 1 'If any of the RGB values is below 0 we don't
  IF g > 0 THEN g = g - 1 'want to change 'em.
  IF b > 0 THEN b = b - 1
  OUT &H3C8, c            'Now we want to make the changes to the palette
  OUT &H3C9, r
  OUT &H3C9, g
  OUT &H3C9, b
NEXT                      'Goto the next colour in the palette.

This doesn't fade the screen smoothly. It just does an 'instant fade' sorta thing. To make it more smooth you should do some timing in one of the loops (not sure which one.... it's late, give me some credit =). You could also make this routine work with textmode. All you gotta do is change the FOR c = 0 TO 255 to FOR c = 0 TO 15 (for 16-colour modes).

Another neat thing you can do is translucencies. When used properly in games, etc, you can get many "Ooooooh"'s and "Aaaaahhh"'s from the users =). We aren't going do something like "alpha-blending". We are going do something which is both memory efficient and speedy. Our translucent pixel routine will make use of an array with 256 integers in it. Here's the code:

DIM SHARED TransData(256)           'Our translucency data

SCREEN 13                           'Use mode 13h

'Here we're gonna initialize the translucent data. You only need to do this
'once at the starting of your program.
FOR i = 0 TO 255                    'Go thru all the colours
 done = 0                           'A tracker variable to see if we've
                                    'done something this time thru the loop
 OUT &H3C7, i                       'Get the RGB values for the current
 r = INP(&H3C9)                     'colour
 g = INP(&H3C9)
 b = INP(&H3C9)

 'Now we want to find the highest of the RGB values and add 'em to the array
 'At the end of the loop our translucency data array will be filled with
 'numbers from 0-15.
 IF r >= b AND r >= g THEN          'Is red highest?
  TransData(i) = r / 4
  done = 1
 IF b >= g AND done <> 1 THEN       'Is blue highest?
  TransData(i) = b / 4
  done = 1
 IF done <> 1 THEN TransData(i) = g / 4 'Is green highest?

'Here "colr" is the colour of the pixel which we're plotting the translucent
'pixel over. Then what we want to do is find out where the gradient of the
'shade we want to put starts and use that colour.
DEF SEG = &HA000
colr = POINT(x, y)
POKE y * 320& + x, TransData(colr) + start_of_desired_shade

The above routine would be much speedier in asm. That is the simplest form of translucency, and is also the fastest that I know of. Something like alpha blending works best in 16-bit and above colour modes. You technically can use alpha blending in 256 colour modes but its not worth it especially when you can do the above.

Anyway thats my article on the palette in QB. Hope you enjoyed it. If you don't understand something then please e-mail me. If you see an error e-mail me too!

This article was written by: Fling-master -

All site content is Copyright 2001, HyperRealistic Games. This excludes content submitted by other people.