1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "WallpaperJSUtil"
16 #include "wallpaper_js_util.h"
17 #include "export/color.h"
18 #include "hilog_wrapper.h"
19
20 namespace OHOS::WallpaperNAPI {
21 constexpr const uint32_t COLOR_MASK{ 0xFF };
22
Convert2String(napi_env env,napi_value jsString)23 std::string WallpaperJSUtil::Convert2String(napi_env env, napi_value jsString)
24 {
25 size_t maxLen = WallpaperJSUtil::MAX_LEN;
26 napi_status status = napi_get_value_string_utf8(env, jsString, NULL, 0, &maxLen);
27 if (status != napi_ok) {
28 GET_AND_THROW_LAST_ERROR((env));
29 maxLen = WallpaperJSUtil::MAX_LEN;
30 }
31 if (maxLen == 0) {
32 return std::string();
33 }
34 char *buf = new (std::nothrow) char[maxLen + 1]();
35 if (buf == nullptr) {
36 return std::string();
37 }
38 size_t len = 0;
39 status = napi_get_value_string_utf8(env, jsString, buf, maxLen + 1, &len);
40 if (status != napi_ok) {
41 GET_AND_THROW_LAST_ERROR((env));
42 }
43 buf[len] = 0;
44 std::string value(buf);
45 delete[] buf;
46 return value;
47 }
48
Convert2JSRgbaArray(napi_env env,const std::vector<uint64_t> & color)49 napi_value WallpaperJSUtil::Convert2JSRgbaArray(napi_env env, const std::vector<uint64_t> &color)
50 {
51 HILOG_DEBUG("Convert2JSRgbaArray in");
52 napi_value result = nullptr;
53 napi_status status = napi_create_array_with_length(env, color.size(), &result);
54 if (status != napi_ok) {
55 HILOG_DEBUG("Convert2JSRgbaArray failed");
56 return nullptr;
57 }
58 int index = 0;
59 for (const auto it : color) {
60 HILOG_DEBUG("Convert2JSRgbaArray for");
61 ColorManager::Color colors(it);
62 napi_value red = nullptr;
63 napi_value green = nullptr;
64 napi_value blue = nullptr;
65 napi_value alpha = nullptr;
66 napi_create_int32(env, static_cast<int32_t>(colors.r * COLOR_MASK), &red);
67 napi_create_int32(env, static_cast<int32_t>(colors.g * COLOR_MASK), &green);
68 napi_create_int32(env, static_cast<int32_t>(colors.b * COLOR_MASK), &blue);
69 napi_create_int32(env, static_cast<int32_t>(colors.a * COLOR_MASK), &alpha);
70 napi_value element = nullptr;
71 napi_create_object(env, &element);
72 napi_set_named_property(env, element, "red", red);
73 napi_set_named_property(env, element, "green", green);
74 napi_set_named_property(env, element, "blue", blue);
75 napi_set_named_property(env, element, "alpha", alpha);
76 napi_set_element(env, result, index++, element);
77 }
78 return result;
79 }
80 } // namespace OHOS::WallpaperNAPI