-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdectohex.js
30 lines (27 loc) · 840 Bytes
/
dectohex.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
// function rgb(r, g, b) {
// const rgb = [r,g,b];
// let res = "";
// for(i = 0; i < rgb.length; i++) {
// (rgb[i] < 0) ? res += "00" :
// (rgb[i] > 255) ? res += "FF" :
// (rgb[i].toString(16).length != 2) ? res += "0" + rgb[i].toString(16) :
// res += rgb[i].toString(16);
// }
// return res;
// }
// Mejor solución usando forEach
function rgb(r, g, b) {
const rgb = [r, g, b];
let res = "";
rgb.forEach( elem => {
(elem < 0) ? res += "00" :
(elem > 255) ? res += "FF" :
(elem.toString(16).length != 2) ? res += "0" + elem.toString(16).toUpperCase() :
res += elem.toString(16).toUpperCase();
} )
return res;
}
console.log(rgb(245,10,277));
console.log(rgb(25,178,235));
console.log(rgb(255,78,0));
console.log(rgb(133,199,203));