Desaturate.qml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (C) 2020 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. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import Qt5Compat.GraphicalEffects.private
  6. /*!
  7. \qmltype Desaturate
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since QtGraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-color
  12. \brief Reduces the saturation of the colors.
  13. Desaturated pixel values are calculated as averages of the original RGB
  14. component values of the source item.
  15. \table
  16. \header
  17. \li Source
  18. \li Effect applied
  19. \row
  20. \li \image Original_bug.png
  21. \li \image Desaturate_bug.png
  22. \endtable
  23. \section1 Example
  24. The following example shows how to apply the effect.
  25. \snippet Desaturate-example.qml example
  26. */
  27. Item {
  28. id: rootItem
  29. /*!
  30. This property defines the source item that provides the source pixels to
  31. the effect.
  32. \note It is not supported to let the effect include itself, for
  33. instance by setting source to the effect's parent.
  34. */
  35. property variant source
  36. /*!
  37. This property defines how much the source colors are desaturated.
  38. The value ranges from 0.0 (no change) to 1.0 (desaturated). By default,
  39. the property is set to \c 0.0 (no change).
  40. \table
  41. \header
  42. \li Output examples with different desaturation values
  43. \li
  44. \li
  45. \row
  46. \li \image Desaturate_desaturation1.png
  47. \li \image Desaturate_desaturation2.png
  48. \li \image Desaturate_desaturation3.png
  49. \row
  50. \li \b { desaturation: 0.0 }
  51. \li \b { desaturation: 0.5 }
  52. \li \b { desaturation: 1.0 }
  53. \endtable
  54. */
  55. property real desaturation: 0.0
  56. /*!
  57. This property allows the effect output pixels to be cached in order to
  58. improve the rendering performance.
  59. Every time the source or effect properties are changed, the pixels in
  60. the cache must be updated. Memory consumption is increased, because an
  61. extra buffer of memory is required for storing the effect output.
  62. It is recommended to disable the cache when the source or the effect
  63. properties are animated.
  64. By default, the property is set to \c false.
  65. */
  66. property bool cached: false
  67. SourceProxy {
  68. id: sourceProxy
  69. input: rootItem.source
  70. interpolation: input && input.smooth ? SourceProxy.LinearInterpolation : SourceProxy.NearestInterpolation
  71. }
  72. ShaderEffectSource {
  73. id: cacheItem
  74. anchors.fill: parent
  75. visible: rootItem.cached
  76. smooth: true
  77. sourceItem: shaderItem
  78. live: true
  79. hideSource: visible
  80. }
  81. ShaderEffect {
  82. id: shaderItem
  83. property variant source: sourceProxy.output
  84. property real desaturation: rootItem.desaturation
  85. anchors.fill: parent
  86. fragmentShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/desaturate.frag.qsb"
  87. }
  88. }