@pairdraw/design-jsx 0.19.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/elements.ts CHANGED
@@ -140,7 +140,7 @@ export const DESIGN_JSX_ELEMENTS: DesignJSXElementDefinition[] = [
140
140
  runtimeType: 'vector',
141
141
  description: 'A vector layer.',
142
142
  nodeType: 'VECTOR',
143
- writes: ['VECTOR']
143
+ writes: ['VECTOR', 'BOOLEAN_OPERATION']
144
144
  },
145
145
  {
146
146
  name: 'Group',
@@ -1,4 +1,4 @@
1
- import type { CharacterStyleOverride, SceneNode } from '@pairdraw/scene-graph'
1
+ import type { CharacterStyleOverride, ColorProfile, SceneNode } from '@pairdraw/scene-graph'
2
2
 
3
3
  import { TEXT_DECORATION_STYLE } from '../enums'
4
4
  import { AUTO, CURRENT_COLOR } from '../overrides/text'
@@ -21,14 +21,15 @@ type DecorationStyle = Pick<
21
21
  export function collectDecorationProps(
22
22
  style: DecorationStyle,
23
23
  node: SceneNode,
24
- props: JSXProp[]
24
+ props: JSXProp[],
25
+ profile: ColorProfile = 'srgb'
25
26
  ): void {
26
27
  const { textDecorationFills: fills } = style
27
28
  if (style.textDecorationStyle !== undefined)
28
29
  props.push(['textDecorationStyle', TEXT_DECORATION_STYLE.print(style.textDecorationStyle)])
29
30
  if (fills) {
30
31
  const fill = fills.at(0)
31
- props.push(['textDecorationColor', fill ? paintPropValue(fill, node) : CURRENT_COLOR])
32
+ props.push(['textDecorationColor', fill ? paintPropValue(fill, node, profile) : CURRENT_COLOR])
32
33
  }
33
34
  if (style.textDecorationThickness !== undefined)
34
35
  props.push(['textDecorationThickness', style.textDecorationThickness ?? AUTO])
@@ -1,14 +1,16 @@
1
- import type { SceneGraph, SceneNode, Effect, GridTrack } from '@pairdraw/scene-graph'
2
- import { colorToHex8 } from '@pairdraw/scene-graph/color'
1
+ import type { ColorProfile, SceneGraph, SceneNode, Effect, GridTrack } from '@pairdraw/scene-graph'
2
+ import { colorToDocumentCSS, colorToHex8 } from '@pairdraw/scene-graph/color'
3
3
  import type { Color } from '@pairdraw/scene-graph/primitives'
4
4
 
5
- export function formatColor(color: Color, opacity = 1): string {
6
- return colorToHex8(color, opacity)
5
+ /** A color as the JSX runtime reads it, in the document's profile, so a P3 color keeps its gamut. */
6
+ export function formatColor(color: Color, opacity = 1, profile: ColorProfile = 'srgb'): string {
7
+ if (profile === 'srgb') return colorToHex8(color, opacity)
8
+ return colorToDocumentCSS({ ...color, a: opacity }, profile)
7
9
  }
8
10
 
9
- export function formatShadow(e: Effect): string | null {
11
+ export function formatShadow(e: Effect, profile: ColorProfile = 'srgb'): string | null {
10
12
  if (e.type !== 'DROP_SHADOW' && e.type !== 'INNER_SHADOW') return null
11
- return `${e.offset.x} ${e.offset.y} ${e.radius} ${formatColor(e.color, e.color.a)}`
13
+ return `${e.offset.x} ${e.offset.y} ${e.radius} ${formatColor(e.color, e.color.a, profile)}`
12
14
  }
13
15
 
14
16
  export function getNodeContext(node: SceneNode, graph: SceneGraph) {
@@ -158,10 +158,12 @@ function nodeToJSX(
158
158
  propAttribute
159
159
  )
160
160
  if (node.type === 'TEXT') {
161
- const { children: content, inline } = textChildren(node, depth)
161
+ const { children: content, inline } = textChildren(node, depth, graph.documentColorSpace)
162
162
  for (let count = elementCount(content); count > 0; count--) layerIds?.push(null)
163
163
  return [jsx.element(tag, attributes, content, depth, inline)]
164
164
  }
165
+ // A boolean's result path draws it, so its operands are not written as children.
166
+ if (node.type === 'BOOLEAN_OPERATION') return [jsx.element(tag, attributes, [], depth)]
165
167
  return [jsx.element(tag, attributes, children(depth + 1), depth)]
166
168
  }
167
169
 
@@ -236,7 +238,7 @@ export function designJSXElement(nodeId: string, graph: SceneGraph): DesignJSXEl
236
238
  .map(({ name, source }) => [name, source])
237
239
  )
238
240
  if (node.type === 'TEXT') {
239
- const { children, inline } = textChildren(node, 0)
241
+ const { children, inline } = textChildren(node, 0, graph.documentColorSpace)
240
242
  return { tag, attributes, text: contentBetween(tag, children, inline), childIds: [] }
241
243
  }
242
244
  if (own) {
@@ -1,12 +1,14 @@
1
1
  import type {
2
+ ColorProfile,
2
3
  Effect,
3
4
  Fill,
4
5
  GradientTransform,
5
6
  SceneNode,
6
- ShadowEffect,
7
+ ShaderEffect,
7
8
  Stroke
8
9
  } from '@pairdraw/scene-graph'
9
10
  import { gradientTransformRows, isIdentityGradientTransform } from '@pairdraw/scene-graph/gradient'
11
+ import type { Vector } from '@pairdraw/scene-graph/primitives'
10
12
  import { isShaderPresetPaint, shaderOfPaint } from '@pairdraw/scene-graph/shaders'
11
13
 
12
14
  import { STROKE_ALIGN, STROKE_CAP, STROKE_JOIN } from '../enums'
@@ -41,7 +43,7 @@ function options(entries: Options): Options | undefined {
41
43
  return Object.values(entries).some((value) => value !== undefined) ? entries : undefined
42
44
  }
43
45
 
44
- function fillValue(fill: Fill, node: SceneNode): JSXValue {
46
+ function fillValue(fill: Fill, node: SceneNode, profile: ColorProfile): JSXValue {
45
47
  const visible = fill.visible ? undefined : false
46
48
  const shader = shaderOfPaint(node, fill)
47
49
  if (shader && isShaderPresetPaint(shader)) {
@@ -72,7 +74,7 @@ function fillValue(fill: Fill, node: SceneNode): JSXValue {
72
74
  )
73
75
  }
74
76
  if (fill.type === 'SOLID') {
75
- const color = formatColor(fill.color, fill.color.a)
77
+ const color = formatColor(fill.color, fill.color.a, profile)
76
78
  const opacity = fill.opacity === fill.color.a ? undefined : fill.opacity
77
79
  return helperCall(
78
80
  'solid',
@@ -83,7 +85,7 @@ function fillValue(fill: Fill, node: SceneNode): JSXValue {
83
85
  const helper = GRADIENT_HELPERS[fill.type]
84
86
  if (helper && fill.gradientStops) {
85
87
  const stops = fill.gradientStops.map((stop) => [
86
- formatColor(stop.color, stop.color.a),
88
+ formatColor(stop.color, stop.color.a, profile),
87
89
  stop.position
88
90
  ])
89
91
  return helperCall(
@@ -108,29 +110,37 @@ function fillValue(fill: Fill, node: SceneNode): JSXValue {
108
110
  }
109
111
 
110
112
  /** A single visible solid paint fits the `bg` or text `color` shorthand. */
111
- function solidShorthand(fills: Fill[]): string | null {
113
+ function solidShorthand(fills: Fill[], profile: ColorProfile): string | null {
112
114
  if (fills.length !== 1) return null
113
115
  const [fill] = fills
114
116
  if (fill.type !== 'SOLID' || !fill.visible || blendMode(fill.blendMode)) return null
115
117
  // The shorthand has one alpha; a fill whose opacity differs from its colour's needs `solid`.
116
118
  if (fill.opacity !== fill.color.a) return null
117
- return formatColor(fill.color, fill.color.a)
119
+ return formatColor(fill.color, fill.color.a, profile)
118
120
  }
119
121
 
120
122
  /** One paint as a colour prop takes it: a colour string when it is plain, its helper otherwise. */
121
- export function paintPropValue(fill: Fill, node: SceneNode): JSXValue {
122
- return solidShorthand([fill]) ?? fillValue(fill, node)
123
+ export function paintPropValue(
124
+ fill: Fill,
125
+ node: SceneNode,
126
+ profile: ColorProfile = 'srgb'
127
+ ): JSXValue {
128
+ return solidShorthand([fill], profile) ?? fillValue(fill, node, profile)
123
129
  }
124
130
 
125
- export function collectFillProps(node: SceneNode, props: JSXProp[]): void {
131
+ export function collectFillProps(
132
+ node: SceneNode,
133
+ props: JSXProp[],
134
+ profile: ColorProfile = 'srgb'
135
+ ): void {
126
136
  if (node.fills.length === 0) {
127
137
  // A text is painted black unless it says it is not painted.
128
138
  if (node.type === 'TEXT') props.push(['fills', []])
129
139
  return
130
140
  }
131
- const shorthand = solidShorthand(node.fills)
141
+ const shorthand = solidShorthand(node.fills, profile)
132
142
  if (shorthand) props.push([node.type === 'TEXT' ? 'color' : 'bg', shorthand])
133
- else props.push(['fills', node.fills.map((fill) => fillValue(fill, node))])
143
+ else props.push(['fills', node.fills.map((fill) => fillValue(fill, node, profile))])
134
144
  }
135
145
 
136
146
  interface StrokeValue {
@@ -146,9 +156,9 @@ interface StrokeValue {
146
156
  visible?: false
147
157
  }
148
158
 
149
- function strokeValue(stroke: Stroke): StrokeValue {
159
+ function strokeValue(stroke: Stroke, profile: ColorProfile): StrokeValue {
150
160
  return {
151
- color: formatColor(stroke.color, stroke.color.a),
161
+ color: formatColor(stroke.color, stroke.color.a, profile),
152
162
  opacity: stroke.opacity === stroke.color.a ? undefined : stroke.opacity,
153
163
  weight: stroke.weight === 1 ? undefined : stroke.weight,
154
164
  align: stroke.align === 'INSIDE' ? undefined : STROKE_ALIGN.print(stroke.align),
@@ -181,8 +191,12 @@ function fitsStrokeShorthand(node: SceneNode, stroke: StrokeValue): boolean {
181
191
  return (stroke.cap ?? nodeCap) === nodeCap && (stroke.join ?? nodeJoin) === nodeJoin
182
192
  }
183
193
 
184
- export function collectStrokeProps(node: SceneNode, props: JSXProp[]): void {
185
- const strokes = node.strokes.map(strokeValue)
194
+ export function collectStrokeProps(
195
+ node: SceneNode,
196
+ props: JSXProp[],
197
+ profile: ColorProfile = 'srgb'
198
+ ): void {
199
+ const strokes = node.strokes.map((stroke) => strokeValue(stroke, profile))
186
200
  const [only] = strokes
187
201
  const shorthand = strokes.length === 1 && fitsStrokeShorthand(node, only)
188
202
  if (shorthand) {
@@ -215,15 +229,22 @@ export function collectStrokeProps(node: SceneNode, props: JSXProp[]): void {
215
229
  }
216
230
  }
217
231
 
218
- const EFFECT_HELPERS: Record<ShadowEffect['type'], string> = {
232
+ const EFFECT_HELPERS: Record<Exclude<Effect['type'], 'SHADER'>, string> = {
219
233
  DROP_SHADOW: 'dropShadow',
220
234
  INNER_SHADOW: 'innerShadow',
221
235
  LAYER_BLUR: 'layerBlur',
222
236
  BACKGROUND_BLUR: 'backgroundBlur',
223
- FOREGROUND_BLUR: 'foregroundBlur'
237
+ FOREGROUND_BLUR: 'foregroundBlur',
238
+ NOISE: 'noise',
239
+ TEXTURE: 'texture'
224
240
  }
225
241
 
226
- function effectValue(effect: ShadowEffect): HelperCall {
242
+ /** A grain size as one number when both axes match, as Figma's plugin API writes it. */
243
+ function noiseSizeValue(size: Vector): JSXValue {
244
+ return size.x === size.y ? size.x : { x: size.x, y: size.y }
245
+ }
246
+
247
+ function effectValue(effect: Exclude<Effect, ShaderEffect>, profile: ColorProfile): HelperCall {
227
248
  const visible = effect.visible ? undefined : false
228
249
  if (effect.type === 'DROP_SHADOW' || effect.type === 'INNER_SHADOW') {
229
250
  return helperCall(
@@ -233,51 +254,92 @@ function effectValue(effect: ShadowEffect): HelperCall {
233
254
  y: effect.offset.y,
234
255
  radius: effect.radius,
235
256
  spread: effect.spread === 0 ? undefined : effect.spread,
236
- color: formatColor(effect.color, effect.color.a),
257
+ color: formatColor(effect.color, effect.color.a, profile),
237
258
  visible,
238
259
  blendMode: blendMode(effect.blendMode),
239
260
  showShadowBehindNode: effect.showShadowBehindNode
240
261
  })
241
262
  )
242
263
  }
264
+ if (effect.type === 'NOISE') {
265
+ const grain = effect.noise
266
+ return helperCall(
267
+ EFFECT_HELPERS.NOISE,
268
+ options({
269
+ noiseType: grain.noiseType,
270
+ noiseSize: noiseSizeValue(grain.noiseSize),
271
+ density: grain.density,
272
+ color: formatColor(effect.color, effect.color.a, profile),
273
+ secondaryColor: grain.secondaryColor
274
+ ? formatColor(grain.secondaryColor, grain.secondaryColor.a, profile)
275
+ : undefined,
276
+ opacity: grain.opacity,
277
+ seed: grain.seed,
278
+ visible
279
+ })
280
+ )
281
+ }
282
+ if (effect.type === 'TEXTURE') {
283
+ const grain = effect.texture
284
+ return helperCall(
285
+ EFFECT_HELPERS.TEXTURE,
286
+ options({
287
+ noiseSize: noiseSizeValue(grain.noiseSize),
288
+ radius: effect.radius,
289
+ clipToShape: grain.clipToShape,
290
+ seed: grain.seed,
291
+ visible
292
+ })
293
+ )
294
+ }
243
295
  return helperCall(
244
296
  EFFECT_HELPERS[effect.type],
245
297
  visible === undefined ? effect.radius : { radius: effect.radius, visible }
246
298
  )
247
299
  }
248
300
 
249
- function plainDropShadow(effect: Effect): string | null {
301
+ function plainDropShadow(effect: Effect, profile: ColorProfile): string | null {
250
302
  const plain =
251
303
  effect.type === 'DROP_SHADOW' &&
252
304
  effect.spread === 0 &&
253
305
  !blendMode(effect.blendMode) &&
254
306
  effect.showShadowBehindNode === undefined
255
- return plain ? formatShadow(effect) : null
307
+ return plain ? formatShadow(effect, profile) : null
256
308
  }
257
309
 
258
310
  /**
259
311
  * `shadow` and `blur` describe a plain drop shadow followed by a layer blur, each optional,
260
312
  * in the order the renderer appends them; anything else needs `effects`.
261
313
  */
262
- function effectShorthand(effects: ShadowEffect[]): JSXProp[] | null {
314
+ function effectShorthand(effects: Effect[], profile: ColorProfile): JSXProp[] | null {
263
315
  if (effects.some((effect) => !effect.visible)) return null
264
- const shadow = effects.length > 0 ? plainDropShadow(effects[0]) : null
316
+ const shadow = effects.length > 0 ? plainDropShadow(effects[0], profile) : null
265
317
  const rest = shadow ? effects.slice(1) : effects
266
318
  if (rest.length > 1 || rest.some((effect) => effect.type !== 'LAYER_BLUR')) return null
267
319
  const props: JSXProp[] = []
268
320
  if (shadow) props.push(['shadow', shadow])
269
- for (const blur of rest) props.push(['blur', blur.radius])
321
+ for (const blur of rest) {
322
+ if (blur.type === 'LAYER_BLUR') props.push(['blur', blur.radius])
323
+ }
270
324
  return props
271
325
  }
272
326
 
273
- /** An effect JSX has a helper for; an imported one the modeller lacks, such as noise, has none. */
274
- function hasJSXHelper(effect: Effect): effect is ShadowEffect {
275
- return Object.hasOwn(EFFECT_HELPERS, effect.type)
327
+ /** An effect JSX has a helper for; a shader effect is not written back as JSX source. */
328
+ function hasJSXHelper(effect: Effect): effect is Exclude<Effect, ShaderEffect> {
329
+ return effect.type !== 'SHADER' && Object.hasOwn(EFFECT_HELPERS, effect.type)
276
330
  }
277
331
 
278
- export function collectEffectProps(node: SceneNode, props: JSXProp[]): void {
332
+ export function collectEffectProps(
333
+ node: SceneNode,
334
+ props: JSXProp[],
335
+ profile: ColorProfile = 'srgb'
336
+ ): void {
279
337
  // An effect without a helper cannot be written as JSX; leaving it out keeps the source importable.
280
338
  const effects = node.effects.filter(hasJSXHelper)
281
339
  if (effects.length === 0) return
282
- props.push(...(effectShorthand(effects) ?? [['effects', effects.map(effectValue)]]))
340
+ props.push(
341
+ ...(effectShorthand(effects, profile) ?? [
342
+ ['effects', effects.map((effect) => effectValue(effect, profile))]
343
+ ])
344
+ )
283
345
  }
@@ -1,6 +1,6 @@
1
1
  import { isEqual, omitBy, pick } from 'es-toolkit'
2
2
 
3
- import type { ArcData, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
3
+ import type { ArcData, ColorProfile, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
4
4
  import { DEFAULT_FONT_FAMILY } from '@pairdraw/scene-graph/constants'
5
5
  import { layoutSizing } from '@pairdraw/scene-graph/layout'
6
6
  import { createDefaultNode } from '@pairdraw/scene-graph/node-defaults'
@@ -118,9 +118,9 @@ function collectCornerRadiiProps(node: SceneNode, props: JSXProp[]): void {
118
118
  }
119
119
  }
120
120
 
121
- function collectAppearanceProps(node: SceneNode, props: JSXProp[]): void {
122
- collectFillProps(node, props)
123
- collectStrokeProps(node, props)
121
+ function collectAppearanceProps(node: SceneNode, props: JSXProp[], profile: ColorProfile): void {
122
+ collectFillProps(node, props, profile)
123
+ collectStrokeProps(node, props, profile)
124
124
  collectCornerRadiiProps(node, props)
125
125
 
126
126
  if (node.cornerSmoothing > 0) props.push(['cornerSmoothing', node.cornerSmoothing])
@@ -132,7 +132,7 @@ function collectAppearanceProps(node: SceneNode, props: JSXProp[]): void {
132
132
  props.push(['blendMode', BLEND_MODE.print(node.blendMode)])
133
133
  }
134
134
  if (node.clipsContent) props.push(['overflow', 'hidden'])
135
- collectEffectProps(node, props)
135
+ collectEffectProps(node, props, profile)
136
136
  }
137
137
 
138
138
  function collectPositionProps(
@@ -257,7 +257,7 @@ export function fontWeightValue(weight: number): string | number {
257
257
  return weight
258
258
  }
259
259
 
260
- function collectTextNodeProps(node: SceneNode, props: JSXProp[]): void {
260
+ function collectTextNodeProps(node: SceneNode, props: JSXProp[], profile: ColorProfile): void {
261
261
  const direction = resolveNodeTextDirection(node)
262
262
  if (node.fontSize !== 14) props.push(['size', node.fontSize])
263
263
  if (node.fontFamily && node.fontFamily !== DEFAULT_FONT_FAMILY)
@@ -277,7 +277,7 @@ function collectTextNodeProps(node: SceneNode, props: JSXProp[]): void {
277
277
  }
278
278
  if (node.textDecoration !== 'NONE')
279
279
  props.push(['textDecoration', TEXT_DECORATION.print(node.textDecoration)])
280
- collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props)
280
+ collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props, profile)
281
281
  collectTextFontSettingProps(node, props)
282
282
  if (node.textCase !== 'ORIGINAL') props.push(['textCase', TEXT_CASE.print(node.textCase)])
283
283
  if (node.maxLines != null) props.push(['maxLines', node.maxLines])
@@ -331,7 +331,7 @@ function collectShapeNodeProps(node: SceneNode, props: JSXProp[]): void {
331
331
  if (node.type === 'POLYGON' && node.pointCount !== 3) {
332
332
  props.push(['points', node.pointCount])
333
333
  }
334
- if (node.type === 'VECTOR' && node.vectorNetwork) {
334
+ if ((node.type === 'VECTOR' || node.type === 'BOOLEAN_OPERATION') && node.vectorNetwork) {
335
335
  // Figma's own form of a vector's geometry, which <Vector> reads back as written.
336
336
  const paths = vectorNetworkPaths(node.vectorNetwork)
337
337
  if (paths.length > 0) props.push(['paths', paths.map((path) => ({ ...path }))])
@@ -357,9 +357,9 @@ export function collectProps(
357
357
  if (ctx.parentIsGrid) collectGridPositionProps(node, props)
358
358
  if (ctx.isFlex) collectFlexAlignmentProps(node, props)
359
359
  if (ctx.isAutoLayout) collectAutoLayoutPaddingProps(node, props)
360
- collectAppearanceProps(node, props)
360
+ collectAppearanceProps(node, props, graph.documentColorSpace)
361
361
  if (node.type === 'TEXT') {
362
- collectTextNodeProps(node, props)
362
+ collectTextNodeProps(node, props, graph.documentColorSpace)
363
363
  collectTextAutoResizeProp(node, ctx, props)
364
364
  }
365
365
  collectShapeNodeProps(node, props)
@@ -2,6 +2,7 @@ import { isEmptyObject, isEqual, omitBy, pick } from 'es-toolkit'
2
2
 
3
3
  import { jsx, type SyntaxNode } from '@pairdraw/emit'
4
4
  import type {
5
+ ColorProfile,
5
6
  SceneNode,
6
7
  TextParagraphSpacingField,
7
8
  TextParagraphStyle
@@ -49,13 +50,13 @@ function characterStyles(node: SceneNode): SpanStyle[] {
49
50
  )
50
51
  }
51
52
 
52
- function spanProps(style: SpanStyle, node: SceneNode): JSXProp[] {
53
+ function spanProps(style: SpanStyle, node: SceneNode, profile: ColorProfile): JSXProp[] {
53
54
  const props: JSXProp[] = []
54
55
  if (style.fontSize !== undefined) props.push(['size', style.fontSize])
55
56
  if (style.fontFamily !== undefined) props.push(['font', style.fontFamily])
56
57
  if (style.fontWeight !== undefined) props.push(['weight', fontWeightValue(style.fontWeight)])
57
58
  if (style.italic !== undefined) props.push(['italic', style.italic])
58
- if (style.fills) collectFillProps({ ...node, fills: style.fills }, props)
59
+ if (style.fills) collectFillProps({ ...node, fills: style.fills }, props, profile)
59
60
  if (style.lineHeight !== undefined) {
60
61
  props.push(['lineHeight', style.lineHeight ? textLengthJSX(style.lineHeight) : AUTO])
61
62
  }
@@ -64,7 +65,7 @@ function spanProps(style: SpanStyle, node: SceneNode): JSXProp[] {
64
65
  }
65
66
  if (style.textDecoration !== undefined)
66
67
  props.push(['textDecoration', TEXT_DECORATION.print(style.textDecoration)])
67
- collectDecorationProps(style, node, props)
68
+ collectDecorationProps(style, node, props, profile)
68
69
  collectFontSettingProps(style, props)
69
70
  if (style.textCase !== undefined) props.push(['textCase', TEXT_CASE.print(style.textCase)])
70
71
  if (style.hyperlink) props.push(['href', style.hyperlink.value])
@@ -76,7 +77,8 @@ function inlineContent(
76
77
  node: SceneNode,
77
78
  styles: readonly SpanStyle[],
78
79
  start: number,
79
- end: number
80
+ end: number,
81
+ profile: ColorProfile
80
82
  ): SyntaxNode[] {
81
83
  const content: SyntaxNode[] = []
82
84
  let from = start
@@ -87,7 +89,7 @@ function inlineContent(
87
89
  content.push(
88
90
  isEmptyObject(style)
89
91
  ? text
90
- : jsx.element('Span', spanProps(style, node).map(propAttribute), [text], 0, true)
92
+ : jsx.element('Span', spanProps(style, node, profile).map(propAttribute), [text], 0, true)
91
93
  )
92
94
  from = index
93
95
  }
@@ -129,7 +131,11 @@ function paragraphProps(style: TextParagraphStyle): JSXProp[] {
129
131
  * `<Span>`s for characters styled apart, and as `<Paragraph>`s when a paragraph is a list item
130
132
  * or has spacing other than the text's.
131
133
  */
132
- export function textChildren(node: SceneNode, depth: number): TextChildren {
134
+ export function textChildren(
135
+ node: SceneNode,
136
+ depth: number,
137
+ profile: ColorProfile = 'srgb'
138
+ ): TextChildren {
133
139
  const styles = characterStyles(node)
134
140
  const ranges = textParagraphRanges(node.text)
135
141
  const paragraphs = ranges.map((_, index) => ownParagraphStyle(node, index))
@@ -140,13 +146,13 @@ export function textChildren(node: SceneNode, depth: number): TextChildren {
140
146
  if (styles.every(isEmptyObject)) {
141
147
  return { children: node.text ? [jsx.text(node.text)] : [], inline: true }
142
148
  }
143
- return { children: inlineContent(node, styles, 0, node.text.length), inline: true }
149
+ return { children: inlineContent(node, styles, 0, node.text.length, profile), inline: true }
144
150
  }
145
151
  const children = ranges.map(({ start, end }, index) =>
146
152
  jsx.element(
147
153
  'Paragraph',
148
154
  paragraphProps(paragraphs[index]).map(propAttribute),
149
- inlineContent(node, styles, start, end),
155
+ inlineContent(node, styles, start, end, profile),
150
156
  depth + 1,
151
157
  true
152
158
  )
@@ -327,7 +327,7 @@ function definitionExport(graph: SceneGraph, rootIds: readonly string[]): Variab
327
327
  if (typeof raw === 'number' || typeof raw === 'boolean') return raw
328
328
  if (typeof raw !== 'object') return undefined
329
329
  if ('aliasId' in raw) return variable(raw.aliasId)
330
- return 'r' in raw ? formatColor(raw, raw.a) : undefined
330
+ return 'r' in raw ? formatColor(raw, raw.a, graph.documentColorSpace) : undefined
331
331
  }
332
332
  const values = (variables: Variable[], modeId: string, base?: string) =>
333
333
  Object.fromEntries(
package/src/helpers.ts CHANGED
@@ -9,8 +9,12 @@ import {
9
9
  foregroundBlur,
10
10
  innerShadow,
11
11
  layerBlur,
12
+ noise,
13
+ texture,
12
14
  type BlurEffectOptions,
13
- type ShadowEffectOptions
15
+ type NoiseEffectOptions,
16
+ type ShadowEffectOptions,
17
+ type TextureEffectOptions
14
18
  } from './effects'
15
19
  import {
16
20
  angularGradient,
@@ -48,6 +52,25 @@ const BLUR_OPTIONS = {
48
52
  visible: true
49
53
  } satisfies OptionKeys<BlurEffectOptions>
50
54
 
55
+ const NOISE_OPTIONS = {
56
+ noiseType: true,
57
+ noiseSize: true,
58
+ density: true,
59
+ color: true,
60
+ secondaryColor: true,
61
+ opacity: true,
62
+ seed: true,
63
+ visible: true
64
+ } satisfies OptionKeys<NoiseEffectOptions>
65
+
66
+ const TEXTURE_OPTIONS = {
67
+ noiseSize: true,
68
+ radius: true,
69
+ clipToShape: true,
70
+ seed: true,
71
+ visible: true
72
+ } satisfies OptionKeys<TextureEffectOptions>
73
+
51
74
  const SOLID_OPTIONS = {
52
75
  opacity: true,
53
76
  visible: true,
@@ -119,6 +142,8 @@ export function designJSXHelpers(warnings: string[]) {
119
142
  layerBlur: checked(warnings, 'layerBlur', layerBlur, BLUR_OPTIONS, 0),
120
143
  backgroundBlur: checked(warnings, 'backgroundBlur', backgroundBlur, BLUR_OPTIONS, 0),
121
144
  foregroundBlur: checked(warnings, 'foregroundBlur', foregroundBlur, BLUR_OPTIONS, 0),
145
+ noise: checked(warnings, 'noise', noise, NOISE_OPTIONS, 0),
146
+ texture: checked(warnings, 'texture', texture, TEXTURE_OPTIONS, 0),
122
147
  defineVars,
123
148
  defineMode,
124
149
  designVar,
package/src/index.ts CHANGED
@@ -63,9 +63,14 @@ export {
63
63
  foregroundBlur,
64
64
  innerShadow,
65
65
  layerBlur,
66
+ noise,
67
+ texture,
66
68
  type BlurEffectOptions,
67
69
  type EffectColor,
68
- type ShadowEffectOptions
70
+ type NoiseEffectOptions,
71
+ type NoiseSize,
72
+ type ShadowEffectOptions,
73
+ type TextureEffectOptions
69
74
  } from './effects'
70
75
 
71
76
  export {
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import type { SceneNode } from '@pairdraw/scene-graph'
3
+ import type { DocumentColorSpace, SceneNode } from '@pairdraw/scene-graph'
4
4
  import { colorSchema } from '@pairdraw/scene-graph/color'
5
5
  import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
6
6
  import { parseCSSShadows } from '@pairdraw/scene-graph/css'
@@ -102,14 +102,18 @@ function forNormalize(effect: ParsedEffect): NormalizableEffect {
102
102
  }
103
103
 
104
104
  /** `effects` takes structured effects; `shadow` appends its shadows and `blur` a layer blur. */
105
- export function applyEffectOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {
105
+ export function applyEffectOverrides(
106
+ props: Record<string, unknown>,
107
+ o: Partial<SceneNode>,
108
+ profile: DocumentColorSpace = 'srgb'
109
+ ): void {
106
110
  if (Array.isArray(props.effects) && props.effects.length > 0) {
107
111
  const effects = parseScriptInput('effects', effectsSchema, props.effects).map(forNormalize)
108
112
  o.effects = normalizeEffects(effects)
109
113
  }
110
114
 
111
115
  // A CSS shadow list, as `box-shadow` takes it.
112
- const shadows = typeof props.shadow === 'string' ? parseCSSShadows(props.shadow) : []
116
+ const shadows = typeof props.shadow === 'string' ? parseCSSShadows(props.shadow, profile) : []
113
117
  if (shadows.length > 0) o.effects = [...(o.effects ?? []), ...shadows]
114
118
 
115
119
  if (typeof props.blur === 'number') {
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import type { Fill, GradientTransform } from '@pairdraw/scene-graph'
3
+ import type { DocumentColorSpace, Fill, GradientTransform } from '@pairdraw/scene-graph'
4
4
  import { colorToFill } from '@pairdraw/scene-graph/color'
5
5
  import { paintTransformSchema } from '@pairdraw/scene-graph/paints'
6
6
  import type { Color } from '@pairdraw/scene-graph/primitives'
@@ -45,8 +45,11 @@ function parseTransform(
45
45
  )
46
46
  }
47
47
 
48
- export function fillFromValue(value: string | Color | Fill): Fill {
49
- if (!isFill(value)) return colorToFill(value)
48
+ export function fillFromValue(
49
+ value: string | Color | Fill,
50
+ profile: DocumentColorSpace = 'srgb'
51
+ ): Fill {
52
+ if (!isFill(value)) return colorToFill(value, profile)
50
53
  const fill = structuredClone(value)
51
54
  // A paint written as an object, as export writes an image, gives its transforms as Figma's rows.
52
55
  for (const field of TRANSFORM_FIELDS) {
@@ -1,7 +1,7 @@
1
1
  import { designJSXProp } from '#design-jsx/definitions'
2
2
  import { STROKE_ALIGN, STROKE_CAP, STROKE_JOIN } from '#design-jsx/enums'
3
3
 
4
- import type { SceneNode, Stroke } from '@pairdraw/scene-graph'
4
+ import type { DocumentColorSpace, SceneNode, Stroke } from '@pairdraw/scene-graph'
5
5
  import { parseColor } from '@pairdraw/scene-graph/color'
6
6
  import type { Color } from '@pairdraw/scene-graph/primitives'
7
7
 
@@ -35,8 +35,8 @@ function dashValue(value: unknown): number[] | undefined {
35
35
  return dash.length > 0 ? dash : undefined
36
36
  }
37
37
 
38
- function toStroke(value: StrokeValue): Stroke {
39
- const color = typeof value.color === 'string' ? parseColor(value.color) : value.color
38
+ function toStroke(value: StrokeValue, profile: DocumentColorSpace): Stroke {
39
+ const color = typeof value.color === 'string' ? parseColor(value.color, profile) : value.color
40
40
  const stroke: Stroke = {
41
41
  type: 'SOLID',
42
42
  color,
@@ -80,7 +80,11 @@ function applyStrokeWeights(value: unknown, o: Partial<SceneNode>): void {
80
80
  }
81
81
 
82
82
  /** `strokes` takes structured strokes; `stroke` and its sibling props describe a single one. */
83
- export function applyStrokeOverrides(props: LayerProps, o: Partial<SceneNode>): void {
83
+ export function applyStrokeOverrides(
84
+ props: LayerProps,
85
+ o: Partial<SceneNode>,
86
+ profile: DocumentColorSpace = 'srgb'
87
+ ): void {
84
88
  applyStrokeWeights(props.strokeWeights, o)
85
89
  // Node-level cap and join, which strokes without their own use.
86
90
  const nodeCap = STROKE_CAP.parse(props.strokeCap)
@@ -91,7 +95,7 @@ export function applyStrokeOverrides(props: LayerProps, o: Partial<SceneNode>):
91
95
  const nodeDash = dashValue(props.dashPattern)
92
96
  if (nodeDash) o.dashPattern = nodeDash
93
97
  const strokes = strokeValues(props).map((value) =>
94
- toStroke({ ...value, dash: value.dash ?? nodeDash })
98
+ toStroke({ ...value, dash: value.dash ?? nodeDash }, profile)
95
99
  )
96
100
  if (strokes.length > 0) o.strokes = strokes
97
101
  }