roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
color_mode_indexed.h
Go to the documentation of this file.
1#pragma once
2
3#include <inttypes.h>
4
5#include "roo_collections.h"
6#include "roo_collections/flat_small_hashtable.h"
10
11namespace roo_display {
12
13class Palette;
14
15namespace internal {
16
21
23 public:
24 using Map =
25 roo_collections::FlatSmallHashtable<uint8_t, Color, internal::ColorHash,
27
28 // For a 'static' index.
29 PaletteIndex(const Color* palette, int size)
30 : colors_(nullptr),
31 index_map_(size, internal::ColorHash(),
32 internal::PaletteElementKey{.palette = palette}),
33 max_size_(size),
34 size_(size) {
35 for (int i = 0; i < size; ++i) {
36 index_map_.insert(i);
37 }
38 }
39
40 // For a 'dynamic' index.
42 : colors_(new Color[max_size]),
43 index_map_(max_size, internal::ColorHash(),
44 internal::PaletteElementKey{.palette = colors_.get()}),
45 max_size_(max_size),
46 size_(0) {}
47
48 Map::ConstIterator find(Color color) const { return index_map_.find(color); }
49
50 Map::ConstIterator end() const { return index_map_.end(); }
51
52 private:
53 friend class ::roo_display::Palette;
54
55 const Color* palette() const { return colors_.get(); }
56
57 int maybeAddColor(Color color) {
58 if (colors_ == nullptr || size_ == max_size_) return 0;
59 colors_[size_] = color;
60 index_map_.insert(size_);
61 return size_++;
62 }
63
64 // Color storage in case of a dynamic palette.
65 std::unique_ptr<Color[]> colors_;
66
67 Map index_map_;
68 int max_size_;
69 int size_;
70};
71
72} // namespace internal
73
74/// Palette storage for `IndexedN` color modes.
75class Palette {
76 public:
77 /// Create a dummy palette with a single transparent color.
78 Palette();
79
80 /// Create a read-only palette backed by the given colors.
81 ///
82 /// The color array is not copied and must remain valid. Read-only palettes
83 /// cannot be used for inverse lookup (ARGB -> index); use `ReadWrite()` or
84 /// `Dynamic()` for that. Transparency is auto-detected.
85 ///
86 /// @param colors Palette colors (array must outlive the palette).
87 /// @param size Number of colors in the palette.
88 /// @return Read-only palette instance.
89 static Palette ReadOnly(const Color* colors, int size);
90
91 /// Read-only palette with explicit transparency mode.
92 ///
93 /// @param colors Palette colors (array must outlive the palette).
94 /// @param size Number of colors in the palette.
95 /// @param transparency_mode Explicit transparency mode to use.
96 /// @return Read-only palette instance.
97 static Palette ReadOnly(const Color* colors, int size,
99
100 /// Create a read/write palette backed by the given colors.
101 ///
102 /// Enables inverse lookup (ARGB -> index), e.g. for Offscreen. The array is
103 /// not copied and must remain valid. Transparency is auto-detected.
104 ///
105 /// @param colors Palette colors (array must outlive the palette).
106 /// @param size Number of colors in the palette.
107 /// @return Read/write palette instance.
108 static Palette ReadWrite(const Color* colors, int size);
109
110 /// Read/write palette with explicit transparency mode.
111 ///
112 /// @param colors Palette colors (array must outlive the palette).
113 /// @param size Number of colors in the palette.
114 /// @param transparency_mode Explicit transparency mode to use.
115 /// @return Read/write palette instance.
116 static Palette ReadWrite(const Color* colors, int size,
118
119 /// Create a dynamic palette for drawing to offscreens.
120 ///
121 /// Colors are added up to `max_size`. After that, missing colors map to
122 /// index 0.
123 ///
124 /// @param max_size Maximum number of colors to store.
125 /// @return Dynamic palette instance.
126 static Palette Dynamic(int max_size);
127
128 /// Return pointer to the color table.
129 const Color* colors() const { return colors_; }
130
131 /// Return palette size.
132 int size() const { return size_; }
133
134 /// Return the `idx`-th color.
135 inline Color getColorAt(int idx) const { return colors_[idx]; }
136
137 /// Return palette transparency mode.
138 TransparencyMode transparency_mode() const { return transparency_mode_; }
139
140 /// Return the index of a specified color.
141 ///
142 /// Not valid for read-only palettes. For dynamic palettes, missing colors
143 /// are added up to `max_size`; otherwise returns 0 when not found.
145
146 private:
147 Palette(std::unique_ptr<internal::PaletteIndex> index, const Color* colors,
149 : index_(std::move(index)),
150 colors_(colors),
151 size_(size),
152 transparency_mode_(transparency_mode) {}
153
154 std::unique_ptr<internal::PaletteIndex> index_;
155 const Color* colors_;
156 int size_;
157 TransparencyMode transparency_mode_;
158};
159
160namespace internal {
161
162/// Indexed color mode with `bits` bits per pixel.
163template <uint8_t bits>
164class Indexed {
165 public:
166 static const int8_t bits_per_pixel = bits;
167
168 Indexed(const Palette* palette) : palette_(palette) {
169 assert(palette_->size() >= 0 && palette_->size() <= (1 << bits));
170 }
171
172 inline uint8_t fromArgbColor(Color color) const {
173 return const_cast<Palette*>(palette_)->getIndexOfColor(color);
174 }
175
176 constexpr TransparencyMode transparency() const {
177 return palette_->transparency_mode();
178 }
179
180 inline constexpr Color toArgbColor(uint8_t in) const {
181 return palette_->getColorAt(in);
182 }
183
184 inline uint8_t rawAlphaBlend(uint8_t bg, Color fg) {
185 return fromArgbColor(AlphaBlend(toArgbColor(bg), fg));
186 }
187
188 const Palette* palette() const { return palette_; }
189
190 private:
191 const Palette* palette_;
192};
193
194} // namespace internal
195
196using Indexed1 = internal::Indexed<1>;
197using Indexed2 = internal::Indexed<2>;
198using Indexed4 = internal::Indexed<4>;
199using Indexed8 = internal::Indexed<8>;
200
201}; // namespace roo_display
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
Palette storage for IndexedN color modes.
const Color * colors() const
Return pointer to the color table.
uint8_t getIndexOfColor(Color color)
Return the index of a specified color.
static Palette ReadWrite(const Color *colors, int size)
Create a read/write palette backed by the given colors.
int size() const
Return palette size.
Color getColorAt(int idx) const
Return the idx-th color.
Palette()
Create a dummy palette with a single transparent color.
static Palette ReadOnly(const Color *colors, int size)
Create a read-only palette backed by the given colors.
TransparencyMode transparency_mode() const
Return palette transparency mode.
static Palette Dynamic(int max_size)
Create a dynamic palette for drawing to offscreens.
PaletteIndex(const Color *palette, int size)
roo_collections::FlatSmallHashtable< uint8_t, Color, internal::ColorHash, internal::PaletteElementKey > Map
Map::ConstIterator find(Color color) const
Defines 140 opaque HTML named colors.
internal::Indexed< 2 > Indexed2
Color AlphaBlend(Color bgc, Color fgc)
Definition blending.h:598
TransparencyMode
Transparency information for a stream or color mode.
Definition blending.h:103
internal::Indexed< 1 > Indexed1
internal::Indexed< 8 > Indexed8
internal::Indexed< 4 > Indexed4
const Palette * palette
Definition png.cpp:30
Hash functor for Color.
Definition color_set.h:12