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
51
52
53
54
55
|
use crate::{color::Color, Sample};
#[derive(Clone)]
pub struct Rainbow;
impl Sample for Rainbow {
fn sample(&mut self, x: f64, _y: f64) -> Color {
static PI: f64 = 3.14;
let x = x * PI * 2.0;
return Color {
r: (x + PI / 3.0 * 0.0).sin() * 0.5 + 0.5,
g: (x + PI / 3.0 * 2.0).sin() * 0.5 + 0.5,
b: (x + PI / 3.0 * 4.0).sin() * 0.5 + 0.5,
};
}
fn clone(&self) -> Box<dyn Sample> {
Box::new(Clone::clone(self))
}
}
#[derive(Clone)]
pub struct Sequence(pub Vec<Color>);
impl Sample for Sequence {
fn sample(&mut self, x: f64, _y: f64) -> Color {
self.0[x.rem_euclid(self.0.len() as f64) as usize]
}
fn clone(&self) -> Box<dyn Sample> {
Box::new(Clone::clone(self))
}
}
#[derive(Clone)]
pub struct Gradient(pub Vec<Color>);
impl Sample for Gradient {
fn sample(&mut self, x: f64, _y: f64) -> Color {
let index_int = x.floor() as usize;
let index_error = x % 1.0;
let a = self.0[index_int % self.0.len()];
let b = self.0[(index_int + 1) % self.0.len()];
Color::mix(a, b, index_error)
}
fn clone(&self) -> Box<dyn Sample> {
Box::new(Clone::clone(self))
}
}
#[derive(Clone)]
pub struct Solid(pub Color);
impl Sample for Solid {
fn sample(&mut self, _x: f64, _y: f64) -> Color {
self.0
}
fn clone(&self) -> Box<dyn Sample> {
Box::new(Clone::clone(self))
}
}
|