gradient/
gradient.rs

1// Oliver Berzs
2// https://github.com/oberzs/duku
3
4// This example creates a gradient and draws a circle with
5// circles of gradient colors
6
7use duku::Duku;
8use duku::Gradient;
9use duku::Result;
10use duku::Rgbf;
11use std::f32::consts::PI;
12
13fn main() -> Result<()> {
14    // create duku context and window
15    let (mut duku, window) = Duku::windowed(500, 500)?;
16
17    // create gradient with 4 colors where the start
18    // and end colors are the same
19    let gradient = Gradient::new(vec![
20        Rgbf::from("#D16BA5"),
21        Rgbf::from("#86A8E7"),
22        Rgbf::from("#5FFBF1"),
23        Rgbf::from("#D16BA5"),
24    ]);
25
26    let big_r = 150.0;
27    let small_r = 35.0;
28    let count = 20;
29
30    // start window loop
31    window.while_open(move |_| {
32        // start drawing on window
33        duku.draw(None, |t| {
34            // for each circle
35            for i in 0..count {
36                // calculate the percentage of how
37                // far around the circle we are
38                let perc = i as f32 / count as f32;
39
40                // calculate the angle or the point
41                let angle = perc * 2.0 * PI;
42
43                // calculate the position
44                // based on polar coordinates
45                let x = big_r * angle.cos();
46                let y = big_r * angle.sin();
47
48                // set the fill to a color from the
49                // gradient
50                t.fill(gradient.get(perc));
51
52                // draw a circle with that color
53                t.circle([x, y], small_r);
54            }
55        });
56    });
57
58    Ok(())
59}