PDA

View Full Version : Understanding QColor's Grayscale formula

bpetty
14th May 2019, 03:06
Looking at QColor's qGray documentation qcolor.html#qGray on how it converts RGB to Grayscale, it uses this formula:
(r * 11 + g * 16 + b * 5)/32

There are a few peculiarities here.
1. I can find no reference to this formula outside of this class in Qt. It is used nowhere, in any article on the subject that I can find.
2. The QColor documentation does not once talk about Gamma Correction, nor does it directly use the term Color Space.

All of that to ask, where is this grayscale conversion formula coming from and does it assume the RGB values are Linear or Non-Linear/Gamma Corrected?

Thank you

bpetty
19th June 2019, 00:02
Does anyone know who I can reach out to for this question? Do they have an issue tracker for their repo somewhere?

anda_skoa
20th June 2019, 10:19
Does anyone know who I can reach out to for this question?

One option might be to look at the code.
Best way to do that is the Woboq Code Browser (https://code.woboq.org)

E.g. Code for QColor (https://code.woboq.org/qt5/qtbase/src/gui/painting/qcolor.h.html)

Do they have an issue tracker for their repo somewhere?

Yes, here (https://bugreports.qt.io/secure/Dashboard.jspa)

Cheers,
_

ChrisW67
23rd June 2019, 08:34
Looking at QColor's qGray documentation qcolor.html#qGray on how it converts RGB to Grayscale, it uses this formula:
(r * 11 + g * 16 + b * 5)/32

There are a few peculiarities here.
1. I can find no reference to this formula outside of this class in Qt. It is used nowhere, in any article on the subject that I can find.
It looks like an approximation of the RGB->Luma conversion in ITU BT.601.

Y = 0.299 R + 0.587 G + 0.114 B

Common approximations are:

Y = 0.33 R + 0.5 G + 0.16 B ( computed like Y = (R+R+G+G+G+B)/6 )
Y = 0.375 R + 0.5 G + 0.125 B ( computed like Y = (R+R+R+G+G+G+G+B) >> 3 )

The equation in QColor is:

Y = 11/32 R + 16/32 G + 5/32 B = 0.343750 R + 0.5 G + 0.156250 B

which is in between the two.