0xc9 (201) Set Colors In GP2-Palette


Tutorial by addie walti; addie@asit.ch
Version 1.1

With this cmd you can set colors of the gp2 palette.

The arguments of the cmd 0xc9 are (probably):

a1: ?always 0
a2: palette index 1
a3: palette index 2
a4: hue angle 1
a5: hue angle 2
a6: saturation 1
a7: saturation 2
a8: value 1
a9: value 2

John Verheijen <joh.verheijen@pi.net> wrote, these cmd's must be in track section 0 else they won't work.



GP2 - Palette


The following picture shows the standard gp2-palette. The 256 colors are indexed from top left to bottom right. We see a little pointer that points to a certain index.


(pal.jpg)

As you may know, in pictures on the computer (e.g. bitmaps), there you have just indices to a color palette, not the colors itself. E.g. if you have a tarmag-texture (which is basically an ordinary bitmap) then you e.g. have a lot of pixelswith the value 192 (actually 9837, i counted them:). When these pixels are supposed to show up on the screen, the graphic-engine looks this value up in the colors-palette and takes the color that is defined there (palette-entry with index 192).

We immediately recognise consequences of this concept. If we want to change the color of a pixel that we see on the screen, we have two possibilities.
We can change the pixel-value in the bitmap, that means we change it to another INDEX to the palette (e.g. 253, to get the nice color far to the right in the bottom row of the palette above).
Or we can go and change THE PALETTE-COLOR that is indexed by the pixel. But that way ANY pixel with the same value gets also changed.

But i guess you all knew this already.


Color definitions


There are several ways for defining colors. You all may know RGB, where you define a Red-, a Green- and a Blue-value. Then these three basic-colors get mixed to the color you see (ever had a VERY close view to the television screen?).

There are also other definitions, e.g. Cyan, Magenta, Yellow, blacK, etc.
GP2 seems to work with a definition called HSV (Hue-Saturation-Value) (at least when dealing with the cmd 0xc9).


HSV- colors

Selecting a HSV color looks like this (as found in any good painting program near you):


(hsv.jpg)

When selecting a HSV-color, we do set a value for the hue angle, the saturation and the (intensity?)-value. These three numbers define the color.


Calculations

But because life is hard, you can not simply insert the values of the above example into the 0xc9 and get some nice pink. As usual, there has to be some calculating first.

In the hue-circle we have a range of 0..359 degrees, but in programming we have the byte, and the byte has a range of 0..255. So we have to divide the 312 of the above example by 360 and then multiply by 256, and we get (about)222.

For the saturation and the (intensity-)value we have a range of 0..100%. In the cmd 0xc9 we seem to have a range of 0..50 that corresponds to the 0..100% (found by trial and error). That means, we have to divide the values of the above example by 2. 70 gives 35, 76 gives 38.

(but for both parameters values above 100% seem to be possible in gp2; see some 0xc9 cmds in the original tracks; see 0xc9_tab.jpg, as exported with the "export track commands"-function of the track editor:

In fact values 0..255 seem to be valid; higher values get clipped, that means e.g. 270 gives 14)

However.

The previously calculated 3 values we now insert in the cmd 0xc9 with a2=192 in t0 (track sector) in monaco. This is the cmd that is said to darken the tarmac in the tunnel.


(c9_ed1.jpg)

We save the track and enter the game. We immediately go to the tunnel of Monaco and see, what we expected to see:



(c9_scr1.jpg)


How come ?


But why is the tarmac in the tunnel pink and not e.g. the sea ?
Because the tarmac-bmp was mainly painted with the color with the index 192 of the GP2-color-palette.


(tar.jpg)


And when we wrote 192 into a2 (and a3), we intended to change the palette entry 192. If the tarmac in the tunnel is the only bmp (or) jam with the color 192, we see our pink just there. but if e.g. the crosswalk texture also has the color 192, then we get pink/white crosswalks.

In the above Palette Info window we see why i knew how many pixel with value 192 are in the tarmac. And we see also, the color 193 is used. Now we realize why there is also a cmd 0xc9 with a2=193 in monaco. To verify what we learned, we now change the color 193 to some different color.

Here again our color-picker-dialog box. This time we choose some nice green:


(hsv2.jpg)

If applying the mentioned algorithms we get

New hue angle:= hue angle * 255 / 360
that is a5= 124 * 255 / 360
that is a5:= (about) 88

New saturation:= saturation / 2
that is a7:= 100 / 2
that is a7:= 50

New value:= value / 2
that is a9:= 80 / 2
that is a9:= 40

We insert in the cmd-editor:



(c9_ed2.jpg)


And again we go and have a look in the tunnel. What we see is no surprise to us:


(c9_scr2.jpg)

We now know how to set ANY color on the tarmac in the tunnel of Monaco, and everywhere else, we just have to follow, the previous examples.

For further experiments, see HSV-colors palette in the appendix at the end of this File.


So far so good. But ...


If looking at the cmd 0xc9, it seems like we can change two colors at once. But thats not the case. To be honest, i couldnt figure out how it works in detail. With the cmd 0xc9 we do definitely affect TWO palette-entries. But the two changes someway interferre.

What i can say is this: if you want to change a certain color like the tarmac colors 192 and 193, you may want to change them one by one with its own cmd 0xc9, insert twice the same index in a2 and a3, like in the example above.

Some more experiments


I made some more experiments with the tarmac in the tunnel of Monaco. I removed all 0xc9 cmds but one and set the pink (P) and the green (G) values according to the argument-list at the top of this text.

In the bar above the following screenshot you see something like 192 P - 193 G, etc. That means in that example i set color 192 to the pink values and the color 193 to the green values, that means a1=0; a2:=192; a3=193; a4=222; a5=88; a6=35; a7=50; a8=38; a9=40. If we had 193 G - 192 P that would give: a1=0; a2:=193; a3=192; a4=88; a5=222; a6=50; a7=35; a8=40; a9=38.


(192_193.jpg)


(193_192.jpg)

(I'm afraid the last part is a bit confusing. And i'm confused also. I cant see the rule.)

Thoughts
-If looking at the cmds 0xc9 in the original tracks, it seems that the index in a2 is always smaller than the index in a3.

-The color with the higher index seem to work as expected, in whatever position it is set. If you look at the screenshots. Exept the very first screenshot (where there is NO index 193), it always gets set to the 2nd(!) color definition.

-But what happens to the color index 192 when there is another index with a higher number ? If it is in the first position, its hue-angle gets shifted to a value about 40 (about 27 in gp2). the other two values seem unchanged. If it is in the second position, it simply gets ignored !?

But we will find out ...


Appendix




Revisions


Version 1.1
-slight revisions here and there
-introduce revisions history
-added appendix with GP2-palette

Version 1
First version