| 191 | // restrict a value to the interval [minimum, maximum] |
| 192 | template <typename Number, typename Limit> |
| 193 | Number clamp(Number value, Limit minValue, Limit maxValue) |
| 194 | { |
| 195 | if (value <= minValue) return minValue; // never smaller than the minimum |
| 196 | if (value >= maxValue) return maxValue; // never bigger than the maximum |
| 197 | return value; // value was inside interval, keep it |
| 198 | } |
| 199 | |
| 200 | // convert from RGB to YCbCr, constants are similar to ITU-R, see https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion |
| 201 | float rgb2y (float r, float g, float b) { return +0.299f * r +0.587f * g +0.114f * b; } |