nannou icon indicating copy to clipboard operation
nannou copied to clipboard

How to use color::Gradient

Open polygon opened this issue 2 years ago • 3 comments

I've been trying to use color::Gradient to implement a color ramp, but I cannot figure out how to use this properly. Here is what I am trying:

let grad = Gradient::new(vec![Rgb::new(1.0, 1.0, 1.0), Rgb::new(1.0, 0.0, 0.0)]);

But I am getting the following error:

error[E0271]: type mismatch resolving `<Srgb as RgbStandard>::TransferFn == LinearFn`
   --> springs/src/main.rs:133:16
    |
133 |     let grad = Gradient::new(vec![Rgb::new(1.0, 1.0, 1.0), Rgb::new(1.0, 0.0, 0.0)]);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `LinearFn`, found struct `Srgb`
    | 
   ::: /home/jan/.cargo/registry/src/github.com-1ecc6299db9ec823/palette-0.5.0/src/gradient.rs:24:24
    |
24  | pub struct Gradient<C: Mix + Clone>(Vec<(C::Scalar, C)>);
    |                        --- required by this bound in `nannou::color::Gradient`
    |
    = note: required because of the requirements on the impl of `Mix` for `nannou::prelude::rgb::Rgb<Srgb, {float}>`

I am a bit stumped here, I tried using rgba(...) instead of Rgb::new but with the same result. How would I use this properly?

polygon avatar Sep 04 '21 15:09 polygon

Was able to get a working resut by creating the component colors with lin_srgb(...). Still seems not very intuitive to me.

polygon avatar Sep 04 '21 15:09 polygon

Hi could you please provide the whole line, that solved your problem?

patrik-cihal avatar Jun 11 '22 12:06 patrik-cihal

@patrik-cihal + future visitors

found a couple of different ways to do it:

let blue1 = LinSrgba::<f64>::from_components((0.0, 0.0, 1.0, 1.0)).into_linear();
let blue2 = lin_srgba(0.0, 0.0, 1.0, 1.0);
let gold1 = LinSrgba::<f64>::from(rgba(GOLD.red, GOLD.green, GOLD.blue, 255).into_lin_srgba());
let gold2: LinSrgba::<f64> = rgba(GOLD.red, GOLD.green, GOLD.blue, 255).into_lin_srgba();

let gradient: Gradient<LinSrgba<f64>> = Gradient::new(
	vec![gold1, blue1, gold2, blue2]
);

freder avatar Mar 06 '23 14:03 freder