10#include <glibmm/regex.h>
11#include <glibmm/stringutils.h>
37 std::istringstream ss(value);
38 if (value.size() != 9 || ss.get() !=
'#') {
39 throw ColorError(
"Baddly formatted color, it must be in #RRGGBBAA format");
42 ss >> std::hex >> hex;
56 std::vector<double> values(3 + opacity);
72 std::ostringstream oo;
73 oo.imbue(std::locale(
"C"));
74 oo <<
"#" << std::setfill(
'0') << std::setw(alpha ? 8 : 6) << std::hex << (alpha ? value : value >> 8);
86 auto name = color->getName();
90 std::ostringstream oo;
93 if (
auto cns = std::dynamic_pointer_cast<Space::NamedColor>(color->getSpace())) {
94 auto name = cns->getNameFor(color->toRGBA());
96 oo <<
"css-" << color->toString();
101 oo << color->getSpace()->getName() <<
"-" << std::hex << std::setfill(
'0');
102 for (
double const &value : color->getValues()) {
103 unsigned int diget = value * 0xff;
104 oo << std::setw(2) << diget;
108 std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
117 auto name = Glib::ustring(desc);
119 static auto const reg1 = Glib::Regex::create(
"[^[:alnum:]]");
120 name = reg1->replace(
name, 0,
"-",
static_cast<Glib::Regex::MatchFlags
>(0));
121 static auto const reg2 = Glib::Regex::create(
"-{2,}");
122 name = reg2->replace(
name, 0,
"-",
static_cast<Glib::Regex::MatchFlags
>(0));
123 static auto const reg3 = Glib::Regex::create(
"(^-|-$)");
124 name = reg3->replace(
name, 0,
"",
static_cast<Glib::Regex::MatchFlags
>(0));
126 static auto const reg4 = Glib::Regex::create(
"^(\\d+)(-?)([^\\d]*)");
127 name = reg4->replace(
name, 0,
"\\3\\2\\1",
static_cast<Glib::Regex::MatchFlags
>(0));
128 return name.lowercase();
139 color->convert(
orig.getSpace());
155 color.set(1, std::min(color[1], 0.8));
157 color.set(2, std::min(color[2] * 0.7, 0.3));
160 color.set(2, std::max(color[2] + (1.0 - color[2]) * 0.5, 0.8));
168 return l <= 0.885645168 ? l * 0.09032962963 : std::cbrt(l) * 0.249914424 - 0.16;
181 double constexpr l_threshold = 0.85;
182 if (l > l_threshold) {
183 auto t = (l - l_threshold) / (1.0 - l_threshold);
184 return {0.0, 0.4 - 0.1 * t};
186 auto t = (l_threshold - l) / l_threshold;
187 return {1.0, 0.6 + 0.1 * t};
bool set(unsigned int index, double value)
Set a specific channel in the color.
std::optional< Color > converted(Color const &other) const
Return a copy of this color converted to the same format as the other color.
constexpr double SP_RGBA32_G_F(uint32_t v)
constexpr double SP_RGBA32_R_F(uint32_t v)
constexpr double SP_RGBA32_A_F(uint32_t v)
constexpr double SP_RGBA32_B_F(uint32_t v)
A set of useful color modifying functions which do not fit as generic methods on the color class itse...
std::vector< double > rgba_to_values(uint32_t rgba, bool opacity)
Convert a 32bit unsigned int into a set of 3 or 4 double values for rgba.
std::pair< double, double > get_contrasting_color(double l)
uint32_t hex_to_rgba(std::string const &value)
Parse a color directly without any CSS or CMS support.
double perceptual_lightness(double l)
double get_perceptual_lightness(Color const &color)
Return a value for how the light the color appears to be using HSLUV.
Color make_contrasted_color(Color const &orig, double amount)
Make a darker or lighter version of the color, useful for making checkerboards.
double lightness(Color color)
std::string color_to_id(std::optional< Color > const &color)
Create a somewhat unique id for the given color used for palette identification.
Color make_theme_color(Color const &orig, bool dark)
Make a themed dark or light color based on a previous shade, returns RGB color.
std::string desc_to_id(std::string const &desc)
Transform a color name or description into an id used for palette identification.
std::string rgba_to_hex(uint32_t value, bool alpha)
Output the RGBA value as a #RRGGBB hex color, if alpha is true then the output will be #RRGGBBAA inst...