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 #ifndef COLOR_PICKER_H
17 #define COLOR_PICKER_H
18 
19 #include <iostream>
20 #include "effect_type.h"
21 #include "color_extract.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 namespace OHOS {
28 namespace Media {
29 class PixelMap;
30 }
31 namespace ColorManager {
32 class Color;
33 }
34 namespace Rosen {
35 
36 enum class ColorBrightnessMode : uint16_t {
37     HIGH_SATURATION_BRIGHT_COLOR = 0,
38     LOW_SATURATION_BRIGHT_COLOR = 1,
39     DARK_COLOR = 2,
40 };
41 
42 enum PictureLightColorDegree {
43     UNKOWN_LIGHT_COLOR_DEGREE_PICTURE = 0,
44     EXTREMELY_LIGHT_COLOR_PICTURE = 1,
45     LIGHT_COLOR_PICTURE = 2,
46     DARK_COLOR_PICTURE = 3,
47     EXTREMELY_DARK_COLOR_PICTURE = 4,
48     FLOWERY_PICTURE = 5,
49     EXTREMELY_FLOWERY_PICTURE = 6,
50 };
51 
52 
53 struct HSV {
54     int h;  // 色度取值(0-360)
55     double s;  // 饱和度取值 (0-100)
56     double v;  // 亮度取值 (0-100)
57 };
58 
59 class ColorPicker : public ColorExtract {
60 public:
61     ColorPicker(std::shared_ptr<Media::PixelMap> pixmap);
62     ColorPicker(std::shared_ptr<Media::PixelMap> pixmap, double* coordinates);
~ColorPicker()63     ~ColorPicker() {}
64     static std::shared_ptr<Media::PixelMap> CreateScaledPixelMap(const std::shared_ptr<Media::PixelMap>& pixmap);
65     NATIVEEXPORT static std::shared_ptr<ColorPicker> CreateColorPicker(const std::shared_ptr<Media::PixelMap>& pixmap,
66                                                                         uint32_t &errorCode);
67     NATIVEEXPORT static std::shared_ptr<ColorPicker> CreateColorPicker(const std::shared_ptr<Media::PixelMap>& pixmap,
68                                                                         double* coordinates, uint32_t &errorCode);
69     NATIVEEXPORT std::shared_ptr<Media::PixelMap> GetScaledPixelMap();
70     NATIVEEXPORT uint32_t GetMainColor(ColorManager::Color &color);
71 
72     static constexpr double GRAY_RATIO_RED = 0.299;
73     static constexpr double GRAY_RATIO_GREEN = 0.587;
74     static constexpr double GRAY_RATIO_BLUE = 0.114;
75     static constexpr double LUMINANCE_RATIO_RED = 0.2126;
76     static constexpr double LUMINANCE_RATIO_GREEN = 0.7152;
77     static constexpr double LUMINANCE_RATIO_BLUE = 0.0722;
78     NATIVEEXPORT uint32_t GetLargestProportionColor(ColorManager::Color &color) const;
79     NATIVEEXPORT uint32_t GetHighestSaturationColor(ColorManager::Color &color) const;
80     NATIVEEXPORT uint32_t GetAverageColor(ColorManager::Color &color) const;
81     NATIVEEXPORT bool IsBlackOrWhiteOrGrayColor(uint32_t color) const;
82     NATIVEEXPORT uint32_t GetMorandiBackgroundColor(ColorManager::Color &color) const;
83     NATIVEEXPORT uint32_t GetMorandiShadowColor(ColorManager::Color &color) const;
84     NATIVEEXPORT uint32_t GetDeepenImmersionColor(ColorManager::Color &color) const;
85     NATIVEEXPORT uint32_t GetImmersiveBackgroundColor(ColorManager::Color &color) const;
86     NATIVEEXPORT uint32_t GetImmersiveForegroundColor(ColorManager::Color &color) const;
87     NATIVEEXPORT uint32_t DiscriminatePitureLightDegree(PictureLightColorDegree &degree) const;
88     NATIVEEXPORT uint32_t GetReverseColor(ColorManager::Color &color) const;
89     NATIVEEXPORT std::vector<ColorManager::Color> GetTopProportionColors(uint32_t colorsNum) const;
90 
91 private:
92     void AdjustLowSaturationBrightColor(HSV &colorHsv, HSV &mainHsv, HSV &secondaryHsv,
93                                         const std::pair<uint32_t, uint32_t> &mainColor,
94                                         const std::pair<uint32_t, uint32_t> &secondaryColor) const;
95     void GenerateMorandiBackgroundColor(HSV& hsv) const;
96     void GenerateMorandiShadowColor(HSV& hsv) const;
97     bool GetDominantColor(std::pair<uint32_t, uint32_t >& mainColor, std::pair<uint32_t,
98         uint32_t >& secondaryColor) const;
99     ColorBrightnessMode DiscriminateDarkOrBrightColor(const HSV& hsv) const;
100     void ProcessToDarkColor(HSV& hsv) const;
101     void ProcessToBrightColor(HSV& hsv) const;
102     void AdjustToBasicColor(HSV& hsv, double basicS, double basicV) const;
103     uint32_t RGB2GRAY(uint32_t color) const;
104     bool IsEquals(double val1, double val2) const;
105     HSV RGB2HSV(uint32_t rgb) const;
106     void AdjustHSVToDefinedIterval(HSV& hsv) const;
107     uint32_t HSVtoRGB(HSV hsv) const;
108     uint32_t CalcGrayVariance() const;
109     double CalcContrastRatioWithWhite() const;
110     double CalcRelaticeLuminance(uint32_t color) const;
111 };
112 } // namespace Rosen
113 } // namespace OHOS
114 
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 #endif // COLOR_PICKER_H
120