-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug-color-picker.js
101 lines (99 loc) · 2.43 KB
/
aug-color-picker.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
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 = {
type: "replace",
matcherDepth: Infinity,
model: languageFor("javascript"),
match: (it) =>
metaexec(it, (capture) => [
(it) => it.type === "string",
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) => /#([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})/i.exec(it),
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")),
}),
all(
[(it) => it.r, capture("r")],
[(it) => it.g, capture("g")],
[(it) => it.b, capture("b")],
),
]),
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"