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
16 #include "color_picker.h"
17 #include "hilog/log.h"
18 #include "effect_errors.h"
19 #include "effect_utils.h"
20 #include "color.h"
21 #include "pixel_map.h"
22 #include "include/core/SkBitmap.h"
23 #include "include/core/SkRect.h"
24 #include "include/core/SkImageFilter.h"
25 #include "include/effects/SkImageFilters.h"
26 #include "include/core/SkCanvas.h"
27 #include "include/core/SkColor.h"
28 #include "include/core/SkColorFilter.h"
29 #include "include/core/SkColorSpace.h"
30 #include "include/core/SkImageInfo.h"
31 #include "include/core/SkPaint.h"
32 #include "include/core/SkPixmap.h"
33 #include "include/core/SkFont.h"
34 #include "include/core/SkTypeface.h"
35 #include <cmath>
36 #include <utility>
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 namespace OHOS {
43 namespace Rosen {
44 using OHOS::HiviewDFX::HiLog;
45
CreateScaledPixelMap(const std::shared_ptr<Media::PixelMap> & pixmap)46 std::shared_ptr<Media::PixelMap> ColorPicker::CreateScaledPixelMap(const std::shared_ptr<Media::PixelMap>& pixmap)
47 {
48 // Create scaled pixelmap
49 if (pixmap == nullptr) {
50 EFFECT_LOG_I("[ColorPicker]failed to create ScaledPixelMap with null pixmap.");
51 return nullptr;
52 }
53 OHOS::Media::InitializationOptions options;
54 options.alphaType = pixmap->GetAlphaType();
55 options.pixelFormat = pixmap->GetPixelFormat();
56 options.scaleMode = OHOS::Media::ScaleMode::FIT_TARGET_SIZE;
57 options.size.width = 100; // 100 represents scaled pixelMap's width
58 options.size.height = 100; // 100 represents scaled pixelMap's height
59 options.editable = true;
60 std::unique_ptr<Media::PixelMap> newPixelMap = Media::PixelMap::Create(*pixmap.get(), options);
61 return std::move(newPixelMap);
62 }
63
CreateColorPicker(const std::shared_ptr<Media::PixelMap> & pixmap,uint32_t & errorCode)64 std::shared_ptr<ColorPicker> ColorPicker::CreateColorPicker(const std::shared_ptr<Media::PixelMap>& pixmap,
65 uint32_t &errorCode)
66 {
67 if (pixmap == nullptr) {
68 EFFECT_LOG_I("[ColorPicker]failed to create ColorPicker with null pixmap.");
69 errorCode = ERR_EFFECT_INVALID_VALUE;
70 return nullptr;
71 }
72
73 std::shared_ptr<Media::PixelMap> scaledPixelMap = CreateScaledPixelMap(pixmap);
74 if (scaledPixelMap == nullptr) {
75 EFFECT_LOG_I("[ColorPicker]failed to scale pixelmap, scaledPixelMap is nullptr.");
76 errorCode = ERR_EFFECT_INVALID_VALUE;
77 return nullptr;
78 }
79 std::shared_ptr<ColorPicker> colorPicker = std::make_shared<ColorPicker>(scaledPixelMap);
80 errorCode = SUCCESS;
81 return colorPicker;
82 }
83
CreateColorPicker(const std::shared_ptr<Media::PixelMap> & pixmap,double * coordinates,uint32_t & errorCode)84 std::shared_ptr<ColorPicker> ColorPicker::CreateColorPicker(const std::shared_ptr<Media::PixelMap>& pixmap,
85 double* coordinates, uint32_t &errorCode)
86 {
87 if (pixmap == nullptr) {
88 EFFECT_LOG_I("[ColorPicker]failed to create ColorPicker with null pixmap.");
89 errorCode = ERR_EFFECT_INVALID_VALUE;
90 return nullptr;
91 }
92
93 std::shared_ptr<Media::PixelMap> scaledPixelMap = CreateScaledPixelMap(pixmap);
94 std::shared_ptr<ColorPicker> colorPicker = std::make_shared<ColorPicker>(scaledPixelMap, coordinates);
95 errorCode = SUCCESS;
96 return colorPicker;
97 }
98
GetScaledPixelMap()99 std::shared_ptr<Media::PixelMap> ColorPicker::GetScaledPixelMap()
100 {
101 // Create scaled pixelmap
102 OHOS::Media::InitializationOptions options;
103 options.alphaType = pixelmap_->GetAlphaType();
104 options.pixelFormat = pixelmap_->GetPixelFormat();
105 options.scaleMode = OHOS::Media::ScaleMode::FIT_TARGET_SIZE;
106 options.size.width = 1;
107 options.size.height = 1;
108 options.editable = true;
109 std::unique_ptr<Media::PixelMap> newPixelMap = Media::PixelMap::Create(*pixelmap_.get(), options);
110 return std::move(newPixelMap);
111 }
112
GetMainColor(ColorManager::Color & color)113 uint32_t ColorPicker::GetMainColor(ColorManager::Color &color)
114 {
115 if (pixelmap_ == nullptr) {
116 return ERR_EFFECT_INVALID_VALUE;
117 }
118
119 // get color
120 uint32_t colorVal = 0;
121 int x = 0;
122 int y = 0;
123 std::shared_ptr<Media::PixelMap> pixelMap = GetScaledPixelMap();
124 if (pixelMap == nullptr) {
125 EFFECT_LOG_E("ColorPicker::GetMainColor pixelMap is nullptr");
126 return ERR_EFFECT_INVALID_VALUE;
127 }
128
129 bool bSucc = pixelMap->GetARGB32Color(x, y, colorVal);
130 EFFECT_LOG_I("[newpix].argb.ret=%{public}d, %{public}x", bSucc, colorVal);
131 color = ColorManager::Color(colorVal);
132 return SUCCESS;
133 }
134
ColorPicker(std::shared_ptr<Media::PixelMap> pixmap)135 ColorPicker::ColorPicker(std::shared_ptr<Media::PixelMap> pixmap):ColorExtract(pixmap) {}
ColorPicker(std::shared_ptr<Media::PixelMap> pixmap,double * coordinates)136 ColorPicker::ColorPicker(
137 std::shared_ptr<Media::PixelMap> pixmap, double* coordinates):ColorExtract(pixmap, coordinates) {}
138
GetLargestProportionColor(ColorManager::Color & color) const139 uint32_t ColorPicker::GetLargestProportionColor(ColorManager::Color &color) const
140 {
141 if (featureColors_.empty()) {
142 return ERR_EFFECT_INVALID_VALUE;
143 }
144 color = ColorManager::Color(featureColors_[0].first | 0xFF000000); // alpha = oxFF
145 return SUCCESS;
146 }
147
GetHighestSaturationColor(ColorManager::Color & color) const148 uint32_t ColorPicker::GetHighestSaturationColor(ColorManager::Color &color) const
149 {
150 if (featureColors_.empty()) {
151 return ERR_EFFECT_INVALID_VALUE;
152 }
153 uint32_t colorPicked = 0;
154 HSV hsv = {0};
155 double maxSaturation = 0.0;
156 for (size_t i = 0; i < featureColors_.size(); i++) {
157 hsv = RGB2HSV(featureColors_[i].first);
158 if (hsv.s >= maxSaturation) {
159 maxSaturation = hsv.s;
160 colorPicked = featureColors_[i].first;
161 }
162 }
163 color = ColorManager::Color(colorPicked | 0xFF000000);
164 return SUCCESS;
165 }
166
GetAverageColor(ColorManager::Color & color) const167 uint32_t ColorPicker::GetAverageColor(ColorManager::Color &color) const
168 {
169 uint32_t colorPicked = 0;
170 uint32_t redSum = 0;
171 uint32_t greenSum = 0;
172 uint32_t blueSum = 0;
173 int totalPixelNum = 0;
174 if (featureColors_.empty()) {
175 return ERR_EFFECT_INVALID_VALUE;
176 }
177 for (size_t i = 0; i < featureColors_.size(); i++) {
178 totalPixelNum += featureColors_[i].second;
179 redSum += featureColors_[i].second * ((featureColors_[i].first >> ARGB_R_SHIFT) & ARGB_MASK);
180 greenSum += featureColors_[i].second * ((featureColors_[i].first >> ARGB_G_SHIFT) & ARGB_MASK);
181 blueSum += featureColors_[i].second * ((featureColors_[i].first >> ARGB_B_SHIFT) & ARGB_MASK);
182 }
183 if (totalPixelNum == 0) {
184 return ERR_EFFECT_INVALID_VALUE;
185 }
186 uint32_t redMean = round(redSum / (float)totalPixelNum);
187 uint32_t greenMean = round(greenSum / (float)totalPixelNum);
188 uint32_t blueMean = round(blueSum / (float)totalPixelNum);
189 colorPicked = redMean << ARGB_R_SHIFT | greenMean << ARGB_G_SHIFT | blueMean << ARGB_B_SHIFT;
190 color = ColorManager::Color(colorPicked | 0xFF000000);
191 return SUCCESS;
192 }
193
IsBlackOrWhiteOrGrayColor(uint32_t color) const194 bool ColorPicker::IsBlackOrWhiteOrGrayColor(uint32_t color) const
195 {
196 HSV hsv = RGB2HSV(color);
197 // 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).
198 if ((hsv.v > 30 && hsv.s <= 5) || (hsv.v > 15 && hsv.v <= 30 && hsv.s <= 15) || (hsv.v <= 15)) {
199 return true;
200 }
201 return false;
202 }
203
204 // Transfrom rgb to ligthtness
RGB2GRAY(uint32_t color) const205 uint32_t ColorPicker::RGB2GRAY(uint32_t color) const
206 {
207 uint32_t r = GetARGB32ColorR(color);
208 uint32_t g = GetARGB32ColorG(color);
209 uint32_t b = GetARGB32ColorB(color);
210 return static_cast<uint32_t>(r * GRAY_RATIO_RED + g * GRAY_RATIO_GREEN + b * GRAY_RATIO_BLUE);
211 }
212 // Calculate Lightness Variance
CalcGrayVariance() const213 uint32_t ColorPicker::CalcGrayVariance() const
214 {
215 long long int grayVariance = 0;
216
217 ColorManager::Color color;
218 bool rst = GetAverageColor(color);
219 if (rst != SUCCESS) {
220 return ERR_EFFECT_INVALID_VALUE;
221 }
222 uint32_t averageColor = ((color.PackValue() >> 32) & 0xFFFFFFFF);
223 uint32_t averageGray = RGB2GRAY(averageColor);
224 for (size_t i = 0; i < featureColors_.size(); i++) {
225 // 2 is square
226 grayVariance += pow(static_cast<long long int>(RGB2GRAY(featureColors_[i].first)) - averageGray, 2) *
227 featureColors_[i].second;
228 }
229 grayVariance /= colorValLen_;
230 return static_cast<uint32_t>(grayVariance);
231 }
232
233 // Relative luminance calculation, normalized to 0 - 1
CalcRelaticeLuminance(uint32_t color) const234 double ColorPicker::CalcRelaticeLuminance(uint32_t color) const
235 {
236 uint32_t r = GetARGB32ColorR(color);
237 uint32_t g = GetARGB32ColorG(color);
238 uint32_t b = GetARGB32ColorB(color);
239 return (r * LUMINANCE_RATIO_RED + g * LUMINANCE_RATIO_GREEN + b * LUMINANCE_RATIO_BLUE) / 255; // 255 is max value.
240 }
241
CalcContrastRatioWithWhite() const242 double ColorPicker::CalcContrastRatioWithWhite() const
243 {
244 double lightColorDegree = 0;
245 for (size_t i = 0; i < featureColors_.size(); i++) {
246 // 0.05 is used to calculate contrast ratio.
247 lightColorDegree += (((1 + 0.05) / (CalcRelaticeLuminance(featureColors_[i].first) + 0.05))
248 * featureColors_[i].second);
249 }
250 lightColorDegree /= colorValLen_;
251 return lightColorDegree;
252 }
253
254 // Discriminate wallpaper color shade mode
DiscriminatePitureLightDegree(PictureLightColorDegree & degree) const255 uint32_t ColorPicker::DiscriminatePitureLightDegree(PictureLightColorDegree °ree) const
256 {
257 if (featureColors_.empty()) {
258 return ERR_EFFECT_INVALID_VALUE;
259 }
260 uint32_t grayVariance = grayMsd_;
261 // Gray variance less than 6000 means not extremly flowerly picture.
262 if (grayVariance < 6000) {
263 double lightColorDegree = contrastToWhite_;
264 // LightColorDegree less than 1.5 means extremly light color picture.
265 if (lightColorDegree < 1.5) {
266 degree = EXTREMELY_LIGHT_COLOR_PICTURE;
267 // LightColorDegree between 1.5 and 1.9 means light color picture.
268 } else if (lightColorDegree >= 1.5 && lightColorDegree < 1.9) {
269 degree = LIGHT_COLOR_PICTURE;
270 // LightColorDegree between 1.9 and 7 means flowerly picture.
271 } else if (lightColorDegree >= 1.9 && lightColorDegree <= 7) {
272 degree = FLOWERY_PICTURE;
273 } else {
274 // GrayVariance more than 3000 means dark color picture.
275 if (grayVariance >= 3000) {
276 degree = DARK_COLOR_PICTURE;
277 } else {
278 degree = EXTREMELY_DARK_COLOR_PICTURE;
279 }
280 }
281 } else {
282 degree = EXTREMELY_FLOWERY_PICTURE;
283 }
284 return SUCCESS;
285 }
286
287 // Reverse picture color
GetReverseColor(ColorManager::Color & color) const288 uint32_t ColorPicker::GetReverseColor(ColorManager::Color &color) const
289 {
290 PictureLightColorDegree lightColorDegree;
291 bool rst = DiscriminatePitureLightDegree(lightColorDegree);
292 if (rst != SUCCESS) {
293 return ERR_EFFECT_INVALID_VALUE;
294 }
295 if (lightColorDegree == EXTREMELY_LIGHT_COLOR_PICTURE) {
296 uint32_t black = 0xFF000000;
297 color = ColorManager::Color(black);
298 return SUCCESS;
299 } else {
300 uint32_t white = 0xFFFFFFFF;
301 color = ColorManager::Color(white);
302 return SUCCESS;
303 }
304 };
305
GenerateMorandiBackgroundColor(HSV & hsv) const306 void ColorPicker::GenerateMorandiBackgroundColor(HSV& hsv) const
307 {
308 hsv.s = 9; // 9 is morandi background color's saturation.
309 hsv.v = 84; // 84 is morandi background color's value.
310 return;
311 }
312
313 // Get morandi background color
GetMorandiBackgroundColor(ColorManager::Color & color) const314 uint32_t ColorPicker::GetMorandiBackgroundColor(ColorManager::Color &color) const
315 {
316 bool rst = GetLargestProportionColor(color);
317 if (rst != SUCCESS) {
318 return ERR_EFFECT_INVALID_VALUE;
319 }
320 uint32_t mostColor = ((color.PackValue() >> 32) & 0xFFFFFFFF);
321 HSV hsv = RGB2HSV(mostColor);
322 bool isBWGColor = IsBlackOrWhiteOrGrayColor(mostColor);
323 if (isBWGColor) {
324 uint32_t nextMostColor = 0;
325 uint32_t nextMostColorCnt = 0;
326 bool isExsitColor = false;
327 for (size_t i = 0; i < featureColors_.size(); i++) {
328 if (!IsBlackOrWhiteOrGrayColor(featureColors_[i].first) && featureColors_[i].second > nextMostColorCnt) {
329 nextMostColor = featureColors_[i].first;
330 nextMostColorCnt = featureColors_[i].second;
331 isExsitColor = true;
332 }
333 }
334 if (isExsitColor) {
335 HSV nextColorHsv = RGB2HSV(nextMostColor);
336 GenerateMorandiBackgroundColor(nextColorHsv);
337 nextMostColor = HSVtoRGB(nextColorHsv);
338 color = ColorManager::Color(nextMostColor | 0xFF000000);
339 return SUCCESS;
340 } else {
341 hsv.s = 0;
342 hsv.v = 77;
343 mostColor = HSVtoRGB(hsv);
344 color = ColorManager::Color(mostColor | 0xFF000000);
345 return SUCCESS;
346 }
347 } else {
348 GenerateMorandiBackgroundColor(hsv);
349 mostColor = HSVtoRGB(hsv);
350 color = ColorManager::Color(mostColor | 0xFF000000);
351 return SUCCESS;
352 }
353 }
354
GenerateMorandiShadowColor(HSV & hsv) const355 void ColorPicker::GenerateMorandiShadowColor(HSV& hsv) const
356 {
357 // When hue between 20 and 60, adjust s and v.
358 if (hsv.h > 20 && hsv.h <= 60) {
359 hsv.s = 53; // Adjust saturation to 53.
360 hsv.v = 46; // Adjust value to 46.
361 // When hue between 60 and 190, adjust s and v.
362 } else if (hsv.h > 60 && hsv.h <= 190) {
363 hsv.s = 23; // Adjust saturation to 23.
364 hsv.v = 36; // Adjust value to 36.
365 // When hue between 190 and 270, adjust s and v.
366 } else if (hsv.h > 190 && hsv.h <= 270) {
367 hsv.s = 34; // Adjust saturation to 34.
368 hsv.v = 35; // Adjust value to 35.
369 } else {
370 hsv.s = 48; // Adjust saturation to 48.
371 hsv.v = 40; // Adjust value to 40.
372 }
373 }
374
375 // Get morandi shadow color
GetMorandiShadowColor(ColorManager::Color & color) const376 uint32_t ColorPicker::GetMorandiShadowColor(ColorManager::Color &color) const
377 {
378 bool rst = GetLargestProportionColor(color);
379 if (rst != SUCCESS) {
380 return ERR_EFFECT_INVALID_VALUE;
381 }
382 uint32_t mostColor = ((color.PackValue() >> 32) & 0xFFFFFFFF);
383
384 HSV hsv = RGB2HSV(mostColor);
385 bool isBWGColor = IsBlackOrWhiteOrGrayColor(mostColor);
386 if (isBWGColor) {
387 uint32_t nextMostColor = 0;
388 uint32_t nextMostColorCnt = 0;
389 bool isExsitColor = false;
390 for (size_t i = 0; i < featureColors_.size(); i++) {
391 if (!IsBlackOrWhiteOrGrayColor(featureColors_[i].first) && featureColors_[i].second > nextMostColorCnt) {
392 nextMostColor = featureColors_[i].first;
393 nextMostColorCnt = featureColors_[i].second;
394 isExsitColor = true;
395 }
396 }
397 if (isExsitColor) {
398 HSV nextColorHsv = RGB2HSV(nextMostColor);
399 GenerateMorandiShadowColor(nextColorHsv);
400 nextMostColor = HSVtoRGB(nextColorHsv);
401 color = ColorManager::Color(nextMostColor | 0xFF000000);
402 return SUCCESS;
403 } else {
404 hsv.s = 0;
405 hsv.v = 26;
406 mostColor = HSVtoRGB(hsv);
407 color = ColorManager::Color(mostColor | 0xFF000000);
408 return SUCCESS;
409 }
410 } else {
411 GenerateMorandiShadowColor(hsv);
412 mostColor = HSVtoRGB(hsv);
413 color = ColorManager::Color(mostColor | 0xFF000000);
414 return SUCCESS;
415 }
416 }
417
DiscriminateDarkOrBrightColor(const HSV & hsv) const418 ColorBrightnessMode ColorPicker::DiscriminateDarkOrBrightColor(const HSV& hsv) const
419 {
420 // 80 is dark color judgement condition.
421 if (hsv.v <= 80) {
422 return ColorBrightnessMode::DARK_COLOR;
423 } else {
424 // 20 and 50 is color judgement condition.
425 if (hsv.h > 20 && hsv.h <= 50) {
426 // 60 is color judgement condition.
427 if (hsv.s > 60) {
428 return ColorBrightnessMode::HIGH_SATURATION_BRIGHT_COLOR;
429 } else {
430 return ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR;
431 }
432 // 50 and 269 is color judgement condition.
433 } else if (hsv.h > 50 && hsv.h <= 269) {
434 // 40 is color judgement condition.
435 if (hsv.s > 40) {
436 return ColorBrightnessMode::DARK_COLOR;
437 } else {
438 return ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR;
439 }
440 } else {
441 // // 50 is color judgement condition.
442 if (hsv.s > 50) {
443 return ColorBrightnessMode::HIGH_SATURATION_BRIGHT_COLOR;
444 } else {
445 return ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR;
446 }
447 }
448 }
449 }
450
ProcessToBrightColor(HSV & hsv) const451 void ColorPicker::ProcessToBrightColor(HSV& hsv) const
452 {
453 // Value less than 95, no process.
454 if (hsv.v < 95) {
455 return;
456 // Value more than 95, adjust to 95.
457 } else {
458 hsv.v = 95; // 95 is min value.
459 }
460 }
461
AdjustToBasicColor(HSV & hsv,double basicS,double basicV) const462 void ColorPicker::AdjustToBasicColor(HSV& hsv, double basicS, double basicV) const
463 {
464 double x = hsv.s + hsv.v;
465 double y = basicS + basicV;
466 if (x <= y) {
467 return;
468 } else {
469 double z = x - y;
470 hsv.s = hsv.s - hsv.s / x * z;
471 hsv.v = hsv.v - hsv.v / x * z;
472 return;
473 }
474 }
ProcessToDarkColor(HSV & hsv) const475 void ColorPicker::ProcessToDarkColor(HSV& hsv) const
476 {
477 // 18 and 69 is basic color threshold.
478 if (hsv.h >= 18 && hsv.h <= 69) {
479 AdjustToBasicColor(hsv, 70, 60); // 70 and 60 is basic color's s and v
480 // 69 and 189 is basic color threshold.
481 } else if (hsv.h > 69 && hsv.h <= 189) {
482 AdjustToBasicColor(hsv, 50, 50); // 50 is basic color's s and v
483 // 189 and 269 is basic color threshold.
484 } else if (hsv.h > 189 && hsv.h <= 269) {
485 AdjustToBasicColor(hsv, 70, 70); // 70 is basic color's s and v
486 } else {
487 AdjustToBasicColor(hsv, 60, 60); // 60 is basic color's s and v
488 }
489 }
490
AdjustLowSaturationBrightColor(HSV & colorHsv,HSV & mainHsv,HSV & secondaryHsv,const std::pair<uint32_t,uint32_t> & mainColor,const std::pair<uint32_t,uint32_t> & secondaryColor) const491 void ColorPicker::AdjustLowSaturationBrightColor(HSV &colorHsv, HSV &mainHsv, HSV &secondaryHsv,
492 const std::pair<uint32_t, uint32_t> &mainColor,
493 const std::pair<uint32_t, uint32_t> &secondaryColor) const
494 {
495 if (colorHsv.s < 10) { // 10 is the saturate's threshold
496 ColorBrightnessMode secondaryColorBrightmode = DiscriminateDarkOrBrightColor(secondaryHsv);
497 if (secondaryColorBrightmode == ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR) {
498 ProcessToBrightColor(mainHsv);
499 colorHsv = mainHsv;
500 } else {
501 // 10 used to calculate threshold.
502 if (mainColor.second - secondaryColor.second > colorValLen_ / 10) {
503 ProcessToBrightColor(mainHsv);
504 colorHsv = mainHsv;
505 } else {
506 secondaryHsv.s = 10; // Adjust secondary color's s to 10
507 secondaryHsv.v = 95; // Adjust secondary color's v to 95
508 colorHsv = secondaryHsv;
509 }
510 }
511 } else {
512 ProcessToBrightColor(mainHsv);
513 colorHsv = mainHsv;
514 }
515 }
516
517 // Get immersive background color
GetImmersiveBackgroundColor(ColorManager::Color & color) const518 uint32_t ColorPicker::GetImmersiveBackgroundColor(ColorManager::Color &color) const
519 {
520 uint32_t colorPicked = 0;
521 HSV colorHsv;
522 std::pair<uint32_t, uint32_t> mainColor;
523 std::pair<uint32_t, uint32_t> secondaryColor;
524 if (featureColors_.empty()) {
525 return ERR_EFFECT_INVALID_VALUE;
526 }
527 bool hasMainColor = GetDominantColor(mainColor, secondaryColor);
528 HSV mainHsv = RGB2HSV(mainColor.first);
529 HSV secondaryHsv = RGB2HSV(secondaryColor.first);
530 if (hasMainColor || (mainHsv.s >= secondaryHsv.s)) {
531 colorHsv = mainHsv;
532 } else {
533 colorHsv = secondaryHsv;
534 }
535 ColorBrightnessMode colorBrightmode = DiscriminateDarkOrBrightColor(colorHsv);
536 switch (colorBrightmode) {
537 case ColorBrightnessMode::HIGH_SATURATION_BRIGHT_COLOR:
538 ProcessToDarkColor(colorHsv);
539 break;
540 case ColorBrightnessMode::LOW_SATURATION_BRIGHT_COLOR:
541 AdjustLowSaturationBrightColor(colorHsv, mainHsv, secondaryHsv, mainColor, secondaryColor);
542 break;
543 case ColorBrightnessMode::DARK_COLOR:
544 ProcessToDarkColor(colorHsv);
545 break;
546 default:
547 break;
548 }
549 colorPicked = HSVtoRGB(colorHsv);
550 color = ColorManager::Color(colorPicked | 0xFF000000);
551 return SUCCESS;
552 }
553
554 // Get immersive foreground color
GetImmersiveForegroundColor(ColorManager::Color & color) const555 uint32_t ColorPicker::GetImmersiveForegroundColor(ColorManager::Color &color) const
556 {
557 // Get mask color
558 bool rst = GetImmersiveBackgroundColor(color);
559 if (rst != SUCCESS) {
560 return ERR_EFFECT_INVALID_VALUE;
561 }
562 uint32_t colorPicked = ((color.PackValue() >> 32) & 0xFFFFFFFF);
563
564 HSV colorHsv = RGB2HSV(colorPicked);
565 ColorBrightnessMode ColorBrightmode = DiscriminateDarkOrBrightColor(colorHsv);
566 if ((ColorBrightmode == ColorBrightnessMode::HIGH_SATURATION_BRIGHT_COLOR) ||
567 (ColorBrightmode == ColorBrightnessMode::DARK_COLOR)) {
568 ProcessToDarkColor(colorHsv);
569 if (colorHsv.s >= 20) { // 20 is saturation threshold.
570 colorHsv.s = 20; // Adjust saturation to 20
571 colorHsv.v = 100;
572 } else {
573 colorHsv.v = 100;
574 }
575 } else {
576 ProcessToBrightColor(colorHsv);
577 colorHsv.s = 30; // Adjust saturation to 30.
578 colorHsv.v = 40; // Adjust value to 40.
579 }
580 colorPicked = HSVtoRGB(colorHsv);
581 color = ColorManager::Color(colorPicked | 0xFF000000);
582 return SUCCESS;
583 }
584
GetDominantColor(std::pair<uint32_t,uint32_t> & mainColor,std::pair<uint32_t,uint32_t> & secondaryColor) const585 bool ColorPicker::GetDominantColor(
586 std::pair<uint32_t, uint32_t>& mainColor, std::pair<uint32_t, uint32_t>& secondaryColor) const
587 {
588 if (featureColors_.empty()) {
589 mainColor.first = 0;
590 mainColor.second = 0;
591 secondaryColor.first = 0;
592 secondaryColor.second = 0;
593 return false;
594 }
595 if (featureColors_.size() == 1) {
596 mainColor.first = featureColors_[0].first;
597 mainColor.second = featureColors_[0].second;
598 secondaryColor.first = 0;
599 secondaryColor.second = 0;
600 return true;
601 } else {
602 mainColor.first = featureColors_[0].first;
603 mainColor.second = featureColors_[0].second;
604 secondaryColor.first = featureColors_[1].first;
605 secondaryColor.second = featureColors_[1].second;
606 // 20 used to calculate threshold.
607 if (mainColor.second - secondaryColor.second > colorValLen_ / 20) {
608 return true;
609 }
610 return false;
611 }
612 }
613
614 // Gradient Mask Coloring - Deepening the Immersion Color (Fusing with the Background but Deeper than the Background)
GetDeepenImmersionColor(ColorManager::Color & color) const615 uint32_t ColorPicker::GetDeepenImmersionColor(ColorManager::Color &color) const
616 {
617 uint32_t colorPicked = 0;
618 std::pair<uint32_t, uint32_t> mainColor;
619 std::pair<uint32_t, uint32_t> secondaryColor;
620 if (featureColors_.empty()) {
621 return ERR_EFFECT_INVALID_VALUE;
622 }
623 bool hasMainColor = GetDominantColor(mainColor, secondaryColor);
624 if (hasMainColor) {
625 HSV hsv = RGB2HSV(mainColor.first);
626 if (hsv.v >= 40) { // 40 is value threshold.
627 hsv.v = 30; // Adjust value to 30.
628 } else if (hsv.v >= 20 && hsv.v < 40) { // 20, 40 is value threshold.
629 hsv.v -= 10; // 10 used to decrease value.
630 } else {
631 hsv.v += 20; // 20 used to increse saturation.
632 }
633 colorPicked = HSVtoRGB(hsv);
634 } else {
635 // If there is no dominant color, return black-0x00000000
636 colorPicked = 0xFF000000;
637 }
638 color = ColorManager::Color(colorPicked | 0xFF000000);
639 return SUCCESS;
640 }
641
642 // Get top proportion colors
GetTopProportionColors(uint32_t colorsNum) const643 std::vector<ColorManager::Color> ColorPicker::GetTopProportionColors(uint32_t colorsNum) const
644 {
645 if (featureColors_.empty() || colorsNum == 0) {
646 return {};
647 }
648 std::vector<ColorManager::Color> colors;
649 uint32_t num = std::min(static_cast<uint32_t>(featureColors_.size()), colorsNum);
650
651 for (uint32_t i = 0; i < num; ++i) {
652 colors.emplace_back(ColorManager::Color(featureColors_[i].first | 0xFF000000));
653 }
654 return colors;
655 };
656
IsEquals(double val1,double val2) const657 bool ColorPicker::IsEquals(double val1, double val2) const
658 {
659 // 0.001 is used for double number compare.
660 return fabs(val1 - val2) < 0.001;
661 }
662
663 // Transform RGB to HSV.
RGB2HSV(uint32_t rgb) const664 HSV ColorPicker::RGB2HSV(uint32_t rgb) const
665 {
666 double r, g, b;
667 double h, s, v;
668 double minComponent, maxComponent;
669 double delta;
670 HSV hsv;
671 r = GetARGB32ColorR(rgb) / 255.0; // 255.0 is used to normalize color to [0, 1].
672 g = GetARGB32ColorG(rgb) / 255.0; // 255.0 is used to normalize color to [0, 1].
673 b = GetARGB32ColorB(rgb) / 255.0; // 255.0 is used to normalize color to [0, 1].
674 if (r > g) {
675 maxComponent = std::max(r, b);
676 minComponent = std::min(g, b);
677 } else {
678 maxComponent = std::max(g, b);
679 minComponent = std::min(r, b);
680 }
681 v = maxComponent;
682 delta = maxComponent - minComponent;
683
684 if (IsEquals(maxComponent, 0)) {
685 s = 0.0;
686 } else {
687 s = delta / maxComponent;
688 }
689
690 if (maxComponent == minComponent) {
691 h = 0.0;
692 } else {
693 if (delta == 0) {
694 return hsv;
695 }
696 if (IsEquals(r, maxComponent) && g >= b) {
697 h = 60 * (g - b) / delta + 0; // 60 is used to calculate color's hue, ranging between 0 and 360.
698 } else if (IsEquals(r, maxComponent) && g < b) {
699 h = 60 * (g - b) / delta + 360; // 60,360 is used to calculate color's hue, ranging between 0 and 360.
700 } else if (IsEquals(g, maxComponent)) {
701 h = 60 * (b - r) / delta + 120; // 60,120 is used to calculate color's hue, ranging between 0 and 360.
702 } else {
703 h = 60 * (r - g) / delta + 240; // 60,240 is used to calculate color's hue, ranging between 0 and 360.
704 }
705 }
706 hsv.h = (int)(h + 0.5); // Hue add 0.5 to round up.
707 hsv.h = (hsv.h > 359) ? (hsv.h - 360) : hsv.h; // 359 is used to adjust hue to range [0, 360].
708 hsv.h = (hsv.h < 0) ? (hsv.h + 360) : hsv.h; // Adjust hue to range [0, 360].
709 hsv.s = s * 100; // Adjust saturation to range [0, 100].
710 hsv.v = v * 100; // Adjust value to range [0, 100].
711
712 return hsv;
713 }
714
AdjustHSVToDefinedIterval(HSV & hsv) const715 void ColorPicker::AdjustHSVToDefinedIterval(HSV& hsv) const
716 {
717 if (hsv.h > 360) { // Adjust hue to range [0, 360].
718 hsv.h = 360; // Adjust hue to range [0, 360].
719 }
720 if (hsv.h < 0) {
721 hsv.h = 0;
722 }
723 if (hsv.s > 100) { // Adjust saturation to range [0, 100].
724 hsv.s = 100; // Adjust saturation to range [0, 100].
725 }
726 if (hsv.s < 0) {
727 hsv.s = 0;
728 }
729 if (hsv.v > 100) { // Adjust value to range [0, 100].
730 hsv.v = 100; // Adjust value to range [0, 100].
731 }
732 if (hsv.v < 0) {
733 hsv.v = 0;
734 }
735 return;
736 }
737
738 // Transform HSV to RGB.
HSVtoRGB(HSV hsv) const739 uint32_t ColorPicker::HSVtoRGB(HSV hsv) const
740 {
741 uint32_t r, g, b;
742 uint32_t rgb = 0;
743 AdjustHSVToDefinedIterval(hsv);
744
745 // The brightness is directly proportional to the maximum value that RGB can reach, which is 2.55 times.
746 float rgb_max = hsv.v * 2.55f;
747
748 /**
749 * Each time the saturation decreases from 100, the minimum value that RGB can achieve increases
750 * linearly by 1/100 from 0 to the maximum value set by the brightness.
751 */
752 float rgb_min = rgb_max * (100 - hsv.s) / 100.0f;
753
754 int i = hsv.h / 60;
755 int difs = hsv.h % 60;
756 float rgb_Adj = (rgb_max - rgb_min) * difs / 60.0f;
757
758 /**
759 * According to the change of H, there are six cases. In each case, a parameter in RBG is linearly
760 * transformed (increased or decreased) between the minimum and maximum values that can be achieved
761 * by RGB.
762 */
763 switch (i) {
764 case 0: // 0: when hue's range is [0, 60).
765 r = rgb_max;
766 g = rgb_min + rgb_Adj;
767 b = rgb_min;
768 break;
769 case 1: // 1: when hue's range is [60, 120).
770 r = rgb_max - rgb_Adj;
771 g = rgb_max;
772 b = rgb_min;
773 break;
774 case 2: // 2: when hue's range is [120, 180).
775 r = rgb_min;
776 g = rgb_max;
777 b = rgb_min + rgb_Adj;
778 break;
779 case 3: // 3: when hue's range is [180, 240).
780 r = rgb_min;
781 g = rgb_max - rgb_Adj;
782 b = rgb_max;
783 break;
784 case 4: // 4: when hue's range is [240, 300).
785 r = rgb_min + rgb_Adj;
786 g = rgb_min;
787 b = rgb_max;
788 break;
789 default:
790 r = rgb_max;
791 g = rgb_min;
792 b = rgb_max - rgb_Adj;
793 break;
794 }
795 rgb = r << ARGB_R_SHIFT | g << ARGB_G_SHIFT | b << ARGB_B_SHIFT;
796 return rgb;
797 }
798 } // namespace Rosen
799 } // namespace OHOS
800
801 #ifdef __cplusplus
802 }
803 #endif
804