-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (88 loc) · 3.74 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
function debounce(func, delay) {
let timeout;
return function () {
clearTimeout(timeout);
timeout = setTimeout(func, delay);
};
}
const debounceSendColorFromCMYK = debounce(function () {
sendColorFromCMYK();
}, 500);
const debounceSendColorFromLAB = debounce(function () {
sendColorFromLAB();
}, 500);
const debounceSendColorFromHSV = debounce(function () {
sendColorFromHSV();
}, 500);
// Функции для синхронизации ползунков с полями CMYK
function syncCMYKSlider(inputId, sliderId) {
document.getElementById(inputId).value = document.getElementById(sliderId).value;
updateColorCircle(); // Обновляем цвет круга сразу
}
// Функции для синхронизации ползунков с полями LAB
function syncLABSlider(inputId, sliderId) {
document.getElementById(inputId).value = document.getElementById(sliderId).value;
}
// Функции для синхронизации ползунков с полями HSV
function syncHSVSlider(inputId, sliderId) {
document.getElementById(inputId).value = document.getElementById(sliderId).value;
}
// Функция отправки данных на сервер для CMYK
function sendColorFromCMYK() {
const c = document.getElementById("c").value;
const m = document.getElementById("m").value;
const y = document.getElementById("y").value;
const k = document.getElementById("k").value;
console.log(`Sending CMYK: C=${c}, M=${m}, Y=${y}, K=${k}`);
fetch('/convert/cmyk', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({c: c, m: m, y: y, k: k}),
})
.then(response => response.json())
.then(updateFields)
.catch(error => console.error('Error:', error));
}
// Обновляем поля на основе данных с сервера
function updateFields(data) {
// Обновляем поля CMYK
document.getElementById("c").value = data.cmyk.c.toFixed(2);
document.getElementById("m").value = data.cmyk.m.toFixed(2);
document.getElementById("y").value = data.cmyk.y.toFixed(2);
document.getElementById("k").value = data.cmyk.k.toFixed(2);
// Обновляем цвет круга
updateColorCircle();
}
// Функция обновления круга цвета
function updateColorCircle() {
const c = parseFloat(document.getElementById("c").value);
const m = parseFloat(document.getElementById("m").value);
const y = parseFloat(document.getElementById("y").value);
const k = parseFloat(document.getElementById("k").value);
const r = Math.round(255 * (1 - c) * (1 - k));
const g = Math.round(255 * (1 - m) * (1 - k));
const b = Math.round(255 * (1 - y) * (1 - k));
document.getElementById("colorDisplay").style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}
// Функция обработки выбора цвета из палитры
function updateFromPicker() {
const color = document.getElementById("colorPicker").value;
document.getElementById("colorDisplay").style.backgroundColor = color;
const r = parseInt(color.slice(1, 3), 16);
const g = parseInt(color.slice(3, 5), 16);
const b = parseInt(color.slice(5, 7), 16);
const k = 1 - Math.max(r / 255, g / 255, b / 255);
const c = (1 - r / 255 - k) / (1 - k);
const m = (1 - g / 255 - k) / (1 - k);
const y = (1 - b / 255 - k) / (1 - k);
document.getElementById("c").value = c.toFixed(2);
document.getElementById("m").value = m.toFixed(2);
document.getElementById("y").value = y.toFixed(2);
document.getElementById("k").value = k.toFixed(2);
sendColorFromCMYK();
}
window.onload = function () {
sendColorFromCMYK();
};