Skip to content

Commit 2d2018e

Browse files
Merge pull request #76 from gridaco/support-radial-gradient
Support `radial-gradient` (1/2)
2 parents 3bc5908 + 98428b7 commit 2d2018e

10 files changed

Lines changed: 149 additions & 24 deletions

File tree

docs/figma-gradient.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Figma Gradient
2+
3+
- [figma paint - gradient paint](https://www.figma.com/plugin-docs/api/Paint/#gradientpaint)
4+
- [css gradient](https://developer.mozilla.org/en-US/docs/Web/CSS/gradient)
5+
6+
## gradient type
7+
8+
- linear gradient
9+
- radial gradient
10+
- angular gradient
11+
- diamond gradient
12+
13+
### linear gradient
14+
15+
**css**
16+
17+
- [linear-gradient()](<https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient()>)
18+
19+
`linear-gradient()` css function
20+
21+
```css
22+
background: linear-gradient(45deg, blue, red);
23+
```
24+
25+
> **syntax**
26+
>
27+
> wip
28+
29+
**flutter**
30+
31+
- [LinearGradient()](https://api.flutter.dev/flutter/painting/LinearGradient-class.html)
32+
33+
### radial gradient
34+
35+
**css**
36+
37+
- [radial-gradient()](<https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/radial-gradient()>)
38+
39+
```css
40+
background: radial-gradient(red, blue);
41+
```
42+
43+
**flutter**
44+
45+
- [RadialGradient()](https://api.flutter.dev/flutter/painting/RadialGradient-class.html)
46+
47+
> **gradient paint opacity**
48+
>
49+
> gradient color's opacity \* fill's opacity
50+
51+
> **gradient paint degree**
52+
> WIP

externals/reflect-core

packages/builder-css-styles/background/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { color } from "../color";
77
import { array } from "@reflect-ui/uiutils";
88
import { Color, GradientType } from "@reflect-ui/core";
99
import { linearGradient } from "../linear-gradient";
10+
import { radialGradient } from "../radial-gradient";
1011

1112
/**
1213
* @todo - not implemented
@@ -29,9 +30,14 @@ export function background(bg: Background): CSSProperties {
2930
background: linearGradient(_primary),
3031
};
3132
}
33+
case GradientType.RADIAL: {
34+
return {
35+
background: radialGradient(_primary),
36+
};
37+
}
3238
default:
3339
console.error(
34-
"other than linear-gradient is not supported yet.",
40+
"other than linear, radial gradient is not supported yet.",
3541
_primary
3642
);
3743
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { RadialGradientManifest } from "@reflect-ui/core";
2+
import { CSSProperty } from "@coli.codes/css";
3+
import { color } from "../color";
4+
5+
/**
6+
* https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/radial-gradient()
7+
*
8+
* @param g
9+
* @returns
10+
*/
11+
export function radialGradient(g: RadialGradientManifest): CSSProperty.Color {
12+
// TODO:
13+
// 1. center support
14+
// 2. radius support
15+
16+
const colors = g.colors.map(color).join(", ");
17+
return `radial-gradient(${colors})`;
18+
}

packages/designto-flutter/make/make-flutter-box-decoration.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { interpretImageFill } from "../interpreter/image.interpret";
66
import * as painting from "../painting";
77
import { makeColorFromRGBO } from "./make-flutter-color";
88
import { tokenizeBorder, tokenize_gradient } from "@designto/token";
9+
import { LinearGradient, RadialGradient } from "@reflect-ui/core";
910

1011
type DecorationBackgroundLike =
1112
| flutter.Color
@@ -106,16 +107,17 @@ export function makeBoxDecorationColorBg(
106107
switch (fill.type) {
107108
case "GRADIENT_ANGULAR":
108109
case "GRADIENT_DIAMOND":
109-
case "GRADIENT_RADIAL":
110110
// TODO: handle above gradient types (only linear is handled)
111-
console.log(
112-
"not handled: `GRADIENT_RADIAL` | `GRADIENT_DIAMOND` | `GRADIENT_ANGULAR`"
113-
);
111+
console.log("not handled: `GRADIENT_DIAMOND` | `GRADIENT_ANGULAR`");
114112
return undefined;
115113
case "GRADIENT_LINEAR":
116-
const g = tokenize_gradient(fill as Figma.GradientPaint);
117-
return painting.linearGradient(g);
114+
const lg = tokenize_gradient(fill as Figma.GradientPaint);
115+
return painting.linearGradient(lg as LinearGradient);
116+
case "GRADIENT_RADIAL":
117+
const rg = tokenize_gradient(fill as Figma.GradientPaint);
118+
return painting.radialGradient(rg as RadialGradient);
118119
case "SOLID":
120+
console.log("solid color");
119121
return makeColorFromRGBO(fill.color, opacity);
120122
default:
121123
throw `making colored box decoraton with fill type "${fill?.type}" is not allowed."`;

packages/designto-flutter/painting/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./painting-box-shape";
66
export * from "./painting-box-shadow";
77
export * from "./painting-border";
88
export * from "./painting-border-side";
9+
export * from "./painting-radial-gradient";
910
export * from "./painting-text-style";
1011
export * from "./painting-text-decoration";
1112
export * from "./painting-font-style";

packages/designto-flutter/painting/painting-box-decoration.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { BoxDecoration } from "@flutter-builder/flutter";
2-
import { Color } from "@reflect-ui/core";
2+
import { Color, Gradient, GradientType } from "@reflect-ui/core";
33
import { Background } from "@reflect-ui/core/lib/background";
44
import * as dartui from "../dart-ui";
5+
import { linearGradient } from "./painting-linear-gradient";
6+
import { radialGradient } from "./painting-radial-gradient";
57

68
function fromColor(color: Color): BoxDecoration {
79
return new BoxDecoration({
@@ -19,7 +21,7 @@ function fromBackground(b: Background): BoxDecoration {
1921
} else {
2022
switch (b.type) {
2123
case "gradient": {
22-
console.error("gradient bg not ready");
24+
return fromGradient(b as Gradient);
2325
break;
2426
}
2527
case "graphics": {
@@ -33,8 +35,32 @@ function fromBackground(b: Background): BoxDecoration {
3335
}
3436
}
3537

36-
function fromGradient(): BoxDecoration {
37-
throw "not ready.";
38+
function fromGradient(g: Gradient): BoxDecoration {
39+
switch (g?._type) {
40+
case GradientType.LINEAR: {
41+
return new BoxDecoration({
42+
gradient: linearGradient(g),
43+
});
44+
}
45+
case GradientType.RADIAL: {
46+
return new BoxDecoration({
47+
gradient: radialGradient(g),
48+
});
49+
}
50+
51+
// It is only used to make it safer in case of an emergency.
52+
case undefined: {
53+
return;
54+
}
55+
default: {
56+
// TODO: add;
57+
// GRADIENT_ANGULAR;
58+
// GRADIENT_DIAMOND;
59+
throw new Error(
60+
`Gradient type of "${g}" is not yet supported on flutter platform.`
61+
);
62+
}
63+
}
3864
}
3965

4066
function fromImage(): BoxDecoration {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { RadialGradient } from "@reflect-ui/core";
2+
import * as flutter from "@flutter-builder/flutter";
3+
import * as dartui from "../dart-ui";
4+
5+
export function radialGradient(g: RadialGradient): flutter.RadialGradient {
6+
return new flutter.RadialGradient({
7+
// TODO: add center
8+
// TODO: add radius
9+
colors: g.colors.map((c) => dartui.color(c)),
10+
stops: g.stops,
11+
});
12+
}

packages/designto-flutter/tokens-to-flutter-widget/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ function compose(
280280
..._deco_part_shape_and_border_radius,
281281
..._deco_part_bg,
282282
boxShadow: painting.boxShadow(widget.boxShadow),
283-
// TODO:
284-
// background:
283+
// TODO: background list
284+
// background: painting.linearGradient(widget.background.gradient),
285285
}),
286286
// key: _key,
287287
});

packages/designto-token/token-gradient/gradient.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Figma } from "@design-sdk/figma-types";
2-
import { Color, LinearGradient } from "@reflect-ui/core";
2+
import {
3+
Color,
4+
LinearGradient,
5+
Gradient,
6+
RadialGradient,
7+
} from "@reflect-ui/core";
38
import { color_utils } from "@design-sdk/core";
49
import { tokenize_gradient_direction_from_angle } from "../token-gradient";
510
import { roundNumber } from "@reflect-ui/uiutils";
611

7-
export function tokenize_gradient(
8-
gradient: Figma.GradientPaint
9-
): LinearGradient {
10-
// TODO Handle transform percisely.
12+
export function tokenize_gradient(gradient: Figma.GradientPaint): Gradient {
13+
// TODO: Handle transform percisely.
1114
// https://www.figma.com/plugin-docs/api/Transform/
1215
// https://www.mathworks.com/discovery/affine-transformation.html
1316
const direction = tokenize_gradient_direction_from_angle(
@@ -45,16 +48,21 @@ export function tokenize_gradient(
4548
stops: stopPoints,
4649
});
4750
case "GRADIENT_RADIAL":
48-
console.error("GRADIENT_RADIAL not handled");
49-
// TODO
50-
break;
51+
return new RadialGradient({
52+
center: direction.begin,
53+
colors: colors,
54+
stops: stopPoints,
55+
// TODO: support radius
56+
});
57+
58+
// TODO:
5159
case "GRADIENT_ANGULAR":
5260
console.error("GRADIENT_ANGULAR not handled");
53-
// TODO
61+
// TODO:
5462
break;
5563
case "GRADIENT_DIAMOND":
5664
console.error("GRADIENT_DIAMOND not handled");
57-
// TODO
65+
// TODO:
5866
break;
5967
}
6068
}

0 commit comments

Comments
 (0)