Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::map<color_t, color_t> recolor_range(const color_range& new_range, const std::vector<color_t>& old_rgb)
- {
- std::map<color_t, color_t> map_rgb;
- uint16_t new_red = new_range.mid().r;
- uint16_t new_green = new_range.mid().g;
- uint16_t new_blue = new_range.mid().b;
- uint16_t max_red = new_range.max().r;
- uint16_t max_green = new_range.max().g;
- uint16_t max_blue = new_range.max().b;
- uint16_t min_red = new_range.min().r;
- uint16_t min_green = new_range.min().g;
- uint16_t min_blue = new_range.min().b;
- // Map first color in vector to exact new color
- color_t temp_rgb = old_rgb.empty() ? color_t() : old_rgb[0];
- uint16_t old_r = temp_rgb.r;
- uint16_t old_g = temp_rgb.g;
- uint16_t old_b = temp_rgb.b;
- uint16_t reference_avg = ( old_r + old_g + old_b) / 3;
- for(const auto& old_c : old_rgb) {
- const uint16_t old_avg = (old_c.r + old_c.g + old_c.b) / 3;
- // Calculate new color
- uint32_t new_r = 0, new_g = 0, new_b = 0;
- if(reference_avg && old_avg <= reference_avg) {
- float old_rat = static_cast<float>(old_avg) / reference_avg;
- new_r = static_cast<uint32_t>(old_rat * new_red + (1 - old_rat) * min_red);
- new_g = static_cast<uint32_t>(old_rat * new_green + (1 - old_rat) * min_green);
- new_b = static_cast<uint32_t>(old_rat * new_blue + (1 - old_rat) * min_blue);
- } else if(reference_avg != 255) {
- float old_rat = (255.0f - static_cast<float>(old_avg)) / (255.0f - reference_avg);
- new_r = static_cast<uint32_t>(old_rat * new_red + (1 - old_rat) * max_red);
- new_g = static_cast<uint32_t>(old_rat * new_green + (1 - old_rat) * max_green);
- new_b = static_cast<uint32_t>(old_rat * new_blue + (1 - old_rat) * max_blue);
- } else {
- // Should never get here.
- // Would imply old_avg > reference_avg = 255
- assert(false);
- }
- if(new_r > 255) new_r = 255;
- if(new_g > 255) new_g = 255;
- if(new_b > 255) new_b = 255;
- color_t newrgb(new_r << 16, new_g << 8, new_b);
- map_rgb[old_c] = newrgb;
- }
- return map_rgb;
- }
Advertisement
Add Comment
Please, Sign In to add comment