Search code examples
colorsrgbrgbapalette

Trying to decipher how RGB values calculate integer values


I'm currently looking at some really old legacy code that unfortunately doesn't have any documentation/comments. I have a palette file that contains rgb values in a comma separated file, and integers that are calculated from those values. I'm unable to determine how those integers are calculated.

Here are the first 12 and last 8 entries. On the left side of the '=' are the R,G,B values, while to the right are the calculated integer values.

0.) 0,0,0 = -16777216   
1.) 0,0,0 = -16777216
2.) 0,0,0 = -16777216
3.) 0,0,0 = -16777216
4.) 43,7,0 = -13957376
5.) 43,7,0 = -13957376
6.) 43,7,0 = -13957376
7.) 43,7,0 = -13957376
8.) 69,15,0 = -12251392  
9.) 69,15,0 = -12251392
10.) 69,15,0 = -12251392
11.) 69,15,0 = -12251392
.
.
.
248.) 254,240,0 = -69632
249.) 254,240,0 = -69632
250.) 254,240,0 = -69632
251.) 254,240,0 = -69632    
252.) 255,248,0 = -2048
253.) 255,248,0 = -2048
254.) 255,248,0 = -2048
255.) 255,248,0 = -2048

Does anyone see a pattern here? Is this a common color-theory concept? All I've managed to be certain of is, is that 255 * 255 * 255 = 16777216 possible values (since R, G, B can be 0-255).

For more context, the integer values are used to apply color to a grayscale image. The entries above are from a bronze palette.


Solution

  • For some reason they are stored with the top byte set to 0xff hence negative numbers you are seeing but otherwise are normal palette values of the form 0xff RR GG BB. The mapping is quite clear and very common.

    Treating them as unsigned 32 bit ints and displaying in hexadecimal will make things clear:

    0.) 0   0   0   =   -16777216   FF000000
    1.) 0   0   0   =   -16777216   FF000000
    2.) 0   0   0   =   -16777216   FF000000
    3.) 0   0   0   =   -16777216   FF000000
    4.) 43  7   0   =   -13957376   FF2B0700
    8.) 69  15  0   =   -12251392   FF450F00
    [snip]
    248.)   254 240 0   =   -69632  FFFEF000
    252.)   255 248 0   =   -2048   FFFFF800
    255.)   255 248 0   =   -2048   FFFFF800
    

    It is a tremendously uninspiring palette with 4 unique values mapped to the same colour so only 64 out of 256 possible colours actually used. I expect for reasons of backwards compatibility with antique hardware. You might want to linearly interpolate between the values for 0, 4, 8 etc to get a nicer look.