Refactor color validator from settings to utility source

This commit is contained in:
Dan Paulat 2024-09-19 23:15:41 -05:00
parent 8fc392681a
commit c8dc8ed630
3 changed files with 18 additions and 11 deletions

View file

@ -5,7 +5,6 @@
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/gil.hpp>
#include <fmt/format.h>
#include <re2/re2.h>
namespace scwx
{
@ -116,8 +115,6 @@ public:
void InitializeLegacyAlerts();
void InitializeAlerts();
static bool ValidateColor(const std::string& value);
PaletteSettings* self_;
std::unordered_map<std::string, SettingsVariable<std::string>> palette_ {};
@ -132,12 +129,6 @@ public:
std::vector<SettingsCategory> alertSettings_ {};
};
bool PaletteSettings::Impl::ValidateColor(const std::string& value)
{
static constexpr LazyRE2 re = {"#[0-9A-Fa-f]{8}"};
return RE2::FullMatch(value, *re);
}
PaletteSettings::PaletteSettings() :
SettingsCategory("palette"), p(std::make_unique<Impl>(this))
{
@ -196,8 +187,8 @@ void PaletteSettings::Impl::InitializeLegacyAlerts()
inactiveVariable.SetDefault(
util::color::ToArgbString(alert.second.second));
activeVariable.SetValidator(&ValidateColor);
inactiveVariable.SetValidator(&ValidateColor);
activeVariable.SetValidator(&util::color::ValidateArgbString);
inactiveVariable.SetValidator(&util::color::ValidateArgbString);
variables_.push_back(&activeVariable);
variables_.push_back(&inactiveVariable);

View file

@ -1,6 +1,7 @@
#include <scwx/qt/util/color.hpp>
#include <fmt/format.h>
#include <re2/re2.h>
#include <QColor>
namespace scwx
@ -38,6 +39,12 @@ boost::gil::rgba32f_pixel_t ToRgba32fPixelT(const std::string& argbString)
rgba8Pixel[3] / 255.0f};
}
bool ValidateArgbString(const std::string& argbString)
{
static constexpr LazyRE2 re = {"#[0-9A-Fa-f]{8}"};
return RE2::FullMatch(argbString, *re);
}
} // namespace color
} // namespace util
} // namespace qt

View file

@ -39,6 +39,15 @@ boost::gil::rgba8_pixel_t ToRgba8PixelT(const std::string& argbString);
*/
boost::gil::rgba32f_pixel_t ToRgba32fPixelT(const std::string& argbString);
/**
* Validates an ARGB string used by Qt libraries.
*
* @param argbString
*
* @return Validity of ARGB string
*/
bool ValidateArgbString(const std::string& argbString);
} // namespace color
} // namespace util
} // namespace qt