Page 1 of 1

Setting VGA color palette dosn't give the right colors

Posted: Thu Jul 28, 2022 9:03 am
by Cyao
I was trying to make some art on the 0x13 screen, and set up some basic colors, but the colors wasn't the right one that I wanted, Is this some qemu bug or me doing it wrong?

I set up the color using this:

Code: Select all

void init_screen() {
    outb(PALETTE_MASK, 0xFF);
    outb(PALETTE_WRITE, 0);

    outb(PALETTE_DATA, 0xFF);
    outb(PALETTE_DATA, 0xFF);
    outb(PALETTE_DATA, 0xFF);

    outb(PALETTE_DATA, 0x00);
    outb(PALETTE_DATA, 0x00);
    outb(PALETTE_DATA, 0x00);

    outb(PALETTE_DATA, 0xB3);
    outb(PALETTE_DATA, 0x32);
    outb(PALETTE_DATA, 0x27);
}
Draw function:

Code: Select all

void draw_mario(bool left) {
    char * address = (char *) (VIDEO_ADDRESS + item.x + item.y * SC_WIDTH); // This isn't wrong
    memset(address + 3, RED, 5); // RED = 2
    UNUSED(left);
}
Output:
Image
Wanted color:
Image

Re: Setting VGA color palette dosn't give the right colors

Posted: Thu Jul 28, 2022 10:33 am
by Octocontrabass
The VGA RAMDAC supports only 6 bits per channel; it ignores the upper two bits. You're writing #B33227 but the RAMDAC interprets that value as (approximately) #CFCB9E.

Some RAMDACs can be switched to 8 bits per channel, but it's not part of standard VGA so the method to switch will depend on the display adapter. (On really old SVGA cards, it also depends on which RAMDAC chip is installed in the RAMDAC socket. Yes, you could swap your RAMDAC to get an upgrade!)

Re: Setting VGA color palette dosn't give the right colors

Posted: Thu Jul 28, 2022 1:14 pm
by Cyao
Octocontrabass wrote:The VGA RAMDAC supports only 6 bits per channel; it ignores the upper two bits. You're writing #B33227 but the RAMDAC interprets that value as (approximately) #CFCB9E.

Some RAMDACs can be switched to 8 bits per channel, but it's not part of standard VGA so the method to switch will depend on the display adapter. (On really old SVGA cards, it also depends on which RAMDAC chip is installed in the RAMDAC socket. Yes, you could swap your RAMDAC to get an upgrade!)
Is there some good way to make the screen 8 bit with the standard stuff? Maybe is there something other then the VGA? I would like to have more colors :(

Re: Setting VGA color palette dosn't give the right colors

Posted: Thu Jul 28, 2022 1:43 pm
by Octocontrabass
The screen is still 8 bits per pixel. You can still show 256 colors simultaneously. The only limitation is that there are only 6 bits per channel, so you need to use numbers that fit in 6 bits when you set the palette in the RAMDAC. Like this:

Code: Select all

    outb(PALETTE_DATA, 0xB3 >> 2);
    outb(PALETTE_DATA, 0x32 >> 2);
    outb(PALETTE_DATA, 0x27 >> 2);
There is a VBE function to set the RAMDAC to 8 bits per channel, but I don't know if it's commonly supported. If you're using VBE anyway, you might as well use it to set up a 32bpp linear framebuffer and forget about palettes.

Re: Setting VGA color palette dosn't give the right colors

Posted: Fri Jul 29, 2022 1:10 am
by Cyao
Ok!! Thanks for your reply!