UNPKG

9.05 kBJavaScriptView Raw
1import { LinearGradient } from './linear-gradient';
2// Types.
3import { Color } from '../../color';
4export class Background {
5 constructor() {
6 this.borderTopWidth = 0;
7 this.borderRightWidth = 0;
8 this.borderBottomWidth = 0;
9 this.borderLeftWidth = 0;
10 this.borderTopLeftRadius = 0;
11 this.borderTopRightRadius = 0;
12 this.borderBottomLeftRadius = 0;
13 this.borderBottomRightRadius = 0;
14 this.clearFlags = 0 /* BackgroundClearFlags.NONE */;
15 }
16 clone() {
17 const clone = new Background();
18 clone.color = this.color;
19 clone.image = this.image;
20 clone.repeat = this.repeat;
21 clone.position = this.position;
22 clone.size = this.size;
23 clone.borderTopColor = this.borderTopColor;
24 clone.borderRightColor = this.borderRightColor;
25 clone.borderBottomColor = this.borderBottomColor;
26 clone.borderLeftColor = this.borderLeftColor;
27 clone.borderTopWidth = this.borderTopWidth;
28 clone.borderRightWidth = this.borderRightWidth;
29 clone.borderBottomWidth = this.borderBottomWidth;
30 clone.borderLeftWidth = this.borderLeftWidth;
31 clone.borderTopLeftRadius = this.borderTopLeftRadius;
32 clone.borderTopRightRadius = this.borderTopRightRadius;
33 clone.borderBottomRightRadius = this.borderBottomRightRadius;
34 clone.borderBottomLeftRadius = this.borderBottomLeftRadius;
35 clone.clipPath = this.clipPath;
36 clone.boxShadow = this.boxShadow;
37 clone.clearFlags = this.clearFlags;
38 return clone;
39 }
40 withColor(value) {
41 const clone = this.clone();
42 clone.color = value;
43 if (!value) {
44 clone.clearFlags |= 1 /* BackgroundClearFlags.CLEAR_BACKGROUND_COLOR */;
45 }
46 return clone;
47 }
48 withImage(value) {
49 const clone = this.clone();
50 clone.image = value;
51 return clone;
52 }
53 withRepeat(value) {
54 const clone = this.clone();
55 clone.repeat = value;
56 return clone;
57 }
58 withPosition(value) {
59 const clone = this.clone();
60 clone.position = value;
61 return clone;
62 }
63 withSize(value) {
64 const clone = this.clone();
65 clone.size = value;
66 return clone;
67 }
68 withBorderTopColor(value) {
69 const clone = this.clone();
70 clone.borderTopColor = value;
71 return clone;
72 }
73 withBorderRightColor(value) {
74 const clone = this.clone();
75 clone.borderRightColor = value;
76 return clone;
77 }
78 withBorderBottomColor(value) {
79 const clone = this.clone();
80 clone.borderBottomColor = value;
81 return clone;
82 }
83 withBorderLeftColor(value) {
84 const clone = this.clone();
85 clone.borderLeftColor = value;
86 return clone;
87 }
88 withBorderTopWidth(value) {
89 const clone = this.clone();
90 clone.borderTopWidth = value;
91 return clone;
92 }
93 withBorderRightWidth(value) {
94 const clone = this.clone();
95 clone.borderRightWidth = value;
96 return clone;
97 }
98 withBorderBottomWidth(value) {
99 const clone = this.clone();
100 clone.borderBottomWidth = value;
101 return clone;
102 }
103 withBorderLeftWidth(value) {
104 const clone = this.clone();
105 clone.borderLeftWidth = value;
106 return clone;
107 }
108 withBorderTopLeftRadius(value) {
109 const clone = this.clone();
110 clone.borderTopLeftRadius = value;
111 return clone;
112 }
113 withBorderTopRightRadius(value) {
114 const clone = this.clone();
115 clone.borderTopRightRadius = value;
116 return clone;
117 }
118 withBorderBottomRightRadius(value) {
119 const clone = this.clone();
120 clone.borderBottomRightRadius = value;
121 return clone;
122 }
123 withBorderBottomLeftRadius(value) {
124 const clone = this.clone();
125 clone.borderBottomLeftRadius = value;
126 return clone;
127 }
128 withClipPath(value) {
129 const clone = this.clone();
130 clone.clipPath = value;
131 return clone;
132 }
133 withBoxShadow(value) {
134 const clone = this.clone();
135 clone.boxShadow = value;
136 if (!value) {
137 clone.clearFlags |= 2 /* BackgroundClearFlags.CLEAR_BOX_SHADOW */;
138 }
139 return clone;
140 }
141 isEmpty() {
142 return !this.color && !this.image && !this.hasBorderWidth() && !this.hasBorderRadius() && !this.clipPath;
143 }
144 static equals(value1, value2) {
145 // both values are falsy
146 if (!value1 && !value2) {
147 return true;
148 }
149 // only one is falsy
150 if (!value1 || !value2) {
151 return false;
152 }
153 let imagesEqual = false;
154 if (value1 instanceof LinearGradient && value2 instanceof LinearGradient) {
155 imagesEqual = LinearGradient.equals(value1, value2);
156 }
157 else {
158 imagesEqual = value1.image === value2.image;
159 }
160 return (Color.equals(value1.color, value2.color) &&
161 imagesEqual &&
162 value1.position === value2.position &&
163 value1.repeat === value2.repeat &&
164 value1.size === value2.size &&
165 Color.equals(value1.borderTopColor, value2.borderTopColor) &&
166 Color.equals(value1.borderRightColor, value2.borderRightColor) &&
167 Color.equals(value1.borderBottomColor, value2.borderBottomColor) &&
168 Color.equals(value1.borderLeftColor, value2.borderLeftColor) &&
169 value1.borderTopWidth === value2.borderTopWidth &&
170 value1.borderRightWidth === value2.borderRightWidth &&
171 value1.borderBottomWidth === value2.borderBottomWidth &&
172 value1.borderLeftWidth === value2.borderLeftWidth &&
173 value1.borderTopLeftRadius === value2.borderTopLeftRadius &&
174 value1.borderTopRightRadius === value2.borderTopRightRadius &&
175 value1.borderBottomRightRadius === value2.borderBottomRightRadius &&
176 value1.borderBottomLeftRadius === value2.borderBottomLeftRadius &&
177 value1.clipPath === value2.clipPath
178 // && value1.clearFlags === value2.clearFlags
179 );
180 }
181 hasBorderColor() {
182 return !!this.borderTopColor || !!this.borderRightColor || !!this.borderBottomColor || !!this.borderLeftColor;
183 }
184 hasBorderWidth() {
185 return this.borderTopWidth > 0 || this.borderRightWidth > 0 || this.borderBottomWidth > 0 || this.borderLeftWidth > 0;
186 }
187 hasBorderRadius() {
188 return this.borderTopLeftRadius > 0 || this.borderTopRightRadius > 0 || this.borderBottomRightRadius > 0 || this.borderBottomLeftRadius > 0;
189 }
190 hasBorder() {
191 return (this.hasBorderColor() && this.hasBorderWidth()) || this.hasBorderRadius();
192 }
193 hasUniformBorderColor() {
194 return Color.equals(this.borderTopColor, this.borderRightColor) && Color.equals(this.borderTopColor, this.borderBottomColor) && Color.equals(this.borderTopColor, this.borderLeftColor);
195 }
196 hasUniformBorderWidth() {
197 return this.borderTopWidth === this.borderRightWidth && this.borderTopWidth === this.borderBottomWidth && this.borderTopWidth === this.borderLeftWidth;
198 }
199 hasUniformBorderRadius() {
200 return this.borderTopLeftRadius === this.borderTopRightRadius && this.borderTopLeftRadius === this.borderBottomRightRadius && this.borderTopLeftRadius === this.borderBottomLeftRadius;
201 }
202 hasUniformBorder() {
203 return this.hasUniformBorderColor() && this.hasUniformBorderWidth() && this.hasUniformBorderRadius();
204 }
205 getUniformBorderColor() {
206 if (this.hasUniformBorderColor()) {
207 return this.borderTopColor;
208 }
209 return undefined;
210 }
211 getUniformBorderWidth() {
212 if (this.hasUniformBorderWidth()) {
213 return this.borderTopWidth;
214 }
215 return 0;
216 }
217 getUniformBorderRadius() {
218 if (this.hasUniformBorderRadius()) {
219 return this.borderTopLeftRadius;
220 }
221 return 0;
222 }
223 hasBoxShadow() {
224 return !!this.boxShadow;
225 }
226 getBoxShadow() {
227 return this.boxShadow;
228 }
229 toString() {
230 return `isEmpty: ${this.isEmpty()}; color: ${this.color}; image: ${this.image}; repeat: ${this.repeat}; position: ${this.position}; size: ${this.size}; borderTopColor: ${this.borderTopColor}; borderRightColor: ${this.borderRightColor}; borderBottomColor: ${this.borderBottomColor}; borderLeftColor: ${this.borderLeftColor}; borderTopWidth: ${this.borderTopWidth}; borderRightWidth: ${this.borderRightWidth}; borderBottomWidth: ${this.borderBottomWidth}; borderLeftWidth: ${this.borderLeftWidth}; borderTopLeftRadius: ${this.borderTopLeftRadius}; borderTopRightRadius: ${this.borderTopRightRadius}; borderBottomRightRadius: ${this.borderBottomRightRadius}; borderBottomLeftRadius: ${this.borderBottomLeftRadius}; clipPath: ${this.clipPath};`;
231 }
232}
233Background.default = new Background();
234//# sourceMappingURL=background-common.js.map
\No newline at end of file