forked from AC6567/geraldanekwe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvertDivColor.js
More file actions
34 lines (30 loc) · 1.29 KB
/
invertDivColor.js
File metadata and controls
34 lines (30 loc) · 1.29 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
$(document).ready(function() {
function invertColor(hexTripletColor) {
var color = hexTripletColor;
color = color.substring(1); // remove #
color = parseInt(color, 16); // convert to integer
color = 0xFFFFFF ^ color; // invert three bytes
color = color.toString(16); // convert to hex
color = ("000000" + color).slice(-6); // pad with leading zeros
color = "#" + color; // prepend #
return color;
}
//converts rgb to hex (source: http://jsfiddle.net/Mottie/xcqpF/1/light/)
function rgb2hex(rgb){
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}
$("div").each(function(){
var r = Math.round(Math.random() * 255);
var g = Math.round(Math.random() * 255);
var b = Math.round(Math.random() * 255);
$(this).css("background-color", "rgb(" + r + "," + g + "," + b + ")");
var bgColor = $(this).css('background-color');
bgColor = rgb2hex(bgColor);
var color = invertColor(bgColor);
$(this).css("color", color);
});
});