void palette_t::normalize_range(uint32_t start, uint32_t end, int lum_min, int lum_max) { // clamp within range start = std::max(start, 0U); end = std::min(end, m_numcolors - 1); // find the minimum and maximum brightness of all the colors in the range int32_t ymin = 1000 * 255, ymax = 0; for (uint32_t index = start; index <= end; index++) { rgb_t rgb = m_entry_color[index]; uint32_t y = 299 * rgb.r() + 587 * rgb.g() + 114 * rgb.b(); ymin = (std::min<uint32_t>)(ymin, y); ymax = (std::max<uint32_t>)(ymax, y); } // determine target minimum/maximum int32_t tmin = (lum_min < 0) ? ((ymin + 500) / 1000) : lum_min; int32_t tmax = (lum_max < 0) ? ((ymax + 500) / 1000) : lum_max; // now normalize the palette for (uint32_t index = start; index <= end; index++) { rgb_t rgb = m_entry_color[index]; int32_t y = 299 * rgb.r() + 587 * rgb.g() + 114 * rgb.b(); int32_t u = ((int32_t)rgb.b()-y /1000)*492 / 1000; int32_t v = ((int32_t)rgb.r()-y / 1000)*877 / 1000; int32_t target = tmin + ((y - ymin) * (tmax - tmin + 1)) / (ymax - ymin); uint8_t r = rgb_t::clamp(target + 1140 * v / 1000); uint8_t g = rgb_t::clamp(target - 395 * u / 1000 - 581 * v / 1000); uint8_t b = rgb_t::clamp(target + 2032 * u / 1000); entry_set_color(index, rgb_t(r, g, b)); } }
void palette_t::normalize_range(UINT32 start, UINT32 end, int lum_min, int lum_max) { // clamp within range start = MAX(start, 0); end = MIN(end, m_numcolors - 1); // find the minimum and maximum brightness of all the colors in the range INT32 ymin = 1000 * 255, ymax = 0; for (UINT32 index = start; index <= end; index++) { rgb_t rgb = m_entry_color[index]; UINT32 y = 299 * rgb.r() + 587 * rgb.g() + 114 * rgb.b(); ymin = MIN(ymin, y); ymax = MAX(ymax, y); } // determine target minimum/maximum INT32 tmin = (lum_min < 0) ? ((ymin + 500) / 1000) : lum_min; INT32 tmax = (lum_max < 0) ? ((ymax + 500) / 1000) : lum_max; // now normalize the palette for (UINT32 index = start; index <= end; index++) { rgb_t rgb = m_entry_color[index]; INT32 y = 299 * rgb.r() + 587 * rgb.g() + 114 * rgb.b(); INT32 u = ((INT32)rgb.b()-y /1000)*492 / 1000; INT32 v = ((INT32)rgb.r()-y / 1000)*877 / 1000; INT32 target = tmin + ((y - ymin) * (tmax - tmin + 1)) / (ymax - ymin); UINT8 r = rgb_t::clamp(target + 1140 * v / 1000); UINT8 g = rgb_t::clamp(target - 395 * u / 1000 - 581 * v / 1000); UINT8 b = rgb_t::clamp(target + 2032 * u / 1000); entry_set_color(index, rgb_t(r, g, b)); } }