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 constexpr QColorVector(float x, float y, float z) : x(x), y(y), z(z) { }
66 explicit 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 { }
71 float x = 0.0f; // X, x or red
72 float y = 0.0f; // Y, y or green
73 float z = 0.0f; // Z, Y or blue
74 float _unused = 0.0f;
75
76 friend inline bool operator==(const QColorVector &v1, const QColorVector &v2);
77 friend inline bool operator!=(const QColorVector &v1, const QColorVector &v2);
78 bool isNull() const
79 {
80 return !x && !y && !z;
81 }
82
83 static bool isValidChromaticity(const QPointF &chr)
84 {
85 if (chr.x() < qreal(0.0) || chr.x() > qreal(1.0))
86 return false;
87 if (chr.y() <= qreal(0.0) || chr.y() > qreal(1.0))
88 return false;
89 if (chr.x() + chr.y() > qreal(1.0))
90 return false;
91 return true;
92 }
93
94 // Common whitepoints:
95 static constexpr QPointF D50Chromaticity() { return QPointF(0.34567, 0.35850); }
96 static constexpr QPointF D65Chromaticity() { return QPointF(0.31271, 0.32902); }
97 static constexpr QColorVector D50() { return QColorVector(D50Chromaticity()); }
98 static constexpr QColorVector D65() { return QColorVector(D65Chromaticity()); }
99};
100
101inline bool operator==(const QColorVector &v1, const QColorVector &v2)
102{
103 return (std::abs(v1.x - v2.x) < (1.0f / 2048.0f))
104 && (std::abs(v1.y - v2.y) < (1.0f / 2048.0f))
105 && (std::abs(v1.z - v2.z) < (1.0f / 2048.0f));
106}
107
108inline bool operator!=(const QColorVector &v1, const QColorVector &v2)
109{
110 return !(v1 == v2);
111}
112
113
114// A matrix mapping 3 value colors.
115// Not using QTransform because only floats are needed and performance is critical.
116class QColorMatrix
117{
118public:
119 // We are storing the matrix transposed as that is more convenient:
120 QColorVector r;
121 QColorVector g;
122 QColorVector b;
123
124 friend inline bool operator==(const QColorMatrix &m1, const QColorMatrix &m2);
125 friend inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2);
126
127 bool isNull() const
128 {
129 return r.isNull() && g.isNull() && b.isNull();
130 }
131 bool isValid() const
132 {
133 // A color matrix must be invertible
134 float det = r.x * (b.z * g.y - g.z * b.y) -
135 r.y * (b.z * g.x - g.z * b.x) +
136 r.z * (b.y * g.x - g.y * b.x);
137 return !qFuzzyIsNull(det);
138 }
139
140 QColorMatrix inverted() const
141 {
142 float det = r.x * (b.z * g.y - g.z * b.y) -
143 r.y * (b.z * g.x - g.z * b.x) +
144 r.z * (b.y * g.x - g.y * b.x);
145 det = 1.0f / det;
146 QColorMatrix inv;
147 inv.r.x = (g.y * b.z - b.y * g.z) * det;
148 inv.r.y = (b.y * r.z - r.y * b.z) * det;
149 inv.r.z = (r.y * g.z - g.y * r.z) * det;
150 inv.g.x = (b.x * g.z - g.x * b.z) * det;
151 inv.g.y = (r.x * b.z - b.x * r.z) * det;
152 inv.g.z = (g.x * r.z - r.x * g.z) * det;
153 inv.b.x = (g.x * b.y - b.x * g.y) * det;
154 inv.b.y = (b.x * r.y - r.x * b.y) * det;
155 inv.b.z = (r.x * g.y - g.x * r.y) * det;
156 return inv;
157 }
158 QColorMatrix operator*(const QColorMatrix &o) const
159 {
160 QColorMatrix comb;
161 comb.r.x = r.x * o.r.x + g.x * o.r.y + b.x * o.r.z;
162 comb.g.x = r.x * o.g.x + g.x * o.g.y + b.x * o.g.z;
163 comb.b.x = r.x * o.b.x + g.x * o.b.y + b.x * o.b.z;
164
165 comb.r.y = r.y * o.r.x + g.y * o.r.y + b.y * o.r.z;
166 comb.g.y = r.y * o.g.x + g.y * o.g.y + b.y * o.g.z;
167 comb.b.y = r.y * o.b.x + g.y * o.b.y + b.y * o.b.z;
168
169 comb.r.z = r.z * o.r.x + g.z * o.r.y + b.z * o.r.z;
170 comb.g.z = r.z * o.g.x + g.z * o.g.y + b.z * o.g.z;
171 comb.b.z = r.z * o.b.x + g.z * o.b.y + b.z * o.b.z;
172 return comb;
173
174 }
175 QColorVector map(const QColorVector &c) const
176 {
177 return QColorVector { c.x * r.x + c.y * g.x + c.z * b.x,
178 c.x * r.y + c.y * g.y + c.z * b.y,
179 c.x * r.z + c.y * g.z + c.z * b.z };
180 }
181 QColorMatrix transposed() const
182 {
183 return QColorMatrix { { r.x, g.x, b.x },
184 { r.y, g.y, b.y },
185 { r.z, g.z, b.z } };
186 }
187
188 static QColorMatrix identity()
189 {
190 return { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } };
191 }
192 static QColorMatrix fromScale(QColorVector v)
193 {
194 return QColorMatrix { { v.x, 0.0f, 0.0f },
195 { 0.0f, v.y, 0.0f },
196 { 0.0f, 0.0f, v.z } };
197 }
198 // These are used to recognize matrices from ICC profiles:
199 static QColorMatrix toXyzFromSRgb()
200 {
201 return QColorMatrix { { 0.4360217452f, 0.2224751115f, 0.0139281144f },
202 { 0.3851087987f, 0.7169067264f, 0.0971015394f },
203 { 0.1430812478f, 0.0606181994f, 0.7141585946f } };
204 }
205 static QColorMatrix toXyzFromAdobeRgb()
206 {
207 return QColorMatrix { { 0.6097189188f, 0.3111021519f, 0.0194766335f },
208 { 0.2052682191f, 0.6256770492f, 0.0608891509f },
209 { 0.1492247432f, 0.0632209629f, 0.7448224425f } };
210 }
211 static QColorMatrix toXyzFromDciP3D65()
212 {
213 return QColorMatrix { { 0.5150973201f, 0.2411795557f, -0.0010491034f },
214 { 0.2919696569f, 0.6922441125f, 0.0418830328f },
215 { 0.1571449190f, 0.0665764511f, 0.7843542695f } };
216 }
217 static QColorMatrix toXyzFromProPhotoRgb()
218 {
219 return QColorMatrix { { 0.7976672649f, 0.2880374491f, 0.0000000000f },
220 { 0.1351922452f, 0.7118769884f, 0.0000000000f },
221 { 0.0313525312f, 0.0000856627f, 0.8251883388f } };
222 }
223};
224
225inline bool operator==(const QColorMatrix &m1, const QColorMatrix &m2)
226{
227 return (m1.r == m2.r) && (m1.g == m2.g) && (m1.b == m2.b);
228}
229
230inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2)
231{
232 return !(m1 == m2);
233}
234
235QT_END_NAMESPACE
236
237#endif // QCOLORMATRIX_P_H
238