1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Graphical Effects module.
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
40import QtQuick 2.12
41import QtGraphicalEffects.private 1.12
42
43/*!
44    \qmltype HueSaturation
45    \inqmlmodule QtGraphicalEffects
46    \since QtGraphicalEffects 1.0
47    \inherits QtQuick2::Item
48    \ingroup qtgraphicaleffects-color
49    \brief Alters the source item colors in the HSL color space.
50
51    HueSaturation is similar to the \l{QtGraphicalEffects::Colorize}{Colorize}
52    effect, but the hue and saturation property values are handled differently.
53    The HueSaturation effect always shifts the hue, saturation, and lightness
54    from the original, instead of setting them.
55
56    \table
57    \header
58        \li Source
59        \li Effect applied
60    \row
61        \li \image Original_bug.png
62        \li \image HueSaturation_bug.png
63    \endtable
64
65    \note This effect is available when running with OpenGL.
66
67    \section1 Example
68
69    The following example shows how to apply the effect.
70    \snippet HueSaturation-example.qml example
71
72*/
73Item {
74    id: rootItem
75
76    /*!
77        This property defines the source item that provides the source pixels
78        for the effect.
79
80        \note It is not supported to let the effect include itself, for
81        instance by setting source to the effect's parent.
82    */
83    property variant source: 0
84
85    /*!
86        This property defines the hue value which is added to the source hue
87        value.
88
89        The value ranges from -1.0 (decrease) to 1.0 (increase). By default, the
90        property is set to \c 0.0 (no change).
91
92        \table
93        \header
94        \li Output examples with different hue values
95        \li
96        \li
97        \row
98            \li \image HueSaturation_hue1.png
99            \li \image HueSaturation_hue2.png
100            \li \image HueSaturation_hue3.png
101        \row
102            \li \b { hue: -0.3 }
103            \li \b { hue: 0.0 }
104            \li \b { hue: 0.3 }
105        \row
106            \li \l saturation: 0
107            \li \l saturation: 0
108            \li \l saturation: 0
109        \row
110            \li \l lightness: 0
111            \li \l lightness: 0
112            \li \l lightness: 0
113        \endtable
114
115    */
116    property real hue: 0.0
117
118    /*!
119        This property defines the saturation value value which is added to the
120        source saturation value.
121
122        The value ranges from -1.0 (decrease) to 1.0 (increase). By default, the
123        property is set to \c 0.0 (no change).
124
125        \table
126        \header
127        \li Output examples with different saturation values
128        \li
129        \li
130        \row
131            \li \image HueSaturation_saturation1.png
132            \li \image HueSaturation_saturation2.png
133            \li \image HueSaturation_saturation3.png
134        \row
135            \li \b { saturation: -0.8 }
136            \li \b { saturation: 0.0 }
137            \li \b { saturation: 1.0 }
138        \row
139            \li \l hue: 0
140            \li \l hue: 0
141            \li \l hue: 0
142        \row
143            \li \l lightness: 0
144            \li \l lightness: 0
145            \li \l lightness: 0
146        \endtable
147
148    */
149    property real saturation: 0.0
150
151    /*!
152        This property defines the lightness value which is added to the source
153        saturation value.
154
155        The value ranges from -1.0 (decrease) to 1.0 (increase). By default, the
156        property is set to \c 0.0 (no change).
157
158        \table
159        \header
160        \li Output examples with different lightness values
161        \li
162        \li
163        \row
164            \li \image HueSaturation_lightness1.png
165            \li \image HueSaturation_lightness2.png
166            \li \image HueSaturation_lightness3.png
167        \row
168            \li \b { lightness: -0.5 }
169            \li \b { lightness: 0.0 }
170            \li \b { lightness: 0.5 }
171        \row
172            \li \l hue: 0
173            \li \l hue: 0
174            \li \l hue: 0
175        \row
176            \li \l saturation: 0
177            \li \l saturation: 0
178            \li \l saturation: 0
179        \endtable
180
181    */
182    property real lightness: 0.0
183
184    /*!
185        This property allows the effect output pixels to be cached in order to
186        improve the rendering performance.
187
188        Every time the source or effect properties are changed, the pixels in
189        the cache must be updated. Memory consumption is increased, because an
190        extra buffer of memory is required for storing the effect output.
191
192        It is recommended to disable the cache when the source or the effect
193        properties are animated.
194
195        By default, the property is set to \c false.
196    */
197    property bool cached: false
198
199    SourceProxy {
200        id: sourceProxy
201        input: rootItem.source
202        interpolation: input && input.smooth ? SourceProxy.LinearInterpolation : SourceProxy.NearestInterpolation
203    }
204
205    ShaderEffectSource {
206        id: cacheItem
207        anchors.fill: parent
208        visible: rootItem.cached
209        smooth: true
210        sourceItem: shaderItem
211        live: true
212        hideSource: visible
213    }
214
215    ShaderEffect {
216        id: shaderItem
217        property variant source: sourceProxy.output
218        property variant hsl: Qt.vector3d(rootItem.hue, rootItem.saturation, rootItem.lightness)
219
220        anchors.fill: parent
221
222        fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/huesaturation.frag"
223    }
224}
225