The possible padding might become a problem when using an array of structs, as you might have problems converting between that and targets expecting tightly packed RGBs. In that case, make sure that the padding is exactly what you expect, using pragma pack()
for instance.
As to whether .x or [] is better, you can overload operator[]
for the struct (using a switch in this case). Any decent compiler will optimise this to be equivalent to static_cast<int*>(this)[index]
, so there is no performance loss. You can of course go with the union, but it doesn't help much. Another possibility is to implement .x () etc. in terms of [].
Anyway, I would recommend using the struct, as you don't loose anything (you can still convert an array of Color
to int*
, but you can write algorithms easier when taking a struct than when taking an int*
-- for instance, if you want luminance, it's easier IMAO to have a nice member/free function taking a Color instead of an int*, because with the int, you can never be sure whether it's RGB, RGBA, YoCoCg while with the Color, you can enforce that.
Last but not least, a struct gives you the possibility to initialize all members to valid/sane/debug values, if wanted.