-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaint-to-css.ts
More file actions
488 lines (423 loc) · 14.7 KB
/
paint-to-css.ts
File metadata and controls
488 lines (423 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import { optimizeHex } from '../../utils/optimize-hex'
import { rgbaToHex } from '../../utils/rgba-to-hex'
import { checkAssetNode } from './check-asset-node'
import { fmtPct } from './fmtPct'
import { solidToString } from './solid-to-string'
import { buildCssUrl } from './wrap-url'
interface Point {
x: number
y: number
}
/**
* This function converts Figma paint to CSS.
*/
export async function paintToCSS(
fill: Paint,
node: SceneNode,
last: boolean,
): Promise<string | null> {
switch (fill.type) {
case 'SOLID':
return last
? await solidToString(fill)
: await convertSolidLinearGradient(fill)
case 'GRADIENT_LINEAR':
return convertGradientLinear(fill, node.width, node.height)
case 'GRADIENT_RADIAL':
return convertRadial(fill, node.width, node.height)
case 'GRADIENT_ANGULAR':
return convertAngular(fill, node.width, node.height)
case 'GRADIENT_DIAMOND':
return convertDiamond(fill, node.width, node.height)
case 'IMAGE':
return convertImage(fill)
case 'PATTERN':
return await convertPattern(fill)
default:
return null
}
}
function convertImage(fill: ImagePaint): string {
if (!fill.visible) return 'transparent'
if (fill.opacity === 0) return 'transparent'
// Get image filename from hash or use default
const imageName = 'image.png'
switch (fill.scaleMode) {
case 'FILL':
return `${buildCssUrl(`/icons/${imageName}`)} center/cover no-repeat`
case 'FIT':
return `${buildCssUrl(`/icons/${imageName}`)} center/contain no-repeat`
case 'CROP':
return `${buildCssUrl(`/icons/${imageName}`)} center/cover no-repeat`
case 'TILE':
return `${buildCssUrl(`/icons/${imageName}`)} repeat`
}
}
function convertDiamond(
fill: GradientPaint,
_width: number,
_height: number,
): string {
// Handle opacity & visibility:
if (!fill.visible) return 'transparent'
if (fill.opacity === 0) return 'transparent'
// 1. Map gradient stops with opacity
const stops = fill.gradientStops
.map((stop) => {
const colorWithOpacity = figma.util.rgba({
...stop.color,
a: stop.color.a * (fill.opacity ?? 1),
})
return `${optimizeHex(rgbaToHex(colorWithOpacity))} ${fmtPct(stop.position * 50)}%`
})
.join(', ')
// 2. Create 4 linear gradients for diamond effect
// Each gradient goes from corner to center
const gradients = [
`linear-gradient(to bottom right, ${stops}) bottom right / 50.1% 50.1% no-repeat`,
`linear-gradient(to bottom left, ${stops}) bottom left / 50.1% 50.1% no-repeat`,
`linear-gradient(to top left, ${stops}) top left / 50.1% 50.1% no-repeat`,
`linear-gradient(to top right, ${stops}) top right / 50.1% 50.1% no-repeat`,
]
// 3. Combine all gradients
return gradients.join(', ')
}
function convertAngular(
fill: GradientPaint,
width: number,
height: number,
): string {
// Handle opacity & visibility:
if (!fill.visible) return 'transparent'
if (fill.opacity === 0) return 'transparent'
// 1. Calculate actual center and start angle from gradient transform
const { center, startAngle } = _calculateAngularPositions(
fill.gradientTransform,
width,
height,
)
// 2. Convert center to percentage values
const centerX = fmtPct((center.x / width) * 100)
const centerY = fmtPct((center.y / height) * 100)
// 3. Map gradient stops with opacity
const stops = fill.gradientStops
.map((stop) => {
const colorWithOpacity = figma.util.rgba({
...stop.color,
a: stop.color.a * (fill.opacity ?? 1),
})
return `${optimizeHex(rgbaToHex(colorWithOpacity))} ${fmtPct(stop.position * 100)}%`
})
.join(', ')
// 4. Generate CSS conic gradient string with calculated start angle
return `conic-gradient(from ${fmtPct(startAngle)}deg at ${centerX}% ${centerY}%, ${stops})`
}
function convertRadial(
fill: GradientPaint,
width: number,
height: number,
): string {
// Handle opacity & visibility:
if (!fill.visible) return 'transparent'
if (fill.opacity === 0) return 'transparent'
// 1. Calculate actual center and radius from gradient transform
const { center, radiusW, radiusH } = _calculateRadialPositions(
fill.gradientTransform,
width,
height,
)
// 2. Convert center to percentage values
const centerX = fmtPct((center.x / width) * 100)
const centerY = fmtPct((center.y / height) * 100)
// 3. Calculate radius percentages for width and height separately
const radiusPercentW = fmtPct((radiusW / width) * 100)
const radiusPercentH = fmtPct((radiusH / height) * 100)
// 4. Map gradient stops with opacity
const stops = fill.gradientStops
.map((stop) => {
const colorWithOpacity = figma.util.rgba({
...stop.color,
a: stop.color.a * (fill.opacity ?? 1),
})
return `${optimizeHex(rgbaToHex(colorWithOpacity))} ${fmtPct(stop.position * 100)}%`
})
.join(', ')
// 5. Generate CSS radial gradient string
return `radial-gradient(${radiusPercentW}% ${radiusPercentH}% at ${centerX}% ${centerY}%, ${stops})`
}
async function convertPattern(fill: PatternPaint): Promise<string> {
const node = await figma.getNodeByIdAsync(fill.sourceNodeId)
const imageExtension = node ? checkAssetNode(node as SceneNode) : null
const imageName = node?.name ?? 'pattern'
const horizontalPosition = convertPosition(
fill.horizontalAlignment,
fill.spacing.x,
{
START: 'left',
CENTER: 'center',
END: 'right',
},
)
const verticalPosition = convertPosition(
'verticalAlignment' in fill
? (
fill as PatternPaint & {
verticalAlignment: 'START' | 'CENTER' | 'END'
}
).verticalAlignment
: 'START',
fill.spacing.y,
{
START: 'top',
CENTER: 'center',
END: 'bottom',
},
)
const position = [horizontalPosition, verticalPosition]
.filter(Boolean)
.join(' ')
const url = buildCssUrl(`/icons/${imageName}.${imageExtension}`)
return `${url}${position ? ` ${position}` : ''} repeat`
}
function convertPosition(
horizontalAlignment: 'START' | 'CENTER' | 'END',
spacing: number,
alignmentMap: Record<'START' | 'CENTER' | 'END', string>,
): string | null {
if (spacing === 0 && horizontalAlignment === 'START') {
return null
}
return `${alignmentMap[horizontalAlignment]} ${fmtPct(spacing * 100)}%`
}
async function convertSolidLinearGradient(fill: SolidPaint): Promise<string> {
if (fill.opacity === 0) return 'transparent'
const color = await solidToString(fill)
return `linear-gradient(${color}, ${color})`
}
function convertGradientLinear(
gradientData: GradientPaint,
width: number,
height: number,
): string | null {
// Handle opacity & visibility:
if (!gradientData.visible) return null
if (gradientData.opacity === 0) return 'transparent'
// 1. Calculate actual start and end points of Figma gradient
const { start, end } = _calculateActualPositions(
gradientData.gradientTransform,
width,
height,
)
// 2. Calculate element center point
const center = { x: width / 2, y: height / 2 }
// 3. Calculate gradient angle
// Figma: right is 0 degrees, increases clockwise
const figmaAngle = _calculateAngle(start, end)
// 3.1 Convert Figma angle to CSS angle
// CSS: up is 0 degrees, increases clockwise
// Conversion formula: cssAngle = (figmaAngle + 90) % 360
const cssAngle = Math.round((figmaAngle - 180) % 360)
// 4. Calculate CSS gradient length
const gradientLength = _calculateGradientLength(width, height, cssAngle)
const gradientLengthHalf = gradientLength / 2
// 5. Calculate CSS gradient start and end points (based on center point)
const { cssStart, cssEnd } = _calculateCSSStartEnd(
center,
cssAngle,
gradientLengthHalf,
)
// 6. Map Figma gradient stops to CSS space
const stops = _mapGradientStops(
gradientData.gradientStops,
start,
end,
cssStart,
cssEnd,
gradientData.opacity,
)
// 7. Generate CSS linear gradient string
return `linear-gradient(${cssAngle}deg, ${stops
.map(
(stop) =>
`${optimizeHex(rgbaToHex(stop.color))} ${fmtPct(stop.position * 100)}%`,
)
.join(', ')})`
}
function _calculateActualPositions(
gradientTransform: number[][],
width: number,
height: number,
) {
const matrixInverse = _inverseMatrix(gradientTransform)
// Transform start and end points from gradient space to standard space
// In gradient space, start and end points are [0, 0.5] [1, 0.5]
// In standard space, start and end points should be transformed based on 0 degrees
const normalizedStart = _applyMatrixToPoint(matrixInverse, [0, 0.5])
const normalizedEnd = _applyMatrixToPoint(matrixInverse, [1, 0.5])
// Convert start and end points from standard space to pixel units
// Should be transformed based on 0 degrees in Figma space
return {
start: { x: normalizedStart.x * width, y: normalizedStart.y * height },
end: { x: normalizedEnd.x * width, y: normalizedEnd.y * height },
}
}
function _calculateAngle(startPoint: Point, endPoint: Point): number {
const deltaX = endPoint.x - startPoint.x
const deltaY = endPoint.y - startPoint.y
// Calculate angle using atan2 function (in radians)
// atan2 returns values between -π and π
let angle = Math.atan2(deltaY, deltaX)
// Convert radians to degrees
angle = angle * (180 / Math.PI)
// Convert to Figma's angle system
angle = (angle - 90) % 360
// Convert negative angles to positive (e.g., -90 degrees -> 270 degrees)
return angle < 0 ? angle + 360 : angle
}
function _calculateGradientLength(
width: number,
height: number,
angleDegrees: number,
): number {
const angleRadians = (angleDegrees * Math.PI) / 180
// Calculate diagonal length (to ensure gradient fully covers the element)
return (
Math.abs(width * Math.sin(angleRadians)) +
Math.abs(height * Math.cos(angleRadians))
)
}
function _calculateCSSStartEnd(
centerPoint: Point,
cssAngle: number,
gradientLengthHalf: number,
) {
// Convert CSS angle to radians (up is 0 degrees, increases clockwise)
const cssAngleRadians = (cssAngle - 90) * (Math.PI / 180)
return {
cssStart: {
x: centerPoint.x - gradientLengthHalf * Math.cos(cssAngleRadians),
y: centerPoint.y - gradientLengthHalf * Math.sin(cssAngleRadians),
},
cssEnd: {
x: centerPoint.x + gradientLengthHalf * Math.cos(cssAngleRadians),
y: centerPoint.y + gradientLengthHalf * Math.sin(cssAngleRadians),
},
}
}
function _mapGradientStops(
stops: readonly ColorStop[],
figmaStartPoint: Point,
figmaEndPoint: Point,
cssStartPoint: Point,
cssEndPoint: Point,
opacity: number = 1,
) {
const figmaVector = {
x: figmaEndPoint.x - figmaStartPoint.x,
y: figmaEndPoint.y - figmaStartPoint.y,
}
const cssVector = {
x: cssEndPoint.x - cssStartPoint.x,
y: cssEndPoint.y - cssStartPoint.y,
}
const cssLengthSquared = cssVector.x ** 2 + cssVector.y ** 2
return stops.map((stop) => {
// Calculate actual pixel position of stop in Figma space (offset)
const offsetX = figmaStartPoint.x + figmaVector.x * stop.position
const offsetY = figmaStartPoint.y + figmaVector.y * stop.position
// Compute signed relative position along CSS gradient line (can be <0 or >1)
// t = dot(P - start, (end - start)) / |end - start|^2
const pointFromStart = {
x: offsetX - cssStartPoint.x,
y: offsetY - cssStartPoint.y,
}
const dot = pointFromStart.x * cssVector.x + pointFromStart.y * cssVector.y
const relativePosition = cssLengthSquared === 0 ? 0 : dot / cssLengthSquared
// Apply gradient opacity to the color stop
const colorWithOpacity = figma.util.rgba({
...stop.color,
a: stop.color.a * opacity,
})
return {
position: relativePosition,
color: colorWithOpacity,
}
})
}
function _inverseMatrix(matrix: number[][]): number[][] {
const [a, b, c] = matrix[0]
const [d, e, f] = matrix[1]
const determinant = a * e - b * d
return [
[e / determinant, -b / determinant, (b * f - c * e) / determinant],
[-d / determinant, a / determinant, (c * d - a * f) / determinant],
]
}
function _applyMatrixToPoint(matrix: number[][], point: number[]): Point {
return {
x: matrix[0][0] * point[0] + matrix[0][1] * point[1] + matrix[0][2],
y: matrix[1][0] * point[0] + matrix[1][1] * point[1] + matrix[1][2],
}
}
function _calculateRadialPositions(
gradientTransform: number[][],
width: number,
height: number,
) {
const matrixInverse = _inverseMatrix(gradientTransform)
// In radial gradient space, center is at [0.5, 0.5] and radius extends to [1, 0.5]
const normalizedCenter = _applyMatrixToPoint(matrixInverse, [0.5, 0.5])
const normalizedRadius = _applyMatrixToPoint(matrixInverse, [1, 1])
// Convert to pixel coordinates
const center = {
x: normalizedCenter.x * width,
y: normalizedCenter.y * height,
}
// Calculate radius point in pixel coordinates
const radiusPoint = {
x: normalizedRadius.x * width,
y: normalizedRadius.y * height,
}
// Calculate radius as distance from center to the radius point (for backward compatibility)
const radius = Math.sqrt(
(radiusPoint.x - center.x) ** 2 + (radiusPoint.y - center.y) ** 2,
)
// Calculate separate radius for width and height in normalized space
// The difference in normalized space represents the actual radius ratio
const normalizedRadiusW = Math.abs(normalizedRadius.x - normalizedCenter.x)
const normalizedRadiusH = Math.abs(normalizedRadius.y - normalizedCenter.y)
// Convert normalized radius to pixel coordinates
const radiusW = normalizedRadiusW * width
const radiusH = normalizedRadiusH * height
return { center, radius, radiusW, radiusH }
}
function _calculateAngularPositions(
gradientTransform: number[][],
width: number,
height: number,
) {
const matrixInverse = _inverseMatrix(gradientTransform)
// In angular gradient space, center is at [0.5, 0.5]
const normalizedCenter = _applyMatrixToPoint(matrixInverse, [0.5, 0.5])
// Calculate start angle by finding the direction from center to the start point
// In angular gradient space, start point is at [1, 0.5] (right side)
const normalizedStart = _applyMatrixToPoint(matrixInverse, [1, 0.5])
// Convert to pixel coordinates
const center = {
x: normalizedCenter.x * width,
y: normalizedCenter.y * height,
}
const startPoint = {
x: normalizedStart.x * width,
y: normalizedStart.y * height,
}
// Calculate angle from center to start point
const deltaX = startPoint.x - center.x
const deltaY = startPoint.y - center.y
let startAngle = Math.atan2(deltaY, deltaX) * (180 / Math.PI)
// Convert to CSS angle (CSS starts from top, increases clockwise)
// Figma starts from right, increases clockwise
startAngle = (startAngle + 90) % 360
if (startAngle < 0) startAngle += 360
return { center, startAngle }
}