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