Debian Codegraph @

src/imports/Components/1.0/Icon.qml

raw
1 /*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18
19 Item {
20 id: icon
21
22 property string name
23
24 /*!
25 \qmlproperty color color
26 */
27 property alias color: colorizedImage.keyColorOut
28
29 /*!
30 \qmlproperty color keyColor
31 */
32 property alias keyColor: colorizedImage.keyColorIn
33
34 implicitWidth: image.implicitWidth
35 implicitHeight: image.implicitHeight
36
37 Image {
38 id: image
39 objectName: "image"
40 anchors.fill: parent
41 fillMode: Image.PreserveAspectFit
42
43 sourceSize {
44 width: icon.width
45 height: icon.height
46 }
47
48 source: {
49 if (icon.hasOwnProperty("source"))
50 return icon.source;
51 else if (icon.name)
52 return "image://theme/%1".arg(icon.name);
53 else
54 return "";
55 }
56
57 cache: true
58 visible: !colorizedImage.visible
59 }
60
61 ShaderEffect {
62 id: colorizedImage
63 objectName: "shader"
64
65 anchors.fill: parent
66
67 // Whether or not a color has been set.
68 visible: image.status == Image.Ready && keyColorOut != Qt.rgba(0.0, 0.0, 0.0, 0.0)
69
70 property Image source: image
71 property color keyColorOut: Qt.rgba(0.0, 0.0, 0.0, 0.0)
72 property color keyColorIn: "#808080"
73 property real threshold: 0.1
74
75 fragmentShader: "
76 varying highp vec2 qt_TexCoord0;
77 uniform sampler2D source;
78 uniform highp vec4 keyColorOut;
79 uniform highp vec4 keyColorIn;
80 uniform lowp float threshold;
81 uniform lowp float qt_Opacity;
82 void main() {
83 lowp vec4 sourceColor = texture2D(source, qt_TexCoord0);
84 gl_FragColor = mix(vec4(keyColorOut.rgb, 1.0) * sourceColor.a, sourceColor, step(threshold, distance(sourceColor.rgb / sourceColor.a, keyColorIn.rgb))) * qt_Opacity;
85 }"
86 }
87 }