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 ();
0 commit comments