1/****************************************************************************
2**
3** Copyright (C) 2018 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 QCOLORMATRIX_H
41#define QCOLORMATRIX_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtGui/qtguiglobal.h>
55#include <QtCore/qpoint.h>
56#include <cmath>
57
58QT_BEGIN_NAMESPACE
59
60// An abstract 3 value color
61class QColorVector
62{
63public:
64 QColorVector() = default;
65 Q_DECL_CONSTEXPR QColorVector(float x, float y, float z) : x(x), y(y), z(z), _unused(0.0f) { }
66 explicit Q_DECL_CONSTEXPR QColorVector(const QPointF &chr) // from XY chromaticity
67 : x(chr.x() / chr.y())
68 , y(1.0f)
69 , z((1.0 - chr.x() - chr.y()) / chr.y())
70 , _unused(0.0f)
71 { }
72 float x; // X, x or red
73 float y; // Y, y or green
74 float z; // Z, Y or blue
75 float _unused;
76
77 friend inline bool operator==(const QColorVector &v1, const QColorVector &v2);
78 friend inline bool operator!=(const QColorVector &v1, const QColorVector &v2);
79 bool isNull() const
80 {
81 return !x && !y && !z;
82 }
83
84 static Q_DECL_CONSTEXPR QColorVector null() { return QColorVector(0.0f, 0.0f, 0.0f); }
85 static bool isValidChromaticity(const QPointF &chr)
86 {
87 if (chr.x() < qreal(0.0) || chr.x() > qreal(1.0))
88 return false;
89 if (chr.y() <= qreal(0.0) || chr.y() > qreal(1.0))
90 return false;
91 if (chr.x() + chr.y() > qreal(1.0))
92 return false;
93 return true;
94 }
95
96 // Common whitepoints:
97 static Q_DECL_CONSTEXPR QPointF D50Chromaticity() { return QPointF(0.34567, 0.35850); }
98 static Q_DECL_CONSTEXPR QPointF D65Chromaticity() { return QPointF(0.31271, 0.32902); }
99 static Q_DECL_CONSTEXPR QColorVector D50() { return QColorVector(D50Chromaticity()); }
100 static Q_DECL_CONSTEXPR QColorVector D65() { return QColorVector(D65Chromaticity()); }
101};
102
103inline bool operator==(const QColorVector &v1, const QColorVector &v2)
104{
105 return (std::abs(v1.x - v2.x) < (1.0f / 2048.0f))
106 && (std::abs(v1.y - v2.y) < (1.0f / 2048.0f))
107 && (std::abs(v1.z - v2.z) < (1.0f / 2048.0f));
108}
109
110inline bool operator!=(const QColorVector &v1, const QColorVector &v2)
111{
112 return !(v1 == v2);
113}
114
115
116// A matrix mapping 3 value colors.
117// Not using QMatrix because only floats are needed and performance is critical.
118class QColorMatrix
119{
120public:
121 // We are storing the matrix transposed as that is more convenient:
122 QColorVector r;
123 QColorVector g;
124 QColorVector b;
125
126 friend inline bool operator==(const QColorMatrix &m1, const QColorMatrix &m2);
127 friend inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2);
128
129 bool isNull() const
130 {
131 return r.isNull() && g.isNull() && b.isNull();
132 }
133 bool isValid() const
134 {
135 // A color matrix must be invertible
136 float det = r.x * (b.z * g.y - g.z * b.y) -
137 r.y * (b.z * g.x - g.z * b.x) +
138 r.z * (b.y * g.x - g.y * b.x);
139 return !qFuzzyIsNull(det);
140 }
141
142 QColorMatrix inverted() const
143 {
144 float det = r.x * (b.z * g.y - g.z * b.y) -
145 r.y * (b.z * g.x - g.z * b.x) +
146 r.z * (b.y * g.x - g.y * b.x);
147 det = 1.0f / det;
148 QColorMatrix inv;
149 inv.r.x = (g.y * b.z - b.y * g.z) * det;
150 inv.r.y = (b.y * r.z - r.y * b.z) * det;
151 inv.r.z = (r.y * g.z - g.y * r.z) * det;
152 inv.g.x = (b.x * g.z - g.x * b.z) * det;
153 inv.g.y = (r.x * b.z - b.x * r.z) * det;
154 inv.g.z = (g.x * r.z - r.x * g.z) * det;
155 inv.b.x = (g.x * b.y - b.x * g.y) * det;
156 inv.b.y = (b.x * r.y - r.x * b.y) * det;
157 inv.b.z = (r.x * g.y - g.x * r.y) * det;
158 return inv;
159 }
160 QColorMatrix operator*(const QColorMatrix &o) const
161 {
162 QColorMatrix comb;
163 comb.r.x = r.x * o.r.x + g.x * o.r.y + b.x * o.r.z;
164 comb.g.x = r.x * o.g.x + g.x * o.g.y + b.x * o.g.z;
165 comb.b.x = r.x * o.b.x + g.x * o.b.y + b.x * o.b.z;
166
167 comb.r.y = r.y * o.r.x + g.y * o.r.y + b.y * o.r.z;
168 comb.g.y = r.y * o.g.x + g.y * o.g.y + b.y * o.g.z;
169 comb.b.y = r.y * o.b.x + g.y * o.b.y + b.y * o.b.z;
170
171 comb.r.z = r.z * o.r.x + g.z * o.r.y + b.z * o.r.z;
172 comb.g.z = r.z * o.g.x + g.z * o.g.y + b.z * o.g.z;
173 comb.b.z = r.z * o.b.x + g.z * o.b.y + b.z * o.b.z;
174 return comb;
175
176 }
177 QColorVector map(const QColorVector &c) const
178 {
179 return QColorVector { c.x * r.x + c.y * g.x + c.z * b.x,
180 c.x * r.y + c.y * g.y + c.z * b.y,
181 c.x * r.z + c.y * g.z + c.z * b.z };
182 }
183 QColorMatrix transposed() const
184 {
185 return QColorMatrix { { r.x, g.x, b.x },
186 { r.y, g.y, b.y },
187 { r.z, g.z, b.z } };
188 }
189
190 static QColorMatrix null()
191 {
192 return { QColorVector::null(), QColorVector::null(), QColorVector::null() };
193 }
194 static QColorMatrix identity()
195 {
196 return { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } };
197 }
198 static QColorMatrix fromScale(QColorVector v)
199 {
200 return QColorMatrix { { v.x, 0.0f, 0.0f },
201 { 0.0f, v.y, 0.0f },
202 { 0.0f, 0.0f, v.z } };
203 }
204 // These are used to recognize matrices from ICC profiles:
205 static QColorMatrix toXyzFromSRgb()
206 {
207 return QColorMatrix { { 0.4360217452f, 0.2224751115f, 0.0139281144f },
208 { 0.3851087987f, 0.7169067264f, 0.0971015394f },
209 { 0.1430812478f, 0.0606181994f, 0.7141585946f } };
210 }
211 static QColorMatrix toXyzFromAdobeRgb()
212 {
213 return QColorMatrix { { 0.6097189188f, 0.3111021519f, 0.0194766335f },
214 { 0.2052682191f, 0.6256770492f, 0.0608891509f },
215 { 0.1492247432f, 0.0632209629f, 0.7448224425f } };
216 }
217 static QColorMatrix toXyzFromDciP3D65()
218 {
219 return QColorMatrix { { 0.5150973201f, 0.2411795557f, -0.0010491034f },
220 { 0.2919696569f, 0.6922441125f, 0.0418830328f },
221 { 0.1571449190f, 0.0665764511f, 0.7843542695f } };
222 }
223 static QColorMatrix toXyzFromProPhotoRgb()
224 {
225 return QColorMatrix { { 0.7976672649f, 0.2880374491f, 0.0000000000f },
226 { 0.1351922452f, 0.7118769884f, 0.0000000000f },
227 { 0.0313525312f, 0.0000856627f, 0.8251883388f } };
228 }
229 static QColorMatrix toXyzFromBt2020()
230 {
231 return QColorMatrix { { 0.6506130099f, 0.2695676684f, -0.0018652577f },
232 { 0.1865101457f, 0.6840794086f, 0.0172256753f },
233 { 0.1270887405f, 0.0463530831f, 0.8098278046f } };
234 }
235};
236
237inline bool operator==(const QColorMatrix &m1, const QColorMatrix &m2)
238{
239 return (m1.r == m2.r) && (m1.g == m2.g) && (m1.b == m2.b);
240}
241
242inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2)
243{
244 return !(m1 == m2);
245}
246
247QT_END_NAMESPACE
248
249#endif // QCOLORMATRIX_P_H
250