1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use color::{ToRgb,ToRgba,Rgb,Rgba,Channel, color_space::LinearRgb};
use rin_math::{lerp, Vec2};

#[derive(Clone,Copy)]
pub enum LinearGradientDirection{
    Vertical,
    Horizontal
}

/// Specifies a gradient that can be used to draw certain primitives
/// instead of a solid color
pub enum Gradient<C>{
    Linear{start_color: C, end_color: C, direction: LinearGradientDirection},
    Circular{start_color: C, end_color: C}
}

impl<C:ToRgb> Gradient<C>{
    pub fn rgb_at<T:Channel>(&self, norm_pos: Vec2) -> Rgb<T>{
        match *self{
            Gradient::Linear{ref start_color, ref end_color, ref direction} => {
                match *direction{
                    LinearGradientDirection::Vertical => {
                    	let start: Rgb<f32, LinearRgb> = start_color.to_rgb().to_linear();
                    	let end: Rgb<f32, LinearRgb> = end_color.to_rgb().to_linear();
                        lerp(start,end,norm_pos.y).to_rgb().to_standard()
                    }
                    LinearGradientDirection::Horizontal => {
                    	let start: Rgb<f32, LinearRgb> = start_color.to_rgb().to_linear();
                    	let end: Rgb<f32, LinearRgb> = end_color.to_rgb().to_linear();
                        lerp(start,end,norm_pos.x).to_rgb().to_standard()
                    }
                }
            }

            Gradient::Circular{ref start_color, ref end_color} => {
                let distance = norm_pos.norm();
                let start: Rgb<f32, LinearRgb> = start_color.to_rgb().to_linear();
                let end: Rgb<f32, LinearRgb> = end_color.to_rgb().to_linear();
                lerp(start,end,distance).to_rgb().to_standard()
            }
        }
    }

    pub fn rgba_at<T:Channel>(&self, norm_pos: Vec2) -> Rgba<T>{
         self.rgb_at::<T>(norm_pos).to_rgba()
    }
}