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 QCOLORTRC_P_H
41#define QCOLORTRC_P_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/private/qtguiglobal_p.h>
55#include "qcolortransferfunction_p.h"
56#include "qcolortransfertable_p.h"
57
58QT_BEGIN_NAMESPACE
59
60
61// Defines an ICC TRC (Tone Reproduction Curve)
62class Q_GUI_EXPORT QColorTrc
63{
64public:
65 QColorTrc() noexcept : m_type(Type::Uninitialized)
66 { }
67 QColorTrc(const QColorTransferFunction &fun) : m_type(Type::Function), m_fun(fun)
68 { }
69 QColorTrc(const QColorTransferTable &table) : m_type(Type::Table), m_table(table)
70 { }
71
72 enum class Type {
73 Uninitialized,
74 Function,
75 Table
76 };
77
78 bool isLinear() const
79 {
80 return m_type == Type::Uninitialized || (m_type == Type::Function && m_fun.isLinear());
81 }
82 bool isValid() const
83 {
84 return m_type != Type::Uninitialized;
85 }
86 float apply(float x) const
87 {
88 if (m_type == Type::Table)
89 return m_table.apply(x);
90 if (m_type == Type::Function)
91 return m_fun.apply(x);
92 return x;
93 }
94 float applyExtended(float x) const
95 {
96 if (x >= 0.0f && x <= 1.0f)
97 return apply(x);
98 if (m_type == Type::Function)
99 return std::copysign(x: m_fun.apply(x: std::abs(x: x)), y: x);
100 if (m_type == Type::Table)
101 return x < 0.0f ? 0.0f : 1.0f;
102 return x;
103 }
104 float applyInverse(float x) const
105 {
106 if (m_type == Type::Table)
107 return m_table.applyInverse(x);
108 if (m_type == Type::Function)
109 return m_fun.inverted().apply(x);
110 return x;
111 }
112 float applyInverseExtended(float x) const
113 {
114 if (x >= 0.0f && x <= 1.0f)
115 return applyInverse(x);
116 if (m_type == Type::Function)
117 return std::copysign(x: applyInverse(x: std::abs(x: x)), y: x);
118 if (m_type == Type::Table)
119 return x < 0.0f ? 0.0f : 1.0f;
120 return x;
121 }
122
123 friend inline bool operator!=(const QColorTrc &o1, const QColorTrc &o2);
124 friend inline bool operator==(const QColorTrc &o1, const QColorTrc &o2);
125
126 Type m_type;
127 QColorTransferFunction m_fun;
128 QColorTransferTable m_table;
129};
130
131inline bool operator!=(const QColorTrc &o1, const QColorTrc &o2)
132{
133 if (o1.m_type != o2.m_type)
134 return true;
135 if (o1.m_type == QColorTrc::Type::Function)
136 return o1.m_fun != o2.m_fun;
137 if (o1.m_type == QColorTrc::Type::Table)
138 return o1.m_table != o2.m_table;
139 return false;
140}
141inline bool operator==(const QColorTrc &o1, const QColorTrc &o2)
142{
143 return !(o1 != o2);
144}
145
146QT_END_NAMESPACE
147
148#endif // QCOLORTRC
149

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