21 const std::vector<Scalar>& stops) {
24 uint32_t texture_size;
25 if (stops.size() == 2) {
26 texture_size = colors.size();
28 auto minimum_delta = 1.0;
29 for (
size_t i = 1; i < stops.size(); i++) {
30 auto value = stops[i] - stops[i - 1];
35 if (
value < minimum_delta) {
36 minimum_delta =
value;
43 texture_size = std::min(
44 static_cast<uint32_t
>(std::round(1.0 / minimum_delta)) + 1, 1024u);
48 .texture_size = texture_size,
50 data.color_bytes.reserve(texture_size * 4);
52 if (texture_size == colors.size() && colors.size() <= 1024) {
53 for (
auto i = 0u; i < colors.size(); i++) {
57 Color previous_color = colors[0];
58 auto previous_stop = 0.0;
59 auto previous_color_index = 0;
64 for (
auto i = 1u; i < texture_size - 1; i++) {
65 auto scaled_i = i / (texture_size - 1.0);
66 Color next_color = colors[previous_color_index + 1];
67 auto next_stop = stops[previous_color_index + 1];
72 previous_color = next_color;
73 previous_stop = next_stop;
74 previous_color_index += 1;
75 }
else if (scaled_i < next_stop) {
77 auto t = (scaled_i - previous_stop) / (next_stop - previous_stop);
78 auto mixed_color =
Color::Lerp(previous_color, next_color, t);
83 previous_color = next_color;
84 previous_stop = next_stop;
85 previous_color_index += 1;
86 next_color = colors[previous_color_index + 1];
87 auto next_stop = stops[previous_color_index + 1];
89 auto t = (scaled_i - previous_stop) / (next_stop - previous_stop);
90 auto mixed_color =
Color::Lerp(previous_color, next_color, t);
GradientData CreateGradientBuffer(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the interpolated color bytes for the linear gradient described by colors and s...
static constexpr Color Lerp(Color a, Color b, Scalar t)
Return a color that is linearly interpolated between colors a and b, according to the value of t.