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