C/C++ API Reference
Loading...
Searching...
No Matches
color.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <math.h>
17#include <stdint.h>
18
19#include <cinttypes>
20#include <cstdint>
21
23namespace pw::display {
24
26
31
33using ColorRgba8888 = uint32_t;
34
36using ColorRgb565 = uint16_t;
37
43constexpr ColorRgb565 EncodeRgb565(uint8_t r, uint8_t g, uint8_t b) {
44 return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3);
45}
46
49 uint8_t r = (rgba8888 & 0xFF);
50 uint8_t g = (rgba8888 & 0xFF00) >> 8;
51 uint8_t b = (rgba8888 & 0xFF0000) >> 16;
52 // Alpha is ignored for RGB565.
53 return EncodeRgb565(r, g, b);
54}
55
58constexpr ColorRgba8888 EncodeRgba8888(uint8_t r,
59 uint8_t g,
60 uint8_t b,
61 uint8_t a) {
62 return (a << 24) | (b << 16) | (g << 8) | r;
63}
64
72 uint8_t r = 255 * ((rgb565 & 0xF800) >> 11) / 31;
73 uint8_t g = 255 * ((rgb565 & 0x7E0) >> 5) / 63;
74 uint8_t b = 255 * (rgb565 & 0x1F) / 31;
75 uint8_t a = 255;
76 return EncodeRgba8888(r, g, b, a);
77}
78
80
81} // namespace pw::display
uint16_t ColorRgb565
Base type for pixels in RGB565 format.
Definition: color.h:36
constexpr ColorRgba8888 EncodeRgba8888(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition: color.h:58
uint32_t ColorRgba8888
Base type for pixels in RGBA8888 format.
Definition: color.h:33
constexpr ColorRgb565 EncodeRgb565(uint8_t r, uint8_t g, uint8_t b)
Definition: color.h:43
Graphic display and framebuffer library.
Definition: color.h:23