35 std::vector<color_t> recolor_range_impl(
const color_range& new_range, std::span<const color_t> old_rgb)
37 template<
typename Container>
38 std::vector<color_t> recolor_range_impl(
const color_range& new_range,
const Container& old_rgb)
41 std::vector<color_t> clist;
42 clist.reserve(old_rgb.size());
49 const uint8_t reference_avg = old_rgb.empty()
51 : (old_rgb[0].r + old_rgb[0].g + old_rgb[0].b) / 3;
53 for(
const color_t& old_c : old_rgb) {
54 const uint8_t old_avg = (old_c.r + old_c.g + old_c.b) / 3;
56 if(reference_avg && old_avg <= reference_avg) {
57 float old_ratio =
static_cast<float>(old_avg) / reference_avg;
60 std::min<uint32_t>(255u, old_ratio * mid_c.r + (1 - old_ratio) * min_c.r),
61 std::min<uint32_t>(255u, old_ratio * mid_c.g + (1 - old_ratio) * min_c.g),
62 std::min<uint32_t>(255u, old_ratio * mid_c.b + (1 - old_ratio) * min_c.b)
64 }
else if(reference_avg != 255) {
65 float old_ratio = (255.0f -
static_cast<float>(old_avg)) / (255.0f - reference_avg);
68 std::min<uint32_t>(255u, old_ratio * mid_c.r + (1 - old_ratio) * max_c.r),
69 std::min<uint32_t>(255u, old_ratio * mid_c.g + (1 - old_ratio) * max_c.g),
70 std::min<uint32_t>(255u, old_ratio * mid_c.b + (1 - old_ratio) * max_c.b)
78 constexpr
auto base_palette = []() {
80 std::array<
color_t, (255 * 2) - 1> res;
81 std::size_t
index = 0;
84 for(uint8_t
i = 255u;
i != 0; --
i) {
88 if(uint8_t j = 255u -
i; j != 0) {
89 res[
index++] = {j, j, 255};
100 auto new_rgb = recolor_range_impl(new_range, old_rgb);
101 assert(new_rgb.size() == old_rgb.size());
104 for(std::size_t
i = 0;
i < new_rgb.size(); ++
i) {
105 res[old_rgb[
i]] = new_rgb[
i];
113 return recolor_range_impl(cr, base_palette);
118 std::ostringstream o;
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
color_t max() const
Maximum color shade.
color_t mid() const
Average color shade.
color_t min() const
Minimum color shade.
std::string debug() const
Return a string describing the color range for debug output.
std::vector< color_t > palette(const color_range &cr)
Creates a reference color palette from a color range.
color_range_map recolor_range(const color_range &new_range, const std::vector< color_t > &old_rgb)
Converts a source palette using the specified color_range object.
std::unordered_map< color_t, color_t > color_range_map
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
The basic class for representing 8-bit RGB or RGBA colour values.
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.