我正在编写一个 Figma 插件来生成随机颜色并修改选择的填充。This works fine when the selection node has a fill. 但是当没有填充时,我在尝试应用时会出错fills[0].color = newColor;
。
在该节点上记录填充时,[]
我假设它是一个空数组。Figma 节点可以有多个填充,并且node.fills[1].color
在分配值时需要格式。
那么如何color
为有空数组的节点创建分配?
import chroma from '../node_modules/chroma-js/chroma'
import clone from './clone'
for (const node of figma.currentPage.selection) {
if ("fills" in node) {
const fills = clone(node.fills);
// Get a random colour from chroma-js.
const random = chroma.random().gl();
// Create an array that matches the fill structure (rgb represented as 0 to 1)
const newColor = {r: random[0], g: random[1], b: random[2]};
// Only change the first fill
fills[0].color = newColor;
// Replace the fills on the node.
node.fills = fills;
}
}
// Make sure to close the plugin when you're done. Otherwise the plugin will
// keep running, which shows the cancel button at the bottom of the screen.
figma.closePlugin();