1 /*
2 * Copyright (c) 2023 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 "render/rs_color_picker.h"
17 #include "include/core/SkBitmap.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkImageFilter.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "include/core/SkCanvas.h"
22 #include "include/core/SkColor.h"
23 #include "include/core/SkColorFilter.h"
24 #include "include/core/SkColorSpace.h"
25 #include "include/core/SkImageInfo.h"
26 #include "include/core/SkPaint.h"
27 #include "include/core/SkPixmap.h"
28 #include "include/core/SkFont.h"
29 #include "include/core/SkTypeface.h"
30 #include <cmath>
31 #include <utility>
32
33 #include "platform/common/rs_log.h"
34
35 namespace OHOS {
36 namespace Rosen {
37
CreateColorPicker(const std::shared_ptr<Drawing::Pixmap> & pixmap,uint32_t & errorCode)38 std::shared_ptr<RSColorPicker> RSColorPicker::CreateColorPicker(const std::shared_ptr<Drawing::Pixmap>& pixmap,
39 uint32_t &errorCode)
40 {
41 if (pixmap == nullptr) {
42 ROSEN_LOGE("[ColorPicker]failed to create ColorPicker with null pixmap.");
43 errorCode = RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
44 return nullptr;
45 }
46
47 RSColorPicker *colorPicker = new (std::nothrow) RSColorPicker(pixmap);
48 if (colorPicker == nullptr) {
49 ROSEN_LOGE("[ColorPicker]failed to create ColorPicker with pixmap.");
50 errorCode = RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
51 return nullptr;
52 }
53
54 errorCode = RS_COLOR_PICKER_SUCCESS;
55 return std::shared_ptr<RSColorPicker>(colorPicker);
56 }
57
CreateColorPicker(const std::shared_ptr<Drawing::Pixmap> & pixmap,double * coordinates,uint32_t & errorCode)58 std::shared_ptr<RSColorPicker> RSColorPicker::CreateColorPicker(const std::shared_ptr<Drawing::Pixmap>& pixmap,
59 double* coordinates, uint32_t &errorCode)
60 {
61 if (pixmap == nullptr) {
62 ROSEN_LOGE("[ColorPicker]failed to create ColorPicker with null pixmap.");
63 errorCode = RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
64 return nullptr;
65 }
66
67 RSColorPicker *colorPicker = new (std::nothrow) RSColorPicker(pixmap, coordinates);
68 if (colorPicker == nullptr) {
69 ROSEN_LOGE("[ColorPicker]failed to create ColorPicker with pixmap.");
70 errorCode = RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
71 return nullptr;
72 }
73
74 errorCode = RS_COLOR_PICKER_SUCCESS;
75 return std::shared_ptr<RSColorPicker>(colorPicker);
76 }
77
RSColorPicker(std::shared_ptr<Drawing::Pixmap> pixmap)78 RSColorPicker::RSColorPicker(std::shared_ptr<Drawing::Pixmap> pixmap):RSColorExtract(pixmap) {}
RSColorPicker(std::shared_ptr<Drawing::Pixmap> pixmap,double * coordinates)79 RSColorPicker::RSColorPicker(
80 std::shared_ptr<Drawing::Pixmap> pixmap, double* coordinates):RSColorExtract(pixmap, coordinates) {}
81
GetLargestProportionColor(Drawing::ColorQuad & color) const82 uint32_t RSColorPicker::GetLargestProportionColor(Drawing::ColorQuad &color) const
83 {
84 if (featureColors_.empty()) {
85 return RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
86 }
87 color = featureColors_[0].first | 0xFF000000; // alpha = oxFF
88 return RS_COLOR_PICKER_SUCCESS;
89 }
90
GetHighestSaturationColor(Drawing::ColorQuad & color) const91 uint32_t RSColorPicker::GetHighestSaturationColor(Drawing::ColorQuad &color) const
92 {
93 if (featureColors_.empty()) {
94 return RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
95 }
96 uint32_t colorPicked = 0;
97 HSV hsv = {0};
98 double maxSaturation = 0.0;
99 for (size_t i = 0; i < featureColors_.size(); i++) {
100 hsv = RGB2HSV(featureColors_[i].first);
101 if (hsv.s >= maxSaturation) {
102 maxSaturation = hsv.s;
103 colorPicked = featureColors_[i].first;
104 }
105 }
106 color = colorPicked | 0xFF000000;
107 return RS_COLOR_PICKER_SUCCESS;
108 }
109
GetAverageColor(Drawing::ColorQuad & color) const110 uint32_t RSColorPicker::GetAverageColor(Drawing::ColorQuad &color) const
111 {
112 uint32_t colorPicked = 0;
113 uint32_t redSum = 0;
114 uint32_t greenSum = 0;
115 uint32_t blueSum = 0;
116 int totalPixelNum = 0;
117 if (featureColors_.empty()) {
118 return RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
119 }
120 for (size_t i = 0; i < featureColors_.size(); i++) {
121 totalPixelNum += featureColors_[i].second;
122 redSum += featureColors_[i].second * ((featureColors_[i].first >> ARGB_R_SHIFT) & ARGB_MASK);
123 greenSum += featureColors_[i].second * ((featureColors_[i].first >> ARGB_G_SHIFT) & ARGB_MASK);
124 blueSum += featureColors_[i].second * ((featureColors_[i].first >> ARGB_B_SHIFT) & ARGB_MASK);
125 }
126 if (totalPixelNum == 0) {
127 return RS_COLOR_PICKER_ERR_EFFECT_INVALID_VALUE;
128 }
129 uint32_t redMean = round(redSum / (float)totalPixelNum);
130 uint32_t greenMean = round(greenSum / (float)totalPixelNum);
131 uint32_t blueMean = round(blueSum / (float)totalPixelNum);
132 colorPicked = redMean << ARGB_R_SHIFT | greenMean << ARGB_G_SHIFT | blueMean << ARGB_B_SHIFT;
133 color = colorPicked | 0xFF000000;
134 return RS_COLOR_PICKER_SUCCESS;
135 }
136
IsBlackOrWhiteOrGrayColor(uint32_t color) const137 bool RSColorPicker::IsBlackOrWhiteOrGrayColor(uint32_t color) const
138 {
139 HSV hsv = RGB2HSV(color);
140 // A color is black, white or gray colr when its hsv satisfy (v>30, s<=5) or (15<v<=30, s<=15) or (v<=15).
141 if ((hsv.v > 30 && hsv.s <= 5) || (hsv.v > 15 && hsv.v <= 30 && hsv.s <= 15) || (hsv.v <= 15)) {
142 return true;
143 }
144 return false;
145 }
146
IsEquals(double val1,double val2) const147 bool RSColorPicker::IsEquals(double val1, double val2) const
148 {
149 // 0.001 is used for double number compare.
150 return fabs(val1 - val2) < 0.001;
151 }
152
153 // Transform RGB to HSV.
RGB2HSV(uint32_t rgb) const154 HSV RSColorPicker::RGB2HSV(uint32_t rgb) const
155 {
156 double r, g, b;
157 double h, s, v;
158 double minComponent, maxComponent;
159 double delta;
160 HSV hsv;
161 r = GetARGB32ColorR(rgb) / 255.0; // 255.0 is used to normalize color to [0, 1].
162 g = GetARGB32ColorG(rgb) / 255.0; // 255.0 is used to normalize color to [0, 1].
163 b = GetARGB32ColorB(rgb) / 255.0; // 255.0 is used to normalize color to [0, 1].
164 if (r > g) {
165 maxComponent = std::max(r, b);
166 minComponent = std::min(g, b);
167 } else {
168 maxComponent = std::max(g, b);
169 minComponent = std::min(r, b);
170 }
171 v = maxComponent;
172 delta = maxComponent - minComponent;
173
174 if (IsEquals(maxComponent, 0)) {
175 s = 0.0;
176 } else {
177 s = delta / maxComponent;
178 }
179
180 if (maxComponent == minComponent) {
181 h = 0.0;
182 } else {
183 if (delta == 0) {
184 return hsv;
185 }
186 if (IsEquals(r, maxComponent) && g >= b) {
187 h = 60 * (g - b) / delta + 0; // 60 is used to calculate color's hue, ranging between 0 and 360.
188 } else if (IsEquals(r, maxComponent) && g < b) {
189 h = 60 * (g - b) / delta + 360; // 60,360 is used to calculate color's hue, ranging between 0 and 360.
190 } else if (IsEquals(g, maxComponent)) {
191 h = 60 * (b - r) / delta + 120; // 60,120 is used to calculate color's hue, ranging between 0 and 360.
192 } else {
193 h = 60 * (r - g) / delta + 240; // 60,240 is used to calculate color's hue, ranging between 0 and 360.
194 }
195 }
196 hsv.h = (int)(h + 0.5); // Hue add 0.5 to round up.
197 hsv.h = (hsv.h > 359) ? (hsv.h - 360) : hsv.h; // 359 is used to adjust hue to range [0, 360].
198 hsv.h = (hsv.h < 0) ? (hsv.h + 360) : hsv.h; // Adjust hue to range [0, 360].
199 hsv.s = s * 100; // Adjust saturation to range [0, 100].
200 hsv.v = v * 100; // Adjust value to range [0, 100].
201
202 return hsv;
203 }
204
AdjustHSVToDefinedIterval(HSV & hsv) const205 void RSColorPicker::AdjustHSVToDefinedIterval(HSV& hsv) const
206 {
207 if (hsv.h > 360) { // Adjust hue to range [0, 360].
208 hsv.h = 360; // Adjust hue to range [0, 360].
209 }
210 if (hsv.h < 0) {
211 hsv.h = 0;
212 }
213 if (hsv.s > 100) { // Adjust saturation to range [0, 100].
214 hsv.s = 100; // Adjust saturation to range [0, 100].
215 }
216 if (hsv.s < 0) {
217 hsv.s = 0;
218 }
219 if (hsv.v > 100) { // Adjust value to range [0, 100].
220 hsv.v = 100; // Adjust value to range [0, 100].
221 }
222 if (hsv.v < 0) {
223 hsv.v = 0;
224 }
225 return;
226 }
227
228 // Transform HSV to RGB.
HSVtoRGB(HSV hsv) const229 uint32_t RSColorPicker::HSVtoRGB(HSV hsv) const
230 {
231 uint32_t r, g, b;
232 uint32_t rgb = 0;
233 AdjustHSVToDefinedIterval(hsv);
234
235 // The brightness is directly proportional to the maximum value that RGB can reach, which is 2.55 times.
236 float rgb_max = hsv.v * 2.55f;
237
238 /**
239 * Each time the saturation decreases from 100, the minimum value that RGB can achieve increases
240 * linearly by 1/100 from 0 to the maximum value set by the brightness.
241 */
242 float rgb_min = rgb_max * (100 - hsv.s) / 100.0f;
243
244 int i = hsv.h / 60;
245 int difs = hsv.h % 60;
246 float rgb_Adj = (rgb_max - rgb_min) * difs / 60.0f;
247
248 /**
249 * According to the change of H, there are six cases. In each case, a parameter in RBG is linearly
250 * transformed (increased or decreased) between the minimum and maximum values that can be achieved
251 * by RGB.
252 */
253 switch (i) {
254 case 0: // 0: when hue's range is [0, 60).
255 r = rgb_max;
256 g = rgb_min + rgb_Adj;
257 b = rgb_min;
258 break;
259 case 1: // 1: when hue's range is [60, 120).
260 r = rgb_max - rgb_Adj;
261 g = rgb_max;
262 b = rgb_min;
263 break;
264 case 2: // 2: when hue's range is [120, 180).
265 r = rgb_min;
266 g = rgb_max;
267 b = rgb_min + rgb_Adj;
268 break;
269 case 3: // 3: when hue's range is [180, 240).
270 r = rgb_min;
271 g = rgb_max - rgb_Adj;
272 b = rgb_max;
273 break;
274 case 4: // 4: when hue's range is [240, 300).
275 r = rgb_min + rgb_Adj;
276 g = rgb_min;
277 b = rgb_max;
278 break;
279 default:
280 r = rgb_max;
281 g = rgb_min;
282 b = rgb_max - rgb_Adj;
283 break;
284 }
285 rgb = r << ARGB_R_SHIFT | g << ARGB_G_SHIFT | b << ARGB_B_SHIFT;
286 return rgb;
287 }
288 } // namespace Rosen
289 } // namespace OHOS
290