• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = GetARGB32ColorR(rgb) / 255.0;  // 255.0 is used to normalize color to [0, 1].
157     double g = GetARGB32ColorG(rgb) / 255.0;  // 255.0 is used to normalize color to [0, 1].
158     double b = GetARGB32ColorB(rgb) / 255.0;  // 255.0 is used to normalize color to [0, 1].
159     HSV hsv;
160     double minComponent;
161     double maxComponent;
162     if (r > g) {
163         maxComponent = std::max(r, b);
164         minComponent = std::min(g, b);
165     } else {
166         maxComponent = std::max(g, b);
167         minComponent = std::min(r, b);
168     }
169     double v = maxComponent;
170     double delta = maxComponent - minComponent;
171     double s;
172     if (IsEquals(maxComponent, 0)) {
173         s = 0.0;
174     } else {
175         s = delta / maxComponent;
176     }
177 
178     double h;
179     if (maxComponent == minComponent) {
180         h = 0.0;
181     } else {
182         if (delta == 0) {
183             return hsv;
184         }
185         if (IsEquals(r, maxComponent) && g >= b) {
186             h = 60 * (g - b) / delta + 0; // 60 is used to calculate color's hue, ranging between 0 and 360.
187         } else if (IsEquals(r, maxComponent) && g < b) {
188             h = 60 * (g - b) / delta + 360; // 60,360 is used to calculate color's hue, ranging between 0 and 360.
189         } else if (IsEquals(g, maxComponent)) {
190             h = 60 * (b - r) / delta + 120; // 60,120 is used to calculate color's hue, ranging between 0 and 360.
191         } else {
192             h = 60 * (r - g) / delta + 240; // 60,240 is used to calculate color's hue, ranging between 0 and 360.
193         }
194     }
195     hsv.h = (int)(h + 0.5); // Hue add 0.5 to round up.
196     hsv.h = (hsv.h > 359) ? (hsv.h - 360) : hsv.h; // 359 is used to adjust hue to range [0, 360].
197     hsv.h = (hsv.h < 0) ? (hsv.h + 360) : hsv.h; // Adjust hue to range [0, 360].
198     hsv.s = s * 100; // Adjust saturation to range [0, 100].
199     hsv.v = v * 100; // Adjust value to range [0, 100].
200 
201     return hsv;
202 }
203 
AdjustHSVToDefinedIterval(HSV & hsv) const204 void RSColorPicker::AdjustHSVToDefinedIterval(HSV& hsv) const
205 {
206     if (hsv.h > 360) { // Adjust hue to range [0, 360].
207         hsv.h = 360; // Adjust hue to range [0, 360].
208     }
209     if (hsv.h < 0) {
210         hsv.h = 0;
211     }
212     if (hsv.s > 100) { // Adjust saturation to range [0, 100].
213         hsv.s = 100; // Adjust saturation to range [0, 100].
214     }
215     if (hsv.s < 0) {
216         hsv.s = 0;
217     }
218     if (hsv.v > 100) { // Adjust value to range [0, 100].
219         hsv.v = 100; // Adjust value to range [0, 100].
220     }
221     if (hsv.v < 0) {
222         hsv.v = 0;
223     }
224     return;
225 }
226 
227 // Transform HSV to RGB.
HSVtoRGB(HSV hsv) const228 uint32_t RSColorPicker::HSVtoRGB(HSV hsv) const
229 {
230     uint32_t r, g, b;
231     uint32_t rgb = 0;
232     AdjustHSVToDefinedIterval(hsv);
233 
234     // The brightness is directly proportional to the maximum value that RGB can reach, which is 2.55 times.
235     float rgb_max = hsv.v * 2.55f;
236 
237     /**
238     * Each time the saturation decreases from 100, the minimum value that RGB can achieve increases
239     * linearly by 1/100 from 0 to the maximum value set by the brightness.
240     */
241     float rgb_min = rgb_max * (100 - hsv.s) / 100.0f;
242 
243     int i = hsv.h / 60;
244     int difs = hsv.h % 60;
245     float rgb_Adj = (rgb_max - rgb_min) * difs / 60.0f;
246 
247     /**
248      * According to the change of H, there are six cases. In each case, a parameter in RBG is linearly
249      * transformed (increased or decreased) between the minimum and maximum values that can be achieved
250      * by RGB.
251      */
252     switch (i) {
253         case 0: // 0: when hue's range is [0, 60).
254             r = rgb_max;
255             g = rgb_min + rgb_Adj;
256             b = rgb_min;
257             break;
258         case 1: // 1: when hue's range is [60, 120).
259             r = rgb_max - rgb_Adj;
260             g = rgb_max;
261             b = rgb_min;
262             break;
263         case 2: // 2: when hue's range is [120, 180).
264             r = rgb_min;
265             g = rgb_max;
266             b = rgb_min + rgb_Adj;
267             break;
268         case 3: // 3: when hue's range is [180, 240).
269             r = rgb_min;
270             g = rgb_max - rgb_Adj;
271             b = rgb_max;
272             break;
273         case 4: // 4: when hue's range is [240, 300).
274             r = rgb_min + rgb_Adj;
275             g = rgb_min;
276             b = rgb_max;
277             break;
278         default:
279             r = rgb_max;
280             g = rgb_min;
281             b = rgb_max - rgb_Adj;
282             break;
283     }
284     rgb = r << ARGB_R_SHIFT | g << ARGB_G_SHIFT | b << ARGB_B_SHIFT;
285     return rgb;
286 }
287 } // namespace Rosen
288 } // namespace OHOS
289