-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorista.js
36 lines (29 loc) · 1.07 KB
/
colorista.js
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
;(function () {
'use strict';
var colorista = function(colors, spec) {
if (!(colors instanceof Array))
throw 'colors must be an Array';
if (spec === null || typeof spec !== 'object')
throw 'spec is not an Object';
var c;
if (colors.length === 0) // Default to completely random
c = '#'+Math.random().toString(16).substr(-6);
else
c = colors[Math.floor(colors.length * Math.random())];
for (var selector in spec) {
if (spec.hasOwnProperty(selector)) {
var elements = document.querySelectorAll(selector);
var properties = spec[selector];
for (var i = 0; i < elements.length; i++) {
var e = elements[i];
for (var j = 0; j < properties.length; j++) {
e.style[properties[j]] = c;
}
}
}
}
return c;
};
if (this.colorista) throw 'already defined';
this.colorista = colorista;
}).call(this);