Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcolorutil.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qcolorutil_p.h"
5
6#include <QtGui/qcolor.h>
7#include <QtGui/qvector3d.h>
8#include <cmath>
9
10QT_BEGIN_NAMESPACE
11
12namespace {
13
14// Poor man's RGB to YUV conversion with BT.709 coefficients
15// from https://en.wikipedia.org/wiki/Y%E2%80%B2UV
16QVector3D RGBToYUV(const QColor &c)
17{
18 const float R = c.redF();
19 const float G = c.greenF();
20 const float B = c.blueF();
21 QVector3D yuv;
22 yuv[0] = 0.2126f * R + 0.7152f * G + 0.0722f * B;
23 yuv[1] = -0.09991f * R - 0.33609f * G + 0.436f * B;
24 yuv[2] = 0.615f * R - 0.55861f * G - 0.05639f * B;
25 return yuv;
26}
27
28} // namespace
29
30bool fuzzyCompare(const QColor &lhs, const QColor &rhs, float tol)
31{
32 const QVector3D lhsYuv = RGBToYUV(lhs);
33 const QVector3D rhsYuv = RGBToYUV(rhs);
34 const float relativeLumaDiff =
35 0.5f * std::abs((lhsYuv[0] - rhsYuv[0]) / (lhsYuv[0] + rhsYuv[0]));
36 const float colorDiff = QVector3D::crossProduct(lhsYuv, rhsYuv).length();
37 return colorDiff < tol && relativeLumaDiff < tol;
38}
39
40QT_END_NAMESPACE
QVector3D RGBToYUV(const QColor &c)
bool fuzzyCompare(const QColor &lhs, const QColor &rhs, float tol)