1 /*
2  * Copyright (c) 2021-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 #ifndef RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COLOR_H
17 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COLOR_H
18 
19 #include <sys/types.h>
20 #include <stdint.h>
21 
22 #include "common/rs_common_def.h"
23 #include "common/rs_macros.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class RSB_EXPORT RSColor final {
28 public:
RSColor()29     RSColor() noexcept : alpha_(0), blue_(0), green_(0), red_(0) {}
30     explicit RSColor(uint32_t rgba) noexcept;
31     RSColor(int16_t red, int16_t green, int16_t blue) noexcept;
32     RSColor(int16_t red, int16_t green, int16_t blue, int16_t alpha) noexcept;
33     RSColor(const RSColor& rhs) noexcept = default;
34     RSColor& operator=(const RSColor& rhs) noexcept = default;
35     ~RSColor() = default;
36     bool operator==(const RSColor& rhs) const;
37     inline bool operator!=(const RSColor& rhs) const
38     {
39         return !operator==(rhs);
40     }
41     bool IsNearEqual(const RSColor& other, int16_t threshold = std::numeric_limits<int16_t>::epsilon()) const;
42     RSColor operator+(const RSColor& rhs) const;
43     RSColor operator-(const RSColor& rhs) const;
44     RSColor operator*(float scale) const;
45     RSColor operator/(float scale) const;
46     RSColor& operator*=(float scale);
47     uint32_t AsRgbaInt() const;
48     static RSColor FromRgbaInt(uint32_t rgba);
49     uint32_t AsArgbInt() const;
50     static RSColor FromArgbInt(uint32_t rgba);
51     uint32_t AsBgraInt() const;
52     static RSColor FromBgraInt(uint32_t bgra);
53 
54     int16_t GetBlue() const;
55     int16_t GetGreen() const;
56     int16_t GetRed() const;
57     int16_t GetAlpha() const;
58     void SetBlue(int16_t blue);
59     void SetGreen(int16_t green);
60     void SetRed(int16_t red);
61     void SetAlpha(int16_t alpha);
62     void MultiplyAlpha(float alpha);
63 
64     void Dump(std::string& out) const;
65 
GetBytesPerPixelInt()66     static constexpr size_t GetBytesPerPixelInt()
67     {
68         return sizeof(int64_t);
69     }
70 
71 private:
72     struct {
73         int16_t alpha_ : 16;
74         int16_t blue_ : 16;
75         int16_t green_ : 16;
76         int16_t red_ : 16;
77     };
78 };
79 } // namespace Rosen
80 } // namespace OHOS
81 
82 using Color = OHOS::Rosen::RSColor;
83 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COLOR_H
84