Vultraz

Untitled

Nov 30th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. std::map<color_t, color_t> recolor_range(const color_range& new_range, const std::vector<color_t>& old_rgb)
  2. {
  3.     std::map<color_t, color_t> map_rgb;
  4.  
  5.     uint16_t new_red   = new_range.mid().r;
  6.     uint16_t new_green = new_range.mid().g;
  7.     uint16_t new_blue  = new_range.mid().b;
  8.  
  9.     uint16_t max_red   = new_range.max().r;
  10.     uint16_t max_green = new_range.max().g;
  11.     uint16_t max_blue  = new_range.max().b;
  12.  
  13.     uint16_t min_red   = new_range.min().r;
  14.     uint16_t min_green = new_range.min().g;
  15.     uint16_t min_blue  = new_range.min().b;
  16.  
  17.     // Map first color in vector to exact new color
  18.     color_t temp_rgb   = old_rgb.empty() ? color_t() : old_rgb[0];
  19.  
  20.     uint16_t old_r = temp_rgb.r;
  21.     uint16_t old_g = temp_rgb.g;
  22.     uint16_t old_b = temp_rgb.b;
  23.  
  24.     uint16_t reference_avg = ( old_r + old_g + old_b) / 3;
  25.  
  26.     for(const auto& old_c : old_rgb) {
  27.         const uint16_t old_avg = (old_c.r + old_c.g + old_c.b) / 3;
  28.  
  29.         // Calculate new color
  30.         uint32_t new_r = 0, new_g = 0, new_b = 0;
  31.  
  32.         if(reference_avg && old_avg <= reference_avg) {
  33.             float old_rat = static_cast<float>(old_avg) / reference_avg;
  34.  
  35.             new_r = static_cast<uint32_t>(old_rat * new_red   + (1 - old_rat) * min_red);
  36.             new_g = static_cast<uint32_t>(old_rat * new_green + (1 - old_rat) * min_green);
  37.             new_b = static_cast<uint32_t>(old_rat * new_blue  + (1 - old_rat) * min_blue);
  38.         } else if(reference_avg != 255) {
  39.             float old_rat = (255.0f - static_cast<float>(old_avg)) / (255.0f - reference_avg);
  40.  
  41.             new_r = static_cast<uint32_t>(old_rat * new_red   + (1 - old_rat) * max_red);
  42.             new_g = static_cast<uint32_t>(old_rat * new_green + (1 - old_rat) * max_green);
  43.             new_b = static_cast<uint32_t>(old_rat * new_blue  + (1 - old_rat) * max_blue);
  44.         } else {
  45.             // Should never get here.
  46.             // Would imply old_avg > reference_avg = 255
  47.             assert(false);
  48.         }
  49.  
  50.         if(new_r > 255) new_r = 255;
  51.         if(new_g > 255) new_g = 255;
  52.         if(new_b > 255) new_b = 255;
  53.  
  54.         color_t newrgb(new_r << 16, new_g << 8, new_b);
  55.         map_rgb[old_c] = newrgb;
  56.     }
  57.  
  58.     return map_rgb;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment