color_t: take arguments as string_view

This commit is contained in:
Charles Dang 2024-08-02 07:47:52 -04:00
parent 4a016139ed
commit c48121ce05
4 changed files with 12 additions and 12 deletions

View file

@ -20,13 +20,13 @@
#include <iomanip>
#include <sstream>
color_t color_t::from_rgba_string(const std::string& c)
color_t color_t::from_rgba_string(std::string_view c)
{
if(c.empty()) {
return null_color();
}
std::vector<std::string> fields = utils::split(c);
std::vector<std::string_view> fields = utils::split_view(c);
// Allow either 3 (automatic opaque alpha) or 4 (explicit alpha) fields
if(fields.size() != 3 && fields.size() != 4) {
@ -41,13 +41,13 @@ color_t color_t::from_rgba_string(const std::string& c)
};
}
color_t color_t::from_rgb_string(const std::string& c)
color_t color_t::from_rgb_string(std::string_view c)
{
if(c.empty()) {
return null_color();
}
std::vector<std::string> fields = utils::split(c);
std::vector<std::string_view> fields = utils::split_view(c);
if(fields.size() != 3) {
throw std::invalid_argument("Wrong number of components for RGB color");
@ -61,7 +61,7 @@ color_t color_t::from_rgb_string(const std::string& c)
};
}
color_t color_t::from_hex_string(const std::string& c)
color_t color_t::from_hex_string(std::string_view c)
{
if(c.length() != 6) {
throw std::invalid_argument("Color hex string should be exactly 6 digits");

View file

@ -78,7 +78,7 @@ struct color_t : SDL_Color
*
* @throw std::invalid_argument if the string is not correctly formatted
*/
static color_t from_rgba_string(const std::string& c);
static color_t from_rgba_string(std::string_view c);
/**
* Creates a new opaque color_t object from a string variable in "R,G,B" format.
@ -89,7 +89,7 @@ struct color_t : SDL_Color
*
* @throw std::invalid_argument if the string is not correctly formatted
*/
static color_t from_rgb_string(const std::string& c);
static color_t from_rgb_string(std::string_view c);
/**
* Creates a new color_t object from a string variable in hex format.
@ -99,7 +99,7 @@ struct color_t : SDL_Color
*
* @throw std::invalid_argument if the string is not correctly formatted
*/
static color_t from_hex_string(const std::string& c);
static color_t from_hex_string(std::string_view c);
/**
* Creates a new color_t object from a uint32_t variable.

View file

@ -441,7 +441,7 @@ theme::label::label(std::size_t sw, std::size_t sh, const config& cfg)
font_ = DefaultFontSize;
if(cfg.has_attribute("font_rgb")) {
font_rgb_ = color_t::from_rgb_string(cfg["font_rgb"]);
font_rgb_ = color_t::from_rgb_string(cfg["font_rgb"].str());
font_rgb_set_ = true;
}
}
@ -463,7 +463,7 @@ theme::status_item::status_item(std::size_t sw, std::size_t sh, const config& cf
}
if(cfg.has_attribute("font_rgb")) {
font_rgb_ = color_t::from_rgb_string(cfg["font_rgb"]);
font_rgb_ = color_t::from_rgb_string(cfg["font_rgb"].str());
font_rgb_set_ = true;
}
}

View file

@ -89,7 +89,7 @@ frame_builder::frame_builder(const config& cfg,const std::string& frame_string)
const auto& text_color_key = cfg[frame_string + "text_color"];
if(!text_color_key.empty()) {
try {
text_color_ = color_t::from_rgb_string(text_color_key);
text_color_ = color_t::from_rgb_string(text_color_key.str());
} catch(const std::invalid_argument& e) {
// Might be thrown either due to an incorrect number of elements or std::stoul failure.
ERR_NG << "Invalid RBG text color in unit animation: " << text_color_key.str()
@ -114,7 +114,7 @@ frame_builder::frame_builder(const config& cfg,const std::string& frame_string)
const auto& blend_color_key = cfg[frame_string + "blend_color"];
if(!blend_color_key.empty()) {
try {
blend_with_ = color_t::from_rgb_string(blend_color_key);
blend_with_ = color_t::from_rgb_string(blend_color_key.str());
} catch(const std::invalid_argument& e) {
// Might be thrown either due to an incorrect number of elements or std::stoul failure.
ERR_NG << "Invalid RBG blend color in unit animation: " << blend_color_key.str()