Porytiles
Loading...
Searching...
No Matches
jasc_pal_saver.cpp
Go to the documentation of this file.
2
3#include <fstream>
4
5#include "fmt/format.h"
6
8
9namespace porytiles2 {
10
11ChainableResult<void> JascPalSaver::save(const Palette<Rgba32> &pal, const std::filesystem::path &path) const
12{
13 // Open in binary so "\r\n" are explicitly written as CRLF on all platforms
14 std::ofstream stream{path, std::ios::binary};
15 if (!stream.is_open()) {
16 return FormattableError{fmt::format("{}: failed to open for writing", path.string())};
17 }
18
19 stream << "JASC-PAL\r\n";
20 stream << "0100\r\n";
21 stream << pal.size() << "\r\n";
22
23 for (const auto &color : pal.colors()) {
24 stream << color.to_jasc_str() << "\r\n";
25 }
26
27 if (stream.fail()) {
28 return FormattableError{fmt::format("{}: failed to write", path.string())};
29 }
30
31 return {};
32}
33
34} // namespace porytiles2
A result type that maintains a chainable sequence of errors for debugging and error reporting.
General-purpose error implementation with formatted message support.
Definition error.hpp:117
ChainableResult< void > save(const Palette< Rgba32 > &pal, const std::filesystem::path &path) const override
A palette container for colors that support transparency checking.
Definition palette.hpp:34
const std::vector< ColorType > & colors() const
Returns a const reference to the internal color vector.
Definition palette.hpp:99
std::size_t size() const
Returns the number of colors in the palette.
Definition palette.hpp:89