-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (58 loc) · 1.58 KB
/
script.js
File metadata and controls
69 lines (58 loc) · 1.58 KB
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
var color;
function randomHex() {
var hexArr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var result = [];
for (var i = 0; i < 6; i++) {
result.push(hexArr[Math.floor(Math.random() * 16)]);
}
return '#' + result.join('');
}
function getBrightness(hex) {
hex = hex.substring(1);
var rgb = parseInt(hex, 16);
var r = (rgb >> 16) & 0xff;
var g = (rgb >> 8) & 0xff;
var b = (rgb >> 0) & 0xff;
var luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return luma < 128;
}
$(document).ready(function () {
color = randomHex();
$('.container').css('background-color', color);
$('.color-hex').html(color.toUpperCase());
if (getBrightness(color)) {
$('.text').css('color', '#ffffff');
} else {
$('.text').css('color', '#000000');
}
});
$(document).ready(function () {
$(this).on('keydown', function () {
if (event.which == 32 || event.which == 13) {
color = randomHex();
$('.container').css('background-color', color);
$('.color-hex').html(color.toUpperCase());
if (getBrightness(color)) {
$('.text').css('color', '#ffffff');
} else {
$('.text').css('color', '#000000');
}
}
});
});
$('.container').click(function () {
$('.color-hex').select(function () {
$('.container').unbind('click');
});
color = randomHex();
$('.container').css('background-color', color);
$('.color-hex').html(color.toUpperCase());
if (getBrightness(color)) {
$('.text').css('color', '#ffffff');
} else {
$('.text').css('color', '#000000');
}
});
$('.color-hex').click(function () {
navigator.clipboard.writeText(color.toUpperCase());
});