9 stops.push_back({t, {r, g,
b,
a}});
11 [](
const Stop &
a,
const Stop &
b) { return a.t < b.t; });
15 if (
stops.empty())
return {};
16 t = std::clamp(t, 0.f, 1.f);
17 if (t <=
stops.front().t)
return stops.front().c;
18 if (t >=
stops.back().t)
return stops.back().c;
19 for (
size_t i = 1; i <
stops.size(); ++i) {
20 if (t <=
stops[i].t) {
23 const float u = (t -
a.t) / std::max(1e-6f,
b.t -
a.t);
24 auto lerp8 = [&](uint8_t
x, uint8_t
y) {
25 return uint8_t(std::lround(
float(
x) + (
float(
y) -
float(
x)) *
u));
27 return {lerp8(
a.c.r,
b.c.r), lerp8(
a.c.g,
b.c.g), lerp8(
a.c.b,
b.c.b),
31 return stops.back().c;
35 bands = std::max(2, bands);
36 t = std::clamp(t, 0.f, 1.f);
37 const float step = 1.f / float(bands - 1);
38 const int idx = int(std::lround(t /
step));
Rgba8 sampleBanded(float t, int bands) const
Quantize continuous t into bands steps then sample (pixel look).
std::vector< Stop > stops
Rgba8 sample(float t) const
void add(float t, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)