-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
107 lines (93 loc) · 3.27 KB
/
script.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
105
106
107
// API endpoints
const API_BASE_URL = 'https://x-colors.yurace.pro/api';
// DOM elements
const optionBtns = document.querySelectorAll('.option-btn');
const optionMenus = document.querySelectorAll('.option-menu');
const copyBtns = document.querySelectorAll('.copy-btn');
const backBtns = document.querySelectorAll('.back-btn');
const colorDisplays = document.querySelectorAll('.color-display');
const hexCodes = document.querySelectorAll('.hex-code');
const rgbCodes = document.querySelectorAll('.rgb-code');
const hslCodes = document.querySelectorAll('.hsl-code');
const hueInput = document.getElementById('hue-input');
const converterInputType = document.getElementById('converter-input-type');
const converterInput = document.getElementById('converter-input');
const converterOutputType = document.getElementById('converter-output-type');
const resultCode = document.querySelector('.result-code');
// Show option menu
function showOptionMenu(option) {
optionMenus.forEach(menu => menu.classList.remove('active'));
document.getElementById(`${option}-menu`).classList.add('active');
document.querySelector('.options').style.display = 'none';
}
// Hide option menu
function hideOptionMenu() {
optionMenus.forEach(menu => menu.classList.remove('active'));
document.querySelector('.options').style.display = 'flex';
}
// Get random color
async function getRandomColor() {
const response = await fetch(`${API_BASE_URL}/random`);
const data = await response.json();
updateColorDisplay(data);
}
// Get random color with hue
async function getRandomColorByHue() {
const hue = hueInput.value || 0;
const response = await fetch(`${API_BASE_URL}/random/${hue}`);
const data = await response.json();
updateColorDisplay(data);
}
// Convert color
async function convertColor() {
const inputType = converterInputType.value;
const outputType = converterOutputType.value;
const value = converterInput.value;
const endpoint = `${API_BASE_URL}/${inputType}2${outputType}?value=${value}`;
const response = await fetch(endpoint);
const data = await response.text();
resultCode.textContent = data;
updateColorDisplay({ [outputType]: data });
}
// Update color display and codes
function updateColorDisplay(data) {
colorDisplays.forEach(display => {
display.style.backgroundColor = data.hex;
});
hexCodes.forEach(code => {
code.textContent = data.hex;
});
rgbCodes.forEach(code => {
code.textContent = data.rgb;
});
hslCodes.forEach(code => {
code.textContent = data.hsl;
});
}
// Copy color code
function copyColorCode(event) {
const code = event.target.previousElementSibling.textContent;
navigator.clipboard.writeText(code);
}
// Event listeners
optionBtns.forEach(btn => {
btn.addEventListener('click', () => {
const option = btn.dataset.option;
showOptionMenu(option);
if (option === 'random') {
getRandomColor();
}
});
});
backBtns.forEach(btn => {
btn.addEventListener('click', hideOptionMenu);
});
copyBtns.forEach(btn => {
btn.addEventListener('click', copyColorCode);
});
hueInput.addEventListener('input', getRandomColorByHue);
converterInput.addEventListener('input', convertColor);
converterInputType.addEventListener('change', convertColor);
converterOutputType.addEventListener('change', convertColor);
// Initial state
getRandomColor();