| 1 |
#ifndef CORSIX_TH_TH_LUA_UI_H_ |
| 2 |
#define CORSIX_TH_TH_LUA_UI_H_ |
| 3 |
|
| 4 |
#include "config.h" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <cassert> |
| 8 |
|
| 9 |
//! Remap a value between low and high to an output between start and end. |
| 10 |
/*! |
| 11 |
* Perform linear scaling of a value from one range to another. This function |
| 12 |
* is primarily intended for mapping bytes to color channel values. |
| 13 |
* |
| 14 |
* @param low Lower bound of input range. Must be less than high. [0-255] |
| 15 |
* @param high Upper bound of input range. Must be greater than low. [0-255] |
| 16 |
* @param val Value in [low, high] to scale. |
| 17 |
* @param start Lower bound of output range. May be greater than end. [0-255] |
| 18 |
* @param end Upper bound of output range. May be less than start. [0-255] |
| 19 |
* @return Scaled value in [start, end] |
| 20 |
*/ |
| 21 |
constexpr uint8_t map_color_channel(int low, int high, int val, int start, |
| 22 |
int end) { |
| 23 |
assert(low < high); |
| 24 |
assert(start >= 0 && start <= 0xFF); |
| 25 |
assert(end >= 0 && end <= 0xFF); |
| 26 |
|
| 27 |
if (val <= low) { |
| 28 |
return start; |
| 29 |
} |
| 30 |
if (val >= high) { |
| 31 |
return end; |
| 32 |
} |
| 33 |
|
| 34 |
return static_cast<uint8_t>(start + |
| 35 |
(end - start) * (val - low) / (high - low)); |
| 36 |
} |
| 37 |
|
| 38 |
#endif // CORSIX_TH_TH_LUA_UI_H_ |