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 "color_picker_common.h"
17
18 #include "effect_errors.h"
19 #include "effect_utils.h"
20 namespace OHOS {
21 namespace Rosen {
22 using namespace OHOS::ColorManager;
23 using namespace OHOS::Media;
24
25 namespace {
26 constexpr uint32_t REGION_SIZE = 4;
27 constexpr double PROPORTION_COLORS_NUM_LIMIT = 10.0; // proportion colors limit num 10
28 constexpr uint32_t MALLOC_FAILED = -1;
29 } // namespace
30 thread_local std::shared_ptr<ColorPickerCommon> ColorPickerCommon::sConstructor_ = nullptr;
31
CreateColorPicker(const std::shared_ptr<PixelMap> & pixmap,uint32_t & errorCode)32 std::shared_ptr<ColorPickerCommon> ColorPickerCommon::CreateColorPicker(
33 const std::shared_ptr<PixelMap>& pixmap, uint32_t& errorCode)
34 {
35 auto context = std::make_shared<ColorPickerCommon>();
36 if (context == nullptr) {
37 EFFECT_LOG_E("[ColorPickerCommon]failed to create ColorPickerCommon with null context.");
38 errorCode = MALLOC_FAILED;
39 return nullptr;
40 }
41
42 context->sColorPicker_ = ColorPicker::CreateColorPicker(pixmap, errorCode);
43 if (errorCode != SUCCESS) {
44 EFFECT_LOG_E("[ColorPickerCommon]failed to create ColorPickerCommon with invalid value");
45 return nullptr;
46 }
47 sConstructor_ = context;
48
49 return context;
50 }
51
CreateColorPicker(const std::shared_ptr<Media::PixelMap> & pixmap,std::vector<double> region,uint32_t & errorCode)52 std::shared_ptr<Rosen::ColorPickerCommon> ColorPickerCommon::CreateColorPicker(
53 const std::shared_ptr<Media::PixelMap>& pixmap, std::vector<double> region, uint32_t& errorCode)
54 {
55 auto context = std::make_shared<ColorPickerCommon>();
56 if (context == nullptr) {
57 EFFECT_LOG_E("[ColorPickerCommon]failed to create ColorPickerCommon with null context.");
58 errorCode = MALLOC_FAILED;
59 return nullptr;
60 }
61
62 double coordinatesBuffer[REGION_SIZE];
63 for (uint32_t i = 0; i < REGION_SIZE; i++) {
64 coordinatesBuffer[i] = std::clamp<double>(region[i], 0.0, 1.0);
65 }
66 context->sColorPicker_ = ColorPicker::CreateColorPicker(pixmap, coordinatesBuffer, errorCode);
67 if (errorCode != SUCCESS) {
68 EFFECT_LOG_E("[ColorPickerCommon]failed to create ColorPickerCommon with invalid value");
69 return nullptr;
70 }
71 sConstructor_ = context;
72
73 return context;
74 }
75
GetMainColor(ColorManager::Color & color)76 uint32_t ColorPickerCommon::GetMainColor(ColorManager::Color& color)
77 {
78 uint32_t errorCode = SUCCESS;
79 std::shared_ptr<Rosen::ColorPickerCommon> thisColorPicker = sConstructor_;
80 if (sConstructor_ != nullptr) {
81 thisColorPicker->nativeColorPicker_ = sConstructor_->sColorPicker_;
82 if (thisColorPicker->nativeColorPicker_ == nullptr) {
83 EFFECT_LOG_E("[ColorPickerCommon]GetMainColor, empty native ColorPicker");
84 errorCode = ERROR;
85 return errorCode;
86 }
87 errorCode = thisColorPicker->nativeColorPicker_->GetMainColor(color);
88 }
89 return errorCode;
90 }
91
GetLargestProportionColor(ColorManager::Color & color)92 uint32_t ColorPickerCommon::GetLargestProportionColor(ColorManager::Color& color)
93 {
94 uint32_t errorCode = SUCCESS;
95 std::shared_ptr<Rosen::ColorPickerCommon> thisColorPicker = sConstructor_;
96 if (thisColorPicker != nullptr) {
97 thisColorPicker->nativeColorPicker_ = sConstructor_->sColorPicker_;
98 if (thisColorPicker->nativeColorPicker_ == nullptr) {
99 EFFECT_LOG_E("[ColorPickerCommon]GetLargestProportionColor, empty native ColorPicker");
100 errorCode = ERROR;
101 return errorCode;
102 }
103 errorCode = thisColorPicker->nativeColorPicker_->GetLargestProportionColor(color);
104 }
105 return errorCode;
106 }
107
GetHighestSaturationColor(ColorManager::Color & color)108 uint32_t ColorPickerCommon::GetHighestSaturationColor(ColorManager::Color& color)
109 {
110 uint32_t errorCode = SUCCESS;
111 std::shared_ptr<Rosen::ColorPickerCommon> thisColorPicker = sConstructor_;
112 if (thisColorPicker != nullptr) {
113 thisColorPicker->nativeColorPicker_ = sConstructor_->sColorPicker_;
114 if (thisColorPicker->nativeColorPicker_ == nullptr) {
115 EFFECT_LOG_E("[ColorPickerCommon]GetHighestSaturationColor, empty native ColorPicker");
116 errorCode = ERROR;
117 return errorCode;
118 }
119 errorCode = thisColorPicker->nativeColorPicker_->GetHighestSaturationColor(color);
120 }
121 return errorCode;
122 }
123
GetAverageColor(ColorManager::Color & color)124 uint32_t ColorPickerCommon::GetAverageColor(ColorManager::Color& color)
125 {
126 uint32_t errorCode = SUCCESS;
127 std::shared_ptr<Rosen::ColorPickerCommon> thisColorPicker = sConstructor_;
128 if (thisColorPicker != nullptr) {
129 thisColorPicker->nativeColorPicker_ = sConstructor_->sColorPicker_;
130 if (thisColorPicker->nativeColorPicker_ == nullptr) {
131 EFFECT_LOG_E("[ColorPickerCommon]GetAverageColor, empty native ColorPicker");
132 errorCode = ERROR;
133 return errorCode;
134 }
135 errorCode = thisColorPicker->nativeColorPicker_->GetAverageColor(color);
136 }
137
138 return errorCode;
139 }
140
IsBlackOrWhiteOrGrayColor(uint32_t color,uint32_t & errorCode)141 bool ColorPickerCommon::IsBlackOrWhiteOrGrayColor(uint32_t color, uint32_t& errorCode)
142 {
143 bool rst = false;
144 std::shared_ptr<Rosen::ColorPickerCommon> thisColorPicker = sConstructor_;
145 if (thisColorPicker != nullptr) {
146 thisColorPicker->nativeColorPicker_ = sConstructor_->sColorPicker_;
147 if (thisColorPicker->nativeColorPicker_ == nullptr) {
148 EFFECT_LOG_E("[ColorPickerCommon]IsBlackOrWhiteOrGrayColor, empty native ColorPicker");
149 errorCode = ERROR;
150 return rst;
151 }
152 rst = thisColorPicker->nativeColorPicker_->IsBlackOrWhiteOrGrayColor(color);
153 }
154 return rst;
155 }
156
GetTopProportionColors(double colorCount,uint32_t & errorCode)157 std::vector<ColorManager::Color> ColorPickerCommon::GetTopProportionColors(double colorCount, uint32_t& errorCode)
158 {
159 unsigned int colorsNum = 0;
160 std::vector<ColorManager::Color> colors = {};
161 std::shared_ptr<Rosen::ColorPickerCommon> thisColorPicker = sConstructor_;
162 if (thisColorPicker != nullptr) {
163 thisColorPicker->nativeColorPicker_ = sConstructor_->sColorPicker_;
164 if (thisColorPicker->nativeColorPicker_ == nullptr) {
165 EFFECT_LOG_E("[ColorPickerCommon]GetTopProportionColors, empty native ColorPicker");
166 errorCode = ERROR;
167 return colors;
168 }
169 }
170 colorsNum = static_cast<unsigned int>(std::clamp(colorCount, 0.0, PROPORTION_COLORS_NUM_LIMIT));
171 colors = thisColorPicker->nativeColorPicker_->GetTopProportionColors(colorsNum);
172
173 return colors;
174 }
175
176 } // namespace Rosen
177 } // namespace OHOS