-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolored_ip.js
104 lines (93 loc) · 3.69 KB
/
colored_ip.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
$(document).ready(function(){
$.getJSON("http://api.ipify.org?format=json", function (data) {
if (!data.ip) {
fail(data);
} else {
render(data);
}
}).fail(function(jqXHR, textStatus, errorThrown){
fail(errorThrown);
});
});
function fail(data) {
$('.section').not('#header').remove();
$('#header').html('Unfortunately there was an error retrieving your IP address :(<br> Sorry about that!');
console.log('ERROR: ', data);
}
function render(data) {
$("#ip").text(data.ip);
var octets = data.ip.split('.').map(function(str){return parseInt(str);});
percentages = octets.map(function(octet){
return Math.round( (octet/255)*1000 ) / 1000;
});
//RGB
var rgb = octets.slice(0,3);
var rgbCss = 'rgb(' + rgb.join(',') + ')';
$('#rgb').css('background-color', rgbCss);
var closestRGB = closestColorName(rgb);
$('#rgb_rgb').text(closestRGB.name);
$('#rgb_rgb').css('color', getForegroundColor(rgb));
$('.red').text(rgb[0]);
$('.green').text(rgb[1]);
$('.blue').text(rgb[2]);
//RGBA
var alpha = percentages[3];
var rgbaCss = 'rgba(' + rgb.join(',') + ',' + alpha + ')';
$('#rgba').css('background-color', rgbaCss);
$('#rgba_rgb').text(closestRGB.name + ' with ' + trunc(100-alpha*100) + '% transparency.');
$('#rgba_rgb').css('color', getForegroundColor(rgb, alpha));
$('.alpha').text(alpha);
//HSL
// hue (0-360), saturation (0-100), lightness (0-100)
var hsl = [percentages[0]*360, percentages[1]*100, percentages[2]*100];
var hslInrgb = colorConvert.hsl2rgb(hsl).map(Math.round);
$('#hsl').css('background-color', 'rgb(' + hslInrgb.join(',') + ')');
var closestHSL = closestColorName(hslInrgb);
$('#hsl_rgb').text(closestHSL.name);
$('#hsl_rgb').css('color', getForegroundColor(hslInrgb));
$('.hue').text(trunc(hsl[0]));
$('.saturation').text(trunc(hsl[1]) + '%');
$('.lightness' ).text(trunc(hsl[2]) + '%');
//HSLA
$('#hsla').css('background-color', 'rgba(' + hslInrgb.join(',') + ',' + alpha + ')');
$('#hsla_rgb').text(closestHSL.name + ' with ' + trunc(100-alpha*100) + '% transparency.');
$('#hsla_rgb').css('color', getForegroundColor(hslInrgb, alpha));
//HSV
var hsv = hsl //same structure, different semantics: hue, saturation, value
hsvInrgb = colorConvert.hsv2rgb(hsv).map(Math.round);
$('#hsv').css('background-color', 'rgb(' + hsvInrgb.join(',') + ')');
var closestHSV = closestColorName(hsvInrgb);
$('#hsv_rgb').text(closestHSV.name);
$('#hsv_rgb').css('color', getForegroundColor(hsvInrgb));
//HWB
var hwb = hsl //same structure, different semantics: hue, whiteness, blackness
hwbInrgb = colorConvert.hwb2rgb(hsv).map(Math.round);
$('#hwb').css('background-color', 'rgb(' + hwbInrgb.join(',') + ')');
var closestHWB = closestColorName(hwbInrgb);
$('#hwb_rgb').text(closestHWB.name);
$('#hwb_rgb').css('color', getForegroundColor(hwbInrgb));
//CMYK
var cmyk = percentages.map(function(p){return p*100;}); //CMYK is 4 values 0-100%
cmykInrgb = colorConvert.cmyk2rgb(cmyk).map(Math.round);
$('#cmyk').css('background-color', 'rgb(' + cmykInrgb.join(',') + ')');
var closestCMYK = closestColorName(cmykInrgb);
$('#cmyk_rgb').text(closestCMYK.name);
$('#cmyk_rgb').css('color', getForegroundColor(cmykInrgb));
$('.cyan').text(trunc(cmyk[0]) + '%');
$('.magenta').text(trunc(cmyk[1]) + '%');
$('.yellow').text(trunc(cmyk[2]) + '%');
$('.key').text(trunc(cmyk[3]) + '%');
}
function getForegroundColor(rgb, alpha) {
var yiq = ((rgb[0]*299)+(rgb[1]*587)+(rgb[2]*114))/1000;
if (yiq >= 128) {
return 'black'
}
if (alpha) {
return (alpha > 0.5) ? 'white': 'black';
}
return 'white'
}
function trunc(number) {
return Math.round(number * 10) / 10
}