svelte-colourpicker 0.0.1
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.
- package/ColourPicker.svelte +525 -0
- package/ColourPicker.svelte.d.ts +36 -0
- package/LICENSE +21 -0
- package/README.md +16 -0
- package/index.d.ts +2 -0
- package/index.js +3 -0
- package/package.json +24 -0
|
@@ -0,0 +1,525 @@
|
|
|
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) % 6);
|
|
112
|
+
break;
|
|
113
|
+
case g1:
|
|
114
|
+
h = 60 * ((b1 - r1) / difference + 2);
|
|
115
|
+
break;
|
|
116
|
+
case b1:
|
|
117
|
+
h = 60 * ((r1 - g1) / difference + 4);
|
|
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
|
+
$: value = c.rgba;
|
|
188
|
+
$: value && onValueChanged();
|
|
189
|
+
function onValueChanged() {
|
|
190
|
+
if (value != c.rgba) {
|
|
191
|
+
c = colour(value);
|
|
192
|
+
dispatch("change", { rgb: { r: c.r, b: c.b, g: c.g }, hsv: { h: c.h, s: c.s, v: c.v }, a: c.a });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
let open = false;
|
|
196
|
+
let dragging = false;
|
|
197
|
+
async function openPicker() {
|
|
198
|
+
open = true;
|
|
199
|
+
await tick();
|
|
200
|
+
drawColourCanvas();
|
|
201
|
+
drawHueSlider();
|
|
202
|
+
drawOpacitySlider();
|
|
203
|
+
}
|
|
204
|
+
async function tryClosePicker() {
|
|
205
|
+
await tick();
|
|
206
|
+
if (!dragging) {
|
|
207
|
+
open = false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
let colourCanvas;
|
|
211
|
+
let hueSlider;
|
|
212
|
+
let opacitySlider;
|
|
213
|
+
//#region Draw Canvas
|
|
214
|
+
function drawColourCanvas() {
|
|
215
|
+
const context = colourCanvas.getContext("2d");
|
|
216
|
+
context.clearRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
217
|
+
const horizontalGradient = context.createLinearGradient(0, 0, colourCanvas.width, 0);
|
|
218
|
+
horizontalGradient.addColorStop(0, "#ffffff");
|
|
219
|
+
horizontalGradient.addColorStop(1, colour({ h: c.h, s: 1, v: 1 }).rgba);
|
|
220
|
+
context.fillStyle = horizontalGradient;
|
|
221
|
+
context.fillRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
222
|
+
const verticalGradient = context.createLinearGradient(0, 0, 0, colourCanvas.height);
|
|
223
|
+
verticalGradient.addColorStop(0, "transparent");
|
|
224
|
+
verticalGradient.addColorStop(1, "#000000");
|
|
225
|
+
context.fillStyle = verticalGradient;
|
|
226
|
+
context.fillRect(0, 0, colourCanvas.width, colourCanvas.height);
|
|
227
|
+
}
|
|
228
|
+
function drawHueSlider() {
|
|
229
|
+
const context = hueSlider.getContext("2d");
|
|
230
|
+
context.clearRect(0, 0, hueSlider.width, hueSlider.height);
|
|
231
|
+
const gradient = context.createLinearGradient(0, 0, 0, hueSlider.height);
|
|
232
|
+
for (let i = 0, offset = 0, step = 1 / (hueColours.length - 1); i < hueColours.length; i++, offset += step) {
|
|
233
|
+
gradient.addColorStop(offset, hueColours[i]);
|
|
234
|
+
}
|
|
235
|
+
context.fillStyle = gradient;
|
|
236
|
+
context.fillRect(0, 0, hueSlider.width, hueSlider.height);
|
|
237
|
+
}
|
|
238
|
+
function drawOpacitySlider() {
|
|
239
|
+
const context = opacitySlider.getContext("2d");
|
|
240
|
+
context.clearRect(0, 0, opacitySlider.width, opacitySlider.height);
|
|
241
|
+
const gradient = context.createLinearGradient(0, 0, 0, opacitySlider.height);
|
|
242
|
+
gradient.addColorStop(0, "transparent");
|
|
243
|
+
gradient.addColorStop(1, colour({ h: c.h, s: c.s, v: c.v, a: 1 }).rgba);
|
|
244
|
+
context.fillStyle = gradient;
|
|
245
|
+
context.fillRect(0, 0, opacitySlider.width, opacitySlider.height);
|
|
246
|
+
}
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region Events
|
|
249
|
+
const initListener = (fn) => {
|
|
250
|
+
const fn_preventDefault = (e) => {
|
|
251
|
+
e.preventDefault();
|
|
252
|
+
fn(e);
|
|
253
|
+
};
|
|
254
|
+
return function (e) {
|
|
255
|
+
if (e.button != 0) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
dragging = true;
|
|
259
|
+
fn(e);
|
|
260
|
+
addEventListener("mousemove", fn_preventDefault);
|
|
261
|
+
addEventListener("mouseup", () => {
|
|
262
|
+
removeEventListener("mousemove", fn_preventDefault);
|
|
263
|
+
dragging = false;
|
|
264
|
+
});
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
function colourCanvasMove(e) {
|
|
268
|
+
const rect = colourCanvas.getBoundingClientRect();
|
|
269
|
+
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 });
|
|
270
|
+
drawOpacitySlider();
|
|
271
|
+
}
|
|
272
|
+
function hueSliderMove(e) {
|
|
273
|
+
c = colour({ h: (clamp(e.pageY - hueSlider.getBoundingClientRect().top, 0, size) / size) * 360, s: c.s, v: c.v, a: c.a });
|
|
274
|
+
drawColourCanvas();
|
|
275
|
+
drawOpacitySlider();
|
|
276
|
+
}
|
|
277
|
+
function opacitySliderMove(e) {
|
|
278
|
+
c = colour({ h: c.h, s: c.s, v: c.v, a: parseFloat((clamp(e.pageY - opacitySlider.getBoundingClientRect().top, 0, size) / size).toFixed(2)) });
|
|
279
|
+
}
|
|
280
|
+
function hexInputChange(e) {
|
|
281
|
+
let value = e.currentTarget.value;
|
|
282
|
+
if (value.startsWith("#")) {
|
|
283
|
+
value = value.substring(1);
|
|
284
|
+
}
|
|
285
|
+
if ([3, 6, 8].includes(value.length)) {
|
|
286
|
+
c = colour(value);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function redInputChange(e) {
|
|
290
|
+
const value = parseFloat(e.currentTarget.value);
|
|
291
|
+
if (value >= 0 && value <= 255) {
|
|
292
|
+
c = colour({ r: value, g: c.g, b: c.b, a: c.a });
|
|
293
|
+
drawColourCanvas();
|
|
294
|
+
drawOpacitySlider();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function greenInputChange(e) {
|
|
298
|
+
const value = parseFloat(e.currentTarget.value);
|
|
299
|
+
if (value >= 0 && value <= 255) {
|
|
300
|
+
c = colour({ r: c.r, g: value, b: c.b, a: c.a });
|
|
301
|
+
drawColourCanvas();
|
|
302
|
+
drawOpacitySlider();
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function blueInputChange(e) {
|
|
306
|
+
const value = parseFloat(e.currentTarget.value);
|
|
307
|
+
if (value >= 0 && value <= 255) {
|
|
308
|
+
c = colour({ r: c.r, g: c.g, b: value, a: c.a });
|
|
309
|
+
drawColourCanvas();
|
|
310
|
+
drawOpacitySlider();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function alphaInputChange(e) {
|
|
314
|
+
const value = parseFloat(e.currentTarget.value);
|
|
315
|
+
if (value >= 0 && value <= 1) {
|
|
316
|
+
c = colour({ h: c.h, s: c.s, v: c.v, a: value });
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
//#endregion
|
|
320
|
+
</script>
|
|
321
|
+
|
|
322
|
+
<svelte:window on:mouseup={open && !dragging ? tryClosePicker : null} />
|
|
323
|
+
|
|
324
|
+
<div class="container">
|
|
325
|
+
<div class="colour-input-container transparent">
|
|
326
|
+
<div class="colour-input" style:--value={value} on:click|stopPropagation={openPicker} on:keypress|stopPropagation={openPicker} />
|
|
327
|
+
</div>
|
|
328
|
+
{#if open}
|
|
329
|
+
<div
|
|
330
|
+
class="colour-picker-container"
|
|
331
|
+
in:fly={{ y: 10, duration: 150 }}
|
|
332
|
+
out:fade={{ duration: 50 }}
|
|
333
|
+
on:click|stopPropagation
|
|
334
|
+
on:keypress|stopPropagation
|
|
335
|
+
on:mouseup={(e) => !dragging && e.stopPropagation()}
|
|
336
|
+
>
|
|
337
|
+
<div class="colour-picker-arrow">
|
|
338
|
+
<div class="colour-picker">
|
|
339
|
+
<div class="main">
|
|
340
|
+
<div class="canvas-container">
|
|
341
|
+
<canvas bind:this={colourCanvas} width={size} height={size} on:mousedown={initListener(colourCanvasMove)} />
|
|
342
|
+
<div
|
|
343
|
+
class="pointer pointer-both"
|
|
344
|
+
style:--pointer-bg={colour({ h: c.h, s: c.s, v: c.v, a: 1 }).rgba}
|
|
345
|
+
style:--x={`${c.s * size}px`}
|
|
346
|
+
style:--y={`${size - c.v * size}px`}
|
|
347
|
+
/>
|
|
348
|
+
</div>
|
|
349
|
+
<div class="canvas-container">
|
|
350
|
+
<canvas bind:this={hueSlider} width={18} height={size} on:mousedown={initListener(hueSliderMove)} />
|
|
351
|
+
<div class="pointer pointer-vertical" style:--pointer-bg={colour({ h: c.h, s: 1, v: 1 }).rgba} style:--y={`${(c.h / 360) * size}px`} />
|
|
352
|
+
</div>
|
|
353
|
+
<div class="canvas-container transparent">
|
|
354
|
+
<canvas bind:this={opacitySlider} width={18} height={size} on:mousedown={initListener(opacitySliderMove)} />
|
|
355
|
+
<div class="pointer pointer-vertical" style:--pointer-bg={c.rgba} style:--y={`${c.a * size}px`} />
|
|
356
|
+
</div>
|
|
357
|
+
</div>
|
|
358
|
+
<div class="inputs-container">
|
|
359
|
+
<input type="text" maxlength={7} id="hex" value={c.hex} on:input={hexInputChange} />
|
|
360
|
+
<input type="number" autocomplete="off" min={0} max={255} id="r" value={c.r} on:input={redInputChange} />
|
|
361
|
+
<input type="number" autocomplete="off" min={0} max={255} id="g" value={c.g} on:input={greenInputChange} />
|
|
362
|
+
<input type="number" autocomplete="off" min={0} max={255} id="b" value={c.b} on:input={blueInputChange} />
|
|
363
|
+
<input type="number" autocomplete="off" min={0} max={255} id="a" value={c.a} on:input={alphaInputChange} />
|
|
364
|
+
<label for="hex">HEX</label>
|
|
365
|
+
<label for="r">R</label>
|
|
366
|
+
<label for="g">G</label>
|
|
367
|
+
<label for="b">B</label>
|
|
368
|
+
<label for="a">A</label>
|
|
369
|
+
</div>
|
|
370
|
+
</div>
|
|
371
|
+
</div>
|
|
372
|
+
</div>
|
|
373
|
+
{/if}
|
|
374
|
+
</div>
|
|
375
|
+
|
|
376
|
+
<style>
|
|
377
|
+
:global([data-theme="dark"]) .container {
|
|
378
|
+
--colour-picker-fg: #fff;
|
|
379
|
+
--colour-picker-bg: #181818;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
* {
|
|
383
|
+
margin: 0;
|
|
384
|
+
padding: 0;
|
|
385
|
+
line-height: 1;
|
|
386
|
+
box-sizing: border-box;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
.container {
|
|
390
|
+
position: relative;
|
|
391
|
+
width: 2rem;
|
|
392
|
+
height: 2rem;
|
|
393
|
+
--colour-picker-fg: #000;
|
|
394
|
+
--colour-picker-bg: #fff;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
.colour-input-container {
|
|
398
|
+
border-radius: 50%;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
.colour-input {
|
|
402
|
+
width: 2rem;
|
|
403
|
+
height: 2rem;
|
|
404
|
+
background-color: var(--value);
|
|
405
|
+
border-radius: 50%;
|
|
406
|
+
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
|
|
407
|
+
cursor: pointer;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
.colour-picker-container {
|
|
411
|
+
margin-top: 0.5rem;
|
|
412
|
+
position: absolute;
|
|
413
|
+
z-index: 1;
|
|
414
|
+
transform-origin: top left;
|
|
415
|
+
filter: drop-shadow(2px 2px 5px rgba(0, 0, 0, 0.2));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
.colour-picker-arrow {
|
|
419
|
+
padding-top: 0.5rem;
|
|
420
|
+
background-image: linear-gradient(180deg, var(--colour-picker-bg) 0 0.5rem, transparent 0.5rem 100%);
|
|
421
|
+
clip-path: polygon(0 0.5rem, 0.5rem 0.5rem, 1rem 0, 1.5rem 0.5rem, 100% 0.5rem, 100% 100%, 0 100%);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
.colour-picker {
|
|
425
|
+
padding: 0.5rem;
|
|
426
|
+
background: var(--colour-picker-bg);
|
|
427
|
+
border-radius: 3px;
|
|
428
|
+
display: flex;
|
|
429
|
+
flex-direction: column;
|
|
430
|
+
gap: 10px;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
.main {
|
|
434
|
+
display: flex;
|
|
435
|
+
justify-content: space-between;
|
|
436
|
+
gap: 10px;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
.canvas-container {
|
|
440
|
+
position: relative;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
canvas {
|
|
444
|
+
display: block;
|
|
445
|
+
border-radius: 3px;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
.pointer {
|
|
449
|
+
position: absolute;
|
|
450
|
+
width: 12px;
|
|
451
|
+
height: 12px;
|
|
452
|
+
background: var(--pointer-bg);
|
|
453
|
+
border: 2px solid #ffffff;
|
|
454
|
+
border-radius: 50%;
|
|
455
|
+
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2);
|
|
456
|
+
transition: 0ms !important;
|
|
457
|
+
pointer-events: none;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
.pointer-both {
|
|
461
|
+
left: var(--x);
|
|
462
|
+
top: var(--y);
|
|
463
|
+
transform: translate(-6px, -6px);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
.pointer-vertical {
|
|
467
|
+
left: 3px;
|
|
468
|
+
top: var(--y);
|
|
469
|
+
transform: translateY(-6px);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
.inputs-container {
|
|
473
|
+
display: grid;
|
|
474
|
+
grid-template-columns: 1fr repeat(4, auto);
|
|
475
|
+
gap: 0.5rem;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
input {
|
|
479
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
|
480
|
+
font-size: 0.9rem;
|
|
481
|
+
color: var(--colour-picker-fg);
|
|
482
|
+
padding: 4px;
|
|
483
|
+
background: none;
|
|
484
|
+
border: 1px solid rgba(0, 0, 0, 0.2);
|
|
485
|
+
border-radius: 3px;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
input[type="text"] {
|
|
489
|
+
width: 5rem;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
input:focus {
|
|
493
|
+
outline: 1px solid rgb(0, 150, 255);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
input[type="number"] {
|
|
497
|
+
width: 2.75rem;
|
|
498
|
+
-moz-appearance: textfield; /* Firefox */
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
input[type="number"]::-webkit-outer-spin-button,
|
|
502
|
+
input[type="number"]::-webkit-inner-spin-button {
|
|
503
|
+
-webkit-appearance: none;
|
|
504
|
+
margin: 0;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
label {
|
|
508
|
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
|
|
509
|
+
font-size: 0.8rem;
|
|
510
|
+
color: rgb(89, 89, 89);
|
|
511
|
+
line-height: 1;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
.transparent {
|
|
515
|
+
--bg: rgba(0, 0, 0, 0.1);
|
|
516
|
+
background-image: linear-gradient(45deg, var(--bg) 25%, transparent 25%, transparent 75%, var(--bg) 75%, var(--bg)),
|
|
517
|
+
linear-gradient(45deg, var(--bg) 25%, transparent 25%, transparent 75%, var(--bg) 75%, var(--bg));
|
|
518
|
+
background-position: 0px 0px, 4px 4px;
|
|
519
|
+
background-size: 8px 8px;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
:global([data-theme="dark"]) .transparent {
|
|
523
|
+
--bg: rgba(50, 50, 50, 0.1);
|
|
524
|
+
}
|
|
525
|
+
</style>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
type RGB = {
|
|
3
|
+
r: number;
|
|
4
|
+
g: number;
|
|
5
|
+
b: number;
|
|
6
|
+
};
|
|
7
|
+
type HSV = {
|
|
8
|
+
h: number;
|
|
9
|
+
s: number;
|
|
10
|
+
v: number;
|
|
11
|
+
};
|
|
12
|
+
type A = {
|
|
13
|
+
a: number;
|
|
14
|
+
};
|
|
15
|
+
declare const __propDef: {
|
|
16
|
+
props: {
|
|
17
|
+
value?: string | undefined;
|
|
18
|
+
};
|
|
19
|
+
events: {
|
|
20
|
+
click: MouseEvent;
|
|
21
|
+
keypress: KeyboardEvent;
|
|
22
|
+
change: CustomEvent<{
|
|
23
|
+
rgb: RGB;
|
|
24
|
+
hsv: HSV;
|
|
25
|
+
} & A>;
|
|
26
|
+
} & {
|
|
27
|
+
[evt: string]: CustomEvent<any>;
|
|
28
|
+
};
|
|
29
|
+
slots: {};
|
|
30
|
+
};
|
|
31
|
+
export type ColourPickerProps = typeof __propDef.props;
|
|
32
|
+
export type ColourPickerEvents = typeof __propDef.events;
|
|
33
|
+
export type ColourPickerSlots = typeof __propDef.slots;
|
|
34
|
+
export default class ColourPicker extends SvelteComponentTyped<ColourPickerProps, ColourPickerEvents, ColourPickerSlots> {
|
|
35
|
+
}
|
|
36
|
+
export {};
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Peter Wobcke
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Svelte Colour Picker
|
|
2
|
+
|
|
3
|
+
```html
|
|
4
|
+
<script>
|
|
5
|
+
let value = "rgba(255, 0, 0, 1)"
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
<div>{value}</div>
|
|
9
|
+
|
|
10
|
+
<ColourPicker bind:value />
|
|
11
|
+
|
|
12
|
+
<!-- Dark Theme -->
|
|
13
|
+
<div data-theme="dark">
|
|
14
|
+
<ColourPicker bind:value />
|
|
15
|
+
</div>
|
|
16
|
+
```
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "svelte-colourpicker",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"devDependencies": {
|
|
5
|
+
"@sveltejs/adapter-auto": "next",
|
|
6
|
+
"@sveltejs/kit": "next",
|
|
7
|
+
"@sveltejs/package": "next",
|
|
8
|
+
"prettier": "^2.6.2",
|
|
9
|
+
"prettier-plugin-svelte": "^2.7.0",
|
|
10
|
+
"svelte": "^3.44.0",
|
|
11
|
+
"svelte-check": "^2.7.1",
|
|
12
|
+
"svelte-preprocess": "^4.10.6",
|
|
13
|
+
"tslib": "^2.3.1",
|
|
14
|
+
"typescript": "^4.7.4",
|
|
15
|
+
"vite": "^3.1.0"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"exports": {
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
"./ColourPicker.svelte": "./ColourPicker.svelte",
|
|
21
|
+
".": "./index.js"
|
|
22
|
+
},
|
|
23
|
+
"svelte": "./index.js"
|
|
24
|
+
}
|