1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QCOLORMATRIX_H |
5 | #define QCOLORMATRIX_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtGui/qtguiglobal.h> |
19 | #include <QtCore/qpoint.h> |
20 | #include <QtCore/private/qglobal_p.h> |
21 | #include <cmath> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | // An abstract 3 value color |
26 | class QColorVector |
27 | { |
28 | public: |
29 | QColorVector() = default; |
30 | constexpr QColorVector(float x, float y, float z) : x(x), y(y), z(z) { } |
31 | explicit constexpr QColorVector(const QPointF &chr) // from XY chromaticity |
32 | : x(chr.x() / chr.y()) |
33 | , y(1.0f) |
34 | , z((1.0 - chr.x() - chr.y()) / chr.y()) |
35 | { } |
36 | float x = 0.0f; // X, x or red |
37 | float y = 0.0f; // Y, y or green |
38 | float z = 0.0f; // Z, Y or blue |
39 | float _unused = 0.0f; |
40 | |
41 | friend inline bool operator==(const QColorVector &v1, const QColorVector &v2); |
42 | friend inline bool operator!=(const QColorVector &v1, const QColorVector &v2); |
43 | bool isNull() const |
44 | { |
45 | return !x && !y && !z; |
46 | } |
47 | |
48 | static bool isValidChromaticity(const QPointF &chr) |
49 | { |
50 | if (chr.x() < qreal(0.0) || chr.x() > qreal(1.0)) |
51 | return false; |
52 | if (chr.y() <= qreal(0.0) || chr.y() > qreal(1.0)) |
53 | return false; |
54 | if (chr.x() + chr.y() > qreal(1.0)) |
55 | return false; |
56 | return true; |
57 | } |
58 | |
59 | // Common whitepoints: |
60 | static constexpr QPointF D50Chromaticity() { return QPointF(0.34567, 0.35850); } |
61 | static constexpr QPointF D65Chromaticity() { return QPointF(0.31271, 0.32902); } |
62 | static constexpr QColorVector D50() { return QColorVector(D50Chromaticity()); } |
63 | static constexpr QColorVector D65() { return QColorVector(D65Chromaticity()); } |
64 | }; |
65 | |
66 | inline bool operator==(const QColorVector &v1, const QColorVector &v2) |
67 | { |
68 | return (std::abs(x: v1.x - v2.x) < (1.0f / 2048.0f)) |
69 | && (std::abs(x: v1.y - v2.y) < (1.0f / 2048.0f)) |
70 | && (std::abs(x: v1.z - v2.z) < (1.0f / 2048.0f)); |
71 | } |
72 | |
73 | inline bool operator!=(const QColorVector &v1, const QColorVector &v2) |
74 | { |
75 | return !(v1 == v2); |
76 | } |
77 | |
78 | |
79 | // A matrix mapping 3 value colors. |
80 | // Not using QTransform because only floats are needed and performance is critical. |
81 | class QColorMatrix |
82 | { |
83 | public: |
84 | // We are storing the matrix transposed as that is more convenient: |
85 | QColorVector r; |
86 | QColorVector g; |
87 | QColorVector b; |
88 | |
89 | friend inline bool operator==(const QColorMatrix &m1, const QColorMatrix &m2); |
90 | friend inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2); |
91 | |
92 | bool isNull() const |
93 | { |
94 | return r.isNull() && g.isNull() && b.isNull(); |
95 | } |
96 | bool isValid() const |
97 | { |
98 | // A color matrix must be invertible |
99 | float det = r.x * (b.z * g.y - g.z * b.y) - |
100 | r.y * (b.z * g.x - g.z * b.x) + |
101 | r.z * (b.y * g.x - g.y * b.x); |
102 | return !qFuzzyIsNull(f: det); |
103 | } |
104 | bool isIdentity() const noexcept |
105 | { |
106 | return *this == identity(); |
107 | } |
108 | |
109 | QColorMatrix inverted() const |
110 | { |
111 | float det = r.x * (b.z * g.y - g.z * b.y) - |
112 | r.y * (b.z * g.x - g.z * b.x) + |
113 | r.z * (b.y * g.x - g.y * b.x); |
114 | det = 1.0f / det; |
115 | QColorMatrix inv; |
116 | inv.r.x = (g.y * b.z - b.y * g.z) * det; |
117 | inv.r.y = (b.y * r.z - r.y * b.z) * det; |
118 | inv.r.z = (r.y * g.z - g.y * r.z) * det; |
119 | inv.g.x = (b.x * g.z - g.x * b.z) * det; |
120 | inv.g.y = (r.x * b.z - b.x * r.z) * det; |
121 | inv.g.z = (g.x * r.z - r.x * g.z) * det; |
122 | inv.b.x = (g.x * b.y - b.x * g.y) * det; |
123 | inv.b.y = (b.x * r.y - r.x * b.y) * det; |
124 | inv.b.z = (r.x * g.y - g.x * r.y) * det; |
125 | return inv; |
126 | } |
127 | QColorMatrix operator*(const QColorMatrix &o) const |
128 | { |
129 | QColorMatrix comb; |
130 | comb.r.x = r.x * o.r.x + g.x * o.r.y + b.x * o.r.z; |
131 | comb.g.x = r.x * o.g.x + g.x * o.g.y + b.x * o.g.z; |
132 | comb.b.x = r.x * o.b.x + g.x * o.b.y + b.x * o.b.z; |
133 | |
134 | comb.r.y = r.y * o.r.x + g.y * o.r.y + b.y * o.r.z; |
135 | comb.g.y = r.y * o.g.x + g.y * o.g.y + b.y * o.g.z; |
136 | comb.b.y = r.y * o.b.x + g.y * o.b.y + b.y * o.b.z; |
137 | |
138 | comb.r.z = r.z * o.r.x + g.z * o.r.y + b.z * o.r.z; |
139 | comb.g.z = r.z * o.g.x + g.z * o.g.y + b.z * o.g.z; |
140 | comb.b.z = r.z * o.b.x + g.z * o.b.y + b.z * o.b.z; |
141 | return comb; |
142 | |
143 | } |
144 | QColorVector map(const QColorVector &c) const |
145 | { |
146 | return QColorVector { c.x * r.x + c.y * g.x + c.z * b.x, |
147 | c.x * r.y + c.y * g.y + c.z * b.y, |
148 | c.x * r.z + c.y * g.z + c.z * b.z }; |
149 | } |
150 | QColorMatrix transposed() const |
151 | { |
152 | return QColorMatrix { .r: { r.x, g.x, b.x }, |
153 | .g: { r.y, g.y, b.y }, |
154 | .b: { r.z, g.z, b.z } }; |
155 | } |
156 | |
157 | static QColorMatrix identity() |
158 | { |
159 | return { .r: { 1.0f, 0.0f, 0.0f }, .g: { 0.0f, 1.0f, 0.0f }, .b: { 0.0f, 0.0f, 1.0f } }; |
160 | } |
161 | static QColorMatrix fromScale(QColorVector v) |
162 | { |
163 | return QColorMatrix { .r: { v.x, 0.0f, 0.0f }, |
164 | .g: { 0.0f, v.y, 0.0f }, |
165 | .b: { 0.0f, 0.0f, v.z } }; |
166 | } |
167 | // These are used to recognize matrices from ICC profiles: |
168 | static QColorMatrix toXyzFromSRgb() |
169 | { |
170 | return QColorMatrix { .r: { 0.4360217452f, 0.2224751115f, 0.0139281144f }, |
171 | .g: { 0.3851087987f, 0.7169067264f, 0.0971015394f }, |
172 | .b: { 0.1430812478f, 0.0606181994f, 0.7141585946f } }; |
173 | } |
174 | static QColorMatrix toXyzFromAdobeRgb() |
175 | { |
176 | return QColorMatrix { .r: { 0.6097189188f, 0.3111021519f, 0.0194766335f }, |
177 | .g: { 0.2052682191f, 0.6256770492f, 0.0608891509f }, |
178 | .b: { 0.1492247432f, 0.0632209629f, 0.7448224425f } }; |
179 | } |
180 | static QColorMatrix toXyzFromDciP3D65() |
181 | { |
182 | return QColorMatrix { .r: { 0.5150973201f, 0.2411795557f, -0.0010491034f }, |
183 | .g: { 0.2919696569f, 0.6922441125f, 0.0418830328f }, |
184 | .b: { 0.1571449190f, 0.0665764511f, 0.7843542695f } }; |
185 | } |
186 | static QColorMatrix toXyzFromProPhotoRgb() |
187 | { |
188 | return QColorMatrix { .r: { 0.7976672649f, 0.2880374491f, 0.0000000000f }, |
189 | .g: { 0.1351922452f, 0.7118769884f, 0.0000000000f }, |
190 | .b: { 0.0313525312f, 0.0000856627f, 0.8251883388f } }; |
191 | } |
192 | }; |
193 | |
194 | inline bool operator==(const QColorMatrix &m1, const QColorMatrix &m2) |
195 | { |
196 | return (m1.r == m2.r) && (m1.g == m2.g) && (m1.b == m2.b); |
197 | } |
198 | |
199 | inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2) |
200 | { |
201 | return !(m1 == m2); |
202 | } |
203 | |
204 | QT_END_NAMESPACE |
205 | |
206 | #endif // QCOLORMATRIX_P_H |
207 | |