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

Convenience functions for converting gray to rgb

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: GUI: Painting, Other
    • Labels:
      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.

        Attachments

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

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes