Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-76392

Convenience functions for converting gray to rgb

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Invalid
    • Icon: Not Evaluated Not Evaluated
    • None
    • None
    • GUI: Painting, Other
    • None

      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.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            laknoll Lars Knoll
            kerndog73 Indi Kernick
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes