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