Porytiles
Loading...
Searching...
No Matches
jasc_pal_saver.cpp
Go to the documentation of this file.
2
3#include <fstream>
4
6
7namespace porytiles {
8
9ChainableResult<void>
10JascPalSaver::save(const Palette<Rgba32, pal::max_size> &pal, const std::filesystem::path &path) const
11{
12 // Open in binary so "\r\n" are explicitly written as CRLF on all platforms
13 std::ofstream stream{path, std::ios::binary};
14 if (!stream.is_open()) {
15 return FormattableError{"'{}': Failed to open for writing.", FormatParam{path.string(), Style::bold}};
16 }
17
18 // JASC-PAL spec uses \r\n, matching vanilla pokeemerald. Could be made configurable if LF output is ever requested.
19 stream << "JASC-PAL\r\n";
20 stream << "0100\r\n";
21 stream << pal.size() << "\r\n";
22
23 for (std::size_t i = 0; i < pal.size(); i++) {
24 if (pal.is_wildcard(i)) {
25 stream << "*\r\n";
26 }
27 else {
28 stream << pal.at(i).to_jasc_str() << "\r\n";
29 }
30 }
31
32 if (stream.fail()) {
33 return FormattableError{"'{}': Failed to write.", FormatParam{path.string(), Style::bold}};
34 }
35
36 return {};
37}
38
39} // namespace porytiles
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:63
ChainableResult< void > save(const Palette< Rgba32, pal::max_size > &pal, const std::filesystem::path &path) const override
A generic palette container for colors that support transparency checking.
Definition palette.hpp:47
std::size_t size() const
Returns the number of slots in the palette.
Definition palette.hpp:229
ColorType at(std::size_t index) const
Gets the color at a specific index.
Definition palette.hpp:276
bool is_wildcard(std::size_t index) const
Checks if a slot is a wildcard.
Definition palette.hpp:198
static const Style bold
Bold text formatting.