Most QB programming seems to be done in screen mode 13h. This is because it has a 256 color
palette. But, the standard palette doesn't not have a lot of good colors, so it's a good idea
to make your own. How? Well to start out, each color in the palette is made of red, green, and
blue (or rgb) values. These values range from 0 to 63, with 0 being not used and 63 being the
brightest. For example, white has rgb values of 63, 63, 63. Blue has rgb values of 0, 0, 63,
respectivly. Mixing the values can produce any color you'll need.
But, how do you use these values to manipulate the palette? Well, to start off, there are two
ways to change the rgb values of any color. The easiest way is the PALETTE command. According
to the QB4.5 help file, the syntax is:
PALETTE color%, rgb&
Which is true, but it should be:
PALETTE color%, r% + g% * 256 + b% * 65536
So, to make white, you would use:
PALETTE 1, 63 + 63 * 256 + 63 * 65536
Or:
PALETTE 1, 4144959
Because 63+63*256+63*65536=4144959. Likewise, to make red, you would
use:
PALETTE 1, 63
The other method is to use OUT. This is a little more complicated, but a lot
faster. To use OUT you need four lines:
OUT &H3C8, colour%
OUT &H3C9, r%
OUT &H3C9, g%
OUT &H3C9, b%
The rgb values used are still the same. To cut down on program size and typing, I suggest that
you make a SUB called 'pal', which contains only these four lines. The
syntax would be:
pal (colour%, r%, g%, b%)).
Now that you know how to set the rgb values, you should learn how to get them. Using OUT and a
command called INP, you can find the rgb values for any color. Before I get into this, I should
show you how I store these values. You can put the following code in your main program so you
can store all the rgb values of all 256 colors in an array called colors:
TYPE rgb
r AS INTEGER
g AS INTEGER
b AS INTEGER
END TYPE
DIM SHARED colors(255) AS rgb
So, the b value of color 53 would be stored in colors(53).b, and the r value of color 135 would
be in colors(135).r. Now, here's how you can find the rgb values:
OK, here's the code to get the rgb values:
OUT &H3C7, colour%
r% = INP(&H3C9)
g% = INP(&H3C9)
b% = INP(&H3C9)
Again, it would be best to use this as a SUB called getpal:
getpal (color%).
If you want to store the rgb values in the colors array, just use colors(color%).r,
colors(color%).g, and colors(color%).b for the respective r%, g%, and b%.
After a little practice (or none at all), you will be able to easily use these commands and
memorize the rgb values for many useful colors.
Also, after a little practice, you'll find that it's tedious to type in the rgb values for every
single color. So, here's a few SUBs that will perform useful palette manipulations.
Note: many of these SUBs will run way too fast without any delay. I will just put "delay"
wherever you should insert your delay SUB. Also, these SUBs use the colors array and the pal and
getpal SUBs, so make sure you include those in you program.
GetPalChunk (col1%, col2%)
Code:
SUB getpalchunk (col1%, col2%)
FOR col% = col1% TO col2%
getpal col%, colors(col%).r, colors(col%).g, colors(col%).b
NEXT
END SUB
What it does:
Gets the rgb values for colors col1% to col2%.
Notes:
Col2% must be greater than col1%.
SetPal (col1%, col2%)
Code:
SUB SetPal (col1%, col2%)
FOR col% = col1% TO col2%
pal col%, colors(col%).r, colors(col%).g, colors(col%).b
NEXT
END SUB
What it does:
Sets the palette, from colors col1% to col2%, to the values stored in the colors array.
Notes:
Col2% must be greater than col1%.
Grad (col1%, r1%, g1%, b1%, col2%, r2%, g2%, b2%)
Code:
SUB Grad (col1%, r1%, g1%, b1%, col2%, r2%, g2%, b2%)
cols% = col2% - col1% + 1
rstep# = (r2% - r1% + 1) / cols%
gstep# = (g2% - g1% + 1) / cols%
bstep# = (b2% - b1% + 1) / cols%
r# = r1%
g# = g1%
b# = b1%
FOR col% = col1% TO col2%
r# = r# + rstep#
g# = g# + gstep#
b# = b# + bstep#
pal col%, CINT(r#), CINT(g#), CINT(b#)
NEXT
END SUB
What it does:
Creates a color gradient from the first color, col1%, with rgb values r1%, g1%, and b1%, to the
last, col2%, with rgb values r2%, g2%, and b2%.
Notes:
Col2% must be higher than col1%.
The rgb values for col1% will not be exactly r1%, g1%, and b1%. This is so, when making 16- or
32-color gradients (which I usually do), the rgb values will come out more evenly.
FadeOut (r%, g%, b%)
Code:
SUB FadeOut (r%, g%, b%)
FOR fadestep% = 0 TO 63
FOR col% = 0 TO 255
r1% = colors(col%).r - fadestep% * (colors(col%).r - r%) / 63
g1% = colors(col%).g - fadestep% * (colors(col%).g - g%) / 63
b1% = colors(col%).b - fadestep% * (colors(col%).b - b%) / 63
pal col%, r1%, g1%, b1%
NEXT
delay
NEXT
END SUB
What it does:
Fades the entire palette gradually to any rgb values. Useful for fading out screens to end
games.
Notes:
Make sure you have the current palette stored in the colors array. Do this by calling the getpal
SUB for each color value and using the colors array for the rgb values.
This SUB does not change the values in the colors array.
FadeIn (r%, g%, b%)
Code:
SUB FadeIn (r%, g%, b%)
FOR fadestep% = 63 TO 0 STEP -1
FOR col% = 0 TO 255
r1% = colors(col%).r - fadestep% * (colors(col%).r - r%) / 63
g1% = colors(col%).g - fadestep% * (colors(col%).g - g%) / 63
b1% = colors(col%).b - fadestep% * (colors(col%).b - b%) / 63
pal col%, r1%, g1%, b1%
NEXT
delay
NEXT
END SUB
What it does:
Does the exact opposite of FadeOut. Fades the entire palette gradually in from any rgb
values. Useful for starting games and can be used with FadeOut for neat effects.
Notes:
Works exactly like FadeOut, but fadestep% goes from 63 to 0 instead of 0 to 63.
Rot (col1%, col2%)
Code:
SUB Rot (col1%, col2%
cols%=col2% - col1% + 1
DO
FOR offset% = 0 TO 255
FOR col% = 0 TO cols%
col3% = col1% + offset%
IF col3% > col2% THEN col3% = col3% - col3%
r% = colors(col3%).r
g% = colors(col3%).g
b% = colors(col3%).b
pal col%, r%, g%, b%
NEXT
IF INKEY$ <> "" THEN EXIT SUB
delay
NEXT
LOOP
END SUB
What it does:
Continuously rotates the palette from color col1% to color col2% until the user presses a key,
like many "plasma" demos you've probably seen.
Notes:
Current palette must be stored in the colors array.
Does not change the values of the colors array.
Col2% must be greater than col1%.
Unless the user presses a key at the end of the main offset loop (when offset is 0), the palette
will end up messed up from the offset. To fix this, move the INKEY$ line two lines down.
Grey (col1%, col2%)
Code:
SUB Grey (col1%, col2%)
FOR col% = col1% TO col2%
r% = colors(col%).r
g% = colors(col%).g
b% = colors(col%).b
greyrgb% = CINT((r% + g% + b%) / 3)
pal col%, greyrgb%, greyrgb%, greyrgb%
NEXT
END SUB
What it does:
Changes the palette, from color col1% to color col2%, to greyscale.
Notes:
Current palette must be stored in the colors array.
Does not change the values of the colors array.
Col2% must be greater than col1%.
FadeGrey (col1%, col2%)
Code:
SUB fadegrey (col1%, col2%)
FOR fadestep% = 0 TO 47
FOR col% = col1% TO col2%
r% = colors(col%).r
g% = colors(col%).g
b% = colors(col%).b
greyrgb% = CINT((r% + g% + b%) / 3)
r2% = colors(col%).r - fadestep% * (colors(col%).r - greyrgb%) / 47
g2% = colors(col%).g - fadestep% * (colors(col%).g - greyrgb%) / 47
b2% = colors(col%).b - fadestep% * (colors(col%).b - greyrgb%) / 47
pal col%, r2%, g2%, b2%
NEXT
'delay
NEXT
END SUB
What it does:
Fades the palette, from color col1% to color col2%, to greyscale.
Notes:
Current palette must be stored in the colors array.
Does not change the values of the colors array.
Col2% must be greater than col1%.
The reason 47 is used in the fadestep% loop is because that is the farthest away any r, g, or b
value can be from its greyscale values.
FadePal (col1%, col2%)
Code:
SUB FadePal (col1%, col2%)
FOR fadestep% = 47 TO 0 STEP -1
FOR col% = col1% TO col2%
r% = colors(col%).r
g% = colors(col%).g
b% = colors(col%).b
greyrgb% = CINT((r% + g% + b%) / 3)
r2% = colors(col%).r - fadestep% * (colors(col%).r - greyrgb%) / 47
g2% = colors(col%).g - fadestep% * (colors(col%).g - greyrgb%) / 47
b2% = colors(col%).b - fadestep% * (colors(col%).b - greyrgb%) / 47
pal col%, r2%, g2%, b2%
NEXT
'delay
NEXT
END SUB
What it does:
Fades the palette, from color col1% to color col2%, to color from greyscale.
Notes:
Current palette must be stored in the colors array.
Does not change the values of the colors array.
Col2% must be greater than col1%.
The reason 47 is used in the fadestep% loop is because that is the farthest away any r, g, or b
value can be from its greyscale values.
Palette should be in greyscale first.
To see all of these SUBs in action, download Palette.bas.