Porytiles
Loading...
Searching...
No Matches
color_palette_printer.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <ranges>
5#include <vector>
6
11
12namespace {
13
14using namespace porytiles;
15
16template <std::size_t N>
17std::vector<std::string> print_palette_with_highlights_impl(
18 const Palette<Rgba32, N> &palette, const std::vector<std::size_t> &highlight_slots, const TextFormatter *format)
19{
20 std::vector<std::string> lines{};
21
22 // JASC palette front matter
23 lines.emplace_back(" JASC-PAL");
24 lines.emplace_back(" 0100");
25 lines.push_back(format->format(" {}", FormatParam{palette.size()}));
26
27 for (std::size_t i = 0; i < palette.size(); ++i) {
28 const bool is_highlighted = std::ranges::find(highlight_slots, i) != highlight_slots.end();
29
30 std::string prefix;
31 if (is_highlighted) {
32 prefix = format->format("{}", FormatParam{"➞ ", Style::bold | Style::yellow});
33 }
34 else {
35 prefix = " ";
36 }
37
38 std::string slot_str;
39 if (palette.is_wildcard(i)) {
40 slot_str = format->format("{}{}", FormatParam{prefix}, FormatParam{"*"});
41 }
42 else {
43 const Rgba32 color = palette.at(i);
44 const Style color_style = rgb_fg_style(color.red(), color.green(), color.blue());
45
46 if (is_highlighted) {
47 const std::string color_text =
48 format->style(color.to_jasc_str(), Style::bold | Style::italic | Style::yellow | color_style);
49 slot_str = format->format("{}{}", FormatParam{prefix}, FormatParam{color_text});
50 }
51 else {
52 const std::string color_text = format->style(color.to_jasc_str(), color_style);
53 slot_str = format->format("{}{}", FormatParam{prefix}, FormatParam{color_text});
54 }
55 }
56
57 lines.push_back(slot_str);
58 }
59
60 return lines;
61}
62
63} // namespace
64
65namespace porytiles {
66
67std::vector<std::string>
69{
70 return print_palette_with_highlights_impl(palette, {}, format_);
71}
72
73std::vector<std::string> ColorPalettePrinter::print_rgba_palette(const Palette<Rgba32> &palette) const
74{
75 return print_palette_with_highlights_impl(palette, {}, format_);
76}
77
79 const Palette<Rgba32> &palette, const std::vector<std::size_t> &slots) const
80{
81 return print_palette_with_highlights_impl(palette, slots, format_);
82}
83
85 const Palette<Rgba32, palette::max_size> &palette, const std::vector<std::size_t> &slots) const
86{
87 return print_palette_with_highlights_impl(palette, slots, format_);
88}
89
91 const PaletteHint &hint, const std::vector<std::size_t> &slots) const
92{
93 std::vector<std::string> lines{};
94
95 // JASC palette front matter
96 lines.push_back(format_->format("name: \"{}\"", FormatParam{hint.name(), Style::bold}));
97 lines.emplace_back("colors:");
98
99 for (std::size_t i = 0; i < hint.palette().size(); ++i) {
100 const bool is_highlighted = std::ranges::find(slots, i) != slots.end();
101
102 std::string prefix;
103 if (is_highlighted) {
104 prefix = format_->format("{}", FormatParam{"➞ ", Style::bold | Style::yellow});
105 }
106 else {
107 prefix = " ";
108 }
109
110 std::string slot_str;
111 if (hint.palette().is_wildcard(i)) {
112 panic("illegal wildcard in palette hint '" + hint.name() + "' at index '" + std::to_string(i) + "'");
113 }
114 const Rgba32 color = hint.palette().at(i);
115 const Style color_style = rgb_fg_style(color.red(), color.green(), color.blue());
116
117 if (is_highlighted) {
118 const std::string color_text =
119 format_->style(color.to_csv_str(), Style::bold | Style::italic | color_style);
120 slot_str = format_->format("{} - [ {} ]", FormatParam{prefix}, FormatParam{color_text});
121 }
122 else {
123 const std::string color_text = format_->style(color.to_csv_str(), color_style);
124 slot_str = format_->format("{} - [ {} ]", FormatParam{prefix}, FormatParam{color_text});
125 }
126
127 lines.push_back(slot_str);
128 }
129
130 return lines;
131}
132
133[[nodiscard]] std::vector<std::string> ColorPalettePrinter::print_rgba_palette_covered_missing(
135 std::set<Rgba32> covered_colors,
136 std::set<Rgba32> missing_colors) const
137{
138 // Build highlight_slots from covered_colors
139 std::vector<std::size_t> highlight_slots;
140 const auto index_to_color = palette.index_to_color_map();
141 for (const auto &[index, color] : index_to_color) {
142 if (covered_colors.contains(color)) {
143 highlight_slots.push_back(index);
144 }
145 }
146
147 // Use the common implementation for palette printing with highlights
148 std::vector<std::string> lines = print_palette_with_highlights_impl(palette, highlight_slots, format_);
149
150 // Append missing colors section if needed
151 if (!missing_colors.empty()) {
152 lines.push_back(format_->format("{}", FormatParam{"------ Palette Still Missing ------", Style::bold}));
153 for (const auto &color : missing_colors) {
154 const Style color_style = rgb_fg_style(color.red(), color.green(), color.blue());
155 const std::string color_text = format_->style(color.to_jasc_str(), color_style);
156 lines.push_back(format_->format("{}", FormatParam{color_text}));
157 }
158 }
159
160 return lines;
161}
162
163std::vector<std::string>
164ColorPalettePrinter::print_rgba_counts(const std::vector<std::pair<Rgba32, unsigned int>> &colors_counts) const
165{
166 std::vector<std::string> lines{};
167 for (const auto &[color, count] : colors_counts) {
168 const Style color_style = rgb_fg_style(color.red(), color.green(), color.blue());
169 const std::string color_text = format_->style(color.to_jasc_str(), color_style);
170 lines.push_back(format_->format("{} ➞ {} pixel(s)", FormatParam{color_text}, FormatParam{count}));
171 }
172 return lines;
173}
174
175} // namespace porytiles
std::vector< std::string > print_rgba_palette_covered_missing(const Palette< Rgba32, palette::max_size > &palette, std::set< Rgba32 > covered_colors, std::set< Rgba32 > missing_colors) const override
std::vector< std::string > print_rgba_counts(const std::vector< std::pair< Rgba32, unsigned int > > &colors_counts) const override
std::vector< std::string > print_rgba_palette_with_highlights(const Palette< Rgba32 > &palette, const std::vector< std::size_t > &slots) const override
std::vector< std::string > print_rgba_palette(const Palette< Rgba32, palette::max_size > &palette) const override
std::vector< std::string > print_palette_hint_with_highlights(const PaletteHint &hint, const std::vector< std::size_t > &slots) const override
A text parameter with associated styling for formatted output.
Represents a palette hint for the palette packing algorithm.
const std::string & name() const
const Palette< Rgba32 > & palette() const
A generic palette container for colors that support transparency checking.
Definition palette.hpp:45
std::map< PaletteIndex, ColorType > index_to_color_map() const
Creates a map from palette indices to their colors.
Definition palette.hpp:310
std::size_t size() const
Returns the number of slots in the palette.
Definition palette.hpp:203
ColorType at(std::size_t index) const
Gets the color at a specific index.
Definition palette.hpp:246
bool is_wildcard(std::size_t index) const
Checks if a slot is a wildcard.
Definition palette.hpp:176
Represents a 32-bit RGBA color.
Definition rgba32.hpp:21
std::string to_csv_str() const
Definition rgba32.cpp:29
std::uint8_t red() const
Definition rgba32.hpp:73
std::uint8_t blue() const
Definition rgba32.hpp:83
std::string to_jasc_str() const
Definition rgba32.cpp:24
std::uint8_t green() const
Definition rgba32.hpp:78
Text styling class supporting formatting, foreground colors, and background colors.
static const Style italic
Italic text formatting.
static const Style yellow
Yellow foreground color.
static const Style bold
Bold text formatting.
Abstract base class for applying text styling with context-aware formatting.
virtual std::string style(const std::string &text, Style styles) const =0
Applies styling to a text string.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43
constexpr Style rgb_fg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Creates a Style value with a custom RGB foreground color.