B-spline interpolation
A lot of color schemes that are used in data visualization (https://github.com/d3/d3-scale-chromatic/) are based on b-spline interpolations. Palette already has linear interpolation (Mix), is adding b-spline interpolation something that you would consider?
I have done first experiments with the bspline crate (which is only passively maintained) and it works well for LinSrgb. Here is an example of the Spectral diverging color scheme:
pub type Color = palette::LinSrgba;
pub fn interpolate_spectral(t: f32) -> Color {
let colors: Vec<palette::LinSrgb> = vec![
palette::LinSrgb::<u8>::new(0x9e, 0x01, 0x42).into_format(),
palette::LinSrgb::<u8>::new(0xd5, 0x3e, 0x4f).into_format(),
palette::LinSrgb::<u8>::new(0xf4, 0x6d, 0x43).into_format(),
palette::LinSrgb::<u8>::new(0xfd, 0xae, 0x61).into_format(),
palette::LinSrgb::<u8>::new(0xfe, 0xe0, 0x8b).into_format(),
palette::LinSrgb::<u8>::new(0xff, 0xff, 0xbf).into_format(),
palette::LinSrgb::<u8>::new(0xe6, 0xf5, 0x98).into_format(),
palette::LinSrgb::<u8>::new(0xab, 0xdd, 0xa4).into_format(),
palette::LinSrgb::<u8>::new(0x66, 0xc2, 0xa5).into_format(),
palette::LinSrgb::<u8>::new(0x32, 0x88, 0xbd).into_format(),
palette::LinSrgb::<u8>::new(0x5e, 0x4f, 0xa2).into_format(),
];
// Similar to the implementation in d3, the first and the last element are tripled.
// More information: https://github.com/d3/d3-shape/blob/master/README.md#curveBasis
let knots = vec![
0f32, 0f32, 0f32,
0f32,
1f32 / 8f32,
2f32 / 8f32,
3f32 / 8f32,
4f32 / 8f32,
5f32 / 8f32,
6f32 / 8f32,
7f32 / 8f32,
1f32,
1f32, 1f32, 1f32
];
let spline = bspline::BSpline::new(3, colors, knots);
let point = spline.point(t);
point.into()
}
Thanks!
Definitely, if the Gradient type should stay in the library. I would like to make it #[no_std] friendly, which requires its internal representation to be changed from Vec to something else. I have kind of started to question if it's really useful or if it's better to make sure the color types can be used with better interpolation crates, such as this b-spline one.
I have kind of started to question if it's really useful or if it's better to make sure the color types can be used with better interpolation crates, such as this b-spline one.
This is a hard decision to make! There is one of advantage of having interpolations coupled to the implementation of colors: We would have more control over how things are interpolated. This can be necessary when you have an alpha channel. Sometimes one might want to only interpolate the color without the alpha, other times the alpha is important as well. Generalizing over this in a seperate interpolation crate might add a lot of complexity downstream.
True. I'm not going to throw it out yet. I would be interested in seeing if it's possible to make it generic over storage and interpolation method first. For the benefits you mention. 🙂