Skip to content

Commit 6ca272a

Browse files
committed
feat: 添加渐变模型
1 parent a1d55f3 commit 6ca272a

File tree

2 files changed

+172
-47
lines changed

2 files changed

+172
-47
lines changed

src/model/Style/Gradient.ts

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,74 @@
1-
import FileFormat from '@sketch-hq/sketch-file-format-ts';
2-
import { defaultContextSettings } from '../utils';
3-
import Color, { ColorType } from './Color';
1+
import Color, { ColorParam } from './Color';
42
import StyleBase from './Base';
3+
import { SketchFormat, CGPoint } from '../../index';
54

65
export interface GradientProps {
7-
color?: ColorType;
8-
blurRadius?: number;
9-
offsetX?: number;
10-
offsetY?: number;
11-
spread?: number;
12-
contextSettings?: FileFormat.GraphicsContextSettings;
6+
type?: SketchFormat.GradientType;
7+
to?: CGPoint;
8+
from?: CGPoint;
9+
stops?: ColorParam[];
1310
name?: string;
1411
}
1512

13+
/**
14+
* 渐变对象
15+
**/
1616
class Gradient extends StyleBase {
1717
constructor(props: GradientProps) {
1818
super();
1919

20-
const { blurRadius, color, offsetX, offsetY, contextSettings } = props;
21-
this.color = new Color(color);
22-
this.blurRadius = blurRadius;
23-
this.offsetX = offsetX;
24-
this.offsetY = offsetY;
25-
this.contextSettings = contextSettings;
26-
this.name = `${this.color.hex} ${this.offsetX}px ${this.offsetY}px ${this.blurRadius}px`;
20+
const { from, to, stops, type, name } = props;
21+
22+
this.from = from;
23+
this.to = to;
24+
this.stops = stops.map((color) => new Color(color));
25+
this.type = type;
26+
this.name = name || 'gradient';
2727
}
2828

2929
/**
30-
* 颜色
31-
*/
32-
color: Color;
33-
/**
34-
* 模糊半径
35-
*/
36-
blurRadius: number;
37-
/**
38-
* X 轴偏移
39-
*/
40-
offsetX: number;
41-
/**
42-
* Y 轴偏移
30+
* 起点
4331
*/
44-
offsetY: number;
32+
from: CGPoint;
4533
/**
46-
* 扩散效果
34+
* 色彩节点
4735
*/
48-
spread: number;
36+
stops: Color[];
4937
/**
50-
* 渲染上下文
38+
* 终点
5139
*/
52-
contextSettings: FileFormat.GraphicsContextSettings;
40+
to: CGPoint;
41+
5342
/**
54-
* 是否启用
55-
*/
56-
isEnabled: boolean;
43+
* 渐变类型
44+
**/
45+
type: SketchFormat.GradientType;
5746

5847
/**
5948
* 转为 Sketch JSON 对象
6049
* @returns {SketchFormat.Gradient}
6150
*/
62-
toSketchJSON = (): FileFormat.Gradient => {
63-
const { offsetY, offsetX, blurRadius, color, spread } = this;
51+
toSketchJSON = (): SketchFormat.Gradient => {
52+
const { from, to, stops } = this;
53+
6454
return {
65-
_class: FileFormat.ClassValue.Gradient,
66-
isEnabled: true,
67-
blurRadius,
68-
color: color.toSketchJson(),
69-
contextSettings: defaultContextSettings,
70-
offsetX,
71-
offsetY,
72-
spread,
55+
_class: SketchFormat.ClassValue.Gradient,
56+
elipseLength: 0, // 这个字段应该是废弃字段
57+
from: `{${from.x}, ${from.y}}`,
58+
gradientType: this.type,
59+
to: `{${to.x}, ${to.y}}`,
60+
stops: stops.map(this.getSketchStop),
7361
};
7462
};
63+
64+
/**
65+
* 将 stop 数组转换为 Sketch 使用的对象
66+
**/
67+
getSketchStop = (color: Color, index): SketchFormat.GradientStop => ({
68+
_class: 'gradientStop',
69+
color: color.toSketchJson(),
70+
position: index,
71+
});
7572
}
7673

7774
export default Gradient;

test/target/style/gradient.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
export const lineGradient = {
2+
_class: 'gradient',
3+
elipseLength: 0,
4+
from: '{0, 0.50000000000000011}',
5+
gradientType: 0,
6+
to: '{1, 0.50000000000000056}',
7+
stops: [
8+
{
9+
_class: 'gradientStop',
10+
position: 0,
11+
color: {
12+
_class: 'color',
13+
alpha: 1,
14+
blue: 0.9326139616824488,
15+
green: 0.9326139616824488,
16+
red: 0.9326139616824488,
17+
},
18+
},
19+
{
20+
_class: 'gradientStop',
21+
position: 1,
22+
color: {
23+
_class: 'color',
24+
alpha: 1,
25+
blue: 0,
26+
green: 0,
27+
red: 0,
28+
},
29+
},
30+
{
31+
_class: 'gradientStop',
32+
position: 1,
33+
color: {
34+
_class: 'color',
35+
alpha: 1,
36+
blue: 0,
37+
green: 0,
38+
red: 1,
39+
},
40+
},
41+
],
42+
};
43+
44+
export const radiusGradient = {
45+
_class: 'gradient',
46+
elipseLength: 0,
47+
from: '{0, 0.50000000000000011}',
48+
gradientType: 1,
49+
to: '{0.7324673827341921, 0.50000000000000744}',
50+
stops: [
51+
{
52+
_class: 'gradientStop',
53+
position: 0,
54+
color: {
55+
_class: 'color',
56+
alpha: 1,
57+
blue: 0.9326139616824488,
58+
green: 0.9326139616824488,
59+
red: 0.9326139616824488,
60+
},
61+
},
62+
{
63+
_class: 'gradientStop',
64+
position: 0.5513275786713286,
65+
color: {
66+
_class: 'color',
67+
alpha: 1,
68+
blue: 0,
69+
green: 0,
70+
red: 0,
71+
},
72+
},
73+
{
74+
_class: 'gradientStop',
75+
position: 1,
76+
color: {
77+
_class: 'color',
78+
alpha: 1,
79+
blue: 0,
80+
green: 0,
81+
red: 1,
82+
},
83+
},
84+
],
85+
};
86+
87+
export const cirlceGradient = {
88+
_class: 'gradient',
89+
elipseLength: 0,
90+
from: '{0, 0.50000000000000011}',
91+
gradientType: 2,
92+
to: '{0.7324673827341921, 0.50000000000000744}',
93+
stops: [
94+
{
95+
_class: 'gradientStop',
96+
position: 0,
97+
color: {
98+
_class: 'color',
99+
alpha: 1,
100+
blue: 0.9326139616824488,
101+
green: 0.9326139616824488,
102+
red: 0.9326139616824488,
103+
},
104+
},
105+
{
106+
_class: 'gradientStop',
107+
position: 0.5513275786713286,
108+
color: {
109+
_class: 'color',
110+
alpha: 1,
111+
blue: 0,
112+
green: 0,
113+
red: 0,
114+
},
115+
},
116+
{
117+
_class: 'gradientStop',
118+
position: 1,
119+
color: {
120+
_class: 'color',
121+
alpha: 1,
122+
blue: 0,
123+
green: 0,
124+
red: 1,
125+
},
126+
},
127+
],
128+
};

0 commit comments

Comments
 (0)