Why is cRGB data structure bgr inside?

There’s a cRGB struct type that’s used in a bunch of Kaleidoscope libraries. It’s defined here, I think

I went searching for it when this code which I added to Kaleidoscope-LED-ActiveModColor based on something that’s already there gave me a blue light:

cRGB ActiveModColorEffect::sticky_color = (cRGB) {
  0xff, 0x00, 0x00
};

Am I missing something, or is it weird that something called cRGB has fields whose order isn’t r/g/b?

It is in BGR order because that’s the order the hardware wants, and having the struct in the same order allows the firmware to just copy buffers over, without having to rearrange them first. This is considerably more performant. It is called cRGB mostly for historical reasons, Color or something would perhaps be more appropriate. Renaming to cBGR would be wrong, though, because other hardware may very well have a different order (for example, the Shortcut is GRB).

You can use the CRGB macro which arranges the r/g/b order to the hardware-specific order at compile time (thus no performance penalty): cRGB ActiveModColorEffect::sticky_color = CRGB(0xff, 0, 0)

4 Likes

That makes sense!

2 Likes

I don’t think of it as ‘historical’, so much as naming by intent rather than implementation.
“RGB” describes the colorspace. (FastLED has cRGB and cHSV)

1 Like