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
48
49
50
use crate::core::object::Object;
use crate::texture::{Texture, Visitor};
use crate::types::{ColorRGBA, Point3, TextureCoordinate};
use std::error::Error;

/// Texture object that represents a uniform color
pub struct ConstantTexture {
    pub id: usize,
    pub color: ColorRGBA,
}

impl ConstantTexture {
    pub fn new(color: ColorRGBA) -> ConstantTexture {
        ConstantTexture {
            id: Object::new_id(),
            color,
        }
    }
}

impl Texture for ConstantTexture {
    fn get_id(&self) -> usize {
        self.id
    }

    fn value(&self, _: &TextureCoordinate, _: &Point3) -> ColorRGBA {
        self.color
    }

    fn has_alpha(&self) -> bool {
        self.color.w < 1.0
    }

    fn accept(&self, visitor: &mut dyn Visitor) -> Result<(), Box<dyn Error>> {
        visitor.visit_constant_texture(&self)
    }
}

#[cfg(test)]
mod constant_texture_test {
    use super::*;
    use crate::random;

    #[test]
    fn value_test() {
        let t = ConstantTexture::new(ColorRGBA::new(1.0, 0.0, 0.0, 1.0));
        let c = t.value(&random::generate_uv(), &random::generate_point3());
        assert_eq!(c, ColorRGBA::new(1.0, 0.0, 0.0, 1.0));
    }
}