-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
235 lines (208 loc) · 6.33 KB
/
Copy pathindex.js
File metadata and controls
235 lines (208 loc) · 6.33 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const { converter, formatHex, okhsl, rgb, parse } = culori;
function toOKHSL(r, g, b) {
return converter("okhsl")({ mode: "rgb", r: r, g: g, b: b });
}
function toRGB(h, s, l) {
return converter("rgb")({ mode: "okhsl", h: h, s: s, l: l });
}
// id binder
const FILED_HEX_IN_OUT = "hex-in-output";
const FIELD_COLOR_NAME = "color-name";
const SLIDER_SATURATION = "slider_saturation";
const SLIDER_LIGHTNESS = "slider_lightness";
const SLIDER_HUE = "slider_hue";
const GRID_SATURATION = "grid_saturation";
const GRID_LIGHTNESS = "grid_lightness";
const GRID_HUE = "grid_hue";
const satSlider = document.getElementById(SLIDER_SATURATION);
const lightSlider = document.getElementById(SLIDER_LIGHTNESS);
const hueSlider = document.getElementById(SLIDER_HUE);
const satGrid = document.getElementById(GRID_SATURATION);
const lightGrid = document.getElementById(GRID_LIGHTNESS);
const hueGrid = document.getElementById(GRID_HUE);
// global values
let lightnessValue;
let saturationValue;
let hueValue;
app = require("photoshop").app;
action = require("photoshop").action;
// setup routine
getColorFromApp();
updatePreview(lightnessValue, saturationValue, hueValue);
function updatePreview() {
//color
let previewColor = document.getElementById("current-color");
let color = toRGB(hueValue, saturationValue, lightnessValue);
previewColor.style.backgroundColor = `rgb(${color.r * 255}, ${
color.g * 255
}, ${color.b * 255})`;
// color value
let previewValue = document.getElementById("current-value");
color = toRGB(0, 0, lightnessValue);
previewValue.style.backgroundColor = `rgb(${color.r * 255}, ${
color.g * 255
}, ${color.b * 255})`;
// hex value
let hexInput = document.getElementById(FILED_HEX_IN_OUT);
color = toRGB(hueValue, saturationValue, lightnessValue);
let hex = formatHex(color);
hexInput.value = hex;
//color name
let colorNameInput = document.getElementById(FIELD_COLOR_NAME);
const colors = colorNameList.reduce(
(o, { name, hex }) => Object.assign(o, { [name]: hex }),
{}
);
const nearest = nearestColor.from(colors);
colorNameInput.value = nearest(hex).name;
setColorInApp();
}
// COLOR BANDS
function redrawColorBands() {
clearGrid();
createHueGrid(lightnessValue, saturationValue, hueValue);
createLightGrid(lightnessValue, saturationValue, hueValue);
createSatGrid(lightnessValue, saturationValue, hueValue);
updatePreview(lightnessValue, saturationValue, hueValue);
}
function clearGrid() {
while (satGrid.firstChild) {
satGrid.removeChild(satGrid.firstChild);
}
while (lightGrid.firstChild) {
lightGrid.removeChild(lightGrid.firstChild);
}
while (hueGrid.firstChild) {
hueGrid.removeChild(hueGrid.firstChild);
}
}
// HUE GRID
function createHueGrid(lightness, saturation, hue) {
const stops = new Array(16).fill(String()).map((_v, i) => {
const oklab = {
mode: "okhsl",
h: (360 / 15) * i,
s: saturation,
l: lightness,
};
return `${formatHex(oklab)}`;
});
createColorBand(stops, GRID_HUE);
}
// SATURATION GRID
function createSatGrid(lightness, saturation, hue) {
const stops = new Array(16).fill(String()).map((_v, i) => {
const oklab = { mode: "okhsl", h: hue, s: (1 / 15) * i, l: lightness };
return `${formatHex(oklab)}`;
});
createColorBand(stops, GRID_SATURATION);
}
// LIGHTNESS GRID
function createLightGrid(lightness, saturation, hue) {
const stops = new Array(16).fill(String()).map((_v, i) => {
const oklab = { mode: "okhsl", h: hue, s: saturation, l: (1 / 15) * i };
return `${formatHex(oklab)}`;
});
createColorBand(stops, GRID_LIGHTNESS);
}
function createColorBand(stops, grid) {
let box = document.createElement("div");
box.classList.add("color-band");
box.style.background = `linear-gradient(to right, ${stops})`;
grid = document.getElementById(grid);
grid.appendChild(box);
}
// Event Listener
// HEX IN OUT
document.getElementById(FILED_HEX_IN_OUT).addEventListener("change", (e) => {
getColorFromHex(e.target.value);
});
// Lightness / hue / saturation
document.getElementById(SLIDER_LIGHTNESS).addEventListener("input", (e) => {
lightnessValue = e.target.value * 0.01;
HandleSliderChange();
});
document.getElementById(SLIDER_SATURATION).addEventListener("input", (e) => {
saturationValue = e.target.value * 0.01;
HandleSliderChange();
});
document.getElementById(SLIDER_HUE).addEventListener("input", (e) => {
hueValue = e.target.value;
HandleSliderChange();
});
// listen for color set with eyedropper
var colorSetListener = (e, d) => {
console.log(e, d);
if (!d._ref == "color") {
return;
}
getColorFromApp();
};
action.addNotificationListener([{ event: "set" }], colorSetListener);
async function setColorInApp() {
rgbColor = toRGB(hueValue, saturationValue, lightnessValue);
const batchCommands = {
_obj: "set",
_target: [
{
_ref: "color",
_property: "foregroundColor",
},
],
to: {
_obj: "RGBColor",
red: rgbColor.r * 255,
grain: rgbColor.g * 255,
blue: rgbColor.b * 255,
},
source: "photoshopPicker",
_options: {
dialogOptions: "dontDisplay",
},
};
return await require("photoshop").core.executeAsModal(async () => {
await require("photoshop").action.batchPlay([batchCommands], {});
});
}
// get Colors
function getColorFromHex(hex) {
okColor = okhsl(hex);
const parsedColor = parse(hex);
if (parsedColor !== null && parsedColor !== undefined) {
return;
}
setColorValues(okColor);
}
function getColorFromApp() {
var foregroundColor = app.foregroundColor;
fRGB = foregroundColor.rgb;
okColor = toOKHSL(fRGB.red / 255, fRGB.green / 255, fRGB.blue / 255);
setColorValues(okColor);
}
// set Color
function setColorValues(okColor) {
lightnessValue = okColor.l;
saturationValue = okColor.s;
hueValue = okColor.h;
redrawColorBands();
lightSlider.value = Math.round(lightnessValue * 100);
satSlider.value = Math.round(saturationValue * 100);
hueSlider.value = Math.round(hueValue);
}
// Util
const throttledRedraw = throttle(redrawColorBands, 50);
function HandleSliderChange() {
throttledRedraw();
}
function throttle(func, limit) {
let inThrottle;
return function () {
const args = arguments;
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}