-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.js
63 lines (48 loc) · 1.56 KB
/
code.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
//Tells Figma to display your custom UI
figma.showUI(__html__);
//Resizes your plugin UI in pixels
figma.ui.resize(400, 400)
/*
This is our custom function
When a user submits an input from our plugin UI,
this will take care of it
*/
function activateWhenUserClicksSubmit(msg){
//extracts the message's vairables
const {keyword, hex} = msg;
//Gets the node for the current Figma Page
const currentPage = figma.currentPage;
//tests for keyword
function testForKeyWord(node){
//a regular expression that tests if the keyword is in the title
const isKeyword = new RegExp(keyword, 'gi');
if(node.name){
return isKeyword.test(node.name);
}
return false;
}
const nodes = currentPage.findAll(testForKeyWord)
//fills all our code with the selected color
for(const node of nodes){
node.fills = [{type:"SOLID", color: colorConverter(hex)}]
}
}
/*
This is an event listener
Whenever the UI makes a call
to our main code
*/
figma.ui.onmessage = activateWhenUserClicksSubmit;
function colorConverter(hexColor){
//a regular expression. It will test if the hex is proper
const isHex = /[0-9abcdef]/i;
//Throws an error if the hex is improper
if(hexColor.length !== 6 && isHex.test(hexColor)){
throw Error('hex colors must have 6 characters');
}
//converts hex code into RGB coloring
const r = parseInt(hexColor.slice(0,2), 16)/255;
const g = parseInt(hexColor.slice(2,4), 16)/255;
const b = parseInt(hexColor.slice(4,6), 16)/255;
return {r,g,b};
}