Search code examples
cintrgba

Unsigned Int to RGB Value in C


I've been working on some code in C to convert three integers to their rgb values (red, green blue), but it isn't working and I can't figure out why. Basically, the code reads in three different integers using the getchar() method (I'm trying to only use getchar() and nothing else). Here is my code:

#include <stdio.h>
#include <string.h>

// functions
void getRGB(unsigned int x);

// input array
unsigned char data[20];
int rounds = 0;
unsigned int num1, num2, num3;

int main(void)
{
int i=0;
int c;
printf("Enter three integers between 0 and 2^32-1\n");
while(( c = getchar() ) != EOF)
{
    if(c == '-')
    {
        puts("Negative numbers are NOT allowed, please try again.");
        return 0;
    }
    while(isspace(c))
    {
        c=getchar();
    }
    while ((isdigit(c)))
    {
        data[i] = c;
        i++;
        c=getchar();
    }
    rounds++;
    if (rounds == 1)
    {
        num1 = atoi(data);
        memset(data, 0, 20);
        i = 0;
    }
    else if(rounds ==2)
    {
        num2 = atoi(data);
        memset(data, 0, 20);
        i = 0;
    }
    else if(rounds ==3)
    {
        num3 = atoi(data);
        break;
    }
}

getRGB(num1);
getRGB(num2);
getRGB(num3);

return 0;
}



void getRGB(unsigned int x)
{

unsigned int red = (x & 0xff000000) >> 24;
unsigned int green = (x & 0x00ff0000) >> 16;
unsigned int blue = (x & 0x0000ff00) >> 8;

printf("num is %u == rgb (%u, %u, %u) \n", x, red, green, blue);

}

Any help would be much appreciated as I'm completely stumped!


Solution

  • The getRGB function you provided throws away the least significant 8 bits. More specifically, it assumes that the RGB components are stored in bits 8 to 31 (where bit 0 is the least significant bit) according to the following layout:

    - Red component   : bits 24-31 (hexadecimal mask `0xff000000`)
    - Green component : bits 16-23 (hexadecimal mask `0x00ff0000`)
    - Blue component  : bits  8-15 (hexadecimal mask `0x0000ff00`)
    

    So for a test input value of 234 with hexadecimal representation 0x000000EA, the corresponding output would be RGB = (0,0,0) because bits 8 to 31 are all zeros. On the other hand, given the posted conversion, the test input value which would result in a RGB = (0,0,234), would be in hexadecimal 0x0000EA00 or equivalently in decimal 256*234 = 59904.

    Alternatively, if you would like to use the 24 least significant bits for the RGB components, you would need to update your conversions to:

    unsigned int red   = (x & 0x00ff0000) >> 16;
    unsigned int green = (x & 0x0000ff00) >> 8;
    unsigned int blue  = (x & 0x000000ff);