1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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 #ifndef FLOATCOLOR_H
17 #define FLOATCOLOR_H
18 
19 #include "utils/Color.h"
20 #include "utils/Macros.h"
21 #include "utils/MathUtils.h"
22 
23 #include <stdint.h>
24 
25 namespace android {
26 namespace uirenderer {
27 
28 struct FloatColor {
29     // "color" is a gamma-encoded sRGB color
30     // After calling this method, the color is stored as a pre-multiplied linear color
31     // if linear blending is enabled. Otherwise, the color is stored as a pre-multiplied
32     // gamma-encoded sRGB color
setFloatColor33     void set(uint32_t color) {
34         a = ((color >> 24) & 0xff) / 255.0f;
35         r = a * EOCF(((color >> 16) & 0xff) / 255.0f);
36         g = a * EOCF(((color >>  8) & 0xff) / 255.0f);
37         b = a * EOCF(((color      ) & 0xff) / 255.0f);
38     }
39 
40     // "color" is a gamma-encoded sRGB color
41     // After calling this method, the color is stored as a un-premultiplied linear color
42     // if linear blending is enabled. Otherwise, the color is stored as a un-premultiplied
43     // gamma-encoded sRGB color
setUnPreMultipliedFloatColor44     void setUnPreMultiplied(uint32_t color) {
45         a = ((color >> 24) & 0xff) / 255.0f;
46         r = EOCF(((color >> 16) & 0xff) / 255.0f);
47         g = EOCF(((color >>  8) & 0xff) / 255.0f);
48         b = EOCF(((color      ) & 0xff) / 255.0f);
49     }
50 
isNotBlackFloatColor51     bool isNotBlack() {
52         return a < 1.0f
53                 || r > 0.0f
54                 || g > 0.0f
55                 || b > 0.0f;
56     }
57 
58     bool operator==(const FloatColor& other) const {
59         return MathUtils::areEqual(r, other.r)
60                 && MathUtils::areEqual(g, other.g)
61                 && MathUtils::areEqual(b, other.b)
62                 && MathUtils::areEqual(a, other.a);
63     }
64 
65     bool operator!=(const FloatColor& other) const {
66         return !(*this == other);
67     }
68 
69     float r;
70     float g;
71     float b;
72     float a;
73 };
74 
75 REQUIRE_COMPATIBLE_LAYOUT(FloatColor);
76 
77 } /* namespace uirenderer */
78 } /* namespace android */
79 
80 #endif /* FLOATCOLOR_H */
81