Added color_utils

This commit is contained in:
2021-08-06 21:54:08 +02:00
parent b12426ffee
commit a436aef990
5 changed files with 200 additions and 28 deletions

30
src/color_utils.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "color_utils.h"
// 3rdparty lib includes
#include <fmt/core.h>
namespace cpputils {
std::string toString(ColorHelper color)
{
return fmt::format("#{:02X}{:02X}{:02X}", color.r, color.g, color.b);
}
tl::expected<ColorHelper, std::string> parseColor(std::string_view str)
{
// input may be "#FFF" or "#FFFFFF" or "#FFFFFFFF"
if (ColorHelper helper; std::sscanf(str.data(), "#%2hhx%2hhx%2hhx", &helper.r, &helper.g, &helper.b) == 3)
return helper;
if (ColorHelper helper; std::sscanf(str.data(), "#%1hhx%1hhx%1hhx", &helper.r, &helper.g, &helper.b) == 3)
{
helper.r <<= 4;
helper.g <<= 4;
helper.b <<= 4;
return helper;
}
return tl::make_unexpected(fmt::format("invalid color {}", str));
}
} // namespace cpputils