-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
59 lines (51 loc) · 1.06 KB
/
code.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
figma.showUI(__html__);
const selection = figma.currentPage.selection[0];
type HasFillNode =
| ComponentNode
| InstanceNode
| VectorNode
| StarNode
| LineNode
| EllipseNode
| PolygonNode
| RectangleNode
| TextNode;
type Color = {
r: number;
g: number;
b: number;
};
type PostMessagePayload = {
select: Color;
};
function hasFillsProperty(selection: SceneNode) {
switch (selection.type) {
case "COMPONENT":
case "ELLIPSE":
case "INSTANCE":
case "LINE":
case "POLYGON":
case "RECTANGLE":
case "STAR":
case "TEXT":
case "VECTOR":
return true;
default:
return false;
}
}
function sendToMessage(payload: PostMessagePayload) {
figma.ui.postMessage(payload, { origin: "*" });
}
if (hasFillsProperty(selection)) {
const paint = (selection as HasFillNode).fills[0] as Paint;
if (paint.type === "SOLID") {
sendToMessage({
select: {
r: Math.round(paint.color.r * 255),
g: Math.round(paint.color.g * 255),
b: Math.round(paint.color.b * 255),
},
});
}
}