forked from predixdesignsystem/px-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
px-vis-behavior-colors.html
244 lines (204 loc) · 7.08 KB
/
px-vis-behavior-colors.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script>
var PxColorsBehavior = window.PxColorsBehavior = (window.PxColorsBehavior || {});
/*
Name:
PxColorsBehavior.dataVisColors
Description:
Polymer behavior that provides the dataVisColors and an order to use them
@polymerBehavior PxColorsBehavior.dataVisColors
*/
PxColorsBehavior.dataVisColors = {
properties: {
/**
* Defines an colors in order that will be used for series.
*
* @type {Array}
*/
seriesColorList: {
type: Array,
value: [
'rgb(0,0,0)',
'rgb(75,75,75)',
'rgb(125,125,125)',
'rgb(200,200,200)'
]
}
}
};
/*
Name:
PxColorsBehavior.dataVisColorTheming
Description:
Attempts to read data vis color theme from CSS style variables or default
values and apply to the visualization element.
@polymerBehavior PxColorsBehavior.dataVisColorTheming
*/
PxColorsBehavior.dataVisColorTheming = [{
observers: ['syncCSSTheme(_stylesUpdated)'],
ready: function() {
// To access any available CSS theming variables, we need to wait for the
// first animation frame to complete
window.requestAnimationFrame(this.syncCSSTheme.bind(this));
},
/**
* Retrieves the CSS style variables set on this element and applies them
* to the appropriate properties, triggering a redraw.
*
* @method syncCSSTheme
*/
syncCSSTheme: function() {
// FIXME Aggressive debounce for simple charts. Look in solving the issue so we can reduce/remove this debounce.
this.debounce('syncCSSTheme', this._debounceSyncCSSTheme, 100);
},
/**
* Debounced function call for `syncCSSTheme` method. Loops through available
* style variables to apply them to appropriate objects.
*
* @private
* @method _debounceSyncCSSTheme
*/
_debounceSyncCSSTheme: function() {
var firstThemedColor;
if (Polymer.Element) {
// 2.0 code
if (window.ShadyCSS) {
firstThemedColor = ShadyCSS.getComputedStyleValue(this, '--px-vis-series-color-0');
} else {
firstThemedColor = getComputedStyle(this).getPropertyValue('--px-vis-series-color-0');
}
} else {
// 1.0 code
firstThemedColor = this.getComputedStyleValue('--px-vis-series-color-0');
}
// If there's a theme color set, attempt to get all CSS style variables
// and prepare to apply them.
if (firstThemedColor) {
this._applyStyleVariables();
}
this.dispatchEvent(new CustomEvent('px-data-vis-colors-applied'));
},
/**
* Called when there is at least one style variable applied (the first is
* expected to be named `--px-vis-series-color-0`). Loops through each
* style variable in the format `--px-vis-series-color-[n]` and applies
* the resulting values and series color order to the element. Stops looping
* through style variables when it finds a gap.
*
* @private
* @method _applyStyleVariables
*/
_applyStyleVariables: function() {
var variableBase = '--px-vis-series-color-';
var newColors = [];
var index = 0;
var stop = false;
var currentColor;
//check if the developer passed in their own seriesColorOrder
var devSet = this._checkIfDevSetSeriesColorOrder();
if(devSet) {
return;
}
// Loop over style variables until a gap is found
while (!stop) {
if (Polymer.Element) {
// 2.0 code
if (window.ShadyCSS) {
currentColor = ShadyCSS.getComputedStyleValue(this, '--px-vis-series-color-' + index);
} else {
currentColor = getComputedStyle(this).getPropertyValue('--px-vis-series-color-' + index);
}
} else {
// 1.0 code
currentColor = this.getComputedStyleValue('--px-vis-series-color-' + index);
}
if (!currentColor) {
// No more colors found, break out of while loop
stop = true;
};
if (currentColor) {
// Ensure the color is converted to RGB
currentColor = currentColor[0] === '#' ? this._colorHexToRgb(currentColor) : currentColor;
newColors.push(currentColor);
};
// Iterate index so we continue to get style variables
index += 1;
};
this.set('seriesColorList', newColors);
},
/**
* Converts a hex-format color to RGB.
*
* @private
* @method _colorHexToRgb
* @param {String} hex - A color in hex format
* @return {String} - A color in RGB format
*/
_colorHexToRgb: function(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? 'rgb(' + parseInt(result[1], 16) + ',' +
parseInt(result[2], 16)+ ',' +
parseInt(result[3], 16) + ')' : null;
},
/**
* Compares seriesColorOrder property value to the seriesColorOrder to determine if the dev set their own override.
*
* @private
* @method _checkIfDevSetSeriesColorOrder
* @return {Boolean} - true if dev set seriesColorOrder
*/
_checkIfDevSetSeriesColorOrder: function() {
var defaultOrder = PxColorsBehavior.dataVisColors.properties.seriesColorList.value;
if(defaultOrder.length !== this.seriesColorList.length) {
return true;
}
for(var i = 0; i < defaultOrder.length; i++) {
if(defaultOrder[i] !== this.seriesColorList[i]) {
return true;
}
}
return false;
}
}, PxColorsBehavior.dataVisColors];
/*
Name:
PxColorsBehavior.getSeriesColors
Description:
Polymer behavior that provides the ability to retrieve the color of a serie given an index
Dependencies:
-
@polymerBehavior PxColorsBehavior.getSeriesColors
*/
PxColorsBehavior.getSeriesColors = [{
/**
* Helper function to return the correct color for a particular index.
**/
_getColor: function(i) {
var l = this.seriesColorList.length,
index = this._calcIndex(i,l);
return this.seriesColorList[ index ];
},
/**
* Helper function to calculate the index. When we run out of indcies, it loops back over valid indicies.
**/
_calcIndex: function(i, l) {
return i < l ? i : this._calcIndex(i - l,l);
}
}, PxColorsBehavior.dataVisColors ];
</script>