1 /**
2  * @file lv_draw_sw_gradient.h
3  *
4  */
5 
6 #ifndef LV_DRAW_SW_GRADIENT_H
7 #define LV_DRAW_SW_GRADIENT_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../../misc/lv_color.h"
17 #include "../../misc/lv_style.h"
18 
19 #if LV_USE_DRAW_SW
20 
21 /*********************
22  *      DEFINES
23  *********************/
24 #if LV_GRADIENT_MAX_STOPS < 2
25 #error LVGL needs at least 2 stops for gradients. Please increase the LV_GRADIENT_MAX_STOPS
26 #endif
27 
28 #define LV_GRAD_LEFT    LV_PCT(0)
29 #define LV_GRAD_RIGHT   LV_PCT(100)
30 #define LV_GRAD_TOP     LV_PCT(0)
31 #define LV_GRAD_BOTTOM  LV_PCT(100)
32 #define LV_GRAD_CENTER  LV_PCT(50)
33 
34 /**********************
35  *      TYPEDEFS
36  **********************/
37 typedef lv_color_t lv_grad_color_t;
38 
39 /**********************
40  *      PROTOTYPES
41  **********************/
42 /** Compute the color in the given gradient and fraction
43  *  Gradient are specified in a virtual [0-255] range, so this function scales the virtual range to the given range
44  * @param dsc       The gradient descriptor to use
45  * @param range     The range to use in computation.
46  * @param frac      The current part used in the range. frac is in [0; range]
47  * @param color_out Calculated gradient color
48  * @param opa_out   Calculated opacity
49  */
50 
51 void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_color_calculate(const lv_grad_dsc_t * dsc, int32_t range,
52                                                              int32_t frac, lv_grad_color_t * color_out, lv_opa_t * opa_out);
53 
54 /** Get a gradient cache from the given parameters */
55 lv_grad_t * lv_gradient_get(const lv_grad_dsc_t * gradient, int32_t w, int32_t h);
56 
57 /**
58  * Clean up the gradient item after it was get with `lv_grad_get_from_cache`.
59  * @param grad      pointer to a gradient
60  */
61 void lv_gradient_cleanup(lv_grad_t * grad);
62 
63 /**
64  * Initialize gradient color map from a table
65  * @param grad      pointer to a gradient descriptor
66  * @param colors    color array
67  * @param fracs     position array (0..255): if NULL, then colors are distributed evenly
68  * @param opa       opacity array: if NULL, then LV_OPA_COVER is assumed
69  * @param num_stops number of gradient stops (1..LV_GRADIENT_MAX_STOPS)
70  */
71 void lv_gradient_init_stops(lv_grad_dsc_t * grad, const lv_color_t colors[], const lv_opa_t opa[],
72                             const uint8_t fracs[], int num_stops);
73 
74 #if LV_USE_DRAW_SW_COMPLEX_GRADIENTS
75 
76 /**
77  * Helper function to initialize linear gradient
78  * @param dsc      gradient descriptor
79  * @param from_x   start x position: can be a coordinate or an lv_pct() value
80  *                 predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well
81  * @param from_y   start y position
82  * @param to_x     end x position
83  * @param to_y     end y position
84  * @param extend   one of LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT or LV_GRAD_EXTEND_REFLECT
85  */
86 void lv_grad_linear_init(lv_grad_dsc_t * dsc, int32_t from_x, int32_t from_y, int32_t to_x, int32_t to_y,
87                          lv_grad_extend_t extend);
88 
89 /**
90  * Helper function to initialize radial gradient
91  * @param dsc      gradient descriptor
92  * @param center_x center x position: can be a coordinate or an lv_pct() value
93  *                 predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well
94  * @param center_y center y position
95  * @param to_x     point on the end circle x position
96  * @param to_y     point on the end circle y position
97  * @param extend   one of LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT or LV_GRAD_EXTEND_REFLECT
98  */
99 void lv_grad_radial_init(lv_grad_dsc_t * dsc, int32_t center_x, int32_t center_y, int32_t to_x, int32_t to_y,
100                          lv_grad_extend_t extend);
101 
102 /**
103  * Set focal (starting) circle of a radial gradient
104  * @param dsc      gradient descriptor
105  * @param center_x center x position: can be a coordinate or an lv_pct() value
106  *                 predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well
107  * @param center_y center y position
108  * @param radius   radius of the starting circle (NOTE: this must be a scalar number, not percentage)
109  */
110 void lv_grad_radial_set_focal(lv_grad_dsc_t * dsc, int32_t center_x, int32_t center_y, int32_t radius);
111 
112 /**
113  * Helper function to initialize conical gradient
114  * @param dsc      gradient descriptor
115  * @param center_x center x position: can be a coordinate or an lv_pct() value
116  *                 predefined constants LV_GRAD_LEFT, LV_GRAD_RIGHT, LV_GRAD_TOP, LV_GRAD_BOTTOM, LV_GRAD_CENTER can be used as well
117  * @param center_y center y position
118  * @param start_angle   start angle in degrees
119  * @param end_angle     end angle in degrees
120  * @param extend   one of LV_GRAD_EXTEND_PAD, LV_GRAD_EXTEND_REPEAT or LV_GRAD_EXTEND_REFLECT
121  */
122 void lv_grad_conical_init(lv_grad_dsc_t * dsc, int32_t center_x, int32_t center_y, int32_t start_angle,
123                           int32_t end_angle, lv_grad_extend_t extend);
124 
125 /**
126  * Calculate constants from the given parameters that are used during rendering
127  * @param dsc      gradient descriptor
128  */
129 void lv_gradient_linear_setup(lv_grad_dsc_t * dsc, const lv_area_t * coords);
130 
131 /**
132  * Free up the allocated memory for the gradient calculation
133  * @param dsc      gradient descriptor
134  */
135 void lv_gradient_linear_cleanup(lv_grad_dsc_t * dsc);
136 
137 /**
138  * Calculate a line segment of a linear gradient
139  * @param dsc       gradient descriptor
140  * @param xp        starting point x coordinate in gradient space
141  * @param yp        starting point y coordinate in gradient space
142  * @param width     width of the line segment in pixels
143  * @param result    color buffer for the resulting line segment
144  */
145 void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_linear_get_line(lv_grad_dsc_t * dsc, int32_t xp, int32_t yp, int32_t width,
146                                                              lv_grad_t * result);
147 
148 /**
149  * Calculate constants from the given parameters that are used during rendering
150  * @param dsc      gradient descriptor
151  */
152 void lv_gradient_radial_setup(lv_grad_dsc_t * dsc, const lv_area_t * coords);
153 
154 /**
155  * Free up the allocated memory for the gradient calculation
156  * @param dsc      gradient descriptor
157  */
158 void lv_gradient_radial_cleanup(lv_grad_dsc_t * dsc);
159 
160 /**
161  * Calculate a line segment of a radial gradient
162  * @param dsc       gradient descriptor
163  * @param xp        starting point x coordinate in gradient space
164  * @param yp        starting point y coordinate in gradient space
165  * @param width     width of the line segment in pixels
166  * @param result    color buffer for the resulting line segment
167  */
168 void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_radial_get_line(lv_grad_dsc_t * dsc, int32_t xp, int32_t yp, int32_t width,
169                                                              lv_grad_t * result);
170 
171 /**
172  * Calculate constants from the given parameters that are used during rendering
173  * @param dsc      gradient descriptor
174  */
175 void lv_gradient_conical_setup(lv_grad_dsc_t * dsc, const lv_area_t * coords);
176 
177 /**
178  * Free up the allocated memory for the gradient calculation
179  * @param dsc      gradient descriptor
180  */
181 void lv_gradient_conical_cleanup(lv_grad_dsc_t * dsc);
182 
183 /**
184  * Calculate a line segment of a conical gradient
185  * @param dsc       gradient descriptor
186  * @param xp        starting point x coordinate in gradient space
187  * @param yp        starting point y coordinate in gradient space
188  * @param width     width of the line segment in pixels
189  * @param result    color buffer for the resulting line segment
190  */
191 void /* LV_ATTRIBUTE_FAST_MEM */ lv_gradient_conical_get_line(lv_grad_dsc_t * dsc, int32_t xp, int32_t yp,
192                                                               int32_t width,
193                                                               lv_grad_t * result);
194 
195 #endif /*LV_USE_DRAW_SW_COMPLEX_GRADIENTS*/
196 
197 #endif /*LV_USE_DRAW_SW*/
198 
199 #ifdef __cplusplus
200 } /*extern "C"*/
201 #endif
202 
203 #endif /*LV_DRAW_GRADIENT_H*/
204