svelte-colourpicker 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
<svelte:options immutable />
|
|
2
|
+
|
|
3
|
+
<script context="module">const hueColours = ["#ff0000", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff", "#ff0000"];
|
|
4
|
+
const size = 230;
|
|
5
|
+
const numberRegExp = "(\\d*\\.?\\d{1,3})";
|
|
6
|
+
const delimiterRegExp = "[, ]+";
|
|
7
|
+
const rgbRegExp = new RegExp(`rgb\\(\\s*${numberRegExp}${delimiterRegExp}${numberRegExp}${delimiterRegExp}${numberRegExp}\\s*\\)`, "i");
|
|
8
|
+
const rgbaRegExp = new RegExp(`rgba\\(\\s*${numberRegExp}${delimiterRegExp}${numberRegExp}${delimiterRegExp}${numberRegExp}${delimiterRegExp}${numberRegExp}\\s*\\)`, "i");
|
|
9
|
+
const hexRegExp = (n) => `([\\dA-F]{${n}})`;
|
|
10
|
+
const hex3RegExp = new RegExp(`^#?${hexRegExp(1).repeat(3)}$`, "i");
|
|
11
|
+
const hex6RegExp = new RegExp(`^#?${hexRegExp(2).repeat(3)}$`, "i");
|
|
12
|
+
const hex8RegExp = new RegExp(`^#?${hexRegExp(2).repeat(4)}$`, "i");
|
|
13
|
+
function colour(input) {
|
|
14
|
+
let r, g, b, h, s, v, a, rgba, hex;
|
|
15
|
+
if (typeof input == "string") {
|
|
16
|
+
let result;
|
|
17
|
+
if (result = rgbRegExp.exec(input)) {
|
|
18
|
+
r = parseFloat(result[1]);
|
|
19
|
+
g = parseFloat(result[2]);
|
|
20
|
+
b = parseFloat(result[2]);
|
|
21
|
+
a = 1;
|
|
22
|
+
} else if (result = rgbaRegExp.exec(input)) {
|
|
23
|
+
r = parseFloat(result[1]);
|
|
24
|
+
g = parseFloat(result[2]);
|
|
25
|
+
b = parseFloat(result[3]);
|
|
26
|
+
a = parseFloat(result[4]);
|
|
27
|
+
} else if (result = hex3RegExp.exec(input)) {
|
|
28
|
+
r = parseInt(result[1].repeat(2), 16);
|
|
29
|
+
g = parseInt(result[2].repeat(2), 16);
|
|
30
|
+
b = parseInt(result[3].repeat(2), 16);
|
|
31
|
+
a = 1;
|
|
32
|
+
hex = `#${input}`;
|
|
33
|
+
} else if (result = hex6RegExp.exec(input)) {
|
|
34
|
+
r = parseInt(result[1], 16);
|
|
35
|
+
g = parseInt(result[2], 16);
|
|
36
|
+
b = parseInt(result[3], 16);
|
|
37
|
+
a = 1;
|
|
38
|
+
hex = `#${input}`;
|
|
39
|
+
} else if (result = hex8RegExp.exec(input)) {
|
|
40
|
+
r = parseInt(result[1], 16);
|
|
41
|
+
g = parseInt(result[2], 16);
|
|
42
|
+
b = parseInt(result[3], 16);
|
|
43
|
+
a = parseInt(result[4], 16);
|
|
44
|
+
hex = `#${input}`;
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error(`Invalid input: "${input}"`);
|
|
47
|
+
}
|
|
48
|
+
const hsv = RGBToHSV(r, g, b);
|
|
49
|
+
h = hsv.h;
|
|
50
|
+
s = hsv.s;
|
|
51
|
+
v = hsv.v;
|
|
52
|
+
} else if ("r" in input) {
|
|
53
|
+
r = input.r;
|
|
54
|
+
g = input.g;
|
|
55
|
+
b = input.b;
|
|
56
|
+
const hsv = RGBToHSV(r, g, b);
|
|
57
|
+
h = hsv.h;
|
|
58
|
+
s = hsv.s;
|
|
59
|
+
v = hsv.v;
|
|
60
|
+
a = "a" in input ? input.a : 1;
|
|
61
|
+
} else {
|
|
62
|
+
h = input.h;
|
|
63
|
+
s = input.s;
|
|
64
|
+
v = input.v;
|
|
65
|
+
const rgb = HSVToRGB(h, s, v);
|
|
66
|
+
r = rgb.r;
|
|
67
|
+
g = rgb.g;
|
|
68
|
+
b = rgb.b;
|
|
69
|
+
a = "a" in input ? input.a : 1;
|
|
70
|
+
}
|
|
71
|
+
rgba = `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
72
|
+
hex ??= `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}${a != 1 ? Math.round(a * 255).toString(16) : ""}`;
|
|
73
|
+
return {
|
|
74
|
+
r,
|
|
75
|
+
g,
|
|
76
|
+
b,
|
|
77
|
+
h,
|
|
78
|
+
s,
|
|
79
|
+
v,
|
|
80
|
+
a,
|
|
81
|
+
rgba,
|
|
82
|
+
hex
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function RGBToHSV(r, g, b) {
|
|
86
|
+
let h, s, v;
|
|
87
|
+
const r1 = r / 255;
|
|
88
|
+
const g1 = g / 255;
|
|
89
|
+
const b1 = b / 255;
|
|
90
|
+
const max = Math.max(r1, g1, b1);
|
|
91
|
+
const min = Math.min(r1, g1, b1);
|
|
92
|
+
const difference = max - min;
|
|
93
|
+
if (difference == 0) {
|
|
94
|
+
h = 0;
|
|
95
|
+
} else {
|
|
96
|
+
switch (max) {
|
|
97
|
+
case r1:
|
|
98
|
+
h = (60 * ((g1 - b1) / difference) + 360) % 360;
|
|
99
|
+
break;
|
|
100
|
+
case g1:
|
|
101
|
+
h = (60 * ((b1 - r1) / difference) + 120) % 360;
|
|
102
|
+
break;
|
|
103
|
+
case b1:
|
|
104
|
+
h = (60 * ((r1 - g1) / difference) + 240) % 360;
|
|
105
|
+
break;
|
|
106
|
+
default:
|
|
107
|
+
throw new Error("h did not match r1 | g1 | b1");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
s = max == 0 ? 0 : difference / max;
|
|
111
|
+
v = max;
|
|
112
|
+
return { h, s, v };
|
|
113
|
+
}
|
|
114
|
+
function HSVToRGB(h, s, v) {
|
|
115
|
+
let r, g, b;
|
|
116
|
+
const chroma = s * v;
|
|
117
|
+
const x = chroma * (1 - Math.abs(h / 60 % 2 - 1));
|
|
118
|
+
const m = v - chroma;
|
|
119
|
+
let r1, g1, b1;
|
|
120
|
+
if (h < 60) {
|
|
121
|
+
r1 = chroma;
|
|
122
|
+
g1 = x;
|
|
123
|
+
b1 = 0;
|
|
124
|
+
} else if (60 <= h && h < 120) {
|
|
125
|
+
r1 = x;
|
|
126
|
+
g1 = chroma;
|
|
127
|
+
b1 = 0;
|
|
128
|
+
} else if (h < 180) {
|
|
129
|
+
r1 = 0;
|
|
130
|
+
g1 = chroma;
|
|
131
|
+
b1 = x;
|
|
132
|
+
} else if (h < 240) {
|
|
133
|
+
r1 = 0;
|
|
134
|
+
g1 = x;
|
|
135
|
+
b1 = chroma;
|
|
136
|
+
} else if (h < 300) {
|
|
137
|
+
r1 = x;
|
|
138
|
+
g1 = 0;
|
|
139
|
+
b1 = chroma;
|
|
140
|
+
} else {
|
|
141
|
+
r1 = chroma;
|
|
142
|
+
g1 = 0;
|
|
143
|
+
b1 = x;
|
|
144
|
+
}
|
|
145
|
+
r = Math.round((r1 + m) * 255);
|
|
146
|
+
g = Math.round((g1 + m) * 255);
|
|
147
|
+
b = Math.round((b1 + m) * 255);
|
|
148
|
+
return { r, g, b };
|
|
149
|
+
}
|
|
150
|
+
function clamp(value, min, max) {
|
|
151
|
+
if (value < min) {
|
|
152
|
+
return min;
|
|
153
|
+
}
|
|
154
|
+
if (value > max) {
|
|
155
|
+
return max;
|
|
156
|
+
}
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
</script>
|
|
160
|
+
|
|
161
|
+
<script>import { createEventDispatcher, tick } from "svelte";
|
|
162
|
+
import { fade, fly } from "svelte/transition";
|
|
163
|
+
export let value = "rgba(255, 0, 0, 1)";
|
|
164
|
+
const dispatch = createEventDispatcher();
|
|
165
|
+
let c = colour(value);
|
|
166
|
+
$:
|
|
167
|
+
c && onColourChanged();
|
|
168
|
+
function onColourChanged() {
|
|
169
|
+
value = c.rgba;
|
|
170
|
+
dispatch("change", { rgb: { r: c.r, b: c.b, g: c.g }, hsv: { h: c.h, s: c.s, v: c.v }, hex: c.hex, a: c.a });
|
|
171
|
+
}
|
|
172
|
+
$:
|
|
173
|
+
value && onValueChanged();
|
|
174
|
+
function onValueChanged() {
|
|
175
|
+
if (value != c.rgba) {
|
|
176
|
+
c = colour(value);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
let open = false;
|
|
180
|
+
let dragging = false;
|
|
181
|
+
async function openPicker() {
|
|
182
|
+
open = true;
|
|
183
|
+
await tick();
|
|
184
|
+
drawColourCanvas();
|
|
185
|
+
drawHueSlider();
|
|
186
|
+
drawOpacitySlider();
|
|
187
|
+
}
|
|
188
|
+
async function tryClosePicker() {
|
|
189
|
+
await tick();
|
|
190
|
+
if (!dragging) {
|
|
191
|
+
open = false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
let colourCanvas;
|
|
195
|
+
let hueSlider;
|
|
196
|
+
let opacitySlider;
|
|
197
|
+
function drawColourCanvas() {
|
|
198
|
+
const context = colourCanvas.getContext("2d");
|
|
199
|
+
context.clearRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
200
|
+
const horizontalGradient = context.createLinearGradient(0, 0, colourCanvas.width, 0);
|
|
201
|
+
horizontalGradient.addColorStop(0, "#ffffff");
|
|
202
|
+
horizontalGradient.addColorStop(1, colour({ h: c.h, s: 1, v: 1 }).rgba);
|
|
203
|
+
context.fillStyle = horizontalGradient;
|
|
204
|
+
context.fillRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
205
|
+
const verticalGradient = context.createLinearGradient(0, 0, 0, colourCanvas.height);
|
|
206
|
+
verticalGradient.addColorStop(0, "transparent");
|
|
207
|
+
verticalGradient.addColorStop(1, "#000000");
|
|
208
|
+
context.fillStyle = verticalGradient;
|
|
209
|
+
context.fillRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
210
|
+
}
|
|
211
|
+
function drawHueSlider() {
|
|
212
|
+
const context = hueSlider.getContext("2d");
|
|
213
|
+
context.clearRect(0, 0, hueSlider.width, hueSlider.height);
|
|
214
|
+
const gradient = context.createLinearGradient(0, 0, 0, hueSlider.height);
|
|
215
|
+
for (let i = 0, offset = 0, step = 1 / (hueColours.length - 1); i < hueColours.length; i++, offset += step) {
|
|
216
|
+
gradient.addColorStop(offset, hueColours[i]);
|
|
217
|
+
}
|
|
218
|
+
context.fillStyle = gradient;
|
|
219
|
+
context.fillRect(0, 0, hueSlider.width, hueSlider.height);
|
|
220
|
+
}
|
|
221
|
+
function drawOpacitySlider() {
|
|
222
|
+
const context = opacitySlider.getContext("2d");
|
|
223
|
+
context.clearRect(0, 0, opacitySlider.width, opacitySlider.height);
|
|
224
|
+
const gradient = context.createLinearGradient(0, 0, 0, opacitySlider.height);
|
|
225
|
+
gradient.addColorStop(0, "transparent");
|
|
226
|
+
gradient.addColorStop(1, colour({ h: c.h, s: c.s, v: c.v, a: 1 }).rgba);
|
|
227
|
+
context.fillStyle = gradient;
|
|
228
|
+
context.fillRect(0, 0, opacitySlider.width, opacitySlider.height);
|
|
229
|
+
}
|
|
230
|
+
const initListener = (fn) => {
|
|
231
|
+
const fn_preventDefault = (e) => {
|
|
232
|
+
e.preventDefault();
|
|
233
|
+
fn(e);
|
|
234
|
+
};
|
|
235
|
+
return function(e) {
|
|
236
|
+
if (e.button != 0) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
dragging = true;
|
|
240
|
+
fn(e);
|
|
241
|
+
addEventListener("mousemove", fn_preventDefault);
|
|
242
|
+
addEventListener("mouseup", () => {
|
|
243
|
+
removeEventListener("mousemove", fn_preventDefault);
|
|
244
|
+
dragging = false;
|
|
245
|
+
});
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
function colourCanvasMove(e) {
|
|
249
|
+
const rect = colourCanvas.getBoundingClientRect();
|
|
250
|
+
c = colour({ h: c.h, s: clamp(e.pageX - rect.left, 0, size) / size, v: (size - clamp(e.pageY - rect.top, 0, size)) / size, a: c.a });
|
|
251
|
+
drawOpacitySlider();
|
|
252
|
+
}
|
|
253
|
+
function hueSliderMove(e) {
|
|
254
|
+
c = colour({ h: clamp(e.pageY - hueSlider.getBoundingClientRect().top, 0, size) / size * 360, s: c.s, v: c.v, a: c.a });
|
|
255
|
+
drawColourCanvas();
|
|
256
|
+
drawOpacitySlider();
|
|
257
|
+
}
|
|
258
|
+
function opacitySliderMove(e) {
|
|
259
|
+
c = colour({ h: c.h, s: c.s, v: c.v, a: parseFloat((clamp(e.pageY - opacitySlider.getBoundingClientRect().top, 0, size) / size).toFixed(2)) });
|
|
260
|
+
}
|
|
261
|
+
function hexInputChange(e) {
|
|
262
|
+
let value2 = e.currentTarget.value;
|
|
263
|
+
if (value2.startsWith("#")) {
|
|
264
|
+
value2 = value2.substring(1);
|
|
265
|
+
}
|
|
266
|
+
if ([3, 6, 8].includes(value2.length)) {
|
|
267
|
+
c = colour(value2);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function redInputChange(e) {
|
|
271
|
+
const value2 = parseFloat(e.currentTarget.value);
|
|
272
|
+
if (value2 >= 0 && value2 <= 255) {
|
|
273
|
+
c = colour({ r: value2, g: c.g, b: c.b, a: c.a });
|
|
274
|
+
drawColourCanvas();
|
|
275
|
+
drawOpacitySlider();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function greenInputChange(e) {
|
|
279
|
+
const value2 = parseFloat(e.currentTarget.value);
|
|
280
|
+
if (value2 >= 0 && value2 <= 255) {
|
|
281
|
+
c = colour({ r: c.r, g: value2, b: c.b, a: c.a });
|
|
282
|
+
drawColourCanvas();
|
|
283
|
+
drawOpacitySlider();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function blueInputChange(e) {
|
|
287
|
+
const value2 = parseFloat(e.currentTarget.value);
|
|
288
|
+
if (value2 >= 0 && value2 <= 255) {
|
|
289
|
+
c = colour({ r: c.r, g: c.g, b: value2, a: c.a });
|
|
290
|
+
drawColourCanvas();
|
|
291
|
+
drawOpacitySlider();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function alphaInputChange(e) {
|
|
295
|
+
const value2 = parseFloat(e.currentTarget.value);
|
|
296
|
+
if (value2 >= 0 && value2 <= 1) {
|
|
297
|
+
c = colour({ h: c.h, s: c.s, v: c.v, a: value2 });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
</script>
|
|
301
|
+
|
|
302
|
+
<svelte:window on:mouseup={open && !dragging ? tryClosePicker : null} />
|
|
303
|
+
|
|
304
|
+
<div class="container">
|
|
305
|
+
<div class="colour-input-container transparent">
|
|
306
|
+
<div class="colour-input" style:--value={value} on:click|stopPropagation={openPicker} on:keypress|stopPropagation={openPicker} />
|
|
307
|
+
</div>
|
|
308
|
+
{#if open}
|
|
309
|
+
<div
|
|
310
|
+
class="colour-picker-container"
|
|
311
|
+
in:fly={{ y: 10, duration: 150 }}
|
|
312
|
+
out:fade={{ duration: 50 }}
|
|
313
|
+
on:click|stopPropagation
|
|
314
|
+
on:keypress|stopPropagation
|
|
315
|
+
on:mouseup={(e) => !dragging && e.stopPropagation()}
|
|
316
|
+
>
|
|
317
|
+
<div class="colour-picker-arrow">
|
|
318
|
+
<div class="colour-picker">
|
|
319
|
+
<div class="main">
|
|
320
|
+
<div class="canvas-container">
|
|
321
|
+
<canvas bind:this={colourCanvas} width={size} height={size} on:mousedown={initListener(colourCanvasMove)} />
|
|
322
|
+
<div
|
|
323
|
+
class="pointer pointer-both"
|
|
324
|
+
style:--pointer-bg={colour({ h: c.h, s: c.s, v: c.v, a: 1 }).rgba}
|
|
325
|
+
style:--x={`${c.s * size}px`}
|
|
326
|
+
style:--y={`${size - c.v * size}px`}
|
|
327
|
+
/>
|
|
328
|
+
</div>
|
|
329
|
+
<div class="canvas-container">
|
|
330
|
+
<canvas bind:this={hueSlider} width={18} height={size} on:mousedown={initListener(hueSliderMove)} />
|
|
331
|
+
<div class="pointer pointer-vertical" style:--pointer-bg={colour({ h: c.h, s: 1, v: 1 }).rgba} style:--y={`${(c.h / 360) * size}px`} />
|
|
332
|
+
</div>
|
|
333
|
+
<div class="canvas-container transparent">
|
|
334
|
+
<canvas bind:this={opacitySlider} width={18} height={size} on:mousedown={initListener(opacitySliderMove)} />
|
|
335
|
+
<div class="pointer pointer-vertical" style:--pointer-bg={c.rgba} style:--y={`${c.a * size}px`} />
|
|
336
|
+
</div>
|
|
337
|
+
</div>
|
|
338
|
+
<div class="inputs-container">
|
|
339
|
+
<input type="text" maxlength={7} id="hex" value={c.hex} on:input={hexInputChange} />
|
|
340
|
+
<input type="number" autocomplete="off" min={0} max={255} id="r" value={c.r} on:input={redInputChange} />
|
|
341
|
+
<input type="number" autocomplete="off" min={0} max={255} id="g" value={c.g} on:input={greenInputChange} />
|
|
342
|
+
<input type="number" autocomplete="off" min={0} max={255} id="b" value={c.b} on:input={blueInputChange} />
|
|
343
|
+
<input type="number" autocomplete="off" min={0} max={255} id="a" value={c.a} on:input={alphaInputChange} />
|
|
344
|
+
<label for="hex">HEX</label>
|
|
345
|
+
<label for="r">R</label>
|
|
346
|
+
<label for="g">G</label>
|
|
347
|
+
<label for="b">B</label>
|
|
348
|
+
<label for="a">A</label>
|
|
349
|
+
</div>
|
|
350
|
+
</div>
|
|
351
|
+
</div>
|
|
352
|
+
</div>
|
|
353
|
+
{/if}
|
|
354
|
+
</div>
|
|
355
|
+
|
|
356
|
+
<style>
|
|
357
|
+
:global([data-theme="dark"]) .container {
|
|
358
|
+
--colour-picker-fg: #fff;
|
|
359
|
+
--colour-picker-bg: #181818;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
* {
|
|
363
|
+
margin: 0;
|
|
364
|
+
padding: 0;
|
|
365
|
+
line-height: 1;
|
|
366
|
+
box-sizing: border-box;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
.container {
|
|
370
|
+
position: relative;
|
|
371
|
+
width: 2rem;
|
|
372
|
+
height: 2rem;
|
|
373
|
+
--colour-picker-fg: #000;
|
|
374
|
+
--colour-picker-bg: #fff;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
.colour-input-container {
|
|
378
|
+
border-radius: 50%;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
.colour-input {
|
|
382
|
+
width: 2rem;
|
|
383
|
+
height: 2rem;
|
|
384
|
+
background-color: var(--value);
|
|
385
|
+
border-radius: 50%;
|
|
386
|
+
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
|
|
387
|
+
cursor: pointer;
|
|
388
|
+
transition: 0ms;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
.colour-picker-container {
|
|
392
|
+
margin-top: 0.5rem;
|
|
393
|
+
position: absolute;
|
|
394
|
+
z-index: 1;
|
|
395
|
+
transform-origin: top left;
|
|
396
|
+
filter: drop-shadow(2px 2px 5px rgba(0, 0, 0, 0.2));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
.colour-picker-arrow {
|
|
400
|
+
padding-top: 0.5rem;
|
|
401
|
+
background-image: linear-gradient(180deg, var(--colour-picker-bg) 0 0.5rem, transparent 0.5rem 100%);
|
|
402
|
+
clip-path: polygon(0 0.5rem, 0.5rem 0.5rem, 1rem 0, 1.5rem 0.5rem, 100% 0.5rem, 100% 100%, 0 100%);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
.colour-picker {
|
|
406
|
+
padding: 0.5rem;
|
|
407
|
+
background: var(--colour-picker-bg);
|
|
408
|
+
border-radius: 3px;
|
|
409
|
+
display: flex;
|
|
410
|
+
flex-direction: column;
|
|
411
|
+
gap: 10px;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
.main {
|
|
415
|
+
display: flex;
|
|
416
|
+
justify-content: space-between;
|
|
417
|
+
gap: 10px;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
.canvas-container {
|
|
421
|
+
position: relative;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
canvas {
|
|
425
|
+
display: block;
|
|
426
|
+
border-radius: 3px;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
.pointer {
|
|
430
|
+
position: absolute;
|
|
431
|
+
width: 12px;
|
|
432
|
+
height: 12px;
|
|
433
|
+
background: var(--pointer-bg);
|
|
434
|
+
border: 2px solid #ffffff;
|
|
435
|
+
border-radius: 50%;
|
|
436
|
+
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2);
|
|
437
|
+
transition: 0ms !important;
|
|
438
|
+
pointer-events: none;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
.pointer-both {
|
|
442
|
+
left: var(--x);
|
|
443
|
+
top: var(--y);
|
|
444
|
+
transform: translate(-6px, -6px);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
.pointer-vertical {
|
|
448
|
+
left: 3px;
|
|
449
|
+
top: var(--y);
|
|
450
|
+
transform: translateY(-6px);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
.inputs-container {
|
|
454
|
+
display: grid;
|
|
455
|
+
grid-template-columns: 1fr repeat(4, auto);
|
|
456
|
+
gap: 0.5rem;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
input {
|
|
460
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
|
461
|
+
font-size: 0.9rem;
|
|
462
|
+
color: var(--colour-picker-fg);
|
|
463
|
+
padding: 4px;
|
|
464
|
+
background: none;
|
|
465
|
+
border: 1px solid rgba(0, 0, 0, 0.2);
|
|
466
|
+
border-radius: 3px;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
input[type="text"] {
|
|
470
|
+
width: 5rem;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
input:focus {
|
|
474
|
+
outline: 1px solid rgb(0, 150, 255);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
input[type="number"] {
|
|
478
|
+
width: 2.75rem;
|
|
479
|
+
-moz-appearance: textfield; /* Firefox */
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
input[type="number"]::-webkit-outer-spin-button,
|
|
483
|
+
input[type="number"]::-webkit-inner-spin-button {
|
|
484
|
+
-webkit-appearance: none;
|
|
485
|
+
margin: 0;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
label {
|
|
489
|
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
|
490
|
+
font-size: 0.8rem;
|
|
491
|
+
color: rgb(89, 89, 89);
|
|
492
|
+
line-height: 1;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
.transparent {
|
|
496
|
+
--bg: rgba(0, 0, 0, 0.1);
|
|
497
|
+
background-image: linear-gradient(45deg, var(--bg) 25%, transparent 25%, transparent 75%, var(--bg) 75%, var(--bg)),
|
|
498
|
+
linear-gradient(45deg, var(--bg) 25%, transparent 25%, transparent 75%, var(--bg) 75%, var(--bg));
|
|
499
|
+
background-position: 0px 0px, 4px 4px;
|
|
500
|
+
background-size: 8px 8px;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
:global([data-theme="dark"]) .transparent {
|
|
504
|
+
--bg: rgba(50, 50, 50, 0.1);
|
|
505
|
+
}
|
|
506
|
+
</style>
|
package/package.json
CHANGED
|
@@ -1,37 +1,56 @@
|
|
|
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
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "svelte-colourpicker",
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"description": "Svelte colour picker component",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"color",
|
|
7
|
+
"colour",
|
|
8
|
+
"picker",
|
|
9
|
+
"svelte"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/cooolbros/svelte-colourpicker#readme",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/cooolbros/svelte-colourpicker.git"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"dev": "vite dev",
|
|
19
|
+
"build": "vite build && npm run package",
|
|
20
|
+
"preview": "vite preview",
|
|
21
|
+
"package": "svelte-kit sync && svelte-package && publint",
|
|
22
|
+
"prepublishOnly": "npm run package",
|
|
23
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
24
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
25
|
+
"lint": "prettier --plugin-search-dir . --check .",
|
|
26
|
+
"format": "prettier --plugin-search-dir . --write ."
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"svelte": "./dist/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"svelte": "^3.55.1"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@sveltejs/adapter-auto": "^2.0.0",
|
|
42
|
+
"@sveltejs/kit": "^1.8.5",
|
|
43
|
+
"@sveltejs/package": "^2.0.2",
|
|
44
|
+
"prettier": "^2.8.4",
|
|
45
|
+
"prettier-plugin-svelte": "^2.9.0",
|
|
46
|
+
"publint": "^0.1.9",
|
|
47
|
+
"svelte": "^3.55.1",
|
|
48
|
+
"svelte-check": "^3.0.4",
|
|
49
|
+
"tslib": "^2.5.0",
|
|
50
|
+
"typescript": "^4.9.5",
|
|
51
|
+
"vite": "^4.1.4"
|
|
52
|
+
},
|
|
53
|
+
"svelte": "./dist/index.js",
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"type": "module"
|
|
56
|
+
}
|
package/ColourPicker.svelte
DELETED
|
@@ -1,529 +0,0 @@
|
|
|
1
|
-
<svelte:options immutable />
|
|
2
|
-
|
|
3
|
-
<script context="module">"use strict";
|
|
4
|
-
const hueColours = ["#ff0000", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff", "#ff0000"];
|
|
5
|
-
const size = 230;
|
|
6
|
-
const numberRegExp = "(\\d*\\.?\\d{1,3})";
|
|
7
|
-
const delimiterRegExp = "[, ]+";
|
|
8
|
-
const rgbRegExp = new RegExp(`rgb\\(\\s*${numberRegExp}${delimiterRegExp}${numberRegExp}${delimiterRegExp}${numberRegExp}\\s*\\)`, "i");
|
|
9
|
-
const rgbaRegExp = new RegExp(`rgba\\(\\s*${numberRegExp}${delimiterRegExp}${numberRegExp}${delimiterRegExp}${numberRegExp}${delimiterRegExp}${numberRegExp}\\s*\\)`, "i");
|
|
10
|
-
const hexRegExp = (n) => `([\\dA-F]{${n}})`;
|
|
11
|
-
const hex3RegExp = new RegExp(`^#?${hexRegExp(1).repeat(3)}$`, "i");
|
|
12
|
-
const hex6RegExp = new RegExp(`^#?${hexRegExp(2).repeat(3)}$`, "i");
|
|
13
|
-
const hex8RegExp = new RegExp(`^#?${hexRegExp(2).repeat(4)}$`, "i");
|
|
14
|
-
function colour(input) {
|
|
15
|
-
let r, g, b, h, s, v, a, rgba, hex;
|
|
16
|
-
if (typeof input == "string") {
|
|
17
|
-
let result;
|
|
18
|
-
if ((result = rgbRegExp.exec(input))) {
|
|
19
|
-
r = parseFloat(result[1]);
|
|
20
|
-
g = parseFloat(result[2]);
|
|
21
|
-
b = parseFloat(result[2]);
|
|
22
|
-
a = 1;
|
|
23
|
-
}
|
|
24
|
-
else if ((result = rgbaRegExp.exec(input))) {
|
|
25
|
-
r = parseFloat(result[1]);
|
|
26
|
-
g = parseFloat(result[2]);
|
|
27
|
-
b = parseFloat(result[3]);
|
|
28
|
-
a = parseFloat(result[4]);
|
|
29
|
-
}
|
|
30
|
-
else if ((result = hex3RegExp.exec(input))) {
|
|
31
|
-
r = parseInt(result[1].repeat(2), 16);
|
|
32
|
-
g = parseInt(result[2].repeat(2), 16);
|
|
33
|
-
b = parseInt(result[3].repeat(2), 16);
|
|
34
|
-
a = 1;
|
|
35
|
-
hex = `#${input}`;
|
|
36
|
-
}
|
|
37
|
-
else if ((result = hex6RegExp.exec(input))) {
|
|
38
|
-
r = parseInt(result[1], 16);
|
|
39
|
-
g = parseInt(result[2], 16);
|
|
40
|
-
b = parseInt(result[3], 16);
|
|
41
|
-
a = 1;
|
|
42
|
-
hex = `#${input}`;
|
|
43
|
-
}
|
|
44
|
-
else if ((result = hex8RegExp.exec(input))) {
|
|
45
|
-
r = parseInt(result[1], 16);
|
|
46
|
-
g = parseInt(result[2], 16);
|
|
47
|
-
b = parseInt(result[3], 16);
|
|
48
|
-
a = parseInt(result[4], 16);
|
|
49
|
-
hex = `#${input}`;
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
throw new Error(`Invalid input: "${input}"`);
|
|
53
|
-
}
|
|
54
|
-
const hsv = RGBToHSV(r, g, b);
|
|
55
|
-
h = hsv.h;
|
|
56
|
-
s = hsv.s;
|
|
57
|
-
v = hsv.v;
|
|
58
|
-
}
|
|
59
|
-
else if ("r" in input) {
|
|
60
|
-
r = input.r;
|
|
61
|
-
g = input.g;
|
|
62
|
-
b = input.b;
|
|
63
|
-
const hsv = RGBToHSV(r, g, b);
|
|
64
|
-
h = hsv.h;
|
|
65
|
-
s = hsv.s;
|
|
66
|
-
v = hsv.v;
|
|
67
|
-
a = "a" in input ? input.a : 1;
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
h = input.h;
|
|
71
|
-
s = input.s;
|
|
72
|
-
v = input.v;
|
|
73
|
-
const rgb = HSVToRGB(h, s, v);
|
|
74
|
-
r = rgb.r;
|
|
75
|
-
g = rgb.g;
|
|
76
|
-
b = rgb.b;
|
|
77
|
-
a = "a" in input ? input.a : 1;
|
|
78
|
-
}
|
|
79
|
-
rgba = `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
80
|
-
hex ??= `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}${a != 1 ? Math.round(a * 255).toString(16) : ""}`;
|
|
81
|
-
return {
|
|
82
|
-
r,
|
|
83
|
-
g,
|
|
84
|
-
b,
|
|
85
|
-
h,
|
|
86
|
-
s,
|
|
87
|
-
v,
|
|
88
|
-
a,
|
|
89
|
-
rgba,
|
|
90
|
-
hex
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* https://www.rapidtables.com/convert/color/rgb-to-hsv.html
|
|
95
|
-
* https://www.geeksforgeeks.org/program-change-rgb-color-model-hsv-color-model/
|
|
96
|
-
*/
|
|
97
|
-
function RGBToHSV(r, g, b) {
|
|
98
|
-
let h, s, v;
|
|
99
|
-
const r1 = r / 255;
|
|
100
|
-
const g1 = g / 255;
|
|
101
|
-
const b1 = b / 255;
|
|
102
|
-
const max = Math.max(r1, g1, b1);
|
|
103
|
-
const min = Math.min(r1, g1, b1);
|
|
104
|
-
const difference = max - min;
|
|
105
|
-
if (difference == 0) {
|
|
106
|
-
h = 0;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
switch (max) {
|
|
110
|
-
case r1:
|
|
111
|
-
h = (60 * ((g1 - b1) / difference) + 360) % 360;
|
|
112
|
-
break;
|
|
113
|
-
case g1:
|
|
114
|
-
h = (60 * ((b1 - r1) / difference) + 120) % 360;
|
|
115
|
-
break;
|
|
116
|
-
case b1:
|
|
117
|
-
h = (60 * ((r1 - g1) / difference) + 240) % 360;
|
|
118
|
-
break;
|
|
119
|
-
default:
|
|
120
|
-
throw new Error("h did not match r1 | g1 | b1");
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
s = max == 0 ? 0 : difference / max;
|
|
124
|
-
v = max;
|
|
125
|
-
return { h, s, v };
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* https://www.rapidtables.com/convert/color/hsv-to-rgb.html
|
|
129
|
-
*/
|
|
130
|
-
function HSVToRGB(h, s, v) {
|
|
131
|
-
let r, g, b;
|
|
132
|
-
const chroma = s * v;
|
|
133
|
-
const x = chroma * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
134
|
-
const m = v - chroma;
|
|
135
|
-
let r1, g1, b1;
|
|
136
|
-
if (h < 60) {
|
|
137
|
-
r1 = chroma;
|
|
138
|
-
g1 = x;
|
|
139
|
-
b1 = 0;
|
|
140
|
-
}
|
|
141
|
-
else if (60 <= h && h < 120) {
|
|
142
|
-
r1 = x;
|
|
143
|
-
g1 = chroma;
|
|
144
|
-
b1 = 0;
|
|
145
|
-
}
|
|
146
|
-
else if (h < 180) {
|
|
147
|
-
r1 = 0;
|
|
148
|
-
g1 = chroma;
|
|
149
|
-
b1 = x;
|
|
150
|
-
}
|
|
151
|
-
else if (h < 240) {
|
|
152
|
-
r1 = 0;
|
|
153
|
-
g1 = x;
|
|
154
|
-
b1 = chroma;
|
|
155
|
-
}
|
|
156
|
-
else if (h < 300) {
|
|
157
|
-
r1 = x;
|
|
158
|
-
g1 = 0;
|
|
159
|
-
b1 = chroma;
|
|
160
|
-
} /* if (h < 360) */
|
|
161
|
-
else {
|
|
162
|
-
r1 = chroma;
|
|
163
|
-
g1 = 0;
|
|
164
|
-
b1 = x;
|
|
165
|
-
}
|
|
166
|
-
r = Math.round((r1 + m) * 255);
|
|
167
|
-
g = Math.round((g1 + m) * 255);
|
|
168
|
-
b = Math.round((b1 + m) * 255);
|
|
169
|
-
return { r, g, b };
|
|
170
|
-
}
|
|
171
|
-
function clamp(value, min, max) {
|
|
172
|
-
if (value < min) {
|
|
173
|
-
return min;
|
|
174
|
-
}
|
|
175
|
-
if (value > max) {
|
|
176
|
-
return max;
|
|
177
|
-
}
|
|
178
|
-
return value;
|
|
179
|
-
}
|
|
180
|
-
</script>
|
|
181
|
-
|
|
182
|
-
<script>import { createEventDispatcher, tick } from "svelte";
|
|
183
|
-
import { fade, fly } from "svelte/transition";
|
|
184
|
-
export let value = "rgba(255, 0, 0, 1)";
|
|
185
|
-
const dispatch = createEventDispatcher();
|
|
186
|
-
let c = colour(value);
|
|
187
|
-
$: c && onColourChanged();
|
|
188
|
-
function onColourChanged() {
|
|
189
|
-
value = c.rgba;
|
|
190
|
-
dispatch("change", { rgb: { r: c.r, b: c.b, g: c.g }, hsv: { h: c.h, s: c.s, v: c.v }, hex: c.hex, a: c.a });
|
|
191
|
-
}
|
|
192
|
-
$: value && onValueChanged();
|
|
193
|
-
function onValueChanged() {
|
|
194
|
-
if (value != c.rgba) {
|
|
195
|
-
c = colour(value);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
let open = false;
|
|
199
|
-
let dragging = false;
|
|
200
|
-
async function openPicker() {
|
|
201
|
-
open = true;
|
|
202
|
-
await tick();
|
|
203
|
-
drawColourCanvas();
|
|
204
|
-
drawHueSlider();
|
|
205
|
-
drawOpacitySlider();
|
|
206
|
-
}
|
|
207
|
-
async function tryClosePicker() {
|
|
208
|
-
await tick();
|
|
209
|
-
if (!dragging) {
|
|
210
|
-
open = false;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
let colourCanvas;
|
|
214
|
-
let hueSlider;
|
|
215
|
-
let opacitySlider;
|
|
216
|
-
//#region Draw Canvas
|
|
217
|
-
function drawColourCanvas() {
|
|
218
|
-
const context = colourCanvas.getContext("2d");
|
|
219
|
-
context.clearRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
220
|
-
const horizontalGradient = context.createLinearGradient(0, 0, colourCanvas.width, 0);
|
|
221
|
-
horizontalGradient.addColorStop(0, "#ffffff");
|
|
222
|
-
horizontalGradient.addColorStop(1, colour({ h: c.h, s: 1, v: 1 }).rgba);
|
|
223
|
-
context.fillStyle = horizontalGradient;
|
|
224
|
-
context.fillRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
225
|
-
const verticalGradient = context.createLinearGradient(0, 0, 0, colourCanvas.height);
|
|
226
|
-
verticalGradient.addColorStop(0, "transparent");
|
|
227
|
-
verticalGradient.addColorStop(1, "#000000");
|
|
228
|
-
context.fillStyle = verticalGradient;
|
|
229
|
-
context.fillRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
230
|
-
}
|
|
231
|
-
function drawHueSlider() {
|
|
232
|
-
const context = hueSlider.getContext("2d");
|
|
233
|
-
context.clearRect(0, 0, hueSlider.width, hueSlider.height);
|
|
234
|
-
const gradient = context.createLinearGradient(0, 0, 0, hueSlider.height);
|
|
235
|
-
for (let i = 0, offset = 0, step = 1 / (hueColours.length - 1); i < hueColours.length; i++, offset += step) {
|
|
236
|
-
gradient.addColorStop(offset, hueColours[i]);
|
|
237
|
-
}
|
|
238
|
-
context.fillStyle = gradient;
|
|
239
|
-
context.fillRect(0, 0, hueSlider.width, hueSlider.height);
|
|
240
|
-
}
|
|
241
|
-
function drawOpacitySlider() {
|
|
242
|
-
const context = opacitySlider.getContext("2d");
|
|
243
|
-
context.clearRect(0, 0, opacitySlider.width, opacitySlider.height);
|
|
244
|
-
const gradient = context.createLinearGradient(0, 0, 0, opacitySlider.height);
|
|
245
|
-
gradient.addColorStop(0, "transparent");
|
|
246
|
-
gradient.addColorStop(1, colour({ h: c.h, s: c.s, v: c.v, a: 1 }).rgba);
|
|
247
|
-
context.fillStyle = gradient;
|
|
248
|
-
context.fillRect(0, 0, opacitySlider.width, opacitySlider.height);
|
|
249
|
-
}
|
|
250
|
-
//#endregion
|
|
251
|
-
//#region Events
|
|
252
|
-
const initListener = (fn) => {
|
|
253
|
-
const fn_preventDefault = (e) => {
|
|
254
|
-
e.preventDefault();
|
|
255
|
-
fn(e);
|
|
256
|
-
};
|
|
257
|
-
return function (e) {
|
|
258
|
-
if (e.button != 0) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
dragging = true;
|
|
262
|
-
fn(e);
|
|
263
|
-
addEventListener("mousemove", fn_preventDefault);
|
|
264
|
-
addEventListener("mouseup", () => {
|
|
265
|
-
removeEventListener("mousemove", fn_preventDefault);
|
|
266
|
-
dragging = false;
|
|
267
|
-
});
|
|
268
|
-
};
|
|
269
|
-
};
|
|
270
|
-
function colourCanvasMove(e) {
|
|
271
|
-
const rect = colourCanvas.getBoundingClientRect();
|
|
272
|
-
c = colour({ h: c.h, s: clamp(e.pageX - rect.left, 0, size) / size, v: (size - clamp(e.pageY - rect.top, 0, size)) / size, a: c.a });
|
|
273
|
-
drawOpacitySlider();
|
|
274
|
-
}
|
|
275
|
-
function hueSliderMove(e) {
|
|
276
|
-
c = colour({ h: (clamp(e.pageY - hueSlider.getBoundingClientRect().top, 0, size) / size) * 360, s: c.s, v: c.v, a: c.a });
|
|
277
|
-
drawColourCanvas();
|
|
278
|
-
drawOpacitySlider();
|
|
279
|
-
}
|
|
280
|
-
function opacitySliderMove(e) {
|
|
281
|
-
c = colour({ h: c.h, s: c.s, v: c.v, a: parseFloat((clamp(e.pageY - opacitySlider.getBoundingClientRect().top, 0, size) / size).toFixed(2)) });
|
|
282
|
-
}
|
|
283
|
-
function hexInputChange(e) {
|
|
284
|
-
let value = e.currentTarget.value;
|
|
285
|
-
if (value.startsWith("#")) {
|
|
286
|
-
value = value.substring(1);
|
|
287
|
-
}
|
|
288
|
-
if ([3, 6, 8].includes(value.length)) {
|
|
289
|
-
c = colour(value);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
function redInputChange(e) {
|
|
293
|
-
const value = parseFloat(e.currentTarget.value);
|
|
294
|
-
if (value >= 0 && value <= 255) {
|
|
295
|
-
c = colour({ r: value, g: c.g, b: c.b, a: c.a });
|
|
296
|
-
drawColourCanvas();
|
|
297
|
-
drawOpacitySlider();
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
function greenInputChange(e) {
|
|
301
|
-
const value = parseFloat(e.currentTarget.value);
|
|
302
|
-
if (value >= 0 && value <= 255) {
|
|
303
|
-
c = colour({ r: c.r, g: value, b: c.b, a: c.a });
|
|
304
|
-
drawColourCanvas();
|
|
305
|
-
drawOpacitySlider();
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
function blueInputChange(e) {
|
|
309
|
-
const value = parseFloat(e.currentTarget.value);
|
|
310
|
-
if (value >= 0 && value <= 255) {
|
|
311
|
-
c = colour({ r: c.r, g: c.g, b: value, a: c.a });
|
|
312
|
-
drawColourCanvas();
|
|
313
|
-
drawOpacitySlider();
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
function alphaInputChange(e) {
|
|
317
|
-
const value = parseFloat(e.currentTarget.value);
|
|
318
|
-
if (value >= 0 && value <= 1) {
|
|
319
|
-
c = colour({ h: c.h, s: c.s, v: c.v, a: value });
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
//#endregion
|
|
323
|
-
</script>
|
|
324
|
-
|
|
325
|
-
<svelte:window on:mouseup={open && !dragging ? tryClosePicker : null} />
|
|
326
|
-
|
|
327
|
-
<div class="container">
|
|
328
|
-
<div class="colour-input-container transparent">
|
|
329
|
-
<div class="colour-input" style:--value={value} on:click|stopPropagation={openPicker} on:keypress|stopPropagation={openPicker} />
|
|
330
|
-
</div>
|
|
331
|
-
{#if open}
|
|
332
|
-
<div
|
|
333
|
-
class="colour-picker-container"
|
|
334
|
-
in:fly={{ y: 10, duration: 150 }}
|
|
335
|
-
out:fade={{ duration: 50 }}
|
|
336
|
-
on:click|stopPropagation
|
|
337
|
-
on:keypress|stopPropagation
|
|
338
|
-
on:mouseup={(e) => !dragging && e.stopPropagation()}
|
|
339
|
-
>
|
|
340
|
-
<div class="colour-picker-arrow">
|
|
341
|
-
<div class="colour-picker">
|
|
342
|
-
<div class="main">
|
|
343
|
-
<div class="canvas-container">
|
|
344
|
-
<canvas bind:this={colourCanvas} width={size} height={size} on:mousedown={initListener(colourCanvasMove)} />
|
|
345
|
-
<div
|
|
346
|
-
class="pointer pointer-both"
|
|
347
|
-
style:--pointer-bg={colour({ h: c.h, s: c.s, v: c.v, a: 1 }).rgba}
|
|
348
|
-
style:--x={`${c.s * size}px`}
|
|
349
|
-
style:--y={`${size - c.v * size}px`}
|
|
350
|
-
/>
|
|
351
|
-
</div>
|
|
352
|
-
<div class="canvas-container">
|
|
353
|
-
<canvas bind:this={hueSlider} width={18} height={size} on:mousedown={initListener(hueSliderMove)} />
|
|
354
|
-
<div class="pointer pointer-vertical" style:--pointer-bg={colour({ h: c.h, s: 1, v: 1 }).rgba} style:--y={`${(c.h / 360) * size}px`} />
|
|
355
|
-
</div>
|
|
356
|
-
<div class="canvas-container transparent">
|
|
357
|
-
<canvas bind:this={opacitySlider} width={18} height={size} on:mousedown={initListener(opacitySliderMove)} />
|
|
358
|
-
<div class="pointer pointer-vertical" style:--pointer-bg={c.rgba} style:--y={`${c.a * size}px`} />
|
|
359
|
-
</div>
|
|
360
|
-
</div>
|
|
361
|
-
<div class="inputs-container">
|
|
362
|
-
<input type="text" maxlength={7} id="hex" value={c.hex} on:input={hexInputChange} />
|
|
363
|
-
<input type="number" autocomplete="off" min={0} max={255} id="r" value={c.r} on:input={redInputChange} />
|
|
364
|
-
<input type="number" autocomplete="off" min={0} max={255} id="g" value={c.g} on:input={greenInputChange} />
|
|
365
|
-
<input type="number" autocomplete="off" min={0} max={255} id="b" value={c.b} on:input={blueInputChange} />
|
|
366
|
-
<input type="number" autocomplete="off" min={0} max={255} id="a" value={c.a} on:input={alphaInputChange} />
|
|
367
|
-
<label for="hex">HEX</label>
|
|
368
|
-
<label for="r">R</label>
|
|
369
|
-
<label for="g">G</label>
|
|
370
|
-
<label for="b">B</label>
|
|
371
|
-
<label for="a">A</label>
|
|
372
|
-
</div>
|
|
373
|
-
</div>
|
|
374
|
-
</div>
|
|
375
|
-
</div>
|
|
376
|
-
{/if}
|
|
377
|
-
</div>
|
|
378
|
-
|
|
379
|
-
<style>
|
|
380
|
-
:global([data-theme="dark"]) .container {
|
|
381
|
-
--colour-picker-fg: #fff;
|
|
382
|
-
--colour-picker-bg: #181818;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
* {
|
|
386
|
-
margin: 0;
|
|
387
|
-
padding: 0;
|
|
388
|
-
line-height: 1;
|
|
389
|
-
box-sizing: border-box;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
.container {
|
|
393
|
-
position: relative;
|
|
394
|
-
width: 2rem;
|
|
395
|
-
height: 2rem;
|
|
396
|
-
--colour-picker-fg: #000;
|
|
397
|
-
--colour-picker-bg: #fff;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
.colour-input-container {
|
|
401
|
-
border-radius: 50%;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
.colour-input {
|
|
405
|
-
width: 2rem;
|
|
406
|
-
height: 2rem;
|
|
407
|
-
background-color: var(--value);
|
|
408
|
-
border-radius: 50%;
|
|
409
|
-
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
|
|
410
|
-
cursor: pointer;
|
|
411
|
-
transition: 0ms;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
.colour-picker-container {
|
|
415
|
-
margin-top: 0.5rem;
|
|
416
|
-
position: absolute;
|
|
417
|
-
z-index: 1;
|
|
418
|
-
transform-origin: top left;
|
|
419
|
-
filter: drop-shadow(2px 2px 5px rgba(0, 0, 0, 0.2));
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
.colour-picker-arrow {
|
|
423
|
-
padding-top: 0.5rem;
|
|
424
|
-
background-image: linear-gradient(180deg, var(--colour-picker-bg) 0 0.5rem, transparent 0.5rem 100%);
|
|
425
|
-
clip-path: polygon(0 0.5rem, 0.5rem 0.5rem, 1rem 0, 1.5rem 0.5rem, 100% 0.5rem, 100% 100%, 0 100%);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
.colour-picker {
|
|
429
|
-
padding: 0.5rem;
|
|
430
|
-
background: var(--colour-picker-bg);
|
|
431
|
-
border-radius: 3px;
|
|
432
|
-
display: flex;
|
|
433
|
-
flex-direction: column;
|
|
434
|
-
gap: 10px;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
.main {
|
|
438
|
-
display: flex;
|
|
439
|
-
justify-content: space-between;
|
|
440
|
-
gap: 10px;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
.canvas-container {
|
|
444
|
-
position: relative;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
canvas {
|
|
448
|
-
display: block;
|
|
449
|
-
border-radius: 3px;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
.pointer {
|
|
453
|
-
position: absolute;
|
|
454
|
-
width: 12px;
|
|
455
|
-
height: 12px;
|
|
456
|
-
background: var(--pointer-bg);
|
|
457
|
-
border: 2px solid #ffffff;
|
|
458
|
-
border-radius: 50%;
|
|
459
|
-
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2);
|
|
460
|
-
transition: 0ms !important;
|
|
461
|
-
pointer-events: none;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
.pointer-both {
|
|
465
|
-
left: var(--x);
|
|
466
|
-
top: var(--y);
|
|
467
|
-
transform: translate(-6px, -6px);
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
.pointer-vertical {
|
|
471
|
-
left: 3px;
|
|
472
|
-
top: var(--y);
|
|
473
|
-
transform: translateY(-6px);
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
.inputs-container {
|
|
477
|
-
display: grid;
|
|
478
|
-
grid-template-columns: 1fr repeat(4, auto);
|
|
479
|
-
gap: 0.5rem;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
input {
|
|
483
|
-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
|
484
|
-
font-size: 0.9rem;
|
|
485
|
-
color: var(--colour-picker-fg);
|
|
486
|
-
padding: 4px;
|
|
487
|
-
background: none;
|
|
488
|
-
border: 1px solid rgba(0, 0, 0, 0.2);
|
|
489
|
-
border-radius: 3px;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
input[type="text"] {
|
|
493
|
-
width: 5rem;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
input:focus {
|
|
497
|
-
outline: 1px solid rgb(0, 150, 255);
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
input[type="number"] {
|
|
501
|
-
width: 2.75rem;
|
|
502
|
-
-moz-appearance: textfield; /* Firefox */
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
input[type="number"]::-webkit-outer-spin-button,
|
|
506
|
-
input[type="number"]::-webkit-inner-spin-button {
|
|
507
|
-
-webkit-appearance: none;
|
|
508
|
-
margin: 0;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
label {
|
|
512
|
-
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
|
513
|
-
font-size: 0.8rem;
|
|
514
|
-
color: rgb(89, 89, 89);
|
|
515
|
-
line-height: 1;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
.transparent {
|
|
519
|
-
--bg: rgba(0, 0, 0, 0.1);
|
|
520
|
-
background-image: linear-gradient(45deg, var(--bg) 25%, transparent 25%, transparent 75%, var(--bg) 75%, var(--bg)),
|
|
521
|
-
linear-gradient(45deg, var(--bg) 25%, transparent 25%, transparent 75%, var(--bg) 75%, var(--bg));
|
|
522
|
-
background-position: 0px 0px, 4px 4px;
|
|
523
|
-
background-size: 8px 8px;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
:global([data-theme="dark"]) .transparent {
|
|
527
|
-
--bg: rgba(50, 50, 50, 0.1);
|
|
528
|
-
}
|
|
529
|
-
</style>
|
|
File without changes
|
|
File without changes
|
|
File without changes
|