1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QRGB_H
41#define QRGB_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qprocessordetection.h>
45
46QT_BEGIN_NAMESPACE
47
48
49typedef unsigned int QRgb; // RGB triplet
50
51// non-namespaced Qt global variable
52[[maybe_unused]] const QRgb RGB_MASK = 0x00ffffff; // masks RGB values
53
54inline constexpr int qRed(QRgb rgb) // get red part of RGB
55{ return ((rgb >> 16) & 0xff); }
56
57inline constexpr int qGreen(QRgb rgb) // get green part of RGB
58{ return ((rgb >> 8) & 0xff); }
59
60inline constexpr int qBlue(QRgb rgb) // get blue part of RGB
61{ return (rgb & 0xff); }
62
63inline constexpr int qAlpha(QRgb rgb) // get alpha part of RGBA
64{ return rgb >> 24; }
65
66inline constexpr QRgb qRgb(int r, int g, int b) // set RGB value
67{ return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
68
69inline constexpr QRgb qRgba(int r, int g, int b, int a) // set RGBA value
70{ return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
71
72inline constexpr int qGray(int r, int g, int b) // convert R,G,B to gray 0..255
73{ return (r*11+g*16+b*5)/32; }
74
75inline constexpr int qGray(QRgb rgb) // convert RGB to gray 0..255
76{ return qGray(qRed(rgb), qGreen(rgb), qBlue(rgb)); }
77
78inline constexpr bool qIsGray(QRgb rgb)
79{ return qRed(rgb) == qGreen(rgb) && qRed(rgb) == qBlue(rgb); }
80
81inline constexpr QRgb qPremultiply(QRgb x)
82{
83 const uint a = qAlpha(x);
84 uint t = (x & 0xff00ff) * a;
85 t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8;
86 t &= 0xff00ff;
87
88 x = ((x >> 8) & 0xff) * a;
89 x = (x + ((x >> 8) & 0xff) + 0x80);
90 x &= 0xff00;
91 return x | t | (a << 24);
92}
93
94Q_GUI_EXPORT extern const uint qt_inv_premul_factor[];
95
96inline QRgb qUnpremultiply(QRgb p)
97{
98 const uint alpha = qAlpha(p);
99 // Alpha 255 and 0 are the two most common values, which makes them beneficial to short-cut.
100 if (alpha == 255)
101 return p;
102 if (alpha == 0)
103 return 0;
104 // (p*(0x00ff00ff/alpha)) >> 16 == (p*255)/alpha for all p and alpha <= 256.
105 const uint invAlpha = qt_inv_premul_factor[alpha];
106 // We add 0x8000 to get even rounding. The rounding also ensures that qPremultiply(qUnpremultiply(p)) == p for all p.
107 return qRgba((qRed(p)*invAlpha + 0x8000)>>16, (qGreen(p)*invAlpha + 0x8000)>>16, (qBlue(p)*invAlpha + 0x8000)>>16, alpha);
108}
109
110QT_END_NAMESPACE
111
112#endif // QRGB_H
113