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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::{
drawings::special::{FreeDrawing, StraightLine},
prelude::{Canvas, Position, RelativePosition, ToPosition},
};
use super::{
surrounding_pixels::SurroundingPixels, ColorSelector, PixelDescriptor, PixelsTable,
RectSelection, Selection,
};
pub trait MoreMethodsForPixelsTable<const H: usize, const W: usize>: PixelsTable<H, W> {
fn change_pixel_color<P: ToPosition<H, W>, C1: ColorSelector>(&mut self, pos: P, color: C1) {
if let Ok(pix) = self.pixel_at_mut(pos.get_position()) {
*pix = color.rgb().into()
}
}
fn surrounding_pixels(&self, pos: &Position) -> SurroundingPixels<H, W, Self>
where
Self: Sized,
{
SurroundingPixels::new(self, *pos)
}
fn boundary_fill<P: ToPosition<H, W>, C1: ColorSelector + Clone>(
&mut self,
pos: P,
fill_color: PixelDescriptor,
boundary_color: C1,
dimensional_penetration: bool,
) {
let pos = pos.get_position();
let bc = boundary_color.rgb();
let pix = match self.pixel_at_mut(pos) {
Ok(pix) => pix,
Err(_) => return,
};
if *pix != fill_color {
match pix {
PixelDescriptor::Nothing => {
*pix = fill_color;
for dir in [
RelativePosition::Top,
RelativePosition::Right,
RelativePosition::Down,
RelativePosition::Left,
] {
if let Some(nex_pos) = dir.get_exact(pos, 1) {
self.boundary_fill(
nex_pos,
fill_color,
boundary_color.clone(),
dimensional_penetration,
)
}
}
if dimensional_penetration {
for dir in [
RelativePosition::RightTop,
RelativePosition::RightDown,
RelativePosition::LeftDown,
RelativePosition::LeftTop,
] {
if let Some(nex_pos) = dir.get_exact(pos, 1) {
self.boundary_fill(
nex_pos,
fill_color,
boundary_color.clone(),
dimensional_penetration,
)
}
}
}
}
PixelDescriptor::Pixel(bc1) => {
if bc1.color() != bc {
*pix = fill_color;
for dir in [
RelativePosition::Top,
RelativePosition::Right,
RelativePosition::Down,
RelativePosition::Left,
] {
if let Some(nex_pos) = dir.get_exact(pos, 1) {
self.boundary_fill(
nex_pos,
fill_color,
boundary_color.clone(),
dimensional_penetration,
)
}
}
if dimensional_penetration {
for dir in [
RelativePosition::RightTop,
RelativePosition::RightDown,
RelativePosition::LeftDown,
RelativePosition::LeftTop,
] {
if let Some(nex_pos) = dir.get_exact(pos, 1) {
self.boundary_fill(
nex_pos,
fill_color,
boundary_color.clone(),
dimensional_penetration,
)
}
}
}
}
}
}
}
}
fn boundary_fill_color<P: ToPosition<H, W>, C1: ColorSelector, C2: ColorSelector + Clone>(
&mut self,
pos: P,
fill_color: C1,
boundary_color: C2,
dimensional_penetration: bool,
) {
self.boundary_fill(
pos,
fill_color.rgb().into(),
boundary_color,
dimensional_penetration,
)
}
fn draw_straight_line<C: ColorSelector, P1: ToPosition<H, W>, P2: ToPosition<H, W>>(
&mut self,
color: C,
start: P1,
end: P2,
) {
let line = StraightLine::from_color(color, start, end);
self.draw_from_table_exact(&line)
}
fn draw_many_straight_lines<C: ColorSelector, P1: ToPosition<H, W>, P2: ToPosition<H, W>>(
&mut self,
color: C,
start: P1,
end: P2,
builder: impl FnOnce(&mut StraightLine<H, W>),
) {
let mut line = StraightLine::from_color(color, start, end);
builder(&mut line);
self.draw_from_table_exact(&line)
}
fn draw_free_drawing<C: ColorSelector>(
&mut self,
color: C,
builder: impl FnOnce(&mut FreeDrawing<H, W>),
) {
let mut free_drawing = FreeDrawing::new(color);
builder(&mut free_drawing);
self.draw_from_table_exact(&free_drawing)
}
/// Get an rectangle like selection with specified top left and bottom right edges.
///
/// ## Example
/// ```
/// let mut selection = pixel_paper.rect_selection(LeftTopEdge, (3, 3));
/// selection.apply_color(Black);
/// ```
fn rect_selection<P1: ToPosition<H, W>, P2: ToPosition<H, W>>(
&mut self,
top_left: P1,
bottom_right: P2,
) -> RectSelection<H, W, Self>
where
Self: Sized,
{
RectSelection::new(self, top_left, bottom_right)
}
/// Get a copy of a fixed size ( `H1`*`W1` ) section of a [`PixelsTable`].
///
/// # Example
/// ```
/// let copy = pixel_paper.section_copy::<3, 3>(LeftTopEdge);
/// ```
fn section_copy<const H1: usize, const W1: usize>(
&mut self,
start: impl ToPosition<H, W>,
) -> Canvas<H1, W1>
where
Self: Sized,
{
let mut canvas = Canvas::<H1, W1>::default();
let start = start.get_position();
let selection = self.rect_selection(start, (start.row() + H1, start.column() + W1));
selection.copy_to(&mut canvas, (0, 0));
canvas
}
/// **Cut, Modify, Replace**
///
/// Modify a fixed size ( `H1`*`W1` ) section of this [`PixelsTable`]
/// separately and in place.
///
/// ## Example
/// Modify a 3*3 cut starting from LeftTopEdge as a separate [`Canvas`].
/// ```
/// pixel_paper.modify_section::<3, 3>(LeftTopEdge, |canvas| {
/// canvas.change_pixel_color(RightBottomEdge, Red);
/// });
/// ```
fn modify_section<const H1: usize, const W1: usize>(
&mut self,
start: impl ToPosition<H, W> + Clone,
modifier: impl FnOnce(&mut Canvas<H1, W1>),
) where
Self: Sized,
{
let mut section = self.section_copy::<H1, W1>(start.clone());
modifier(&mut section);
self.draw_from_table(§ion, start);
}
}