-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb-hex.html
146 lines (137 loc) · 4.13 KB
/
rgb-hex.html
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>RGB <==> Hex</title>
<style>
#rgbBox,
#hexBox {
position: absolute;
top: 20px;
width: 100px;
height: 40px;
/* +10px of box padding, added in height */
}
.parent-box {
text-align: center;
position: relative;
}
.box {
min-width: 250px;
height: 30px;
display: inline-block;
margin-top: 20px;
text-align: center;
background-color: #e9eaeb;
padding: 5px;
}
</style>
</head>
<body>
<div class="parent-box">
<div id="rgbBox"> </div>
<div class="box">
<label for="rgb">
RGB(
<input type="text" id="rgb" placeholder="255 255 255 / 255, 255, 255" onkeyup="rgbToHex()" size="25" />
)
</label>
<span id="hexSTR"></span>
</div>
</div>
<div class="parent-box">
<div id="hexBox"> </div>
<div class="box">
<label for="hex">
#
<input type="text" id="hex" placeholder="0099ff" onkeyup="hexToRGB()" />
</label>
<span id="rgbSTR"></span>
</div>
</div>
<script>
function rgbToHex() {
let rgb = "";
rgb = document.getElementById("rgb").value.trim();
rgb = rgb.replace(/\s+/g, " ");
let hexValue = 'invalid RGB value: ' + rgb;
// if (!rgb.length || rgb.length < 5 || (rgb.match(/ /g) || []).length !== 2) {
if (!rgb.length || rgb.length < 5) {
return setHexa(hexValue);
}
const rgbaSplit = rgb.split(rgb.indexOf(',') > -1 ? "," : " ");
const r = rgbaSplit[0];
const g = rgbaSplit[1];
const b = rgbaSplit[2];
const a = rgbaSplit.length > 3 ? rgbaSplit[3] : 1;
hexValue = "#";
hexValue += toHexa(r);
hexValue += toHexa(g);
hexValue += toHexa(b);
hexValue += toAlpha(a);
setHexa(hexValue);
}
function toHexa(deciVal = "") {
const deciInINT = parseInt(deciVal, 10);
if (isNaN(deciInINT)) return "00";
return convertToHex(deciInINT);
}
function toAlpha(alphaValue = "") {
let alphaInFloat = parseFloat(alphaValue, 10);
if (alphaInFloat === undefined || alphaInFloat === null || isNaN(alphaInFloat) || alphaInFloat > 1 || alphaInFloat < 0) return "ff";
alphaInFloat = Math.round(alphaInFloat * 100) / 100; // for 0.95, two decimal places
alphaInFloat = Math.round(alphaInFloat * 255);
return convertToHex(alphaInFloat);
}
function convertToHex(deci) {
const inHex = deci.toString(16).toUpperCase();
if (inHex.length === 1) return "0" + inHex;
return inHex;
}
function setHexa(hexStr = "") {
document.getElementById("hexSTR").innerHTML = hexStr;
document.getElementById("rgbBox").setAttribute("style", "background-color: " + hexStr);
}
function hexToRGB() {
let hexa = "";
hexa = document.getElementById("hex").value.trim();
hexa = hexa.replace(/ /g, "");
let rgbValue = "Invalid Hex Value: " + hexa;
if (!hexa.length || hexa.length < 6) {
return setRGB(rgbValue);
}
if (hexa.indexOf("#") > -1) {
hexa = hexa.substr(1);
}
hexa = hexa.substr(0, 6);
const r = hexa.substr(0, 2);
const g = hexa.substr(2, 2);
const b = hexa.substr(4, 2);
hexa = "rgb(";
hexa += toDeci(r) + ", ";
hexa += toDeci(g) + ", ";
hexa += toDeci(b) + ")";
setRGB(hexa);
}
function toDeci(hexVal = "0") {
return parseInt(hexVal, 16);
}
function setRGB(rgbStr = "") {
document.getElementById("rgbSTR").innerHTML = rgbStr;
document.getElementById("hexBox").setAttribute("style", "background-color: " + rgbStr);
}
// function parseInteger(integer) {
// let parsed = parseInt(integer);
// return parsed || parsed === 0 ? parsed : 0;
// }
// function dividedBy(x, y) {
// let z = x / y;
// return isNaN(z) ? 0 : z;
// }
</script>
</body>
</html>