@pairdraw/dom-css 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/dist/export.d.ts CHANGED
@@ -412,7 +412,7 @@ type TokenStylesheet = {
412
412
  css: string;
413
413
  issues: TokenStylesheetIssue[];
414
414
  };
415
- type TokenSource = Pick<SceneGraph, 'variables' | 'variableCollections'>;
415
+ type TokenSource = Pick<SceneGraph, 'variables' | 'variableCollections' | 'documentColorSpace'>;
416
416
  /** The attribute a collection's manual modes are switched by, unless it names its own. */
417
417
  declare function defaultModeAttributeName(collection: VariableCollection): string;
418
418
  /** The attribute that puts an element in a mode with no condition of its own: `data-theme`. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pairdraw/dom-css",
3
- "version": "0.19.2",
3
+ "version": "0.21.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -11,7 +11,7 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "dependencies": {
13
13
  "@acemir/cssom": "^0.9.31",
14
- "@pairdraw/emit": "^0.19.2",
14
+ "@pairdraw/emit": "^0.21.0",
15
15
  "css-tree": "^3.2.1",
16
16
  "cssesc": "^3.0.0",
17
17
  "dedent": "^1.7.2",
@@ -24,7 +24,7 @@
24
24
  "twirlwind": "^0.4.1"
25
25
  },
26
26
  "peerDependencies": {
27
- "@pairdraw/scene-graph": "^0.19.2"
27
+ "@pairdraw/scene-graph": "^0.21.0"
28
28
  },
29
29
  "exports": {
30
30
  ".": {
package/src/export/css.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ ColorProfile,
2
3
  Effect,
3
4
  Fill,
4
5
  GradientStop,
@@ -7,21 +8,21 @@ import type {
7
8
  Stroke,
8
9
  TextLength
9
10
  } from '@pairdraw/scene-graph'
10
- import { colorToCSS, colorToHex } from '@pairdraw/scene-graph/color'
11
+ import { colorToDocumentCSS } from '@pairdraw/scene-graph/color'
11
12
  import { blurSigma } from '@pairdraw/scene-graph/geometry'
12
13
  import { gradientPoint } from '@pairdraw/scene-graph/gradient'
13
14
  import type { Color, Vector } from '@pairdraw/scene-graph/primitives'
14
15
 
15
16
  import type { DesignStyleDeclaration } from '../types'
16
17
 
17
- /** Opaque colors as hex, which Tailwind matches to its palette; translucent ones as `rgba()`. */
18
- export function cssColor(color: Color): string {
19
- return color.a >= 1 ? colorToHex(color) : colorToCSS(color)
18
+ /** A color as CSS in the document's profile, so a Display P3 color keeps its gamut. */
19
+ export function cssColor(color: Color, profile: ColorProfile = 'srgb'): string {
20
+ return colorToDocumentCSS(color, profile)
20
21
  }
21
22
 
22
- export function fillToCSS(fill: Fill | undefined): string | undefined {
23
+ export function fillToCSS(fill: Fill | undefined, profile: ColorProfile = 'srgb') {
23
24
  if (fill?.type !== 'SOLID' || !fill.visible) return undefined
24
- return cssColor({ ...fill.color, a: fill.opacity })
25
+ return cssColor({ ...fill.color, a: fill.opacity }, profile)
25
26
  }
26
27
 
27
28
  /** A number CSS reads the same back: six decimals, no trailing zeros or negative zero. */
@@ -30,8 +31,8 @@ function cssNumber(value: number): string {
30
31
  return String(rounded === 0 ? 0 : rounded)
31
32
  }
32
33
 
33
- function stopToCSS(stop: GradientStop, opacity: number): string {
34
- const color = cssColor({ ...stop.color, a: stop.color.a * opacity })
34
+ function stopToCSS(stop: GradientStop, opacity: number, profile: ColorProfile): string {
35
+ const color = cssColor({ ...stop.color, a: stop.color.a * opacity }, profile)
35
36
  return `${color} ${cssNumber(stop.position * 100)}%`
36
37
  }
37
38
 
@@ -45,13 +46,18 @@ function positionToCSS(point: Vector): string {
45
46
  * maps the box's unit square onto gradient space; its inverse gives the line's ends, the
46
47
  * ellipse's centre and radii, or the sweep's start ray. Diamond gradients have no CSS spelling.
47
48
  */
48
- export function gradientFillToCSS(fill: Fill, width: number, height: number): string | undefined {
49
+ export function gradientFillToCSS(
50
+ fill: Fill,
51
+ width: number,
52
+ height: number,
53
+ profile: ColorProfile = 'srgb'
54
+ ): string | undefined {
49
55
  const transform = fill.gradientTransform
50
56
  const stops = fill.gradientStops
51
57
  if (!fill.visible || !transform || !stops || stops.length === 0 || width <= 0 || height <= 0) {
52
58
  return undefined
53
59
  }
54
- const painted = stops.map((stop) => stopToCSS(stop, fill.opacity)).join(', ')
60
+ const painted = stops.map((stop) => stopToCSS(stop, fill.opacity, profile)).join(', ')
55
61
  if (fill.type === 'GRADIENT_LINEAR') {
56
62
  const start = gradientPoint(transform, { x: 0, y: 0.5 })
57
63
  const end = gradientPoint(transform, { x: 1, y: 0.5 })
@@ -90,11 +96,12 @@ export function gradientFillToCSS(fill: Fill, width: number, height: number): st
90
96
  export function gradientFillsToCSS(
91
97
  fills: Fill[],
92
98
  width: number,
93
- height: number
99
+ height: number,
100
+ profile: ColorProfile = 'srgb'
94
101
  ): string | undefined {
95
102
  const layers: string[] = []
96
103
  for (let index = fills.length - 1; index >= 0; index--) {
97
- const css = gradientFillToCSS(fills[index], width, height)
104
+ const css = gradientFillToCSS(fills[index], width, height, profile)
98
105
  if (css) layers.push(css)
99
106
  }
100
107
  return layers.length > 0 ? layers.join(', ') : undefined
@@ -107,9 +114,10 @@ export type BorderSide = 'top' | 'right' | 'bottom' | 'left'
107
114
  * border insets the box its absolutely positioned children are placed in, where a stroke does not.
108
115
  */
109
116
  export function borderWidths(node: SceneNode): Record<BorderSide, number> {
110
- if (!strokeColorToCSS(node.strokes.at(0))) return { top: 0, right: 0, bottom: 0, left: 0 }
117
+ const stroke = node.strokes.find((item) => item.visible)
118
+ if (!strokeColorToCSS(stroke)) return { top: 0, right: 0, bottom: 0, left: 0 }
111
119
  if (!node.independentStrokeWeights) {
112
- const weight = node.strokes[0]?.weight ?? 0
120
+ const weight = stroke?.weight ?? 0
113
121
  return { top: weight, right: weight, bottom: weight, left: weight }
114
122
  }
115
123
  return {
@@ -120,9 +128,12 @@ export function borderWidths(node: SceneNode): Record<BorderSide, number> {
120
128
  }
121
129
  }
122
130
 
123
- export function strokeColorToCSS(stroke: Stroke | undefined): string | undefined {
131
+ export function strokeColorToCSS(
132
+ stroke: Stroke | undefined,
133
+ profile: ColorProfile = 'srgb'
134
+ ): string | undefined {
124
135
  if (!stroke?.visible) return undefined
125
- return cssColor({ ...stroke.color, a: stroke.opacity })
136
+ return cssColor({ ...stroke.color, a: stroke.opacity }, profile)
126
137
  }
127
138
 
128
139
  function px(value: number): string {
@@ -133,7 +144,7 @@ function px(value: number): string {
133
144
  * A shadow as `box-shadow` writes it, or as `text-shadow` does without its spread. CSS blurs a
134
145
  * shadow by half its blur length, so the length is twice the sigma the canvas blurs it by.
135
146
  */
136
- function shadowToCSS(effect: ShadowEffect, spread = true): string {
147
+ function shadowToCSS(effect: ShadowEffect, spread = true, profile: ColorProfile = 'srgb'): string {
137
148
  const inset = effect.type === 'INNER_SHADOW' ? 'inset ' : ''
138
149
  const parts = [
139
150
  `${inset}${px(effect.offset.x)}`,
@@ -141,7 +152,7 @@ function shadowToCSS(effect: ShadowEffect, spread = true): string {
141
152
  px(2 * blurSigma(effect.radius))
142
153
  ]
143
154
  if (spread) parts.push(px(effect.spread))
144
- parts.push(cssColor(effect.color))
155
+ parts.push(cssColor(effect.color, profile))
145
156
  return parts.join(' ')
146
157
  }
147
158
 
@@ -173,12 +184,15 @@ function addBlurs(style: DesignStyleDeclaration, visible: Effect[]): void {
173
184
  * Every visible shadow, in order, plus layer and background blur, blurred as the canvas draws
174
185
  * them: CSS's `blur()` takes the Gaussian sigma itself.
175
186
  */
176
- export function effectsToCSS(effects: Effect[]): DesignStyleDeclaration {
187
+ export function effectsToCSS(
188
+ effects: Effect[],
189
+ profile: ColorProfile = 'srgb'
190
+ ): DesignStyleDeclaration {
177
191
  const style: DesignStyleDeclaration = {}
178
192
  const visible = effects.filter((effect) => effect.visible)
179
193
  const shadows = visible.filter(isShadow)
180
194
  if (shadows.length > 0)
181
- style['box-shadow'] = shadows.map((effect) => shadowToCSS(effect)).join(', ')
195
+ style['box-shadow'] = shadows.map((effect) => shadowToCSS(effect, true, profile)).join(', ')
182
196
  addBlurs(style, visible)
183
197
  return style
184
198
  }
@@ -187,12 +201,15 @@ export function effectsToCSS(effects: Effect[]): DesignStyleDeclaration {
187
201
  * Text's effects: each visible drop shadow as `text-shadow`, which has no spread and shadows the
188
202
  * glyphs, and its layer and background blurs. An inner shadow has no form CSS gives text.
189
203
  */
190
- export function textEffectsToCSS(effects: Effect[]): DesignStyleDeclaration {
204
+ export function textEffectsToCSS(
205
+ effects: Effect[],
206
+ profile: ColorProfile = 'srgb'
207
+ ): DesignStyleDeclaration {
191
208
  const style: DesignStyleDeclaration = {}
192
209
  const visible = effects.filter((effect) => effect.visible)
193
210
  const shadows = visible.filter(isDropShadow)
194
211
  if (shadows.length > 0) {
195
- style['text-shadow'] = shadows.map((effect) => shadowToCSS(effect, false)).join(', ')
212
+ style['text-shadow'] = shadows.map((effect) => shadowToCSS(effect, false, profile)).join(', ')
196
213
  }
197
214
  addBlurs(style, visible)
198
215
  return style
@@ -1,7 +1,7 @@
1
1
  import { isEqual } from 'es-toolkit/predicate'
2
2
  import { fromUint8Array } from 'js-base64'
3
3
 
4
- import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
4
+ import type { ColorProfile, SceneGraph, SceneNode, Stroke } from '@pairdraw/scene-graph'
5
5
  import { BLACK } from '@pairdraw/scene-graph/constants'
6
6
  import { readIcon } from '@pairdraw/scene-graph/icons'
7
7
  import { imageMimeType } from '@pairdraw/scene-graph/images'
@@ -166,9 +166,14 @@ function addCornerRadii(style: DesignStyleDeclaration, node: SceneNode, css: Fie
166
166
  if (node.cornerRadius > 0) style['border-radius'] = css('cornerRadius', px(node.cornerRadius))
167
167
  }
168
168
 
169
- function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
170
- const stroke = node.strokes.at(0)
171
- const literalColor = strokeColorToCSS(stroke)
169
+ function addStroke(
170
+ style: DesignStyleDeclaration,
171
+ node: SceneNode,
172
+ css: FieldCSS,
173
+ profile: ColorProfile
174
+ ): void {
175
+ const stroke = node.strokes.find((item) => item.visible)
176
+ const literalColor = strokeColorToCSS(stroke, profile)
172
177
  if (!literalColor || !stroke) return
173
178
  const color = css('strokes/0/color', literalColor)
174
179
  const borderStyle = node.dashPattern.length > 0 ? 'dashed' : 'solid'
@@ -186,6 +191,35 @@ function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS
186
191
  style['border-left-width'] = css('borderLeftWeight', px(node.borderLeftWeight))
187
192
  }
188
193
 
194
+ /**
195
+ * CSS gives an element one border, so the visible strokes after the first become rings in
196
+ * `box-shadow`, in layer order. An outside ring grows from the border's outer edge, an inside one
197
+ * from its inner edge, and a centred stroke gets a ring on each side of the border. Rings cannot
198
+ * dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
199
+ * stroke order that alternates them is approximated.
200
+ */
201
+ function extraStrokeShadows(strokes: Stroke[], first: Stroke, profile: ColorProfile): string[] {
202
+ const outer: string[] = []
203
+ const inner: string[] = []
204
+ let outside = 0
205
+ let inside = first.weight
206
+ for (const stroke of strokes) {
207
+ if (!stroke.visible) continue
208
+ const color = strokeColorToCSS(stroke, profile)
209
+ if (!color) continue
210
+ if (stroke.align !== 'OUTSIDE') {
211
+ inside += stroke.align === 'CENTER' ? stroke.weight / 2 : stroke.weight
212
+ inner.push(`inset 0 0 0 ${px(inside)} ${color}`)
213
+ }
214
+ if (stroke.align !== 'INSIDE') {
215
+ outside += stroke.align === 'CENTER' ? stroke.weight / 2 : stroke.weight
216
+ outer.push(`0 0 0 ${px(outside)} ${color}`)
217
+ }
218
+ }
219
+ // The first ring listed paints on top, so each group stays nearest first.
220
+ return [...outer, ...inner]
221
+ }
222
+
189
223
  const PADDING_FIELDS = ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'] as const
190
224
 
191
225
  /** Sides compare by their CSS, so a token and a literal of the same size stay separate. */
@@ -323,19 +357,29 @@ const CSS_BLEND_MODES: Partial<Record<SceneNode['blendMode'], string>> = {
323
357
  function styleFromSceneNode(
324
358
  node: SceneNode,
325
359
  parent: SceneNode | undefined,
326
- css: FieldCSS
360
+ css: FieldCSS,
361
+ profile: ColorProfile
327
362
  ): DesignStyleDeclaration {
328
363
  const style: DesignStyleDeclaration = {}
329
364
  addSize(style, node, parent, css)
330
365
  addPositioning(style, node, parent)
331
366
  addSizeConstraints(style, node, css)
332
- const fill = fillToCSS(node.fills.at(0))
367
+ const fill = fillToCSS(node.fills.at(0), profile)
333
368
  if (fill) style['background-color'] = css('fills/0/color', fill)
334
- const gradients = gradientFillsToCSS(node.fills, node.width, node.height)
369
+ const gradients = gradientFillsToCSS(node.fills, node.width, node.height, profile)
335
370
  if (gradients) style['background-image'] = gradients
336
371
  addImageStyle(style, node)
337
- addStroke(style, node, css)
338
- Object.assign(style, effectsToCSS(node.effects))
372
+ addStroke(style, node, css, profile)
373
+ Object.assign(style, effectsToCSS(node.effects, profile))
374
+ const visibleStrokes = node.strokes.filter((stroke) => stroke.visible)
375
+ if (visibleStrokes.length > 1) {
376
+ const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0], profile)
377
+ if (rings.length > 0) {
378
+ style['box-shadow'] = [rings.join(', '), style['box-shadow']]
379
+ .filter((shadow): shadow is string => Boolean(shadow))
380
+ .join(', ')
381
+ }
382
+ }
339
383
  if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
340
384
  const blend = CSS_BLEND_MODES[node.blendMode]
341
385
  if (blend) style['mix-blend-mode'] = blend
@@ -370,15 +414,16 @@ function styleFromSceneNode(
370
414
  function styleFromTextNode(
371
415
  node: SceneNode,
372
416
  parent: SceneNode | undefined,
373
- css: FieldCSS
417
+ css: FieldCSS,
418
+ profile: ColorProfile
374
419
  ): DesignStyleDeclaration {
375
420
  const style: DesignStyleDeclaration = {}
376
421
  addSize(style, node, parent, css)
377
422
  addPositioning(style, node, parent)
378
423
  addLayoutChild(style, node, parent)
379
424
  if (resolveNodeTextDirection(node) === 'RTL') style.direction = 'rtl'
380
- const color = fillToCSS(node.fills.at(0))
381
- style.color = color ? css('fills/0/color', color) : cssColor(BLACK)
425
+ const color = fillToCSS(node.fills.at(0), profile)
426
+ style.color = color ? css('fills/0/color', color) : cssColor(BLACK, profile)
382
427
  style['font-family'] = node.fontFamily
383
428
  style['font-size'] = css('fontSize', px(node.fontSize))
384
429
  style['font-weight'] = String(node.fontWeight)
@@ -397,7 +442,7 @@ function styleFromTextNode(
397
442
  if (node.textAlignHorizontal !== 'LEFT')
398
443
  style['text-align'] = node.textAlignHorizontal.toLowerCase()
399
444
  if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
400
- Object.assign(style, textEffectsToCSS(node.effects))
445
+ Object.assign(style, textEffectsToCSS(node.effects, profile))
401
446
  if (node.textDecoration !== 'NONE') {
402
447
  style['text-decoration-line'] =
403
448
  node.textDecoration === 'UNDERLINE' ? 'underline' : 'line-through'
@@ -481,6 +526,59 @@ function vectorDesignNode(
481
526
  }
482
527
  }
483
528
 
529
+ /** The outline a vector mask clips siblings to, in the sibling's own box; null where CSS cannot. */
530
+ function vectorMaskClip(mask: SceneNode, sibling: SceneNode): string | null {
531
+ // The renderer clips to the rotated outline; a polygon built from the unrotated box would
532
+ // cut a different region, so a rotated mask stays unclipped as the export documents.
533
+ if (mask.maskType !== 'VECTOR' || mask.childIds.length > 0 || mask.rotation !== 0) return null
534
+ const left = mask.x - sibling.x
535
+ const top = mask.y - sibling.y
536
+ if (mask.type === 'RECTANGLE') {
537
+ const right = left + mask.width
538
+ const bottom = top + mask.height
539
+ return `polygon(${px(left)} ${px(top)}, ${px(right)} ${px(top)}, ${px(right)} ${px(bottom)}, ${px(left)} ${px(bottom)})`
540
+ }
541
+ if (mask.type === 'ELLIPSE') {
542
+ const rx = mask.width / 2
543
+ const ry = mask.height / 2
544
+ return `ellipse(${px(rx)} ${px(ry)} at ${px(left + rx)} ${px(top + ry)})`
545
+ }
546
+ return null
547
+ }
548
+
549
+ /**
550
+ * Projects a parent's children as the canvas draws them: a mask layer is never a layer of its
551
+ * own, and one vector mask clips the siblings after it. Alpha and luminance masks have no
552
+ * faithful CSS form over several elements, and two masks combine, so those stay unclipped.
553
+ */
554
+ function projectChildren(
555
+ context: ProjectionContext,
556
+ parent: SceneNode,
557
+ modes: CSSModes,
558
+ root: boolean
559
+ ): DesignNode[] {
560
+ const projected: DesignNode[] = []
561
+ let masks: SceneNode[] = []
562
+ let afterMask = false
563
+ for (const child of nodeChildren(context.graph, parent)) {
564
+ if (child.visible && child.isMask) {
565
+ masks = afterMask ? [...masks, child] : [child]
566
+ afterMask = true
567
+ continue
568
+ }
569
+ afterMask = false
570
+ const node = sceneNodeToDesignNode(context, child, modes, root)
571
+ if (!node) continue
572
+ const mask = masks.length === 1 ? masks[0] : null
573
+ if (mask && node.type === 'element') {
574
+ const clip = vectorMaskClip(mask, child)
575
+ if (clip) node.inlineStyle = { ...node.inlineStyle, 'clip-path': clip }
576
+ }
577
+ projected.push(node)
578
+ }
579
+ return projected
580
+ }
581
+
484
582
  function sceneNodeToDesignNode(
485
583
  context: ProjectionContext,
486
584
  node: SceneNode,
@@ -496,7 +594,11 @@ function sceneNodeToDesignNode(
496
594
  const css: FieldCSS = (field, literal) => tokens?.reference(node, field, modes) ?? literal
497
595
 
498
596
  if (node.type === 'TEXT') {
499
- const content = textElementContent(node, styleFromTextNode(node, parent, css))
597
+ const content = textElementContent(
598
+ node,
599
+ styleFromTextNode(node, parent, css, graph.documentColorSpace),
600
+ graph.documentColorSpace
601
+ )
500
602
  return {
501
603
  type: 'element',
502
604
  tagName: content.tagName,
@@ -510,13 +612,16 @@ function sceneNodeToDesignNode(
510
612
 
511
613
  // An icon is one drawing, however many paths it has.
512
614
  if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
513
- const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css))
615
+ const vector = vectorDesignNode(
616
+ context,
617
+ node,
618
+ attrs,
619
+ styleFromSceneNode(node, parent, css, graph.documentColorSpace)
620
+ )
514
621
  if (vector) return vector
515
622
  }
516
623
 
517
- const children = nodeChildren(graph, node)
518
- .map((child) => sceneNodeToDesignNode(context, child, modes, false))
519
- .filter((child): child is DesignNode => child !== null)
624
+ const children = projectChildren(context, node, modes, false)
520
625
 
521
626
  if (node.type === 'CANVAS') {
522
627
  return {
@@ -529,7 +634,7 @@ function sceneNodeToDesignNode(
529
634
  }
530
635
  }
531
636
 
532
- const style = styleFromSceneNode(node, parent, css)
637
+ const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace)
533
638
  const shader = layerShader(graph, node)
534
639
  if (shader) addShaderLayerStyle(style)
535
640
  return {
@@ -571,11 +676,7 @@ export function sceneGraphToDesignDocument(
571
676
  const root = graph.getNode(options.rootId ?? graph.rootId)
572
677
  const context = projectionContext(graph, options)
573
678
  const modes = context.tokens?.defaultModes() ?? new Map<string, string>()
574
- const children = root
575
- ? nodeChildren(graph, root)
576
- .map((child) => sceneNodeToDesignNode(context, child, modes, true))
577
- .filter((child): child is DesignNode => child !== null)
578
- : []
679
+ const children = root ? projectChildren(context, root, modes, true) : []
579
680
 
580
681
  return { type: 'document', sourceGraph: graph, tokens: context.tokens, children }
581
682
  }
@@ -3,6 +3,7 @@ import { isEqual } from 'es-toolkit/predicate'
3
3
 
4
4
  import type {
5
5
  CharacterStyleOverride,
6
+ ColorProfile,
6
7
  HyperlinkTarget,
7
8
  SceneNode,
8
9
  TextLength
@@ -157,7 +158,7 @@ function styledRanges(node: SceneNode): StyledRange[] {
157
158
  }
158
159
 
159
160
  /** Underline or strikethrough as CSS writes it: Figma's offset is CSS's, from the baseline. */
160
- function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
161
+ function decorationCSS(style: CharacterStyle, profile: ColorProfile): DesignStyleDeclaration {
161
162
  if (style.textDecoration === 'NONE') return {}
162
163
  const underline = style.textDecoration === 'UNDERLINE'
163
164
  const css: DesignStyleDeclaration = {
@@ -166,7 +167,10 @@ function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
166
167
  if (style.textDecorationStyle !== 'SOLID') {
167
168
  css['text-decoration-style'] = style.textDecorationStyle.toLowerCase()
168
169
  }
169
- const color = fillToCSS(style.textDecorationFills.find((fill) => fill.visible))
170
+ const color = fillToCSS(
171
+ style.textDecorationFills.find((fill) => fill.visible),
172
+ profile
173
+ )
170
174
  if (color) css['text-decoration-color'] = color
171
175
  if (style.textDecorationThickness !== null) {
172
176
  css['text-decoration-thickness'] = px(style.textDecorationThickness)
@@ -178,9 +182,13 @@ function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
178
182
  return css
179
183
  }
180
184
 
181
- function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDeclaration {
185
+ function characterCSS(
186
+ style: CharacterStyle,
187
+ decorated: boolean,
188
+ profile: ColorProfile
189
+ ): DesignStyleDeclaration {
182
190
  return {
183
- color: fillToCSS(style.fills.at(0)) ?? '',
191
+ color: fillToCSS(style.fills.at(0), profile) ?? '',
184
192
  'font-family': style.fontFamily,
185
193
  'font-size': px(style.fontSize),
186
194
  'font-weight': String(style.fontWeight),
@@ -188,7 +196,7 @@ function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDec
188
196
  'letter-spacing': letterSpacingToCSS(style.letterSpacing),
189
197
  'line-height': style.lineHeight === null ? 'normal' : lineHeightToCSS(style.lineHeight),
190
198
  'text-transform': textCaseToCSS(style.textCase) ?? 'none',
191
- ...(decorated ? decorationCSS(style) : {})
199
+ ...(decorated ? decorationCSS(style, profile) : {})
192
200
  }
193
201
  }
194
202
 
@@ -200,9 +208,10 @@ function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDec
200
208
  function spanCSS(
201
209
  style: CharacterStyle,
202
210
  decorated: boolean,
203
- base: DesignStyleDeclaration
211
+ base: DesignStyleDeclaration,
212
+ profile: ColorProfile
204
213
  ): DesignStyleDeclaration {
205
- const own = characterCSS(style, decorated)
214
+ const own = characterCSS(style, decorated, profile)
206
215
  const css = differing(own, base)
207
216
  const { unit, value } = style.letterSpacing
208
217
  if (unit === 'PERCENT' && value !== 0 && 'font-size' in css) {
@@ -225,10 +234,14 @@ function differing(
225
234
  * Whether the text decorates every character alike. CSS draws an element's decoration through
226
235
  * its descendants, which cannot take it off, so mixed decorations go on the spans instead.
227
236
  */
228
- function uniformDecoration(node: SceneNode, ranges: readonly StyledRange[]): boolean {
237
+ function uniformDecoration(
238
+ node: SceneNode,
239
+ ranges: readonly StyledRange[],
240
+ profile: ColorProfile
241
+ ): boolean {
229
242
  const decorations = ranges
230
243
  .filter((range) => node.text.slice(range.start, range.end).replaceAll('\n', '') !== '')
231
- .map((range) => decorationCSS(range.style))
244
+ .map((range) => decorationCSS(range.style, profile))
232
245
  return decorations.every((decoration) => isEqual(decoration, decorations[0]))
233
246
  }
234
247
 
@@ -237,6 +250,7 @@ interface TextProjection {
237
250
  ranges: StyledRange[]
238
251
  base: DesignStyleDeclaration
239
252
  decorated: boolean
253
+ profile: ColorProfile
240
254
  }
241
255
 
242
256
  function textNode(text: string): DesignNode {
@@ -265,13 +279,13 @@ function linkElement(href: string, css: DesignStyleDeclaration, text: string): D
265
279
  * linked characters are a link instead.
266
280
  */
267
281
  function inlineContent(projection: TextProjection, start: number, end: number): DesignNode[] {
268
- const { node, ranges, base, decorated } = projection
282
+ const { node, ranges, base, decorated, profile } = projection
269
283
  const pieces: { css: DesignStyleDeclaration; text: string; href: string | null }[] = []
270
284
  for (const range of ranges) {
271
285
  const from = Math.max(start, range.start)
272
286
  const to = Math.min(end, range.end)
273
287
  if (to <= from) continue
274
- const css = spanCSS(range.style, !decorated, base)
288
+ const css = spanCSS(range.style, !decorated, base, profile)
275
289
  const text = node.text.slice(from, to)
276
290
  const previous = pieces.at(-1)
277
291
  if (previous && previous.href === range.href && isEqual(previous.css, css)) {
@@ -443,19 +457,21 @@ function groupEm(
443
457
  */
444
458
  export function textElementContent(
445
459
  node: SceneNode,
446
- layerStyle: DesignStyleDeclaration
460
+ layerStyle: DesignStyleDeclaration,
461
+ profile: ColorProfile = 'srgb'
447
462
  ): { tagName: string; style: DesignStyleDeclaration; children: DesignNode[] } {
448
463
  const ranges = styledRanges(node)
449
- const decorated = uniformDecoration(node, ranges)
464
+ const decorated = uniformDecoration(node, ranges, profile)
450
465
  const style = Object.fromEntries(
451
466
  Object.entries(layerStyle).filter(([property]) => !DECORATION_PROPERTIES.has(property))
452
467
  )
453
- if (decorated) Object.assign(style, decorationCSS(characterStyle(node)))
468
+ if (decorated) Object.assign(style, decorationCSS(characterStyle(node), profile))
454
469
  const projection: TextProjection = {
455
470
  node,
456
471
  ranges,
457
- base: characterCSS(characterStyle(node), decorated),
458
- decorated
472
+ base: characterCSS(characterStyle(node), decorated, profile),
473
+ decorated,
474
+ profile
459
475
  }
460
476
  if (!needsBlocks(node)) {
461
477
  return { tagName: 'span', style, children: inlineContent(projection, 0, node.text.length) }