diff options
author | crupest <crupest@outlook.com> | 2021-05-10 19:34:01 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-05-10 19:34:01 +0800 |
commit | 43a1883ac80ce3ff4782cd74e5ad16a03887c2aa (patch) | |
tree | daa3c374813f478e0ce99dfca858393b6ad95a3a /src/platform/Color.cpp | |
parent | ae5042599688af2d8b462e49e3cd103b6ec92fe9 (diff) | |
download | cru-43a1883ac80ce3ff4782cd74e5ad16a03887c2aa.tar.gz cru-43a1883ac80ce3ff4782cd74e5ad16a03887c2aa.tar.bz2 cru-43a1883ac80ce3ff4782cd74e5ad16a03887c2aa.zip |
...
Diffstat (limited to 'src/platform/Color.cpp')
-rw-r--r-- | src/platform/Color.cpp | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/platform/Color.cpp b/src/platform/Color.cpp index 325cf413..d1c60217 100644 --- a/src/platform/Color.cpp +++ b/src/platform/Color.cpp @@ -1,12 +1,48 @@ #include "cru/platform/Color.hpp" #include <cstdint> +#include <gsl/gsl> #include <optional> #include <stdexcept> +#include <string> #include <string_view> -#include "gsl/gsl_util" +#include "fmt/core.h" namespace cru::platform { +std::string Color::ToUtf8String() const { + auto to_hex = [](std::uint8_t v) -> char { + return v >= 10 ? v - 10 + 'a' : v + '0'; + }; + + auto to_two_hex_digit = [to_hex](std::uint8_t v) -> std::string { + return {to_hex(v /= 16), to_hex(v %= 16)}; + }; + + std::string result = "#"; + result.append(to_two_hex_digit(alpha)); + result.append(to_two_hex_digit(red)); + result.append(to_two_hex_digit(green)); + result.append(to_two_hex_digit(blue)); + return result; +} + +std::u16string Color::ToString() const { + auto to_hex = [](std::uint8_t v) -> char16_t { + return v >= 10 ? v - 10 + u'a' : v + u'0'; + }; + + auto to_two_hex_digit = [to_hex](std::uint8_t v) -> std::u16string { + return {to_hex(v /= 16), to_hex(v %= 16)}; + }; + + std::u16string result = u"#"; + result.append(to_two_hex_digit(alpha)); + result.append(to_two_hex_digit(red)); + result.append(to_two_hex_digit(green)); + result.append(to_two_hex_digit(blue)); + return result; +} + std::optional<Color> Color::Parse(std::u16string_view string, bool parse_predefined_color) { if (parse_predefined_color) { |