blob: b424f97a5004a38e58fdf7e164266f5a1dc63649 [file] [log] [blame]
Chris Craik0519c812015-02-11 13:17:06 -08001/*
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
Romain Guy253f2c22016-09-28 17:34:42 -070019#include "utils/Color.h"
Chris Craik0519c812015-02-11 13:17:06 -080020#include "utils/Macros.h"
sergeyvf42bf3e2016-03-11 13:45:15 -080021#include "utils/MathUtils.h"
Chris Craik0519c812015-02-11 13:17:06 -080022
Chris Craik922d3a72015-02-13 17:47:21 -080023#include <stdint.h>
24
Chris Craik0519c812015-02-11 13:17:06 -080025namespace android {
26namespace uirenderer {
27
28struct FloatColor {
Romain Guy253f2c22016-09-28 17:34:42 -070029 // "color" is a gamma-encoded sRGB color
30 // After calling this method, the color is stored as a pre-multiplied linear color
Romain Guy8762e332016-10-12 12:14:07 -070031 // if linear blending is enabled. Otherwise, the color is stored as a pre-multiplied
32 // gamma-encoded sRGB color
Chris Craik922d3a72015-02-13 17:47:21 -080033 void set(uint32_t color) {
34 a = ((color >> 24) & 0xff) / 255.0f;
Romain Guy8762e332016-10-12 12:14:07 -070035 r = a * EOCF(((color >> 16) & 0xff) / 255.0f);
John Reck1bcacfd2017-11-03 10:12:19 -070036 g = a * EOCF(((color >> 8) & 0xff) / 255.0f);
37 b = a * EOCF(((color)&0xff) / 255.0f);
Romain Guy8762e332016-10-12 12:14:07 -070038 }
39
40 // "color" is a gamma-encoded sRGB color
Romain Guy6183c972017-03-16 12:24:55 -070041 // 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
44 void setUnPreMultiplied(uint32_t color) {
Romain Guy8762e332016-10-12 12:14:07 -070045 a = ((color >> 24) & 0xff) / 255.0f;
Romain Guy6183c972017-03-16 12:24:55 -070046 r = EOCF(((color >> 16) & 0xff) / 255.0f);
John Reck1bcacfd2017-11-03 10:12:19 -070047 g = EOCF(((color >> 8) & 0xff) / 255.0f);
48 b = EOCF(((color)&0xff) / 255.0f);
Chris Craik922d3a72015-02-13 17:47:21 -080049 }
50
John Reck1bcacfd2017-11-03 10:12:19 -070051 bool isNotBlack() { return a < 1.0f || r > 0.0f || g > 0.0f || b > 0.0f; }
Chris Craik2bb8f562015-02-17 16:42:02 -080052
sergeyvf42bf3e2016-03-11 13:45:15 -080053 bool operator==(const FloatColor& other) const {
John Reck1bcacfd2017-11-03 10:12:19 -070054 return MathUtils::areEqual(r, other.r) && MathUtils::areEqual(g, other.g) &&
55 MathUtils::areEqual(b, other.b) && MathUtils::areEqual(a, other.a);
sergeyvf42bf3e2016-03-11 13:45:15 -080056 }
57
John Reck1bcacfd2017-11-03 10:12:19 -070058 bool operator!=(const FloatColor& other) const { return !(*this == other); }
sergeyvf42bf3e2016-03-11 13:45:15 -080059
Chris Craik0519c812015-02-11 13:17:06 -080060 float r;
61 float g;
62 float b;
63 float a;
64};
65
66REQUIRE_COMPATIBLE_LAYOUT(FloatColor);
67
68} /* namespace uirenderer */
69} /* namespace android */
70
71#endif /* FLOATCOLOR_H */