Desaturate.qml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. /*!
  42. \qmltype Desaturate
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-color
  47. \brief Reduces the saturation of the colors.
  48. Desaturated pixel values are calculated as averages of the original RGB
  49. component values of the source item.
  50. \table
  51. \header
  52. \li Source
  53. \li Effect applied
  54. \row
  55. \li \image Original_bug.png
  56. \li \image Desaturate_bug.png
  57. \endtable
  58. \note This effect is available when running with OpenGL.
  59. \section1 Example
  60. The following example shows how to apply the effect.
  61. \snippet Desaturate-example.qml example
  62. */
  63. Item {
  64. id: rootItem
  65. /*!
  66. This property defines the source item that provides the source pixels to
  67. the effect.
  68. \note It is not supported to let the effect include itself, for
  69. instance by setting source to the effect's parent.
  70. */
  71. property variant source
  72. /*!
  73. This property defines how much the source colors are desaturated.
  74. The value ranges from 0.0 (no change) to 1.0 (desaturated). By default,
  75. the property is set to \c 0.0 (no change).
  76. \table
  77. \header
  78. \li Output examples with different desaturation values
  79. \li
  80. \li
  81. \row
  82. \li \image Desaturate_desaturation1.png
  83. \li \image Desaturate_desaturation2.png
  84. \li \image Desaturate_desaturation3.png
  85. \row
  86. \li \b { desaturation: 0.0 }
  87. \li \b { desaturation: 0.5 }
  88. \li \b { desaturation: 1.0 }
  89. \endtable
  90. */
  91. property real desaturation: 0.0
  92. /*!
  93. This property allows the effect output pixels to be cached in order to
  94. improve the rendering performance.
  95. Every time the source or effect properties are changed, the pixels in
  96. the cache must be updated. Memory consumption is increased, because an
  97. extra buffer of memory is required for storing the effect output.
  98. It is recommended to disable the cache when the source or the effect
  99. properties are animated.
  100. By default, the property is set to \c false.
  101. */
  102. property bool cached: false
  103. SourceProxy {
  104. id: sourceProxy
  105. input: rootItem.source
  106. interpolation: input && input.smooth ? SourceProxy.LinearInterpolation : SourceProxy.NearestInterpolation
  107. }
  108. ShaderEffectSource {
  109. id: cacheItem
  110. anchors.fill: parent
  111. visible: rootItem.cached
  112. smooth: true
  113. sourceItem: shaderItem
  114. live: true
  115. hideSource: visible
  116. }
  117. ShaderEffect {
  118. id: shaderItem
  119. property variant source: sourceProxy.output
  120. property real desaturation: rootItem.desaturation
  121. anchors.fill: parent
  122. fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/desaturate.frag"
  123. }
  124. }