Skip to content

AI-native design editor. Open-source Figma alternative.

Extract color constants: BLACK, TRANSPARENT, DEFAULT_SHADOW_COLOR

Danila Poyarkov4e1c15c4ff71
diff --git a/packages/core/src/clipboard.ts b/packages/core/src/clipboard.ts
index 368601f84..a3e3d43c7 100644
--- a/packages/core/src/clipboard.ts
+++ b/packages/core/src/clipboard.ts
@@ -6,6 +6,7 @@ import {
parseFigKiwiChunks,
decompressFigKiwiDataAsync
} from './kiwi-serialize'
+import { BLACK } from './constants'
import { styleToWeight } from './fonts'
import { initCodec, getCompiledSchema, getSchemaBytes } from './kiwi/codec'
import { decodeBinarySchema, compileSchema, ByteBuffer } from './kiwi/kiwi-schema'
@@ -195,7 +196,7 @@ export function importClipboardNodes(
.filter((p) => p.type === 'SOLID' && p.color)
.map((p) => ({
type: 'SOLID' as const,
- color: p.color ?? { r: 0, g: 0, b: 0, a: 1 },
+ color: p.color ?? { ...BLACK },
opacity: p.opacity ?? 1,
visible: p.visible ?? true
}))
@@ -203,7 +204,7 @@ export function importClipboardNodes(
const strokes: Stroke[] = (nc.strokePaints ?? [])
.filter((p) => p.type === 'SOLID' && p.color)
.map((p) => ({
- color: p.color ?? { r: 0, g: 0, b: 0, a: 1 },
+ color: p.color ?? { ...BLACK },
weight: nc.strokeWeight ?? 1,
opacity: p.opacity ?? 1,
visible: p.visible ?? true,
diff --git a/packages/core/src/color.ts b/packages/core/src/color.ts
index b0abd006d..ea180f6ea 100644
--- a/packages/core/src/color.ts
+++ b/packages/core/src/color.ts
@@ -1,12 +1,14 @@
import { parse, formatHex, converter } from 'culori'
+import { BLACK } from './constants'
+
import type { Color } from './types'
const toRgb = converter('rgb')
export function parseColor(input: string): Color {
const parsed = parse(input)
- if (!parsed) return { r: 0, g: 0, b: 0, a: 1 }
+ if (!parsed) return { ...BLACK }
const rgb = toRgb(parsed)
return {
r: rgb?.r ?? 0,
diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts
index ec8257fcb..a09bbee90 100644
--- a/packages/core/src/constants.ts
+++ b/packages/core/src/constants.ts
@@ -2,6 +2,9 @@ import type { Color } from './types'
export const IS_TAURI = typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window
+export const BLACK: Color = { r: 0, g: 0, b: 0, a: 1 }
+export const TRANSPARENT: Color = { r: 0, g: 0, b: 0, a: 0 }
+export const DEFAULT_SHADOW_COLOR: Color = { r: 0, g: 0, b: 0, a: 0.25 }
export const SELECTION_COLOR = { r: 0.23, g: 0.51, b: 0.96, a: 1 } satisfies Color
export const COMPONENT_COLOR = { r: 0.592, g: 0.278, b: 1, a: 1 } satisfies Color
export const SNAP_COLOR = { r: 1.0, g: 0.0, b: 0.56, a: 1 } satisfies Color
@@ -71,7 +74,7 @@ export const RULER_TARGET_PIXEL_SPACING = 100
export const RULER_MAJOR_TOLERANCE = 0.01
export const TEXT_SELECTION_COLOR = { r: 0.26, g: 0.52, b: 0.96, a: 0.3 }
-export const TEXT_CARET_COLOR = { r: 0, g: 0, b: 0, a: 1 }
+export const TEXT_CARET_COLOR = BLACK
export const TEXT_CARET_WIDTH = 1
export interface ModelOption {
diff --git a/packages/core/src/fig-export.ts b/packages/core/src/fig-export.ts
index 83bfd8cc8..03786e1ae 100644
--- a/packages/core/src/fig-export.ts
+++ b/packages/core/src/fig-export.ts
@@ -1,6 +1,6 @@
import { zipSync, deflateSync } from 'fflate'
-import { IS_TAURI } from './constants'
+import { CANVAS_BG_COLOR, IS_TAURI } from './constants'
import { sceneNodeToKiwi, fractionalPosition, buildFigKiwi } from './kiwi-serialize'
import { initCodec, getCompiledSchema, getSchemaBytes } from './kiwi/codec'
import { renderThumbnail } from './render-image'
@@ -72,7 +72,7 @@ export async function exportFigFile(
strokeAlign: 'CENTER',
strokeJoin: 'MITER',
backgroundOpacity: 1,
- backgroundColor: { r: 0.96, g: 0.96, b: 0.96, a: 1 },
+ backgroundColor: { ...CANVAS_BG_COLOR },
backgroundEnabled: true
})
diff --git a/packages/core/src/kiwi/fig-import.ts b/packages/core/src/kiwi/fig-import.ts
index c2f956e0f..153d47c36 100644
--- a/packages/core/src/kiwi/fig-import.ts
+++ b/packages/core/src/kiwi/fig-import.ts
@@ -1,4 +1,4 @@
-import { DEFAULT_STROKE_MITER_LIMIT } from '../constants'
+import { BLACK, DEFAULT_STROKE_MITER_LIMIT } from '../constants'
import { styleToWeight } from '../fonts'
import { SceneGraph } from '../scene-graph'
import { decodeVectorNetworkBlob } from '../vector'
@@ -40,7 +40,7 @@ function guidToString(guid: GUID): string {
}
function convertColor(color?: { r: number; g: number; b: number; a: number }): Color {
- if (!color) return { r: 0, g: 0, b: 0, a: 1 }
+ if (!color) return { ...BLACK }
return { r: color.r, g: color.g, b: color.b, a: color.a }
}
diff --git a/packages/core/src/render/renderer.ts b/packages/core/src/render/renderer.ts
index d775ff6ee..e2c33bcbc 100644
--- a/packages/core/src/render/renderer.ts
+++ b/packages/core/src/render/renderer.ts
@@ -1,4 +1,5 @@
import { parseColor, colorToFill } from '../color'
+import { TRANSPARENT } from '../constants'
import { isTreeNode } from './tree'
import type { SceneGraph, SceneNode, NodeType, LayoutMode, Stroke } from '../scene-graph'
@@ -287,7 +288,7 @@ function propsToOverrides(props: Record<string, unknown>, isText: boolean): Part
type: 'LAYER_BLUR',
radius: props.blur as number,
visible: true,
- color: { r: 0, g: 0, b: 0, a: 0 },
+ color: { ...TRANSPARENT },
offset: { x: 0, y: 0 },
spread: 0
}
diff --git a/packages/core/src/scene-graph.ts b/packages/core/src/scene-graph.ts
index b8313f33e..1a0ba57aa 100644
--- a/packages/core/src/scene-graph.ts
+++ b/packages/core/src/scene-graph.ts
@@ -1,4 +1,4 @@
-import { DEFAULT_STROKE_MITER_LIMIT } from './constants'
+import { BLACK, DEFAULT_STROKE_MITER_LIMIT } from './constants'
export type { GUID, Color } from './types'
@@ -479,7 +479,7 @@ export class SceneGraph {
const defaultValue =
value ??
(type === 'COLOR'
- ? { r: 0, g: 0, b: 0, a: 1 }
+ ? { ...BLACK }
: type === 'FLOAT'
? 0
: type === 'BOOLEAN'
diff --git a/packages/core/src/tools/schema.ts b/packages/core/src/tools/schema.ts
index 8cb465310..6706f9d01 100644
--- a/packages/core/src/tools/schema.ts
+++ b/packages/core/src/tools/schema.ts
@@ -7,6 +7,7 @@
*/
import { parseColor } from '../color'
+import { DEFAULT_SHADOW_COLOR } from '../constants'
import type { FigmaAPI, FigmaNodeProxy } from '../figma-api'
@@ -283,7 +284,7 @@ export const setEffects = defineTool({
}
if (!isBlur) {
- effect.color = args.color ? parseColor(args.color) : { r: 0, g: 0, b: 0, a: 0.25 }
+ effect.color = args.color ? parseColor(args.color) : { ...DEFAULT_SHADOW_COLOR }
effect.offset = { x: args.offset_x ?? 0, y: args.offset_y ?? 4 }
effect.spread = args.spread ?? 0
}