Retrieving neopixel RGBW Values - bits and decimals

i'm trying to retrieve the r g b w values from the "32 bit packed" color info neopixel works on.

  led.setPixelColor(0,led.Color(255,120,100,2)); //(Red,Green,Blue,White)
  uint32_t color0 = led.getPixelColor(0);
  uint8_t white = (color0>>24) & 255;
  uint8_t red = (color0>>16) & 255;
  uint8_t green = (color0>>8) & 255;
  uint8_t blue = color0 & 255;
  
  Serial.println(red);
  Serial.println(green);
  Serial.println(blue);
  Serial.println(white);
  Serial.println(color0);

  show32(color0);

void show32(uint32_t test){
    for (int i = 31; i >= 0; i--)
  {
    bool n = bitRead(test, i);
    if (n == 0)
    {
      Serial.print('0');
    }
    else
    {
      Serial.print(n, BIN);
    }
  }
}

results in:

250
115
95
0
16413535
00000000111110100111001101011111

which is 5 to low (red should be 255, is 250 etc..) which becomes a problem with values lower then 6 (always 0)

I'm not a hero on the binary/bitwise whatever stuff, so i have no idea what is going on here :frowning:

any pointers ? better ways to do it ? what am i missing....

What happens if you declare color0 as:

uint32_t color0 = led.Color(255,120,100,2);

And btw, you can do Serial.println(color0, BIN) istead of your show32()..

EDIT: If you have a call to lcd.setBrightness() somewhere, that may be the cause of your problem, because the library will multiply the color components with the brightness.

And also, if library configured with non-standard color order (not RGB, but BRG for example) - direct color reading gives incorrect result too

Yes, but that is hardly the issue in this case.. :wink:

whoaa so now i'm realy at a loss.

I'm using Adafruit neopixel library, i forgot to mention (but you figured that out)

Setting brightness to 255 (max) indeed solves the "5 to low" "issiue", doh.

So, if i get it right, when i give a value of 255, it does that times 255 (or 50, the original brightness setting) and that would make the stored values 65.025 ? so how is it displaying 255 on the output instead of 65025 ? (omg i feel so stupid atm... i'm so sorry )

The idea is to retrieve current r g b w values and display then through a webpage. but those values are also used to set new values and like this, it will get progressively lower (-5 every time)

The adafruit library puts things in order, the only thing i got wrong first time is that they add white infront of r g b values in the 32bit (so to the left). now the values match order, just -5 (because of brightness setting apparently!)

The source code uses: cc = (cc * brightness) >> 8. That means, that the color component is multiplied with the brightness value which would give a value in the range 0..65535 but then the value is shifted 8 to the right which means that only the high byte of the word is used: 255 * 255 = 65535 or 0xFFFF and then the shift: 0xFFFF >> 8 = 0xFF or 255.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.