16#ifndef MiL_SVG_COLOR_H
17#define MiL_SVG_COLOR_H 1
227 using enum_map_type = std::map<color, std::string>;
229 static enum_map_type enum_map;
230 if (enum_map.empty())
423 string m(
"to_string(color)::color map size fail ");
425 m += std::to_string(enum_map.size());
426 m +=
" not equal to named colors of size ";
429 throw std::runtime_error(
m);
451 std::ostringstream oss;
452 oss <<
"rgb(" << s.
r <<
',' << s.
g <<
',' << s.
b <<
")";
461 if (s.empty() || s.size() < 5 || s[0] !=
'r')
463 string m(
"color_qi::from_string input is not in rbg form: ");
466 throw std::runtime_error(
m);
475 std::istringstream iss(s);
517 const bool t1 = c1.
r == c2.
r;
518 const bool t2 = c1.
g == c2.
g;
519 const bool t3 = c1.
b == c2.
b;
520 return t1 && t2 && t3;
557 auto [r, g, b] = cqi;
561 std::initializer_list<double> l1 = { rf, gf, bf };
562 double rgb_max = std::max(l1);
571 std::initializer_list<double> l2 = { nrf, ngf, nbf };
572 double nrgb_min = std::min(l2);
573 double nrgb_max = std::max(l2);
576 s = nrgb_max - nrgb_min;
580 double srf((nrf - nrgb_min) /
s);
581 double sgf((ngf - nrgb_min) /
s);
582 double sbf((nbf - nrgb_min) /
s);
583 std::initializer_list<double> l3 = { srf, sgf, sbf };
584 double srgb_max = std::max(l3);
588 h = 0.0 + 60.0 * (sgf - sbf);
592 else if (srgb_max == sgf)
594 h = 120.0 + (60.0 * (sbf - srf));
599 h = 240.0 + (60.0 * (srf - sgf));
621 std::ostringstream oss;
622 oss << std::fixed << std::setprecision(2);
623 oss <<
"hsv(" <<
s.h <<
',' <<
s.s <<
',' <<
s.v <<
")";
633 const ftype x = c * (1.0 - std::fabs(std::fmod(
h / 60.0, 2) - 1.0));
637 if (
h >= 0 &&
h < 60)
641 else if (
h >= 60 &&
h < 120)
645 else if (
h >= 120 &&
h < 180)
649 else if (
h >= 180 &&
h < 240)
653 else if (
h >= 240 &&
h < 300)
664 return color_qi(lpixel(r), lpixel(g), lpixel(b));
679 const bool eqh = k1.
h == k2.
h;
680 const bool lth = k1.
h < k2.
h;
681 const bool lts = k1.
v < k2.
v;
692 const bool eqh = k1.
h == k2.
h;
693 const bool lth = k1.
h < k2.
h;
699 const bool ltothers = k1hyp < k2hyp;
710 const ftype sdist = std::abs(k1.
s - k2.
s);
711 const ftype vdist = std::abs(k1.
v - k2.
v);
713 const ftype habs = std::abs(k1.
h - k2.
h);
714 const ftype hdist = habs > 180 ? 360 - habs : habs;
721 return hdist*1.2 + sdist*2 + vdist*3;
736 const bool ret = d1 < d2;
750 const bool t1 = c1.
h == c2.
h;
751 const bool t2 = c1.
s == c2.
s;
752 const bool t3 = c1.
v == c2.
v;
753 return t1 && t2 && t3;
766 static std::mt19937_64 rg(std::random_device{}());
767 auto distr = std::uniform_real_distribution<>(0.5, 1);
770 double stry = distr(rg);
775 double vtry = distr(rg);
797 double denom = ad + bd;
798 double ur = ((a.r * ad) + (b.r * bd)) / denom;
799 double ug = ((a.g * ad) + (b.g * bd)) / denom;
800 double ub = ((a.b * ad) + (b.b * bd)) / denom;
803 itype cr =
static_cast<itype
>(ur);
804 itype cg =
static_cast<itype
>(ug);
805 itype cb =
static_cast<itype
>(ub);
constexpr uint color_max_size
Total number of enumerated colors.
color
Color enumerated as types.
unsigned short ushort
Base integer type: positive and negative, signed integral value.
double scale_value_on_range(const ssize_type value, const ssize_type min, const ssize_type max, const ssize_type nfloor, const ssize_type nceil)
Scale value from min to max on range (nfloor, nceil).
std::vector< color_qf > color_qfs
const string to_string(const unit e)
std::vector< color_qi > color_qis
Types for Color iteration and combinatorics.
bool operator<(const color_qf &c1, const color_qf &c2)
color_qi average_color_qi(const color_qi &a, const color_qi &b)
Average two colors, return the result.
color_qf mutate_color_qf(const color_qf &k)
Return a variant on saturation/value only.
bool color_qf_lt_hue_v2(const color_qf &k1, const color_qf &k2)
bool operator==(const color_qi &c1, const color_qi &c2)
color_qi combine_color_qi(const color_qi &a, const double ad, const color_qi &b, const double bd)
color_qf::ftype color_qf_distance(const color_qf &k1, const color_qf &k2)
bool color_qf_lt_v(const color_qf &k1, const color_qf &k2)
Default compare distances from k1,k2 to black.
bool color_qf_lt(const color_qf &k1, const color_qf &k2)
Forwarding function.
bool color_qf_lt_hue_v1(const color_qf &k1, const color_qf &k2)
Less than compare for color_qf.
ftype v
Saturation between 0.0 (gray) and 1.0.
const color_qi to_color_qi()
Back to RGB https://www.rapidtables.com/convert/color/hsv-to-rgb.html.
color_qf(ftype vh, ftype vs, ftype vv)
color_qf()=default
Value between 0.0 (black) and 1.0.
ftype s
Hue degree between 0.0 and 360.0.
color_qf & operator=(const color_qf &)=default
color_qf(const color_qf &)=default
static string to_string(color_qf s)
color_qf(const color_qi &cqi)
Color quantified as integral RGB components in the range [0,255]. aka like Scalar in OpenCV.
color_qi(const color_qi &)=default
color_qi & operator=(const color_qi &)=default
static string to_string(color_qi s)
color_qi(itype ra, itype ga, itype ba)
static color_qi from_string(string s)