@@ -3,12 +3,13 @@ use crate::util::PixelData;
3
3
4
4
pub enum ColourMethod {
5
5
Iteration ,
6
- // IterationSquareRoot,
7
- // Histogram
6
+ IterationSquareRoot ,
7
+ Histogram ,
8
+ Distance
8
9
}
9
10
10
11
impl ColourMethod {
11
- pub fn run ( & self , pixels : & Vec < PixelData > , image : & mut Image , maximum_iterations : usize ) {
12
+ pub fn run ( & self , pixel_data : & Vec < PixelData > , image : & mut Image , maximum_iteration : usize , delta_pixel : f64 ) {
12
13
// Palette is temporarily here
13
14
let mut colours = Vec :: new ( ) ;
14
15
@@ -56,10 +57,12 @@ impl ColourMethod {
56
57
57
58
match self {
58
59
ColourMethod :: Iteration => {
59
- for pixel in pixels {
60
- let ( red, green, blue) = if pixel. glitched && image. display_glitches {
60
+ // No smooth colouring at the moment
61
+
62
+ for pixel in pixel_data {
63
+ let ( r, g, b) = if pixel. glitched && image. display_glitches {
61
64
( 255 , 0 , 0 )
62
- } else if pixel. iteration >= maximum_iterations {
65
+ } else if pixel. iteration >= maximum_iteration {
63
66
( 0 , 0 , 0 )
64
67
} else {
65
68
// 0.1656
@@ -70,87 +73,79 @@ impl ColourMethod {
70
73
( colour. 0 as u8 , colour. 1 as u8 , colour. 2 as u8 )
71
74
} ;
72
75
73
- image. plot ( pixel. image_x , pixel. image_y , red , green , blue ) ;
76
+ image. plot ( pixel. image_x , pixel. image_y , r , g , b ) ;
74
77
}
75
78
} ,
76
- // ColourMethod::IterationSquareRoot => {
77
- // for point in points {
78
- // let index = point.index;
79
- //
80
- // if point.glitched && display_glitches {
81
- // image[3 * index] = 255u8;
82
- // image[3 * index + 1] = 0u8;
83
- // image[3 * index + 2] = 0u8;
84
- // } else if point.iterations >= maximum_iterations {
85
- // image[3 * index] = 0u8;
86
- // image[3 * index + 1] = 0u8;
87
- // image[3 * index + 2] = 0u8;
88
- // } else {
89
- // let hue = 1600.0 * (point.iterations as f32 + point.smooth).sqrt() % 8192.0;
90
- //
91
- // let colour = colours[(hue.floor() as usize) % 8192];
92
- // let colour2 = colours[(hue.floor() as usize + 1) % 8192];
93
- //
94
- // let red = (colour.0 + ((colour2.0 - colour.0) * hue.fract())) as u8;
95
- // let green = (colour.1 + ((colour2.1 - colour.1) * hue.fract())) as u8;
96
- // let blue = (colour.2 + ((colour2.2 - colour.2) * hue.fract())) as u8;
97
- //
98
- // image[3 * index] = red;
99
- // image[3 * index + 1] = green;
100
- // image[3 * index + 2] = blue;
101
- // }
102
- // }
103
- // },
104
- // ColourMethod::Histogram => {
105
- // let mut iteration_counts = vec![0usize; maximum_iterations + 1];
106
- //
107
- // for point in points {
108
- // iteration_counts[point.iterations as usize] += 1
109
- // }
110
- //
111
- // for i in 1..iteration_counts.len() {
112
- // iteration_counts[i] += iteration_counts[i - 1];
113
- // }
114
- //
115
- // let total = iteration_counts[maximum_iterations - 1];
116
- //
117
- // for point in points {
118
- // let index = point.index;
119
- //
120
- // if point.glitched && display_glitches {
121
- // image[3 * index] = 255u8;
122
- // image[3 * index + 1] = 0u8;
123
- // image[3 * index + 2] = 0u8;
124
- // } else if point.iterations >= maximum_iterations {
125
- // image[3 * index] = 0u8;
126
- // image[3 * index + 1] = 0u8;
127
- // image[3 * index + 2] = 0u8;
128
- // } else {
129
- // let factor = if point.smooth == std::f32::NAN || point.iterations as f32 + point.smooth < 0.0 {
130
- // point.iterations as f32
131
- // } else {
132
- // point.iterations as f32 + point.smooth
133
- // };
134
- //
135
- // let v1 = iteration_counts[factor as usize] as f32 / total as f32;
136
- // let v2 = iteration_counts[factor as usize + 1] as f32 / total as f32;
137
- //
138
- // // the hue is used to smooth the histogram bins. The hue is in the range 0.0-1.0
139
- // let hue = (v1 + (v2 - v1) * factor.fract()) * 8192.0;
140
- //
141
- // let colour = colours[hue.floor() as usize % 8192];
142
- // let colour2 = colours[(hue.floor() as usize + 1) % 8192];
143
- //
144
- // let red = (colour.0 + ((colour2.0 - colour.0) * factor.fract())) as u8;
145
- // let green = (colour.1 + ((colour2.1 - colour.1) * factor.fract())) as u8;
146
- // let blue = (colour.2 + ((colour2.2 - colour.2) * factor.fract())) as u8;
147
- //
148
- // image[3 * index] = red;
149
- // image[3 * index + 1] = green;
150
- // image[3 * index + 2] = blue;
151
- // }
152
- // }
153
- // }
79
+ ColourMethod :: IterationSquareRoot => {
80
+ for pixel in pixel_data {
81
+ let ( r, g, b) = if pixel. glitched && image. display_glitches {
82
+ ( 255 , 0 , 0 )
83
+ } else if pixel. iteration >= maximum_iteration {
84
+ ( 0 , 0 , 0 )
85
+ } else {
86
+ // 0.1656
87
+ let hue = ( 0.1656 * pixel. iteration as f64 ) . sqrt ( ) as usize % 8192 ;
88
+
89
+ let colour = colours[ hue] ;
90
+
91
+ ( colour. 0 as u8 , colour. 1 as u8 , colour. 2 as u8 )
92
+ } ;
93
+
94
+ image. plot ( pixel. image_x , pixel. image_y , r, g, b) ;
95
+ }
96
+ } ,
97
+ ColourMethod :: Histogram => {
98
+ let mut iteration_counts = vec ! [ 0usize ; maximum_iteration + 2 ] ;
99
+
100
+ for pixel in pixel_data {
101
+ iteration_counts[ pixel. iteration as usize ] += 1
102
+ }
103
+
104
+ for i in 1 ..iteration_counts. len ( ) {
105
+ iteration_counts[ i] += iteration_counts[ i - 1 ] ;
106
+ }
107
+
108
+ // Don't count the pixels that are inside the set
109
+ let total = iteration_counts[ maximum_iteration - 1 ] ;
110
+
111
+
112
+ for pixel in pixel_data {
113
+ let ( r, g, b) = if pixel. glitched && image. display_glitches {
114
+ ( 255 , 0 , 0 )
115
+ } else if pixel. iteration >= maximum_iteration {
116
+ ( 0 , 0 , 0 )
117
+ } else {
118
+ let v1 = iteration_counts[ pixel. iteration ] as f32 / total as f32 ;
119
+
120
+ // the hue is used to smooth the histogram bins. The hue is in the range 0.0-1.0
121
+ let hue = ( v1 * 8192.0 ) as usize ;
122
+
123
+ let colour = colours[ hue % 8192 ] ;
124
+
125
+ ( colour. 0 as u8 , colour. 1 as u8 , colour. 2 as u8 )
126
+ } ;
127
+
128
+ image. plot ( pixel. image_x , pixel. image_y , r, g, b) ;
129
+ }
130
+ } ,
131
+ ColourMethod :: Distance => {
132
+ // At the moment distance has a white-black gradient
133
+ for pixel in pixel_data {
134
+ let ( r, g, b) = if pixel. glitched && image. display_glitches {
135
+ ( 255 , 0 , 0 )
136
+ } else {
137
+ if pixel. escaped {
138
+ let de = 2.0 * pixel. delta_current . norm ( ) * pixel. delta_current . norm ( ) . ln ( ) / pixel. derivative_current . norm ( ) ;
139
+ let out = ( 255.0 * ( de / delta_pixel) . tanh ( ) ) as u8 ;
140
+ ( out, out, out)
141
+ } else {
142
+ ( 0 , 0 , 0 )
143
+ }
144
+ } ;
145
+
146
+ image. plot ( pixel. image_x , pixel. image_y , r, g, b) ;
147
+ }
148
+ }
154
149
}
155
150
}
156
151
}
0 commit comments