-
Notifications
You must be signed in to change notification settings - Fork 7
/
rgba-matrix.html
105 lines (89 loc) · 2.93 KB
/
rgba-matrix.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<link rel="import" href="dist/polymer.html">
<dom-module id="rgba-matrix">
<template>
<style>
/* local styles go here */
:host {
}
</style>
<!-- local DOM goes here -->
<svg width="100%" height="100%" viewBox="0 0 200 100" preserveAspectRatio = "xMinYMin slice">
<defs>
<filter id="colorChange">
<feColorMatrix
type = "matrix"
rgbaToFeColorMatrix = "255, 0, 0, 1"
values="1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0 "/>
</filter>
</defs>
<image id="randomImage"
xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img/bg_01.jpg"
width="100%" height="100%" x="0" y="0"
filter="url(#colorChange)">
</image>
</svg>
</template>
<script>
Polymer({
is: 'rgba-matrix',
ready: function(){
//setup
},
//instance methods
buildMatrix: function(){
return `${this.r} 0 0 0 0 `
+ `0 ${this.g} 0 0 0 `
+ `0 0 ${this.b} 0 0 `
+ `0 0 0 ${this.a} 0 `;
},
setMatrix: function(r,g,b,a){
this.r = this.parseValue(r);
this.g = this.parseValue(g);
this.b = this.parseValue(b);
this.a = a;
this.refresh();
},
parseValue: function(val){
return Math.round((val / 255) * 100) / 100;
},
refresh: function(){
var matrix = this.buildMatrix();
// Refresh the image with the new Matrix
this.$.colorChange.querySelector('feColorMatrix').setAttribute('values', matrix);
},
//public properties
properties: {
r: {
type: Number,
value: 1,
reflectToAttribute: true
},
g: {
type: Number,
value: 1,
reflectToAttribute: true
},
b: {
type: Number,
value: 1,
reflectToAttribute: true
},
a: {
type: Number,
value: 1,
reflectToAttribute: true
}
},
//observers
observers : [
'propsChanged(r,g,b,a)'
],
propsChanged(){
this.refresh();
}
});
</script>
</dom-module>