@@ -11,10 +11,7 @@ export const swiftUISolidColor = (fill: Paint): string => {
11
11
return "" ;
12
12
} ;
13
13
14
- /**
15
- * Retrieve the SOLID color for SwiftUI when existent, otherwise ""
16
- */
17
- export const swiftuiColorFromFills = (
14
+ export const swiftuiSolidColor = (
18
15
fills : ReadonlyArray < Paint > | PluginAPI [ "mixed" ]
19
16
) : string => {
20
17
const fill = retrieveTopFill ( fills ) ;
@@ -24,9 +21,8 @@ export const swiftuiColorFromFills = (
24
21
const opacity = fill . opacity ?? 1.0 ;
25
22
return swiftuiColor ( fill . color , opacity ) ;
26
23
} else if ( fill ?. type === "GRADIENT_LINEAR" ) {
27
- return swiftuiGradient ( fill ) ;
24
+ return swiftuiRGBAColor ( fill . gradientStops [ 0 ] . color ) ;
28
25
} else if ( fill ?. type === "IMAGE" ) {
29
- // placeholder for the image. Apparently SwiftUI doesn't support Image.network(...).
30
26
return swiftuiColor (
31
27
{
32
28
r : 0.5 ,
@@ -40,6 +36,27 @@ export const swiftuiColorFromFills = (
40
36
return "" ;
41
37
} ;
42
38
39
+ export const swiftuiBackground = (
40
+ node : SceneNode ,
41
+ fills : ReadonlyArray < Paint > | PluginAPI [ "mixed" ]
42
+ ) : string => {
43
+ const fill = retrieveTopFill ( fills ) ;
44
+
45
+ if ( fill && fill . type === "SOLID" ) {
46
+ // opacity should only be null on set, not on get. But better be prevented.
47
+ const opacity = fill . opacity ?? 1.0 ;
48
+ return swiftuiColor ( fill . color , opacity ) ;
49
+ } else if ( fill ?. type === "GRADIENT_LINEAR" ) {
50
+ return swiftuiGradient ( fill ) ;
51
+ } else if ( fill ?. type === "IMAGE" ) {
52
+ return `AsyncImage(url: URL(string: "https://via.placeholder.com/${ node . width . toFixed (
53
+ 0
54
+ ) } x${ node . height . toFixed ( 0 ) } "))`;
55
+ }
56
+
57
+ return "" ;
58
+ } ;
59
+
43
60
export const swiftuiGradient = ( fill : GradientPaint ) : string => {
44
61
const direction = gradientDirection ( gradientAngle ( fill ) ) ;
45
62
@@ -74,6 +91,8 @@ const gradientDirection = (angle: number): string => {
74
91
}
75
92
} ;
76
93
94
+ export const swiftuiRGBAColor = ( color : RGBA ) => swiftuiColor ( color , color . a ) ;
95
+
77
96
export const swiftuiColor = ( color : RGB , opacity : number ) : string => {
78
97
// Using Color.black.opacity() is not reccomended, as per:
79
98
// https://stackoverflow.com/a/56824114/4418073
0 commit comments