Convert r-g-b to pal for the x16

All aspects of programming on the Commander X16.
hstubbs3
Posts: 72
Joined: Thu Oct 26, 2023 12:14 pm

Re: Convert r-g-b to pal for the x16

Post by hstubbs3 »

16 is not allowed.

https://www.youtube.com/watch?v=xOrgLj9lOwk&t=89s


what you're describing, as it being desirable to have 239 round up to 240 to then be considered 15 whence one does proceed with the division, would require some min or rescaling somewhere ...

R4 = int(min(R8,247)/16 + 0.5)

or indeed, as it may not actually be the case that the output DAC on the VERA board is linearly aligned with what one may be expecting of the 8bit per color source, it may actually be needed to employ some sort of table or other device to properly map R8G8B8 to R4G4B4 ... As I recall this is true of the Sega Genesis's output circuitry, but I have not yet put my X16 hardware together, for lack of desk space at the moment, and so have only seen it via the emulator...
User avatar
ahenry3068
Posts: 1152
Joined: Tue Apr 04, 2023 9:57 pm

Re: Convert r-g-b to pal for the x16

Post by ahenry3068 »

hstubbs3 wrote: Mon Mar 18, 2024 8:16 pm 16 is not allowed.

https://www.youtube.com/watch?v=xOrgLj9lOwk&t=89s


what you're describing, as it being desirable to have 239 round up to 240 to then be considered 15 whence one does proceed with the division, would require some min or rescaling somewhere ...

R4 = int(min(R8,247)/16 + 0.5)

or indeed, as it may not actually be the case that the output DAC on the VERA board is linearly aligned with what one may be expecting of the 8bit per color source, it may actually be needed to employ some sort of table or other device to properly map R8G8B8 to R4G4B4 ... As I recall this is true of the Sega Genesis's output circuitry, but I have not yet put my X16 hardware together, for lack of desk space at the moment, and so have only seen it via the emulator...
I've actually done this in code, to map images with a 24 bit palette. Really for Most images. Just dividing by 16 gives OK results. But on side by side comparison the Round up (by bumping with .5) gives better results. Remember. The only thing that really needs to be clipped is values > 247. Logically those values should Map to 15 anyway.
TomXP411
Posts: 1785
Joined: Tue May 19, 2020 8:49 pm

Re: Convert r-g-b to pal for the x16

Post by TomXP411 »

hstubbs3 wrote: Mon Mar 18, 2024 8:16 pm or indeed, as it may not actually be the case that the output DAC on the VERA board is linearly aligned with what one may be expecting of the 8bit per color source, it may actually be needed to employ some sort of table or other device to properly map R8G8B8 to R4G4B4 ... As I recall this is true of the Sega Genesis's output circuitry, but I have not yet put my X16 hardware together, for lack of desk space at the moment, and so have only seen it via the emulator...
Well, the emulator actually just extends the color nibbles by 4 bits to get a byte, but I'm not sure about the resistor ladder in the hardware. Come to think of it, I haven't actually compared the color from my X16 to what the emulator produces. As you say, if there's a non-linearity to the resistor ladder, then the colors will look different.

So whether you add the offset for rounding is subjective - if someone is designing a palette from scratch for X16, they'll want to stick to multiples of 16, so the palette converts 1:1. But if someone is actually converting a 24-bit palette to 12 bit, they will probably need to hand adjust at least some of the colors to look more like what they're expecting.
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

Re: Convert r-g-b to pal for the x16

Post by funkheld »

Hi good afternoon.
I find the topic very interesting.

In order to have an almost real color tone for the x16 (emu) out of 256 colors, I would like to ask whether there were the RGB values somewhere in your archives when I drafted them, as I found them here in the forum or displayed them on the first page here and the ones for the x16 2-byte storage values for the RGB.

I would be grateful if you could find it somewhere.
All the math doesn't get me anywhere, please.

Thanks.
greetingg
hstubbs3
Posts: 72
Joined: Thu Oct 26, 2023 12:14 pm

Re: Convert r-g-b to pal for the x16

Post by hstubbs3 »

Funkheld,

"In order to have an almost real color tone for the x16 (emu) out of 256 color" -
for what image??? if you have some full-color image you want to display, you would have to get best 256 colors -for that image- something like telling GIMP to use an "indexed" color mode and set it to "optimized palette"

if instead you want to take some random image and use the default palette, that could be done in GIMP say, using the 'indexed' image mode but telling GIMP to use a particular palette... And then you would have to supply the palette that the X16 is using (or your palette if you are loading a custom one )

The default 256 palette that is in the x16 at startup is depicted in a png in the vera doc -
If you just want to know where a shade of Red or orange is by default, this is arranged 16 to a row, so 8th color in the 2nd row is a middle gray and would be color $18 using the 8bit color mode..

https://github.com/X16Community/x16-doc ... alette.png

Image

at some point I wrote a python script or something to pull the 8bit per color RGB values from these in order here -
https://github.com/hstubbs3/CommanderX1 ... x16pal.txt
Where line number is palette # +1 ... so the first line is color 0...

I could see maybe later / tomorrow or so making a file one could import into GIMP to have the default 256 palette there.. Then making image from true color to indexed, specify use that palette... and then saving the PNG would automagickally have that PNG use the same palette color numbers as the x16...

I would assume _someone_ has already made some sort of color quantization tool to map to the default X16 palette though???

writing something in Python wouldn't take too much effort to have a command line utility that accepted true color png and spat out 8bit x16 color data, question would be what format(s) should it generate???


possibly worth noting ....

Is that these RRGGBB values all have same high/low nibbles... IE -> this palette suggests that if you take some 8bit R,G,B value ... You would divide by 17 and do no rounding.. the scale appears to be -
decimal | 8bit | 4bit 0 | $00 | 0 17 | $11 | 1 34 | $22 | 2 51 | $33 | 3 68 | $44 | 4 85 | $55 | 5 102 | $66 | 6 119 | $77 | 7 136 | $88 | 8 153 | $99 | 9 170 | $AA | A 187 | $BB | B 204 | $CC | C 221 | $DD | D 238 | $EE | E 255 | $FF | F 163 | $99 | 9 163 | $99 | 9 163 | $99 | 9

but mapping 8bit range to the 4bit could be as simple or complex as one desires to make it - maintaining hue and such is complicated.. Color spaces can be tricky..
User avatar
ahenry3068
Posts: 1152
Joined: Tue Apr 04, 2023 9:57 pm

Re: Convert r-g-b to pal for the x16

Post by ahenry3068 »

I went ahead and wrote some code for you.
FUNKPAL.zip
(7.69 KiB) Downloaded 177 times
Is it supposed to look likes this. ?
FUNKPAL.png
FUNKPAL.png (8.01 KiB) Viewed 1553 times
User avatar
ahenry3068
Posts: 1152
Joined: Tue Apr 04, 2023 9:57 pm

Re: Convert r-g-b to pal for the x16

Post by ahenry3068 »

hstubbs3 wrote: Tue Mar 19, 2024 8:12 pm Funkheld,

"In order to have an almost real color tone for the x16 (emu) out of 256 color" -
for what image??? if you have some full-color image you want to display, you would have to get best 256 colors -for that image- something like telling GIMP to use an "indexed" color mode and set it to "optimized palette"

(On the above I concur)

......................................

but mapping 8bit range to the 4bit could be as simple or complex as one desires to make it - maintaining hue and such is complicated.. Color spaces can be tricky..

There really is no need to make it complicated. The only real decision is to just discard the lower 4 bits of each R,G,B value or use it for some sort of scaling. At this point I've converted literally a couple thousand images of various palettes using Both approachs. Sometimes you can see the difference between the two approachs and sometimes not so much, Even when you can see the difference it's always very subtle, There's no getting around the fact that your Mapping a possible 16 million colors to only 4096 possible colors.
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

Re: Convert r-g-b to pal for the x16

Post by funkheld »

Hello, thank you.
hello hstubbs3, I now have your pal in there:
gimp_cx16pal.txt
Your pal corresponds to the picture shown of the x16 system.

I'm happy that I can now set the color like this according to the x16 system.

Many thanks to everyone for the effort.
greeting
User avatar
ahenry3068
Posts: 1152
Joined: Tue Apr 04, 2023 9:57 pm

Re: Convert r-g-b to pal for the x16

Post by ahenry3068 »

hstubbs3 wrote: Tue Mar 19, 2024 8:12 pm Funkheld,

"In order to have an almost real color tone for the x16 (emu) out of 256 color" -
for what image??? if you have some full-color image you want to display, you would have to get best 256 colors -for that image- something like telling GIMP to use an "indexed" color mode and set it to "optimized palette"

if instead you want to take some random image and use the default palette, that could be done in GIMP say, using the 'indexed' image mode but telling GIMP to use a particular palette... And then you would have to supply the palette that the X16 is using (or your palette if you are loading a custom one )

The default 256 palette that is in the x16 at startup is depicted in a png in the vera doc -
If you just want to know where a shade of Red or orange is by default, this is arranged 16 to a row, so 8th color in the 2nd row is a middle gray and would be color $18 using the 8bit color mode..

https://github.com/X16Community/x16-doc ... alette.png

Image

at some point I wrote a python script or something to pull the 8bit per color RGB values from these in order here -
https://github.com/hstubbs3/CommanderX1 ... x16pal.txt
Where line number is palette # +1 ... so the first line is color 0...

I could see maybe later / tomorrow or so making a file one could import into GIMP to have the default 256 palette there.. Then making image from true color to indexed, specify use that palette... and then saving the PNG would automagickally have that PNG use the same palette color numbers as the x16...

I would assume _someone_ has already made some sort of color quantization tool to map to the default X16 palette though???

writing something in Python wouldn't take too much effort to have a command line utility that accepted true color png and spat out 8bit x16 color data, question would be what format(s) should it generate???


possibly worth noting ....

Is that these RRGGBB values all have same high/low nibbles... IE -> this palette suggests that if you take some 8bit R,G,B value ... You would divide by 17 and do no rounding.. the scale appears to be -
decimal | 8bit | 4bit 0 | $00 | 0 17 | $11 | 1 34 | $22 | 2 51 | $33 | 3 68 | $44 | 4 85 | $55 | 5 102 | $66 | 6 119 | $77 | 7 136 | $88 | 8 153 | $99 | 9 170 | $AA | A 187 | $BB | B 204 | $CC | C 221 | $DD | D 238 | $EE | E 255 | $FF | F 163 | $99 | 9 163 | $99 | 9 163 | $99 | 9

but mapping 8bit range to the 4bit could be as simple or complex as one desires to make it - maintaining hue and such is complicated.. Color spaces can be tricky..


There's already quite a good Gimp Plugin that's been done. By Jestin. It is here : viewtopic.php?p=26533#p26533
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

Re: Convert r-g-b to pal for the x16

Post by funkheld »

Hi good afternoon.

ahenry3068 , what kind of pal do you have please?
Does it look a little different than the picture above with your bas?

Thanks.
greeting
Post Reply