Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qcolormatrix_p.h
Go to the documentation of this file.
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
24
25// An abstract 3 value color
27{
28public:
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
66inline bool operator==(const QColorVector &v1, const QColorVector &v2)
67{
68 return (std::abs(v1.x - v2.x) < (1.0f / 2048.0f))
69 && (std::abs(v1.y - v2.y) < (1.0f / 2048.0f))
70 && (std::abs(v1.z - v2.z) < (1.0f / 2048.0f));
71}
72
73inline 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.
82{
83public:
84 // We are storing the matrix transposed as that is more convenient:
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(det);
103 }
104 bool isIdentity() const noexcept
105 {
106 return *this == identity();
107 }
108
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 }
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 }
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 }
151 {
152 return QColorMatrix { { r.x, g.x, b.x },
153 { r.y, g.y, b.y },
154 { r.z, g.z, b.z } };
155 }
156
158 {
159 return { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } };
160 }
162 {
163 return QColorMatrix { { v.x, 0.0f, 0.0f },
164 { 0.0f, v.y, 0.0f },
165 { 0.0f, 0.0f, v.z } };
166 }
167 // These are used to recognize matrices from ICC profiles:
169 {
170 return QColorMatrix { { 0.4360217452f, 0.2224751115f, 0.0139281144f },
171 { 0.3851087987f, 0.7169067264f, 0.0971015394f },
172 { 0.1430812478f, 0.0606181994f, 0.7141585946f } };
173 }
175 {
176 return QColorMatrix { { 0.6097189188f, 0.3111021519f, 0.0194766335f },
177 { 0.2052682191f, 0.6256770492f, 0.0608891509f },
178 { 0.1492247432f, 0.0632209629f, 0.7448224425f } };
179 }
181 {
182 return QColorMatrix { { 0.5150973201f, 0.2411795557f, -0.0010491034f },
183 { 0.2919696569f, 0.6922441125f, 0.0418830328f },
184 { 0.1571449190f, 0.0665764511f, 0.7843542695f } };
185 }
187 {
188 return QColorMatrix { { 0.7976672649f, 0.2880374491f, 0.0000000000f },
189 { 0.1351922452f, 0.7118769884f, 0.0000000000f },
190 { 0.0313525312f, 0.0000856627f, 0.8251883388f } };
191 }
192};
193
194inline 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
199inline bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2)
200{
201 return !(m1 == m2);
202}
203
205
206#endif // QCOLORMATRIX_P_H
QColorMatrix inverted() const
static QColorMatrix toXyzFromSRgb()
static QColorMatrix toXyzFromAdobeRgb()
bool isValid() const
friend bool operator==(const QColorMatrix &m1, const QColorMatrix &m2)
QColorVector g
static QColorMatrix identity()
QColorMatrix transposed() const
static QColorMatrix fromScale(QColorVector v)
QColorMatrix operator*(const QColorMatrix &o) const
QColorVector b
QColorVector r
bool isIdentity() const noexcept
static QColorMatrix toXyzFromDciP3D65()
static QColorMatrix toXyzFromProPhotoRgb()
QColorVector map(const QColorVector &c) const
friend bool operator!=(const QColorMatrix &m1, const QColorMatrix &m2)
bool isNull() const
static constexpr QColorVector D50()
static constexpr QPointF D65Chromaticity()
static bool isValidChromaticity(const QPointF &chr)
friend bool operator!=(const QColorVector &v1, const QColorVector &v2)
friend bool operator==(const QColorVector &v1, const QColorVector &v2)
bool isNull() const
QColorVector()=default
constexpr QColorVector(float x, float y, float z)
static constexpr QPointF D50Chromaticity()
constexpr QColorVector(const QPointF &chr)
static constexpr QColorVector D65()
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
Combined button and popup list for selecting options.
bool operator!=(const QColorVector &v1, const QColorVector &v2)
bool operator==(const QColorVector &v1, const QColorVector &v2)
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:303
GLint GLfloat GLfloat GLfloat v2
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLboolean r
[2]
GLint GLfloat GLfloat v1
GLboolean GLboolean g
GLint y
const GLubyte * c
double qreal
Definition qtypes.h:92