forked from SE7ENSKY/jquery-text-gradient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-text-gradient.js
57 lines (48 loc) · 1.75 KB
/
jquery-text-gradient.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
// Generated by CoffeeScript 1.6.3
/*
@name jquery-text-gradient
@description Simple linear text gradient plugin for jQuery
@version 2.0.1
@author Se7enSky studio <[email protected]>
@example <h1 class="text-gradient" data-text-gradient="#000000-#ffffff">Just an example</h1>
*/
/*! jquery-text-gradient 2.0.1 http://github.com/Se7enSky/jquery-text-gradient*/
(function() {
var arr2hex, hex2arr;
hex2arr = function(s) {
return [parseInt(s.substr(0, 2), 16), parseInt(s.substr(2, 2), 16), parseInt(s.substr(4, 2), 16)];
};
arr2hex = function(a) {
return a[0].toString(16) + a[1].toString(16) + a[2].toString(16);
};
$.fn.textGradient = function() {
$.each(this, function() {
var color, configString, from, i, j, letter, letters, m, to, _i, _ref, _results;
letters = $(this).text().split('');
from = [0, 0, 0];
to = [255, 255, 255];
configString = $(this).data('text-gradient');
if (m = configString.match(new RegExp('^#([0-9a-fA-F]{6})-#([0-9a-fA-F]{6})$'))) {
from = hex2arr(m[1]);
to = hex2arr(m[2]);
}
$(this).empty();
_results = [];
for (i = _i = 0, _ref = letters.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
letter = letters[i];
color = (function() {
var _j, _results1;
_results1 = [];
for (j = _j = 0; _j <= 2; j = ++_j) {
_results1.push(from[j] + Math.round((to[j] - from[j]) * (i / letters.length)));
}
return _results1;
})();
_results.push($(this).append("<span style=\"color: #" + (arr2hex(color)) + "\">" + letter + "</span>"));
}
return _results;
});
return this;
};
$(".text-gradient").textGradient();
}).call(this);