Question about "uint32_t"

I am using the neopixel library, and I saw this snippet of code and was completely amazed it worked.

uint32_t red      = flora.Color(0,  255,  0);
...
flora.setPixelColor(1,red);

my question is, I thought uint32_t was an unsigned integer (just numbers), how does this nifty trick work. I don't get how the object.attribute was allowed to be used that way.

Can someone explain this to me. What is going on behind the scenes here? Is this the general usage of uint32_t?

Not quite - it's not uint32_t that's doing the magic here. That's just a variable type (32 bit unsigned integer).

The Color() function is the important bit. It takes the three separate RGB values, and combines them into a single 32 bit number that you can store for later use.

It's explained here, about half-way down. Arduino Library Use | Adafruit NeoPixel Überguide | Adafruit Learning System

You have the library :wink: Look at what the specific method does.

From Adafruit_NeoPixel/Adafruit_NeoPixel.cpp at master · adafruit/Adafruit_NeoPixel · GitHub

// Convert separate R,G,B into packed 32-bit RGB color.
// Packed format is always RGB, regardless of LED strand color order.
uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) {
  return ((uint32_t)r << 16) | ((uint32_t)g <<  8) | b;
}

// Convert separate R,G,B,W into packed 32-bit WRGB color.
// Packed format is always WRGB, regardless of LED strand color order.
uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g <<  8) | b;
}

It simply places the value of each color in a specific byte in the variable.

// Edit: code snippet that demonstrates

  char txBuffer[32];
  uint32_t red = flora.Color(0,  255,  0);
  sprintf(txBuffer, "0x%08lX (%lu)", red, red);
  Serial.print(txBuffer);

The output will be

0x0000FF00 (65280)

The first number is the hexadecimal representation so you can see what is placed where, the second number (between brackets) the decimal representation.
If you use uint32_t red = flora.Color(0x33,  0xFF,  0x44);, it might be clearer; output:

0x0033FF44 (3407684)

Note: the name red is wrong, should be green :wink: