1 /*
2 * Copyright (c) 2024 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 "ui/view/theme/token_colors.h"
17
18 #include <vector>
19
20 #include "core/common/resource/resource_parse_utils.h"
21
22 namespace OHOS::Ace {
23
24 static const std::vector<TokenColorData> colorData = {
25 { "brand", 125830976 }, /* BRAND = 0 */
26 { "warning", 125830979 }, /* WARNING = 1; */
27 { "alert", 125830980 }, /* ALERT = 2 */
28 { "confirm", 125830981 }, /* CONFIRM = 3 */
29
30 { "fontPrimary", 125830982 }, /* FONT_PRIMARY = 4 */
31 { "fontSecondary", 125830983 }, /* FONT_SECONDARY = 5 */
32 { "fontTertiary", 125830984 }, /* FONT_TERTIARY = 6 */
33 { "fontFourth", 125830985 }, /* FONT_FOURTH = 7 */
34 { "fontEmphasize", 125830986 }, /* FONT_EMPHASIZE = 8 */
35
36 { "fontOnPrimary", 125830987 }, /* FONT_ON_PRIMARY = 9 */
37 { "fontOnSecondary", 125830988 }, /* FONT_ON_SECONDARY = 10 */
38 { "fontOnTertiary", 125830989 }, /* FONT_ON_TERTIARY = 11 */
39 { "fontOnFourth", 125830990 }, /* FONT_ON_FOURTH = 12 */
40
41 { "iconPrimary", 125830991 }, /* ICON_PRIMARY = 13 */
42 { "iconSecondary", 125830992 }, /* ICON_SECONDARY = 14 */
43 { "iconTertiary", 125830993 }, /* ICON_TERTIARY = 15 */
44 { "iconFourth", 125830994 }, /* ICON_FOURTH = 16 */
45 { "iconEmphasize", 125830995 }, /* ICON_EMPHASIZE = 17 */
46 { "iconSubEmphasize", 125830996 }, /* ICON_SUB_EMPHASIZE = 18 */
47
48 { "iconOnPrimary", 125831057 }, /* ICON_ON_PRIMARY = 19 */
49 { "iconOnSecondary", 125831058 }, /* ICON_ON_SECONDARY = 20 */
50 { "iconOnTertiary", 125831059 }, /* ICON_ON_TERTIARY = 21 */
51 { "iconOnFourth", 125831060 }, /* ICON_ON_FOURTH = 22 */
52
53 { "backgroundPrimary", 125831061 }, /* BACKGROUND_PRIMARY = 23 */
54 { "backgroundSecondary", 125831062 }, /* BACKGROUND_SECONDARY = 24 */
55 { "backgroundTertiary", 125831063 }, /* BACKGROUND_TERTIARY = 25 */
56 { "backgroundFourth", 125831064 }, /* BACKGROUND_FOURTH = 26 */
57 { "backgroundEmphasize", 125831065 }, /* BACKGROUND_EMPHASIZE = 27 */
58
59 { "compForegroundPrimary", 125831003 }, /* COMP_FOREGROUND_PRIMARY = 28 */
60 { "compBackgroundPrimary", 125831004 }, /* COMP_BACKGROUND_PRIMARY = 29 */
61 { "compBackgroundPrimaryTran", -1 }, /* COMP_BACKGROUND_PRIMARY_TRAN = 30 */
62 { "compBackgroundPrimaryContrary", 125831005 }, /* COMP_BACKGROUND_PRIMARY_CONTRARY = 31 */
63 { "compBackgroundGray", 125831006 }, /* COMP_BACKGROUND_GRAY = 32 */
64 { "compBackgroundSecondary", 125831007 }, /* COMP_BACKGROUND_SECONDARY = 33 */
65 { "compBackgroundTertiary", 125831008 }, /* COMP_BACKGROUND_TERTIARY = 34 */
66 { "compBackgroundEmphasize", 125831009 }, /* COMP_BACKGROUND_EMPHASIZE = 35 */
67 { "compBackgroundNeutral", 125831066 }, /* COMP_BACKGROUND_NEUTRAL = 36 */
68 { "compEmphasizeSecondary", 125831011 }, /* COMP_EMPHASIZE_SECONDARY = 37 */
69 { "compEmphasizeTertiary", 125831012 }, /* COMP_EMPHASIZE_TERTIARY = 38 */
70 { "compDivider", 125831013 }, /* COMP_DIVIDER = 39 */
71 { "compCommonContrary", 125831014 }, /* COMP_COMMON_CONTRARY = 40 */
72 { "compBackgroundFocus", 125831015 }, /* COMP_BACKGROUND_FOCUS = 41 */
73 { "compFocusedPrimary", 125831016 }, /* COMP_FOCUSED_PRIMARY = 42 */
74 { "compFocusedSecondary", 125831017 }, /* COMP_FOCUSED_SECONDARY = 43 */
75 { "compFocusedTertiary", 125831018 }, /* COMP_FOCUSED_TERTIARY = 44 */
76
77 { "interactiveHover", 125831019 }, /* INTERACTIVE_HOVER = 45 */
78 { "interactivePressed", 125831020 }, /* INTERACTIVE_PRESSED = 46 */
79 { "interactiveFocus", 125831021 }, /* INTERACTIVE_FOCUS = 47 */
80 { "interactiveActive", 125831022 }, /* INTERACTIVE_ACTIVE = 48 */
81 { "interactiveSelect", 125831023 }, /* INTERACTIVE_SELECT = 49 */
82 { "interactiveClick", 125831024 }, /* INTERACTIVE_CLICK = 50 */
83
84 { nullptr, -1 } /* TOTAL_NUMBER = 51; */
85 };
86
SetColors(const std::vector<Color> & colors)87 void TokenColors::SetColors(const std::vector<Color>& colors)
88 {
89 colors_ = colors;
90 }
91
SetColor(uint32_t idx,const Color & color)92 void TokenColors::SetColor(uint32_t idx, const Color& color)
93 {
94 if (idx >= 0 && idx < colors_.size()) {
95 colors_[idx] = color;
96 }
97 }
98
GetColors()99 const std::vector<Color>& TokenColors::GetColors()
100 {
101 return colors_;
102 }
103
GetColorWithResourceObject(int32_t colorIndex) const104 Color TokenColors::GetColorWithResourceObject(int32_t colorIndex) const
105 {
106 if (colorIndex < 0 || colorIndex >= TokenColors::TOTAL_NUMBER) {
107 return Color();
108 }
109 if ((resObjs_.size() == TokenColors::TOTAL_NUMBER) && resObjs_[colorIndex]) {
110 Color color;
111 if (colorMode_ == ColorMode::COLOR_MODE_UNDEFINED) {
112 ResourceParseUtils::ParseResColor(resObjs_[colorIndex], color);
113 } else {
114 ResourceParseUtils::ParseResColorWithColorMode(resObjs_[colorIndex], color, colorMode_);
115 }
116 return color;
117 }
118 return colors_[colorIndex];
119 }
120
Brand() const121 Color TokenColors::Brand() const
122 {
123 return GetColorWithResourceObject(BRAND);
124 }
125
Warning() const126 Color TokenColors::Warning() const
127 {
128 return GetColorWithResourceObject(WARNING);
129 }
130
Alert() const131 Color TokenColors::Alert() const
132 {
133 return GetColorWithResourceObject(ALERT);
134 }
135
Confirm() const136 Color TokenColors::Confirm() const
137 {
138 return GetColorWithResourceObject(CONFIRM);
139 }
140
FontPrimary() const141 Color TokenColors::FontPrimary() const
142 {
143 return GetColorWithResourceObject(FONT_PRIMARY);
144 }
145
FontSecondary() const146 Color TokenColors::FontSecondary() const
147 {
148 return GetColorWithResourceObject(FONT_SECONDARY);
149 }
150
FontTertiary() const151 Color TokenColors::FontTertiary() const
152 {
153 return GetColorWithResourceObject(FONT_TERTIARY);
154 }
155
FontFourth() const156 Color TokenColors::FontFourth() const
157 {
158 return GetColorWithResourceObject(FONT_FOURTH);
159 }
160
FontEmphasize() const161 Color TokenColors::FontEmphasize() const
162 {
163 return GetColorWithResourceObject(FONT_EMPHASIZE);
164 }
165
FontOnPrimary() const166 Color TokenColors::FontOnPrimary() const
167 {
168 return GetColorWithResourceObject(FONT_ON_PRIMARY);
169 }
170
FontOnSecondary() const171 Color TokenColors::FontOnSecondary() const
172 {
173 return GetColorWithResourceObject(FONT_ON_SECONDARY);
174 }
175
FontOnTertiary() const176 Color TokenColors::FontOnTertiary() const
177 {
178 return GetColorWithResourceObject(FONT_ON_TERTIARY);
179 }
180
FontOnFourth() const181 Color TokenColors::FontOnFourth() const
182 {
183 return GetColorWithResourceObject(FONT_ON_FOURTH);
184 }
185
IconPrimary() const186 Color TokenColors::IconPrimary() const
187 {
188 return GetColorWithResourceObject(ICON_PRIMARY);
189 }
190
IconSecondary() const191 Color TokenColors::IconSecondary() const
192 {
193 return GetColorWithResourceObject(ICON_SECONDARY);
194 }
195
IconTertiary() const196 Color TokenColors::IconTertiary() const
197 {
198 return GetColorWithResourceObject(ICON_TERTIARY);
199 }
200
IconFourth() const201 Color TokenColors::IconFourth() const
202 {
203 return GetColorWithResourceObject(ICON_FOURTH);
204 }
205
IconEmphasize() const206 Color TokenColors::IconEmphasize() const
207 {
208 return GetColorWithResourceObject(ICON_EMPHASIZE);
209 }
210
IconSubEmphasize() const211 Color TokenColors::IconSubEmphasize() const
212 {
213 return GetColorWithResourceObject(ICON_SUB_EMPHASIZE);
214 }
215
IconOnPrimary() const216 Color TokenColors::IconOnPrimary() const
217 {
218 return GetColorWithResourceObject(ICON_ON_PRIMARY);
219 }
220
IconOnSecondary() const221 Color TokenColors::IconOnSecondary() const
222 {
223 return GetColorWithResourceObject(ICON_ON_SECONDARY);
224 }
225
IconOnTertiary() const226 Color TokenColors::IconOnTertiary() const
227 {
228 return GetColorWithResourceObject(ICON_ON_TERTIARY);
229 }
230
IconOnFourth() const231 Color TokenColors::IconOnFourth() const
232 {
233 return GetColorWithResourceObject(ICON_ON_FOURTH);
234 }
235
BackgroundPrimary() const236 Color TokenColors::BackgroundPrimary() const
237 {
238 return GetColorWithResourceObject(BACKGROUND_PRIMARY);
239 }
240
BackgroundSecondary() const241 Color TokenColors::BackgroundSecondary() const
242 {
243 return GetColorWithResourceObject(BACKGROUND_SECONDARY);
244 }
245
BackgroundTertiary() const246 Color TokenColors::BackgroundTertiary() const
247 {
248 return GetColorWithResourceObject(BACKGROUND_TERTIARY);
249 }
250
BackgroundFourth() const251 Color TokenColors::BackgroundFourth() const
252 {
253 return GetColorWithResourceObject(BACKGROUND_FOURTH);
254 }
255
BackgroundEmphasize() const256 Color TokenColors::BackgroundEmphasize() const
257 {
258 return GetColorWithResourceObject(BACKGROUND_EMPHASIZE);
259 }
260
CompForegroundPrimary() const261 Color TokenColors::CompForegroundPrimary() const
262 {
263 return GetColorWithResourceObject(COMP_FOREGROUND_PRIMARY);
264 }
265
CompBackgroundPrimary() const266 Color TokenColors::CompBackgroundPrimary() const
267 {
268 return GetColorWithResourceObject(COMP_BACKGROUND_PRIMARY);
269 }
270
CompBackgroundPrimaryTran() const271 Color TokenColors::CompBackgroundPrimaryTran() const
272 {
273 return GetColorWithResourceObject(COMP_BACKGROUND_PRIMARY_TRAN);
274 }
275
CompBackgroundPrimaryContrary() const276 Color TokenColors::CompBackgroundPrimaryContrary() const
277 {
278 return GetColorWithResourceObject(COMP_BACKGROUND_PRIMARY_CONTRARY);
279 }
280
CompBackgroundGray() const281 Color TokenColors::CompBackgroundGray() const
282 {
283 return GetColorWithResourceObject(COMP_BACKGROUND_GRAY);
284 }
285
CompBackgroundSecondary() const286 Color TokenColors::CompBackgroundSecondary() const
287 {
288 return GetColorWithResourceObject(COMP_BACKGROUND_SECONDARY);
289 }
290
CompBackgroundTertiary() const291 Color TokenColors::CompBackgroundTertiary() const
292 {
293 return GetColorWithResourceObject(COMP_BACKGROUND_TERTIARY);
294 }
295
CompBackgroundEmphasize() const296 Color TokenColors::CompBackgroundEmphasize() const
297 {
298 return GetColorWithResourceObject(COMP_BACKGROUND_EMPHASIZE);
299 }
300
CompBackgroundNeutral() const301 Color TokenColors::CompBackgroundNeutral() const
302 {
303 return GetColorWithResourceObject(COMP_BACKGROUND_NEUTRAL);
304 }
305
CompEmphasizeSecondary() const306 Color TokenColors::CompEmphasizeSecondary() const
307 {
308 return GetColorWithResourceObject(COMP_EMPHASIZE_SECONDARY);
309 }
310
CompEmphasizeTertiary() const311 Color TokenColors::CompEmphasizeTertiary() const
312 {
313 return GetColorWithResourceObject(COMP_EMPHASIZE_TERTIARY);
314 }
315
CompDivider() const316 Color TokenColors::CompDivider() const
317 {
318 return GetColorWithResourceObject(COMP_DIVIDER);
319 }
320
CompCommonContrary() const321 Color TokenColors::CompCommonContrary() const
322 {
323 return GetColorWithResourceObject(COMP_COMMON_CONTRARY);
324 }
325
CompBackgroundFocus() const326 Color TokenColors::CompBackgroundFocus() const
327 {
328 return GetColorWithResourceObject(COMP_BACKGROUND_FOCUS);
329 }
330
CompFocusedPrimary() const331 Color TokenColors::CompFocusedPrimary() const
332 {
333 return GetColorWithResourceObject(COMP_FOCUSED_PRIMARY);
334 }
335
CompFocusedSecondary() const336 Color TokenColors::CompFocusedSecondary() const
337 {
338 return GetColorWithResourceObject(COMP_FOCUSED_SECONDARY);
339 }
340
CompFocusedTertiary() const341 Color TokenColors::CompFocusedTertiary() const
342 {
343 return GetColorWithResourceObject(COMP_FOCUSED_TERTIARY);
344 }
345
InteractiveHover() const346 Color TokenColors::InteractiveHover() const
347 {
348 return GetColorWithResourceObject(INTERACTIVE_HOVER);
349 }
350
InteractivePressed() const351 Color TokenColors::InteractivePressed() const
352 {
353 return GetColorWithResourceObject(INTERACTIVE_PRESSED);
354 }
355
InteractiveFocus() const356 Color TokenColors::InteractiveFocus() const
357 {
358 return GetColorWithResourceObject(INTERACTIVE_FOCUS);
359 }
360
InteractiveActive() const361 Color TokenColors::InteractiveActive() const
362 {
363 return GetColorWithResourceObject(INTERACTIVE_ACTIVE);
364 }
365
InteractiveSelect() const366 Color TokenColors::InteractiveSelect() const
367 {
368 return GetColorWithResourceObject(INTERACTIVE_SELECT);
369 }
370
InteractiveClick() const371 Color TokenColors::InteractiveClick() const
372 {
373 return GetColorWithResourceObject(INTERACTIVE_CLICK);
374 }
375
GetColorNameByIndex(int32_t idx)376 const char* TokenColors::GetColorNameByIndex(int32_t idx)
377 {
378 return GetTokenColorDataByIndex(idx).colorName;
379 }
380
GetSystemColorResIdByIndex(int32_t idx)381 int32_t TokenColors::GetSystemColorResIdByIndex(int32_t idx)
382 {
383 return GetTokenColorDataByIndex(idx).systemResourceId;
384 }
385
GetTokenColorDataByIndex(int32_t idx)386 const TokenColorData& TokenColors::GetTokenColorDataByIndex(int32_t idx)
387 {
388 return (idx >= 0 && idx < TokenColors::TOTAL_NUMBER) ? colorData[idx] : colorData[TokenColors::TOTAL_NUMBER];
389 }
390
GetByIndex(uint32_t idx)391 Color TokenColors::GetByIndex(uint32_t idx)
392 {
393 return (idx >= 0 && idx < colors_.size()) ? colors_[idx] : Color{};
394 }
395 } // namespace OHOS::Ace::NG
396