This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
/
favicon.js
62 lines (59 loc) · 1.89 KB
/
favicon.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
(function() {
var hsltorgb;
hsltorgb = function(h, s, l) {
var hue, m1, m2;
h = (h % 360) / 360;
m2 = l * (s + 1);
m1 = (l * 2) - m2;
hue = function(num) {
if (num < 0) {
num += 1;
} else if (num > 1) {
num -= 1;
}
if ((num * 6) < 1) {
return m1 + (m2 - m1) * num * 6;
} else if ((num * 2) < 1) {
return m2;
} else if ((num * 3) < 2) {
return m1 + (m2 - m1) * (2 / 3 - num) * 6;
} else {
return m1;
}
};
return [hue(h + 1 / 3) * 255, hue(h) * 255, hue(h - 1 / 3) * 255];
};
window.plugins.favicon = {
create: function() {
var angle, canvas, colprep, cos, ctx, dark, fav, light, p, scale, sin, x, y;
$('body').append($('<canvas />').attr('width', 32).attr('height', 32).attr('id', 'favmaker').attr('display', 'none').hide());
canvas = document.getElementById('favmaker');
ctx = canvas.getContext('2d');
light = hsltorgb(Math.random() * 360, .78, .50);
dark = hsltorgb(Math.random() * 360, .78, .25);
angle = 2 * (Math.random() - 0.5);
sin = Math.sin(angle);
cos = Math.cos(angle);
scale = Math.abs(sin) + Math.abs(cos);
colprep = function(col, p) {
return Math.floor(light[col] * p + dark[col] * (1 - p)) % 255;
};
for (x = 0; x <= 31; x++) {
for (y = 0; y <= 31; y++) {
p = sin >= 0 ? sin * x + cos * y : -sin * (31 - x) + cos * y;
p = p / 31 / scale;
ctx.fillStyle = "rgba(" + (colprep(0, p)) + ", " + (colprep(1, p)) + ", " + (colprep(2, p)) + ", 1)";
ctx.fillRect(x, y, 1, 1);
}
}
fav = canvas.toDataURL();
$('#favicon').attr('href', fav);
$('.favicon').attr('src', fav);
return $.post('/favicon.png', {
image: fav
}, function(data) {
return console.log(data);
});
}
};
}).call(this);