Page 1 of 1

VGA Color not Working

Posted: Thu Feb 03, 2022 6:51 pm
by Caffeine
Hi, I am trying to be able to plot pixels of any color using hexadecimal values as input. Any tips on how to do this?

My code so far:

Code: Select all

void plotPixle(int x, int y, unsigned char color) {
    /*
    Plot a pixle in a certain color to the screen

    Parameters
    ----------
        x : int
            The x position of the pixle
        y : int
            The y position of the pixle
        color : unsigned char
            The color inputted in hexadecimal
    */

    switch (mbi->framebuffer_bpp) {
    case 8:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 1 * x + i] = 4; }
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * (y + i) + 1 * x] = 4; }
        break;
    case 15:
        break;
    case 16:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 2 * x + i] = color; }
        break;
    case 24:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 3 * x + i] = color; }
        break;
    case 32:
        for (int i = 0; i < 4; i++) { framebuffer[mbi->framebuffer_pitch * y + 4 * x + i] = color; }
        break;
    default:
        // error
        break;
    }
}
Changing the color value between 0-255 changes how bright it is. Anything above 255 does not work.

Re: VGA Color not Working

Posted: Thu Feb 03, 2022 7:15 pm
by klange
Nearly everything about the code you posted is wrong. You are passing color as an unsigned char, so of course it only accepts values from 0-255. Anything else will wrap around and stay in that range. The logic for every single one of your bpp switch cases is wrong. I have no idea what you're trying to do but none of it makes sense. The one that is closest to being reasonable is the 32bpp case, but you are still assigning the same value to every channel and are only ever going to get shades of gray because of that. In all of the other cases, you are just completely wrong and I can't even begin to understand what you think you're doing.

Re: VGA Color not Working

Posted: Thu Feb 03, 2022 7:45 pm
by Caffeine
klange wrote:Nearly everything about the code you posted is wrong. You are passing color as an unsigned char, so of course it only accepts values from 0-255. Anything else will wrap around and stay in that range. The logic for every single one of your bpp switch cases is wrong. I have no idea what you're trying to do but none of it makes sense. The one that is closest to being reasonable is the 32bpp case, but you are still assigning the same value to every channel and are only ever going to get shades of gray because of that. In all of the other cases, you are just completely wrong and I can't even begin to understand what you think you're doing.
Yeah no I just figured it out and I am so sorry. I completely misunderstood what it was doing. Sorry for the mistake.