1 /*
2 * Copyright (c) 2020-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 "gfx_utils/color.h"
17 namespace OHOS {
GetMixColor(ColorType c1,ColorType c2,uint8_t mix)18 ColorType Color::GetMixColor(ColorType c1, ColorType c2, uint8_t mix)
19 {
20 ColorType ret;
21 /* COLOR_DEPTH is 16 or 32 */
22 // 8: right move 8 bit. 255: 2^8-1
23 ret.red = (static_cast<uint16_t>(c1.red) * mix + (static_cast<uint16_t>(c2.red) * (255 ^ mix))) >> 8;
24 // 8: right move 8 bit. 255: 2^8-1
25 ret.green = (static_cast<uint16_t>(c1.green) * mix + (static_cast<uint16_t>(c2.green) * (255 ^ mix))) >> 8;
26 // 8: right move 8 bit. 255: 2^8-1
27 ret.blue = (static_cast<uint16_t>(c1.blue) * mix + (static_cast<uint16_t>(c2.blue) * (255 ^ mix))) >> 8;
28 #if COLOR_DEPTH == 32
29 ret.alpha = 0xFF;
30 #endif
31 return ret;
32 }
33
GetColorFromRGB(uint8_t r8,uint8_t g8,uint8_t b8)34 ColorType Color::GetColorFromRGB(uint8_t r8, uint8_t g8, uint8_t b8)
35 {
36 return GetColorFromRGBA(r8, g8, b8, 0xFF);
37 }
38
GetColorFromRGBA(uint8_t r8,uint8_t g8,uint8_t b8,uint8_t alpha)39 ColorType Color::GetColorFromRGBA(uint8_t r8, uint8_t g8, uint8_t b8, uint8_t alpha)
40 {
41 ColorType rColor;
42 #if COLOR_DEPTH == 16
43 rColor.red = r8 >> 3; // 3: shift right 3 places
44 rColor.blue = b8 >> 3; // 3: shift right 3 places
45 rColor.green = g8 >> 2; // 2: shift right 2 places
46 #elif COLOR_DEPTH == 32
47 // 24, 16, 8: shift right places
48 rColor.full = (alpha << 24) | (r8 << 16) | (g8 << 8) | (b8);
49 #endif
50 return rColor;
51 }
52
ColorTo32(ColorType color)53 uint32_t Color::ColorTo32(ColorType color)
54 {
55 #if COLOR_DEPTH == 16
56 Color32 ret;
57 ret.red = color.red << 3; /* (2^8 - 1)/(2^5 - 1) = 255/31 = 8 */
58 ret.green = color.green << 2; /* (2^8 - 1)/(2^6 - 1) = 255/63 = 4 */
59 ret.blue = color.blue << 3; /* (2^8 - 1)/(2^5 - 1) = 255/31 = 8 */
60 ret.alpha = 0xFF;
61 return ret.full;
62 #elif COLOR_DEPTH == 32
63 return color.full;
64 #endif
65 }
66
ColorTo32(Color16 color,uint8_t alpha)67 uint32_t Color::ColorTo32(Color16 color, uint8_t alpha)
68 {
69 Color32 ret;
70 /*
71 * when 16-bitmap image is transformed to 32-bitmap,
72 * R should shift left 3 bits,
73 * G should shift left 2 bits,
74 * B should shift left 3 bits,
75 */
76 ret.red = color.red << 3; /* (2^8 - 1)/(2^5 - 1) = 255/31 = 8 */
77 ret.green = color.green << 2; /* (2^8 - 1)/(2^6 - 1) = 255/63 = 4 */
78 ret.blue = color.blue << 3; /* (2^8 - 1)/(2^5 - 1) = 255/31 = 8 */
79 ret.alpha = alpha;
80 return ret.full;
81 }
82
ColorTo16(Color32 color)83 uint16_t Color::ColorTo16(Color32 color)
84 {
85 /*
86 * when 32-bitmap image is transformed to 16-bitmap,
87 * R should shift right 3 bits,
88 * G should shift right 2 bits,
89 * B should shift right 3 bits,
90 */
91 Color16 rColor;
92 rColor.red = color.red >> 3;
93 rColor.green = color.green >> 2;
94 rColor.blue = color.blue >> 3;
95 return rColor.full;
96 }
97
White()98 ColorType Color::White()
99 {
100 return GetColorFromRGB(0xFF, 0xFF, 0xFF);
101 }
102
Silver()103 ColorType Color::Silver()
104 {
105 return GetColorFromRGB(0xC0, 0xC0, 0xC0);
106 }
107
Gray()108 ColorType Color::Gray()
109 {
110 return GetColorFromRGB(0x80, 0x80, 0x80);
111 }
112
Black()113 ColorType Color::Black()
114 {
115 return GetColorFromRGB(0x00, 0x00, 0x00);
116 }
117
Red()118 ColorType Color::Red()
119 {
120 return GetColorFromRGB(0xFF, 0x00, 0x00);
121 }
122
Maroon()123 ColorType Color::Maroon()
124 {
125 return GetColorFromRGB(0x80, 0x00, 0x00);
126 }
127
Yellow()128 ColorType Color::Yellow()
129 {
130 return GetColorFromRGB(0xFF, 0xFF, 0x00);
131 }
132
Olive()133 ColorType Color::Olive()
134 {
135 return GetColorFromRGB(0x80, 0x80, 0x00);
136 }
137
Lime()138 ColorType Color::Lime()
139 {
140 return GetColorFromRGB(0x00, 0xFF, 0x00);
141 }
142
Green()143 ColorType Color::Green()
144 {
145 return GetColorFromRGB(0x00, 0xFF, 0x00);
146 }
147
Cyan()148 ColorType Color::Cyan()
149 {
150 return GetColorFromRGB(0x00, 0xFF, 0xFF);
151 }
152
Aqua()153 ColorType Color::Aqua()
154 {
155 return GetColorFromRGB(0x00, 0xFF, 0xFF);
156 }
157
Teal()158 ColorType Color::Teal()
159 {
160 return GetColorFromRGB(0x00, 0x80, 0x80);
161 }
162
Blue()163 ColorType Color::Blue()
164 {
165 return GetColorFromRGB(0x00, 0x00, 0xFF);
166 }
167
Navy()168 ColorType Color::Navy()
169 {
170 return GetColorFromRGB(0x00, 0x00, 0x80);
171 }
172
Magenta()173 ColorType Color::Magenta()
174 {
175 return GetColorFromRGB(0xFF, 0x00, 0xFF);
176 }
177
Purple()178 ColorType Color::Purple()
179 {
180 return GetColorFromRGB(0x80, 0x00, 0x80);
181 }
182
Orange()183 ColorType Color::Orange()
184 {
185 return GetColorFromRGB(0xFF, 0xA5, 0x00);
186 }
187 } // namespace OHOS
188