Skip to content

Commit f837d5c

Browse files
committed
- improve color literals and general parsing, by adding context to the parsed form.
- add color literal operator, that returns HSVColor instead of color - add possibility, to get that context at the outside, this is useful for settings parsing and correct re-serialization later - add option to to_string() force_alpha, that forces alpha, if alphas == 0xFF, otherwise alpha is omitted
1 parent fb427c4 commit f837d5c

File tree

3 files changed

+209
-56
lines changed

3 files changed

+209
-56
lines changed

src/helper/color.cpp

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,45 @@
77

88
#include <fmt/format.h>
99

10+
11+
helper::expected<HSVColor, std::string> HSVColor::from_string(const std::string& value) {
12+
13+
const auto result = detail::get_hsv_color_from_string(value);
14+
15+
if (result.has_value()) {
16+
return result.value().first;
17+
}
18+
19+
return helper::unexpected<std::string>{ result.error() };
20+
}
21+
22+
helper::expected<std::pair<HSVColor, color::SerializeMode>, std::string> HSVColor::from_string_with_serialization(
23+
const std::string& value
24+
) {
25+
26+
const auto result = detail::get_hsv_color_from_string(value);
27+
28+
if (result.has_value()) {
29+
return result.value();
30+
}
31+
32+
return helper::unexpected<std::string>{ result.error() };
33+
}
34+
35+
1036
[[nodiscard]] Color HSVColor::to_rgb_color() const {
1137
return Color{ *this };
1238
}
1339

14-
[[nodiscard]] std::string HSVColor::to_string() const {
15-
return fmt::format("hsva({:.2f}, {:.5f}, {:.5f}, {:#2x})", h, s, v, a);
40+
[[nodiscard]] std::string HSVColor::to_string(bool force_alpha) const {
41+
42+
const auto need_alpha = force_alpha || a != 0xFF;
43+
44+
if (need_alpha) {
45+
return fmt::format("hsva({:.2f}, {:.5f}, {:.5f}, {:#2x})", h, s, v, a);
46+
}
47+
48+
return fmt::format("({:.2f}, {:.5f}, {:.5f})", h, s, v);
1649
}
1750

1851

@@ -26,6 +59,19 @@ helper::expected<Color, std::string> Color::from_string(const std::string& value
2659

2760
const auto result = detail::get_color_from_string(value);
2861

62+
if (result.has_value()) {
63+
return result.value().first;
64+
}
65+
66+
return helper::unexpected<std::string>{ result.error() };
67+
}
68+
69+
helper::expected<std::pair<Color, color::SerializeMode>, std::string> Color::from_string_with_serialization(
70+
const std::string& value
71+
) {
72+
73+
const auto result = detail::get_color_from_string(value);
74+
2975
if (result.has_value()) {
3076
return result.value();
3177
}
@@ -87,18 +133,28 @@ namespace {
87133
}
88134

89135
//Note: this output formats are all deserializable by the from_string method!
90-
[[nodiscard]] std::string Color::to_string(SerializeMode mode) const {
136+
[[nodiscard]] std::string Color::to_string(color::SerializeMode mode, bool force_alpha) const {
137+
138+
const auto need_alpha = force_alpha || a != 0xFF;
91139

92140
switch (mode) {
93-
case SerializeMode::Hex: {
94-
return fmt::format("#{:02x}{:02x}{:02x}{:02x}", r, g, b, a);
141+
case color::SerializeMode::Hex: {
142+
if (need_alpha) {
143+
return fmt::format("#{:02x}{:02x}{:02x}{:02x}", r, g, b, a);
144+
}
145+
146+
return fmt::format("#{:02x}{:02x}{:02x}", r, g, b);
95147
}
96-
case SerializeMode::RGB: {
97-
return fmt::format("rgba({}, {}, {}, {:#2x})", r, g, b, a);
148+
case color::SerializeMode::RGB: {
149+
if (need_alpha) {
150+
return fmt::format("rgba({}, {}, {}, {:#2x})", r, g, b, a);
151+
}
152+
153+
return fmt::format("rgb({}, {}, {})", r, g, b);
98154
}
99-
case SerializeMode::HSV: {
155+
case color::SerializeMode::HSV: {
100156
const auto color = to_hsv_color();
101-
return color.to_string();
157+
return color.to_string(force_alpha);
102158
}
103159
default:
104160
utils::unreachable();

src/helper/color.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
struct Color;
1414

15+
namespace color {
16+
17+
enum class SerializeMode : u8 { Hex, RGB, HSV };
18+
}
19+
1520
struct HSVColor {
1621
double h;
1722
double s;
@@ -49,9 +54,14 @@ struct HSVColor {
4954

5055
constexpr HSVColor(double h, double s, double v) : HSVColor{ h, s, v, 0xFF } { }
5156

57+
[[nodiscard]] static helper::expected<HSVColor, std::string> from_string(const std::string& value);
58+
59+
[[nodiscard]] static helper::expected<std::pair<HSVColor, color::SerializeMode>, std::string>
60+
from_string_with_serialization(const std::string& value);
61+
5262
[[nodiscard]] Color to_rgb_color() const;
5363

54-
[[nodiscard]] std::string to_string() const;
64+
[[nodiscard]] std::string to_string(bool force_alpha = false) const;
5565

5666
std::ostream& operator<<(std::ostream& os) const;
5767
};
@@ -119,6 +129,10 @@ struct Color {
119129

120130
[[nodiscard]] static helper::expected<Color, std::string> from_string(const std::string& value);
121131

132+
[[nodiscard]] static helper::expected<std::pair<Color, color::SerializeMode>, std::string>
133+
from_string_with_serialization(const std::string& value);
134+
135+
122136
[[nodiscard]] HSVColor to_hsv_color() const;
123137

124138
constexpr Color(const HSVColor& color) {
@@ -230,9 +244,8 @@ struct Color {
230244
return Color{ 0xFF, 0xFF, 0xFF, alpha };
231245
};
232246

233-
enum class SerializeMode : u8 { Hex, RGB, HSV };
234-
235-
[[nodiscard]] std::string to_string(SerializeMode mode = SerializeMode::RGB) const;
247+
[[nodiscard]] std::string to_string(color::SerializeMode mode = color::SerializeMode::RGB, bool force_alpha = false)
248+
const;
236249

237250
std::ostream& operator<<(std::ostream& os) const;
238251
};

0 commit comments

Comments
 (0)