34 std::vector<color_t> clist;
35 clist.reserve(old_rgb.
size());
42 const uint8_t reference_avg = old_rgb.
empty()
44 : (old_rgb[0].r + old_rgb[0].g + old_rgb[0].b) / 3;
46 for(
const color_t& old_c : old_rgb) {
47 const uint8_t old_avg = (old_c.r + old_c.g + old_c.b) / 3;
49 if(reference_avg && old_avg <= reference_avg) {
50 float old_ratio =
static_cast<float>(old_avg) / reference_avg;
53 std::min(255u,
static_cast<uint32_t
>(old_ratio * mid_c.r + (1 - old_ratio) * min_c.r)),
54 std::min(255u,
static_cast<uint32_t
>(old_ratio * mid_c.g + (1 - old_ratio) * min_c.g)),
55 std::min(255u,
static_cast<uint32_t
>(old_ratio * mid_c.b + (1 - old_ratio) * min_c.b))
57 }
else if(reference_avg != 255) {
58 float old_ratio = (255.0f -
static_cast<float>(old_avg)) / (255.0f - reference_avg);
61 std::min(255u,
static_cast<uint32_t
>(old_ratio * mid_c.r + (1 - old_ratio) * max_c.r)),
62 std::min(255u,
static_cast<uint32_t
>(old_ratio * mid_c.g + (1 - old_ratio) * max_c.g)),
63 std::min(255u,
static_cast<uint32_t
>(old_ratio * mid_c.b + (1 - old_ratio) * max_c.b))
71 constexpr
auto base_palette = []() {
73 std::array<
color_t, (255 * 2) - 1> res;
74 std::size_t
index = 0;
77 for(uint8_t
i = 255u;
i != 0; --
i) {
81 if(uint8_t j = 255u -
i; j != 0) {
82 res[
index++] = {j, j, 255};
93 auto new_rgb = recolor_range_impl(new_range, old_rgb);
94 assert(new_rgb.size() == old_rgb.size());
97 for(std::size_t
i = 0;
i < new_rgb.size(); ++
i) {
98 res[old_rgb[
i]] = new_rgb[
i];
106 return recolor_range_impl(cr, base_palette);
111 std::ostringstream o;
constexpr bool empty() const noexcept
constexpr size_type size() const noexcept
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(std::string_view 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.