-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug-color-picker.ts
112 lines (110 loc) · 2.85 KB
/
aug-color-picker.ts
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
import { languageFor } from "./core/languages.js";
import { h } from "./external/preact.mjs";
import {
metaexec,
query,
all,
first,
debugIt,
spawnArray,
optional,
type,
also,
log,
} from "./sandblocks/query-builder/functionQueries.js";
export const augColor = (model) => ({
type: "replace" as const,
matcherDepth: Infinity,
model: model,
match: (it) =>
metaexec(it, (capture) => [
query('"$content"'),
(it) => it.content,
capture("node"),
(it) => it.text,
first(
[
(it) => /rgb\((\d+),(\d+),(\d+)\)/i.exec(it),
(it) => [...it, 1],
also([() => 10, capture("base")]),
],
[
(it) => /rgb\(\ ?(\d+),\ ?(\d+),\ ?(\d+),\ ?([01].\d+)\)/gm.exec(it),
also([() => 10, capture("base")]),
],
[
(it) =>
/#([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})/i.exec(it),
also([() => 16, capture("base")]),
],
[
(it) => /#([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})/i.exec(it),
(it) => [...it, 1],
also([() => 16, capture("base")]),
],
),
(it) => ({
r: parseInt(it[1], capture.get("base")),
g: parseInt(it[2], capture.get("base")),
b: parseInt(it[3], capture.get("base")),
a: it[4] < 1 ? parseFloat(it[4]) : parseInt(it[4], capture.get("base")),
}),
all(
[(it) => it.r, capture("r")],
[(it) => it.g, capture("g")],
[(it) => it.b, capture("b")],
[(it) => it.a, capture("a")],
),
]),
view: ({ r, g, b, a, node }) => {
return h(
"div",
{
style: {
display: "flow",
flowDirection: "column",
boxShadow: "0 4px 8px 0 rgba(0,0,0,0.2)",
padding: "1rem",
},
},
h(
"div",
{},
"color: ",
h("input", {
type: "color",
onchange: (e) => {
const new_alpha = a < 1 ? Math.round(a * 255).toString(16) : a;
node.replaceWith(e.target.value + new_alpha);
},
value: `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`,
}),
),
h(
"div",
{},
"Opacity: ",
h("input", {
type: "range",
min: 0,
max: 1,
step: 0.01,
value: a < 1 ? a : a / 255,
onchange: (e) => {
const new_alpha = Math.round(e.target.value * 255).toString(16);
const replacement = `#${r.toString(16)}${g.toString(
16,
)}${b.toString(16)}${new_alpha}`;
node.replaceWith(replacement, "expression");
},
}),
),
);
},
rerender: () => true,
examples: [
['const color = "rgb(12,34,56)"', [0, 0]],
['const color = "#123456"', [0, 0]],
],
});
//const a = "#123456"