1// Copyright (C) 2018 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 QCOLORTRC_P_H
5#define QCOLORTRC_P_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/private/qtguiglobal_p.h>
19#include "qcolortransferfunction_p.h"
20#include "qcolortransfergeneric_p.h"
21#include "qcolortransfertable_p.h"
22
23QT_BEGIN_NAMESPACE
24
25// Defines a TRC (Tone Reproduction Curve)
26class Q_GUI_EXPORT QColorTrc
27{
28public:
29 QColorTrc() noexcept : m_type(Type::Uninitialized) { }
30 QColorTrc(const QColorTransferFunction &fun) : m_type(Type::ParameterizedFunction), m_fun(fun) { }
31 QColorTrc(const QColorTransferTable &table) : m_type(Type::Table), m_table(table) { }
32 QColorTrc(const QColorTransferGenericFunction &hdr) : m_type(Type::GenericFunction), m_hdr(hdr) { }
33 QColorTrc(QColorTransferFunction &&fun) noexcept : m_type(Type::ParameterizedFunction), m_fun(std::move(fun)) { }
34 QColorTrc(QColorTransferTable &&table) noexcept : m_type(Type::Table), m_table(std::move(table)) { }
35 QColorTrc(QColorTransferGenericFunction &&hdr) noexcept : m_type(Type::GenericFunction), m_hdr(std::move(hdr)) { }
36
37 enum class Type {
38 Uninitialized,
39 ParameterizedFunction,
40 GenericFunction,
41 Table,
42 };
43
44 bool isIdentity() const
45 {
46 return (m_type == Type::ParameterizedFunction && m_fun.isIdentity())
47 || (m_type == Type::Table && m_table.isIdentity());
48 }
49 bool isValid() const
50 {
51 return m_type != Type::Uninitialized;
52 }
53 float apply(float x) const
54 {
55 switch (m_type) {
56 case Type::ParameterizedFunction:
57 return fun().apply(x);
58 case Type::GenericFunction:
59 return hdr().apply(x);
60 case Type::Table:
61 return table().apply(x);
62 default:
63 break;
64 }
65 return x;
66 }
67 float applyExtended(float x) const
68 {
69 switch (m_type) {
70 case Type::ParameterizedFunction:
71 return std::copysign(x: fun().apply(x: std::abs(x: x)), y: x);
72 case Type::GenericFunction:
73 return hdr().apply(x);
74 case Type::Table:
75 return table().apply(x);
76 default:
77 break;
78 }
79 return x;
80 }
81 float applyInverse(float x) const
82 {
83 switch (m_type) {
84 case Type::ParameterizedFunction:
85 return fun().inverted().apply(x);
86 case Type::GenericFunction:
87 return hdr().applyInverse(x);
88 case Type::Table:
89 return table().applyInverse(x);
90 default:
91 break;
92 }
93 return x;
94 }
95 float applyInverseExtended(float x) const
96 {
97 switch (m_type) {
98 case Type::ParameterizedFunction:
99 return std::copysign(x: applyInverse(x: std::abs(x: x)), y: x);
100 case Type::GenericFunction:
101 return hdr().applyInverse(x);
102 case Type::Table:
103 return table().applyInverse(x);
104 default:
105 break;
106 }
107 return x;
108 }
109
110 const QColorTransferTable &table() const { return m_table; }
111 const QColorTransferFunction &fun() const{ return m_fun; }
112 const QColorTransferGenericFunction &hdr() const { return m_hdr; }
113 Type type() const noexcept { return m_type; }
114
115 Type m_type;
116
117 friend inline bool comparesEqual(const QColorTrc &lhs, const QColorTrc &rhs);
118 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QColorTrc);
119
120 QColorTransferFunction m_fun;
121 QColorTransferTable m_table;
122 QColorTransferGenericFunction m_hdr;
123};
124
125inline bool comparesEqual(const QColorTrc &o1, const QColorTrc &o2)
126{
127 if (o1.m_type != o2.m_type)
128 return false;
129 if (o1.m_type == QColorTrc::Type::ParameterizedFunction)
130 return o1.m_fun == o2.m_fun;
131 if (o1.m_type == QColorTrc::Type::Table)
132 return o1.m_table == o2.m_table;
133 if (o1.m_type == QColorTrc::Type::GenericFunction)
134 return o1.m_hdr == o2.m_hdr;
135 return true;
136}
137
138QT_END_NAMESPACE
139
140#endif // QCOLORTRC
141

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/gui/painting/qcolortrc_p.h