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 "qcolortransfertable_p.h"
21
22QT_BEGIN_NAMESPACE
23
24
25// Defines an ICC TRC (Tone Reproduction Curve)
26class Q_GUI_EXPORT QColorTrc
27{
28public:
29 QColorTrc() noexcept : m_type(Type::Uninitialized)
30 { }
31 QColorTrc(const QColorTransferFunction &fun) : m_type(Type::Function), m_fun(fun)
32 { }
33 QColorTrc(const QColorTransferTable &table) : m_type(Type::Table), m_table(table)
34 { }
35
36 enum class Type {
37 Uninitialized,
38 Function,
39 Table
40 };
41
42 bool isLinear() const
43 {
44 return m_type == Type::Uninitialized || (m_type == Type::Function && m_fun.isLinear());
45 }
46 bool isValid() const
47 {
48 return m_type != Type::Uninitialized;
49 }
50 float apply(float x) const
51 {
52 if (m_type == Type::Table)
53 return m_table.apply(x);
54 if (m_type == Type::Function)
55 return m_fun.apply(x);
56 return x;
57 }
58 float applyExtended(float x) const
59 {
60 if (x >= 0.0f && x <= 1.0f)
61 return apply(x);
62 if (m_type == Type::Function)
63 return std::copysign(x: m_fun.apply(x: std::abs(x: x)), y: x);
64 if (m_type == Type::Table)
65 return x < 0.0f ? 0.0f : 1.0f;
66 return x;
67 }
68 float applyInverse(float x) const
69 {
70 if (m_type == Type::Table)
71 return m_table.applyInverse(x);
72 if (m_type == Type::Function)
73 return m_fun.inverted().apply(x);
74 return x;
75 }
76 float applyInverseExtended(float x) const
77 {
78 if (x >= 0.0f && x <= 1.0f)
79 return applyInverse(x);
80 if (m_type == Type::Function)
81 return std::copysign(x: applyInverse(x: std::abs(x: x)), y: x);
82 if (m_type == Type::Table)
83 return x < 0.0f ? 0.0f : 1.0f;
84 return x;
85 }
86
87 friend inline bool operator!=(const QColorTrc &o1, const QColorTrc &o2);
88 friend inline bool operator==(const QColorTrc &o1, const QColorTrc &o2);
89
90 Type m_type;
91 QColorTransferFunction m_fun;
92 QColorTransferTable m_table;
93};
94
95inline bool operator!=(const QColorTrc &o1, const QColorTrc &o2)
96{
97 if (o1.m_type != o2.m_type)
98 return true;
99 if (o1.m_type == QColorTrc::Type::Function)
100 return o1.m_fun != o2.m_fun;
101 if (o1.m_type == QColorTrc::Type::Table)
102 return o1.m_table != o2.m_table;
103 return false;
104}
105inline bool operator==(const QColorTrc &o1, const QColorTrc &o2)
106{
107 return !(o1 != o2);
108}
109
110QT_END_NAMESPACE
111
112#endif // QCOLORTRC
113

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