1# Color Effect
2
3
4## Color
5
6You can use the color gradient APIs to apply a background color gradient effect to a component.
7
8| API| Description|
9| -------- | -------- |
10| [linearGradient](../reference/apis-arkui/arkui-ts/ts-universal-attributes-gradient-color.md#lineargradient) | Applies a linear gradient to the component.|
11| [sweepGradient](../reference/apis-arkui/arkui-ts/ts-universal-attributes-gradient-color.md#sweepgradient) | Applies a sweep gradient effect to the component.|
12| [radialGradient](../reference/apis-arkui/arkui-ts/ts-universal-attributes-gradient-color.md#radialgradient) | Applies a radial gradient effect to the component.|
13
14
15## Applying Linear Gradient Effect
16
17
18```ts
19@Entry
20@Component
21struct LinearGradientDemo {
22  build() {
23    Grid() {
24      GridItem() {
25        Column() {
26          Text('angle: 180')
27            .fontSize(15)
28        }
29        .width(100)
30        .height(100)
31        .justifyContent(FlexAlign.Center)
32        .borderRadius(10)
33        .linearGradient({
34          // A positive value indicates a clockwise rotation from the origin, (0, 0). The default value of the linear gradient start angle is 180°.
35          colors: [
36            [0xf56c6c, 0.0], // Color and weight of color stop 1, corresponding to the start position of the component in the 180° direction.
37            [0xffffff, 1.0], // Color and weight of color stop 2, corresponding to the end position of the component in the 180° direction.
38          ]
39        })
40      }
41
42      GridItem() {
43        Column() {
44          Text('angle: 45')
45            .fontSize(15)
46        }
47        .width(100)
48        .height(100)
49        .justifyContent(FlexAlign.Center)
50        .borderRadius(10)
51        .linearGradient({
52          angle: 45, // Set the linear gradient start angle to 45°.
53          colors: [
54            [0xf56c6c, 0.0],
55            [0xffffff, 1.0],
56          ]
57        })
58      }
59
60      GridItem() {
61        Column() {
62          Text('repeat: true')
63            .fontSize(15)
64        }
65        .width(100)
66        .height(100)
67        .justifyContent(FlexAlign.Center)
68        .borderRadius(10)
69        .linearGradient({
70          repeating: true, // Repeat the gradients of the area from 0 to 0.3 in the area from 0.3 to 1.0.
71          colors: [
72            [0xf56c6c, 0.0],
73            [0xE6A23C, .3],
74          ]
75        })
76      }
77
78      GridItem() {
79        Column() {
80          Text('repeat: false')
81            .fontSize(15)
82        }
83        .width(100)
84        .height(100)
85        .justifyContent(FlexAlign.Center)
86        .borderRadius(10)
87        .linearGradient({
88          colors: [
89            [0xf56c6c, 0.0], // As repeating is not specified, the default value false is used. In this case, only the area from 0 to 0.3 in the component have the color gradient effect.
90            [0xE6A23C, .3],
91          ]
92        })
93      }
94    }
95    .columnsGap(10)
96    .rowsGap(10)
97    .columnsTemplate('1fr 1fr')
98    .rowsTemplate('1fr 1fr 1fr')
99    .width('100%')
100    .height('100%')
101  }
102}
103```
104
105![en-us_image_0000001641176829](figures/en-us_image_0000001641176829.png)
106
107
108## Applying Sweep Gradient Effect
109
110
111```ts
112@Entry
113@Component
114struct SweepGradientDemo {
115  build() {
116    Grid() {
117      GridItem() {
118        Column() {
119          Text('center: 50')
120            .fontSize(15)
121        }
122        .width(100)
123        .height(100)
124        .justifyContent(FlexAlign.Center)
125        .borderRadius(10)
126        .sweepGradient({
127          center: [50, 50], // Center point of the sweep gradient.
128          start: 0, // Start point of the sweep gradient.
129          end: 360, // End point of the sweep gradient.
130          repeating: true, // The gradients are repeated.
131          colors: [
132          // Based on the center point, start point, and end point settings,
133          // the color changes from color stop 1 to color stop 2 in the area within angles from 0 to 0.125;
134          // the color changes from color stop 2 to color stop 3 in the area within angles from 0.125 to 0.25;
135          // the color gradients of the area within angles from 0 to 0.25 are repeated in the area within angles from 0.25 to 1.
136            [0xf56c6c, 0], // Color and weight of color stop 1. The corresponding angle is 0° (0 x 360°), and the corner is the center point.
137            [0xffffff, 0.125], // Color and weight of color stop 2.
138            [0x409EFF, 0.25] // Color and weight of color stop 3.
139          ]
140        })
141      }
142
143      GridItem() {
144        Column() {
145          Text('center: 0')
146            .fontSize(15)
147        }
148        .width(100)
149        .height(100)
150        .justifyContent(FlexAlign.Center)
151        .borderRadius(10)
152        .sweepGradient({
153          center: [0, 0], // Center point of the sweep gradient, which is the coordinate of the upper left corner of the component in this example.
154          start: 0,
155          end: 360,
156          repeating: true,
157          colors: [
158          // In the current component, the sweep gradient center is the upper left corner of the component. Therefore, the angle range from color stop 1 to color stop 3 can cover the entire component.
159            [0xf56c6c, 0], // Color and weight of color stop 1. The corresponding angle is 0° (0 x 360°)
160            [0xffffff, 0.125], // Color and weight of color stop 2. The corresponding angle is 45° (0.125 x 360°).
161            [0x409EFF, 0.25] // Color and weight of color stop 3. The corresponding angle is 90° (0.25 x 360°).
162          ]
163        })
164      }
165
166      GridItem() {
167        Column() {
168          Text('repeat: true')
169            .fontSize(15)
170        }
171        .width(100)
172        .height(100)
173        .justifyContent(FlexAlign.Center)
174        .borderRadius(10)
175        .sweepGradient({
176          center: [50, 50],
177          start: 0,
178          end: 360,
179          repeating: true,
180          colors: [
181            [0xf56c6c, 0],
182            [0xffffff, 0.125],
183            [0x409EFF, 0.25]
184          ]
185        })
186      }
187
188      GridItem() {
189        Column() {
190          Text('repeat: false')
191            .fontSize(15)
192        }
193        .width(100)
194        .height(100)
195        .justifyContent(FlexAlign.Center)
196        .borderRadius(10)
197        .sweepGradient({
198          center: [50, 50],
199          start: 0,
200          end: 360,
201          repeating: false, // The color gradient effect is generated only within the coverage of the color stop angles.
202          colors: [
203            [0xf56c6c, 0],
204            [0xffffff, 0.125],
205            [0x409EFF, 0.25]
206          ]
207        })
208      }
209    }
210    .columnsGap(10)
211    .rowsGap(10)
212    .columnsTemplate('1fr 1fr')
213    .rowsTemplate('1fr 1fr 1fr')
214    .width('100%')
215    .height(437)
216  }
217}
218```
219
220![en-us_image_0000001641177073](figures/en-us_image_0000001641177073.png)
221
222
223## Applying Radial Gradient Effect
224
225
226```ts
227@Entry
228@Component
229struct radialGradientDemo {
230  build() {
231    Grid() {
232      GridItem() {
233        Column() {
234          Text('center: 50')
235            .fontSize(15)
236        }
237        .width(100)
238        .height(100)
239        .justifyContent(FlexAlign.Center)
240        .borderRadius(10)
241        .radialGradient({
242          center: [50, 50], // Center point of the radial gradient.
243          radius: 100, // Radius of the radial gradient.
244          repeating: true, // The gradients are repeated outside the specified range to fill the entire component.
245          colors: [
246            // With [50, 50] as the center point, the gradient changes from color stop 1 to color stop 2 within the range of radius 0 to 12.5;
247            // the gradient changes from color stop 2 to color stop 3 within the range of radius 12.5 to 25;
248            // the gradients in the range of radius 0 to 25 are repeated to fill the entire component.
249            [0xf56c6c, 0], // Color and weight of color stop 1. The corresponding radius is 0 (0 x 100).
250            [0xffffff, 0.125], // Color and weight of color stop 2. The corresponding radius is 12.5 (0.125 x 100).
251            [0x409EFF, 0.25] // Color and weight of color stop 3. The corresponding radius is 25 (0.25 x 100).
252          ]
253        })
254      }
255
256      GridItem() {
257        Column() {
258          Text('center: 0')
259            .fontSize(15)
260        }
261        .width(100)
262        .height(100)
263        .justifyContent(FlexAlign.Center)
264        .borderRadius(10)
265        .radialGradient({
266          center: [0, 0], // Center point of the radial gradient, which is the coordinate of the upper left corner of the component in this example.
267          radius: 100,
268          repeating: true,
269          colors: [
270            [0xf56c6c, 0],
271            [0xffffff, 0.125],
272            [0x409EFF, 0.25]
273          ]
274        })
275      }
276
277      GridItem() {
278        Column() {
279          Text('repeat: true')
280            .fontSize(15)
281        }
282        .width(100)
283        .height(100)
284        .justifyContent(FlexAlign.Center)
285        .borderRadius(10)
286        .radialGradient({
287          center: [50, 50],
288          radius: 100,
289          repeating: true,
290          colors: [
291            [0xf56c6c, 0],
292            [0xffffff, 0.125],
293            [0x409EFF, 0.25]
294          ]
295        })
296      }
297
298      GridItem() {
299        Column() {
300          Text('repeat: false')
301            .fontSize(15)
302        }
303        .width(100)
304        .height(100)
305        .justifyContent(FlexAlign.Center)
306        .borderRadius(10)
307        .radialGradient({
308          center: [50, 50],
309          radius: 100,
310          repeating: false, // The gradients are not repeated.
311          colors: [
312            [0xf56c6c, 0],
313            [0xffffff, 0.125],
314            [0x409EFF, 0.25]
315          ]
316        })
317      }
318    }
319    .columnsGap(10)
320    .rowsGap(10)
321    .columnsTemplate('1fr 1fr')
322    .rowsTemplate('1fr 1fr 1fr')
323    .width('100%')
324    .height('100%')
325  }
326}
327```
328
329![en-us_image_0000001592904050](figures/en-us_image_0000001592904050.png)
330