1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gfx_utils/style.h"
17
18 namespace OHOS {
19 Style StyleDefault::defaultStyle_;
20 Style StyleDefault::brightStyle_;
21 Style StyleDefault::brightColorStyle_;
22 Style StyleDefault::buttonReleasedStyle_;
23 Style StyleDefault::buttonPressedStyle_;
24 Style StyleDefault::buttonInactiveStyle_;
25 Style StyleDefault::labelStyle_;
26 Style StyleDefault::editTextStyle_;
27 Style StyleDefault::backgroundTransparentStyle_;
28 Style StyleDefault::progressBackgroundStyle_;
29 Style StyleDefault::progressForegroundStyle_;
30 Style StyleDefault::sliderKnobStyle_;
31 Style StyleDefault::pickerBackgroundStyle_;
32 Style StyleDefault::pickerHighlightStyle_;
33 Style StyleDefault::scrollBarBackgroundStyle_;
34 Style StyleDefault::scrollBarForegroundStyle_;
35
Style()36 Style::Style()
37 : bgColor_(Color::Black()),
38 bgOpa_(OPA_OPAQUE),
39 borderOpa_(OPA_OPAQUE),
40 borderWidth_(0),
41 borderRadius_(0),
42 borderColor_(Color::Black()),
43 paddingLeft_(0),
44 paddingRight_(0),
45 paddingTop_(0),
46 paddingBottom_(0),
47 marginLeft_(0),
48 marginRight_(0),
49 marginTop_(0),
50 marginBottom_(0),
51 imageOpa_(OPA_OPAQUE),
52 textOpa_(OPA_OPAQUE),
53 font_(16), // 16 : default font size
54 lineSpace_(2), // 2 : default line space
55 letterSpace_(0),
56 lineHeight_(0),
57 textColor_(Color::White()),
58 lineColor_(Color::GetColorFromRGB(0x20, 0x20, 0x20)),
59 lineOpa_(OPA_OPAQUE),
60 lineCap_(CapType::CAP_NONE),
61 lineWidth_(2) // 2 : default line width
62 {
63 }
64
GetStyle(uint8_t key) const65 int64_t Style::GetStyle(uint8_t key) const
66 {
67 switch (key) {
68 case STYLE_BACKGROUND_COLOR:
69 return bgColor_.full;
70 case STYLE_BACKGROUND_OPA:
71 return bgOpa_;
72 case STYLE_BORDER_RADIUS:
73 return borderRadius_;
74 case STYLE_BORDER_COLOR:
75 return borderColor_.full;
76 case STYLE_BORDER_OPA:
77 return borderOpa_;
78 case STYLE_BORDER_WIDTH:
79 return borderWidth_;
80 case STYLE_PADDING_LEFT:
81 return paddingLeft_;
82 case STYLE_PADDING_RIGHT:
83 return paddingRight_;
84 case STYLE_PADDING_TOP:
85 return paddingTop_;
86 case STYLE_PADDING_BOTTOM:
87 return paddingBottom_;
88 case STYLE_MARGIN_LEFT:
89 return marginLeft_;
90 case STYLE_MARGIN_RIGHT:
91 return marginRight_;
92 case STYLE_MARGIN_TOP:
93 return marginTop_;
94 case STYLE_MARGIN_BOTTOM:
95 return marginBottom_;
96 case STYLE_IMAGE_OPA:
97 return imageOpa_;
98 case STYLE_TEXT_COLOR:
99 return textColor_.full;
100 case STYLE_TEXT_FONT:
101 return font_;
102 case STYLE_LINE_SPACE:
103 return lineSpace_;
104 case STYLE_LETTER_SPACE:
105 return letterSpace_;
106 case STYLE_LINE_HEIGHT:
107 return lineHeight_;
108 case STYLE_TEXT_OPA:
109 return textOpa_;
110 case STYLE_LINE_COLOR:
111 return lineColor_.full;
112 case STYLE_LINE_WIDTH:
113 return lineWidth_;
114 case STYLE_LINE_OPA:
115 return lineOpa_;
116 case STYLE_LINE_CAP:
117 return lineCap_;
118 default:
119 return 0;
120 }
121 }
122
SetStyle(uint8_t key,int64_t value)123 void Style::SetStyle(uint8_t key, int64_t value)
124 {
125 switch (key) {
126 case STYLE_BACKGROUND_COLOR:
127 bgColor_.full = value;
128 break;
129 case STYLE_BACKGROUND_OPA:
130 bgOpa_ = value;
131 break;
132 case STYLE_BORDER_RADIUS:
133 borderRadius_ = value;
134 break;
135 case STYLE_BORDER_COLOR:
136 borderColor_.full = value;
137 break;
138 case STYLE_BORDER_OPA:
139 borderOpa_ = value;
140 break;
141 case STYLE_BORDER_WIDTH:
142 borderWidth_ = value;
143 break;
144 case STYLE_PADDING_LEFT:
145 paddingLeft_ = value;
146 break;
147 case STYLE_PADDING_RIGHT:
148 paddingRight_ = value;
149 break;
150 case STYLE_PADDING_TOP:
151 paddingTop_ = value;
152 break;
153 case STYLE_PADDING_BOTTOM:
154 paddingBottom_ = value;
155 break;
156 case STYLE_MARGIN_LEFT:
157 marginLeft_ = value;
158 break;
159 case STYLE_MARGIN_RIGHT:
160 marginRight_ = value;
161 break;
162 case STYLE_MARGIN_TOP:
163 marginTop_ = value;
164 break;
165 case STYLE_MARGIN_BOTTOM:
166 marginBottom_ = value;
167 break;
168 case STYLE_IMAGE_OPA:
169 imageOpa_ = value;
170 break;
171 case STYLE_TEXT_COLOR:
172 textColor_.full = value;
173 break;
174 case STYLE_TEXT_FONT:
175 font_ = value;
176 break;
177 case STYLE_LINE_SPACE:
178 lineSpace_ = value;
179 break;
180 case STYLE_LETTER_SPACE:
181 letterSpace_ = value;
182 break;
183 case STYLE_LINE_HEIGHT:
184 if (value < 0) {
185 value = 0;
186 }
187 lineHeight_ = value;
188 break;
189 case STYLE_TEXT_OPA:
190 textOpa_ = value;
191 break;
192 case STYLE_LINE_COLOR:
193 lineColor_.full = value;
194 break;
195 case STYLE_LINE_WIDTH:
196 lineWidth_ = value;
197 break;
198 case STYLE_LINE_OPA:
199 lineOpa_ = value;
200 break;
201 case STYLE_LINE_CAP:
202 lineCap_ = value;
203 break;
204 default:
205 break;
206 }
207 }
208
Init()209 void StyleDefault::Init()
210 {
211 InitStyle();
212 InitButtonStyle();
213 InitLabelStyle();
214 InitEditTextStyle();
215 InitBackgroundTransparentStyle();
216 InitProgressStyle();
217 InitPickerStyle();
218 InitScrollBarStyle();
219 }
220
InitStyle()221 void StyleDefault::InitStyle()
222 {
223 brightStyle_ = defaultStyle_;
224 brightStyle_.SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
225 brightStyle_.SetStyle(STYLE_BORDER_RADIUS, 3); // 3: rect radius
226 brightStyle_.SetStyle(STYLE_BORDER_COLOR, Color::GetColorFromRGB(0x40, 0x40, 0x40).full);
227 brightStyle_.SetStyle(STYLE_BORDER_WIDTH, 3); // 3: rect border width
228 brightStyle_.SetStyle(STYLE_BORDER_OPA, 76); // 76: default opa
229 brightStyle_.SetStyle(STYLE_TEXT_COLOR, Color::GetColorFromRGB(0x20, 0x20, 0x20).full);
230 brightStyle_.SetStyle(STYLE_LINE_COLOR, Color::GetColorFromRGB(0x20, 0x20, 0x20).full);
231
232 brightColorStyle_ = brightStyle_;
233 brightColorStyle_.SetStyle(STYLE_TEXT_COLOR, Color::GetColorFromRGB(0xe0, 0xe0, 0xe0).full);
234 brightColorStyle_.SetStyle(STYLE_LINE_COLOR, Color::GetColorFromRGB(0xc0, 0xc0, 0xc0).full);
235 brightColorStyle_.SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0x6b, 0x9a, 0xc7).full);
236 brightColorStyle_.SetStyle(STYLE_BORDER_COLOR, Color::GetColorFromRGB(0x15, 0x2c, 0x42).full);
237 }
238
InitButtonStyle()239 void StyleDefault::InitButtonStyle()
240 {
241 /* Button released style */
242 buttonReleasedStyle_ = defaultStyle_;
243 buttonReleasedStyle_.SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0x00, 0x7d, 0xff).full);
244 buttonReleasedStyle_.SetStyle(STYLE_BORDER_RADIUS, 200); // 200: button radius
245 buttonReleasedStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
246
247 buttonReleasedStyle_.SetStyle(STYLE_BORDER_COLOR, Color::GetColorFromRGB(0x0b, 0x19, 0x28).full);
248 buttonReleasedStyle_.SetStyle(STYLE_BORDER_WIDTH, 0);
249 buttonReleasedStyle_.SetStyle(STYLE_BORDER_OPA, 178); // 178: default opa
250
251 /* Button pressed style */
252 buttonPressedStyle_ = buttonReleasedStyle_;
253 buttonPressedStyle_.SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0x19, 0x96, 0xff).full);
254 buttonPressedStyle_.SetStyle(STYLE_BORDER_COLOR, Color::GetColorFromRGB(0x0b, 0x19, 0x28).full);
255
256 /* Button inactive style */
257 buttonInactiveStyle_ = buttonReleasedStyle_;
258 buttonInactiveStyle_.SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0x61, 0x7e, 0x9d).full);
259 buttonInactiveStyle_.SetStyle(STYLE_BORDER_COLOR, Color::GetColorFromRGB(0x90, 0x90, 0x90).full);
260 }
261
InitLabelStyle()262 void StyleDefault::InitLabelStyle()
263 {
264 labelStyle_ = defaultStyle_;
265 labelStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
266 }
267
InitEditTextStyle()268 void StyleDefault::InitEditTextStyle()
269 {
270 editTextStyle_ = defaultStyle_;
271 editTextStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
272 editTextStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
273 editTextStyle_.SetStyle(STYLE_BORDER_COLOR, Color::White().full);
274 editTextStyle_.SetStyle(STYLE_BORDER_WIDTH, 2); // 2: border width
275 editTextStyle_.SetStyle(STYLE_BORDER_RADIUS, 5); // 5: border radius
276 editTextStyle_.SetStyle(STYLE_BORDER_OPA, OPA_OPAQUE);
277 }
278
InitBackgroundTransparentStyle()279 void StyleDefault::InitBackgroundTransparentStyle()
280 {
281 backgroundTransparentStyle_ = defaultStyle_;
282 backgroundTransparentStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
283 }
284
InitPickerStyle()285 void StyleDefault::InitPickerStyle()
286 {
287 pickerBackgroundStyle_ = defaultStyle_;
288 pickerBackgroundStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
289 pickerBackgroundStyle_.SetStyle(STYLE_TEXT_COLOR, Color::Gray().full);
290
291 pickerHighlightStyle_ = defaultStyle_;
292 pickerHighlightStyle_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
293 pickerHighlightStyle_.SetStyle(STYLE_TEXT_FONT, 18); // 18: font value
294 pickerHighlightStyle_.SetStyle(STYLE_TEXT_COLOR, Color::GetColorFromRGB(0x45, 0xa5, 0xff).full);
295 }
296
InitProgressStyle()297 void StyleDefault::InitProgressStyle()
298 {
299 progressBackgroundStyle_ = defaultStyle_;
300 progressBackgroundStyle_.SetStyle(STYLE_BORDER_WIDTH, 0);
301 progressBackgroundStyle_.SetStyle(STYLE_BORDER_RADIUS, 0);
302
303 progressForegroundStyle_ = brightStyle_;
304 progressForegroundStyle_.SetStyle(STYLE_BORDER_WIDTH, 0);
305 progressForegroundStyle_.SetStyle(STYLE_BORDER_RADIUS, 0);
306 progressForegroundStyle_.SetStyle(STYLE_LINE_COLOR, Color::GetColorFromRGB(0x45, 0xa5, 0xff).full);
307
308 sliderKnobStyle_ = brightColorStyle_;
309 sliderKnobStyle_.SetStyle(STYLE_BORDER_WIDTH, 0);
310 }
311
InitScrollBarStyle()312 void StyleDefault::InitScrollBarStyle()
313 {
314 scrollBarBackgroundStyle_.SetStyle(STYLE_BACKGROUND_COLOR,
315 Color::GetColorFromRGB(0x7f, 0x7f, 0x7f).full); // 0x7f: default color
316 scrollBarBackgroundStyle_.SetStyle(STYLE_BACKGROUND_OPA, 128); // 128: default opa
317 scrollBarBackgroundStyle_.SetStyle(STYLE_LINE_COLOR,
318 Color::GetColorFromRGB(0x7f, 0x7f, 0x7f).full); // 0x7f: default color
319 scrollBarBackgroundStyle_.SetStyle(STYLE_LINE_OPA, 128); // 128: default opa
320 scrollBarBackgroundStyle_.SetStyle(STYLE_LINE_WIDTH, 8); // 8: default width
321
322 scrollBarForegroundStyle_.SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
323 scrollBarForegroundStyle_.SetStyle(STYLE_BACKGROUND_OPA, 168); // 168: default opa
324 scrollBarForegroundStyle_.SetStyle(STYLE_LINE_COLOR, Color::White().full);
325 scrollBarForegroundStyle_.SetStyle(STYLE_LINE_OPA, 168); // 168: default opa
326 scrollBarForegroundStyle_.SetStyle(STYLE_LINE_WIDTH, 6); // 6: default width
327 }
328 } // namespace OHOS
329