-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocolor.uncompressed.js
170 lines (131 loc) · 4.14 KB
/
autocolor.uncompressed.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
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
/*
* jQuery auto-color plugin 1.01
*
* Copyright (c) 2018-2019 Michael Daum http://michaeldaumconsulting.com
*
* Licensed under the GPL license http://www.gnu.org/licenses/gpl.html
*
*/
"use strict";
(function($) {
// Create the defaults once
var defaults = {
text: undefined,
source: undefined,
target: undefined,
property: undefined,
saturation: [50,65,80],
lightness: [35,50,65,80],
hueFrom: 0,
hueTo: 359,
light: '#fff',
dark: '#222',
seed: '0'
};
// The actual plugin constructor
function AutoColor(elem, opts) {
var self = this;
self.elem = $(elem);
// gather opts by merging global defaults, plugin defaults and element defaults
self.opts = $.extend({}, defaults, self.elem.data(), opts);
self.init();
}
AutoColor.prototype.config = function(opts) {
var self = this;
$.extend(self.opts, opts);
self.init();
};
AutoColor.prototype.init = function () {
var self = this, text, hsl;
if (typeof(self.opts.text) !== 'undefined') {
text = self.opts.text;
} else if (typeof(self.opts.source) !== 'undefined') {
text = $.map(self.elem.find(self.opts.source), function(elem) {
return $(elem).text();
}).join(" ");
} else {
text = self.elem.text();
}
if (typeof(self.opts.target) !== 'undefiend') {
self.target = self.elem.find(self.opts.target);
}
if (typeof(self.target) === 'undefined' || self.target.length == 0) {
self.target = self.elem;
}
text = text.replace(/^\s*|\s*$/g, "");
if (!text.length) {
return;
}
hsl = self.formatHSL(self.getHSL(text));
//console.log("text=",text,"hash=",self.getHash(text),"hue=",self.getHue(self.getHash(text)));
if (self.opts.property) {
self.target.css(self.opts.property, hsl);
} else {
self.target
.addClass("jqAutoColor")
.css("background", hsl)
.css("color", self.getMatchingForeground());
}
};
AutoColor.prototype.getHash = function(text) {
var self = this, hash = 2, i, len;
text += self.opts.seed;
len = text.length;
for (i = 0; i < len; i++) {
hash = text.charCodeAt(i) + ((hash << 5) - hash);
}
return Math.abs(hash);
};
AutoColor.prototype.getHue = function(hash) {
var self = this,
hue = self.opts.hueFrom + hash % Math.abs(self.opts.hueTo - self.opts.hueFrom);
//console.log("hash=",hash,"from=",self.opts.hueFrom,"to=",self.opts.hueTo,"hue=",hue);
return hue;
};
AutoColor.prototype.getSaturation = function(hash) {
var self = this;
if (typeof(self.opts.saturation) === 'object') {
return self.opts.saturation[hash % self.opts.saturation.length];
}
return self.opts.saturation;
};
AutoColor.prototype.getLightness = function(hash) {
var self = this;
if (typeof(self.opts.lightness) === 'object') {
return self.opts.lightness[hash % self.opts.lightness.length];
}
return self.opts.lightness;
};
AutoColor.prototype.getHSL = function(text) {
var self = this,
hash = self.getHash(text);
return [self.getHue(hash), self.getSaturation(hash), self.getLightness(hash)];
};
AutoColor.prototype.formatHSL = function(hsl) {
return "hsl("+hsl[0]+","+hsl[1]+"%,"+hsl[2]+"%)";
};
AutoColor.prototype.getMatchingForeground = function() {
var self = this,
bg = self.target.css("background-color"),
rgb = bg.match(/\d+/g),
o = Math.round(((rgb[0] * 299) + (rgb[1] * 587) + (rgb[2] * 114)) /1000);
return o > 125 ? self.opts.dark: self.opts.light;
};
// A plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn.autoColor = function (opts) {
return this.each(function () {
var $this = $(this),
autoColor = $this.data("autoColor");
if (autoColor) {
autoColor.config(opts);
} else {
$this.data("autoColor", new AutoColor(this, opts));
}
});
};
// Enable declarative widget instanziation
$(".jqAutoColor").livequery(function() {
$(this).autoColor();
});
})(jQuery);