interface SetFillColorParams {
nodeId: string;
color: { r: number; g: number; b: number; a?: number };
}
interface SetStrokeColorParams {
nodeId: string;
color: { r: number; g: number; b: number; a?: number };
strokeWeight?: number;
}
interface SetCornerRadiusParams {
nodeId: string;
radius: number;
}
/**
* Set fill color on a node
*/
export async function setFillColor(params: SetFillColorParams): Promise<{
id: string;
name: string;
fills: readonly Paint[];
}> {
const { nodeId, color } = params;
const node = await figma.getNodeByIdAsync(nodeId);
if (!node) {
throw new Error(`Node not found with ID: ${nodeId}`);
}
if (!("fills" in node)) {
throw new Error(`Node does not support fills: ${nodeId}`);
}
const geometryNode = node as GeometryMixin & BaseNode;
geometryNode.fills = [
{
type: "SOLID",
color: { r: color.r, g: color.g, b: color.b },
opacity: color.a ?? 1,
},
];
return {
id: node.id,
name: node.name,
fills: geometryNode.fills,
};
}
/**
* Set stroke color on a node
*/
export async function setStrokeColor(params: SetStrokeColorParams): Promise<{
id: string;
name: string;
strokes: readonly Paint[];
strokeWeight: number;
}> {
const { nodeId, color, strokeWeight } = params;
const node = await figma.getNodeByIdAsync(nodeId);
if (!node) {
throw new Error(`Node not found with ID: ${nodeId}`);
}
if (!("strokes" in node)) {
throw new Error(`Node does not support strokes: ${nodeId}`);
}
const geometryNode = node as GeometryMixin & BaseNode;
geometryNode.strokes = [
{
type: "SOLID",
color: { r: color.r, g: color.g, b: color.b },
opacity: color.a ?? 1,
},
];
if (strokeWeight !== undefined && "strokeWeight" in geometryNode) {
(geometryNode as GeometryMixin).strokeWeight = strokeWeight;
}
return {
id: node.id,
name: node.name,
strokes: geometryNode.strokes,
strokeWeight:
"strokeWeight" in geometryNode
? ((geometryNode as GeometryMixin).strokeWeight as number)
: 1,
};
}
/**
* Set corner radius on a node
*/
export async function setCornerRadius(params: SetCornerRadiusParams): Promise<{
id: string;
name: string;
cornerRadius: number;
}> {
const { nodeId, radius } = params;
const node = await figma.getNodeByIdAsync(nodeId);
if (!node) {
throw new Error(`Node not found with ID: ${nodeId}`);
}
if (!("cornerRadius" in node)) {
throw new Error(`Node does not support corner radius: ${nodeId}`);
}
const cornerNode = node as CornerMixin & BaseNode;
cornerNode.cornerRadius = radius;
return {
id: node.id,
name: node.name,
cornerRadius: cornerNode.cornerRadius as number,
};
}