28 #include <unordered_set>
34 const uint16_t new_red = new_range.
mid().r;
35 const uint16_t new_green = new_range.
mid().g;
36 const uint16_t new_blue = new_range.
mid().b;
38 const uint16_t max_red = new_range.
max().r;
39 const uint16_t max_green = new_range.
max().g;
40 const uint16_t max_blue = new_range.
max().b;
42 const uint16_t min_red = new_range.
min().r;
43 const uint16_t min_green = new_range.
min().g;
44 const uint16_t min_blue = new_range.
min().b;
49 const uint16_t reference_avg = (temp_rgb.r + temp_rgb.g + temp_rgb.b) / 3;
51 for(
const auto& old_c : old_rgb) {
52 const uint16_t old_avg = (old_c.r + old_c.g + old_c.b) / 3;
55 uint32_t new_r = 0, new_g = 0, new_b = 0;
57 if(reference_avg && old_avg <= reference_avg) {
58 float old_ratio =
static_cast<float>(old_avg) / reference_avg;
60 new_r =
static_cast<uint32_t
>(old_ratio * new_red + (1 - old_ratio) * min_red);
61 new_g =
static_cast<uint32_t
>(old_ratio * new_green + (1 - old_ratio) * min_green);
62 new_b =
static_cast<uint32_t
>(old_ratio * new_blue + (1 - old_ratio) * min_blue);
63 }
else if(reference_avg != 255) {
64 float old_ratio = (255.0f -
static_cast<float>(old_avg)) / (255.0f - reference_avg);
66 new_r =
static_cast<uint32_t
>(old_ratio * new_red + (1 - old_ratio) * max_red);
67 new_g =
static_cast<uint32_t
>(old_ratio * new_green + (1 - old_ratio) * max_green);
68 new_b =
static_cast<uint32_t
>(old_ratio * new_blue + (1 - old_ratio) * max_blue);
75 new_r = std::min<uint32_t>(new_r, 255);
76 new_g = std::min<uint32_t>(new_g, 255);
77 new_b = std::min<uint32_t>(new_b, 255);
79 map_rgb[old_c] = {
static_cast<uint8_t
>(new_r),
static_cast<uint8_t
>(new_g),
static_cast<uint8_t
>(new_b)};
87 std::vector<color_t> temp, res;
88 std::unordered_set<color_t> clist;
91 for(
int i = 255;
i != 0;
i--) {
92 const int j = 255 -
i;
94 temp.emplace_back(
static_cast<uint8_t
>(0),
static_cast<uint8_t
>(0),
static_cast<uint8_t
>(
i));
95 temp.emplace_back(
static_cast<uint8_t
>(j),
static_cast<uint8_t
>(j),
static_cast<uint8_t
>(255));
101 for(
const auto& cm : cmap) {
102 clist.insert(cm.second);
105 res.push_back(cmap[{0,0,255}]);
107 for(
const auto& cs : clist) {
108 if(cs != res[0] && !cs.null() && cs !=
color_t(255, 255, 255)) {
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
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.