2

I'm using the rainbowduino and it has some methods that take individual r g b values as unsigned chars, and some that take a 24bit rgb colour code.

I want to convert r g b values into this 24bit colour code of type uint32_t (so that all my code only has to use r g b values.

Any ideas?

I have already tried uint32_t result = r << 16 + g << 8 + b; r = 100 g =200 b=0 gave green, but r=0 g=200 b=0 gave nothing

Rb.setPixelXY(unsigned char x, unsigned char y, unsigned char colorR, unsigned char colorG, unsigned char colorB)
This sets the pixel(x,y)by specifying each channel(color) with 8bit number.

Rb.setPixelXY(unsigned char x, unsigned char y, unit32_t colorRGB) 
This sets the pixel(x,y)by specifying a 24bit RGB color code.
holmeswatson
  • 969
  • 3
  • 14
  • 39

2 Answers2

3

The drivers code is:

void Rainbowduino::setPixelXY(unsigned char x, unsigned char y, uint32_t colorRGB /*24-bit RGB Color*/)
{
    if(x > 7 || y > 7)
    {
     // Do nothing.
     // This check is used to avoid writing to out-of-bound pixels by graphics function. 
     // But this might slow down setting pixels (remove this check if fast disply is desired)
    }
    else
    {
    colorRGB = (colorRGB & 0x00FFFFFF);
    frameBuffer[0][x][y]=(colorRGB & 0x0000FF); //channel Blue
    colorRGB = (colorRGB >> 8);
    frameBuffer[1][x][y]=(colorRGB & 0x0000FF); //channel Green
    colorRGB = (colorRGB >> 8);
    frameBuffer[2][x][y]=(colorRGB & 0x0000FF); //channel Red
    }
}

So I would think similar to the above :

uint8_t x,y,r,b,g;
uint32_t result = (r << 16) | (g << 8) | b;
Rb.setPixelXY(x, y, result); 

should work. It I think the above likely needs the parenthesis, to ensure proper ordering, as "+" is higher than "<<". Also likely won't hurt but the "|" is better, as not to prevent undesired carry's.

P.S. Remember when shifting to be unsigned, unless you want arithmetic shift versus logical. and on that note I don't like shifts as they are often messed up and inefficient. Rather a union is simple and efficient.

union rgb {
  uint32_t word;
  uint8_t  byte[3];
  struct {
    uint8_t  blue;
    uint8_t  green;
    uint8_t  red;
  } color ;
}rgb ;

// one way to assign by discrete names.
rbg.color.blue = b;
rbg.color.green = g;
rbg.color.red = r;
//or assign using array
rgb.byte[0] = b;
rgb.byte[1] = g;
rgb.byte[2] = r;
// then interchangeably use the whole integer word when desired.
Rb.setPixelXY(x, y, rgb.word); 

no messing with keeping track of shifts.

mpflaga
  • 2,807
  • 2
  • 15
  • 19
0

One way to approach this would be to shift the bits to the left...

uint32_t result = r << 16 + g << 8 + b;
RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • 1
    tried that, doesnt seem to work, not sure why. when giving the uint32_t methods 0xFF0000 I get red, 0x00FF00 green, 0x0000FF blue, but doing the above doesn't seem to work – holmeswatson Feb 26 '13 at 20:58