1
2 /*
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "color.h"
18 #include <cstdint>
19
20 namespace OHOS {
21 namespace ColorManager {
Alpha(uint64_t color)22 static float Alpha(uint64_t color)
23 {
24 return ((color >> 56) & 0xff) / 255.0f; // shift 56 bits to get alpha value and 255.0f is the max value
25 }
26
Red(uint64_t color)27 static float Red(uint64_t color)
28 {
29 return ((color >> 48) & 0xff) / 255.0f; // shift 48 bits to get red value and 255.0f is the max value
30 }
31
Green(uint64_t color)32 static float Green(uint64_t color)
33 {
34 return ((color >> 40) & 0xff) / 255.0f; // shift 40 bits to get green value and 255.0f is the max value
35 }
36
Blue(uint64_t color)37 static float Blue(uint64_t color)
38 {
39 return ((color >> 32) & 0xff) / 255.0f; // shift 32 bits to get blue value and 255.0f is the max value
40 }
41
Name(uint64_t color)42 static ColorSpaceName Name(uint64_t color)
43 {
44 return (ColorSpaceName)(color & 0xffffffff);
45 }
46
Color(float r,float g,float b,float a)47 Color::Color(float r, float g, float b, float a)
48 : r(r), g(g), b(b), a(a) {}
49
Color(float r,float g,float b,float a,const ColorSpaceName name)50 Color::Color(float r, float g, float b, float a, const ColorSpaceName name)
51 : r(r), g(g), b(b), a(a), srcName(name) {}
52
Color(unsigned int color)53 Color::Color(unsigned int color)
54 : r(Red((uint64_t)color<<32)),
55 g(Green((uint64_t)color<<32)),
56 b(Blue((uint64_t)color<<32)),
57 a(Alpha((uint64_t)color<<32)) {}
58
Color(uint64_t color)59 Color::Color(uint64_t color)
60 : r(Red(color)),
61 g(Green(color)),
62 b(Blue(color)),
63 a(Alpha(color)),
64 srcName(Name(color)) {}
65
PackValue() const66 uint64_t Color::PackValue() const
67 {
68 // shift 48, 40, 32 bits to get rgb value and pack the name into it
69 uint64_t argbn = ((uint64_t)(a * 255.0f + 0.5f) << 56) |
70 ((uint64_t)(r * 255.0f + 0.5f) << 48) |
71 ((uint64_t)(g * 255.0f + 0.5f) << 40) |
72 ((uint64_t)(b * 255.0f + 0.5f) << 32) |
73 ((uint32_t)srcName);
74 return argbn;
75 }
76
77 // alpha value not involved in convert
Convert(ColorSpaceConvertor & convertor) const78 Color Color::Convert(ColorSpaceConvertor &convertor) const
79 {
80 Vector3 srcColor = {r, g, b};
81 Vector3 dstColor = convertor.Convert(srcColor);
82 // dstColor[0], dstColor[1], dstColor[2] : rgb
83 return Color(dstColor[0], dstColor[1], dstColor[2], a, convertor.GetDstColorSpace().GetColorSpaceName());
84 }
85
Convert(const ColorSpace & dst) const86 Color Color::Convert(const ColorSpace &dst) const
87 {
88 ColorSpaceConvertor convertor(ColorSpace(srcName), dst, GamutMappingMode::GAMUT_MAP_CONSTANT);
89 return Convert(convertor);
90 }
91
Convert(const ColorSpaceName dstName) const92 Color Color::Convert(const ColorSpaceName dstName) const
93 {
94 ColorSpaceConvertor convertor(ColorSpace(srcName), ColorSpace(dstName), GamutMappingMode::GAMUT_MAP_CONSTANT);
95 return Convert(convertor);
96 }
97
ColorEqual(const Color & val) const98 bool Color::ColorEqual(const Color &val) const
99 {
100 if ((val.srcName != srcName)) {
101 if ((srcName != ColorSpaceName::CUSTOM) && (val.srcName != ColorSpaceName::CUSTOM)) {
102 return false;
103 }
104 }
105 return FloatEqual(r, val.r) && FloatEqual(g, val.g) && FloatEqual(b, val.b) && FloatEqual(a, val.a);
106 }
107 }
108 }