-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex.js
More file actions
47 lines (43 loc) · 1.63 KB
/
hex.js
File metadata and controls
47 lines (43 loc) · 1.63 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
const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
const btn = document.getElementById("btn");
const color = document.querySelector(".color");
const RGB = document.querySelector(".RGB_color");
btn.addEventListener("click",function(){
let hexColor = '#';
for(let i = 0; i<6;i++){
hexColor += hex[getRandomNum()];
}
let RGB_color = Hex_toRGB(hexColor);
document.body.style.backgroundColor = hexColor;
color.textContent = hexColor;
RGB.textContent = RGB_color;
})
function getRandomNum(){
return Math.floor(Math.random() * hex.length);
}
function Hex_toRGB(hex){
//let R,G,B = 0 이런 식으로 쓰면 안되나?
let R = 0;
let G = 0;
let B = 0;
//hex 값은 문자열이니까 함수 안에서 값을 바꿔도 외부에 영향을 주지 않는다.
//멍충아 값이 안 바뀌지
//배열을 이용해야 하나
const hex_arr=[];
for (let i = 1; i < hex.length; i++) {
switch(hex[i]){
//case 1,2,3,4,5,6,7,8,9: hex_arr[i] = hex[i];break; 이렇게 쓰면 안되나?
case "0":case "1":case "2":case "3":case '4':case '5':case '6':case '7':case '8': case '9': hex_arr[i] = +hex[i];break;
case "A": hex_arr[i] = 10; break;
case "B": hex_arr[i] = 11; break;
case "C": hex_arr[i] = 12; break;
case "D": hex_arr[i] = 13; break;
case "E": hex_arr[i] = 14; break;
case "F": hex_arr[i] = 15; break;
}
}
R+=(hex_arr[1]*16+hex_arr[2]);
G+=(hex_arr[3]*16+hex_arr[4]);
B+=(hex_arr[5]*16+hex_arr[6]);
return "rgb(" + R + "," + G +","+B+ ")";
}