Details
-
Suggestion
-
Resolution: Invalid
-
Not Evaluated
-
None
-
None
-
None
Description
I find that I quite often store a shade of gray into a qRgb. I know at compile-time that the color is going to be gray. I propose a set of convenience functions for converting gray to color.
Here is my proposed implementation for qRgb. Of course, the same should be done for qRgba64.
inline Q_DECL_CONSTEXPR QRgb qGray2Rgb(int y) { return ((y << 24) >> 24) | (0xFF << 24); } inline Q_DECL_CONSTEXPR QRgb qGray2Rgba(int y, int a) { return ((y << 24) >> 24) | (a << 24); }
I wrote a test on compiler explorer and noticed that these functions are not only more convenient but actually faster than the naive implementations. It seems that modern compilers aren't able to take advantage of a sign extension yet. The speed increase might make a significant difference when doing something like converting a color image to grayscale.