termi_graphics/
pixel_art.rs

1#[cfg(target_os = "unix")]
2use crate::ubuntu_terminal_sceme::*;
3#[cfg(target_os = "linux")]
4use crate::ubuntu_terminal_sceme::*;
5#[cfg(target_os = "windows")]
6use crate::windows_cmd_sceme::*;
7///the enum used to check and send colors
8pub enum PixelColors{
9    Red, 
10    Green,
11    Yellow,
12    Blue,
13    Black,
14    Cyan,
15    Magenta,
16    White,
17}
18///the screen struct, used to attach canvases
19pub struct Screen{
20    width:u8,
21    height:u8,
22    color:PixelColors,
23    pixel_vec:Vec<Vec<u8>>,
24} 
25//the new does'nt need to worry about negatives because it's unsigned but still about zeros...
26impl Screen{
27    /// defines a new screen
28    /// #Examples
29    ///```
30    /// use termi_graphics::pixel_art::{Screen,PixelColors};
31    /// let screen1 = Screen::new(25,25,PixelColors::Red).unwrap();
32    ///```
33    ///creates a new 25x25 blank red screen
34    pub fn new(width:u8,height:u8,color:PixelColors)->Option<Screen>{
35        if height == 0 || width == 0{
36            return None;
37        }
38        let mut vec1 = Vec::new();
39        let mut pixel_vec = Vec::new();
40        for i in 0..height{
41           for j in 0..width{
42                &vec1.push(0);
43           } 
44           &pixel_vec.push(vec1);
45           vec1 = Vec::new();
46        }
47        let screen1 = Screen{width,height,color,pixel_vec,};
48        Some(screen1)
49    }
50    /// output function, should not usually be used, you should use animation.play()
51    pub fn print_screen(&self){
52        for y in &self.pixel_vec{
53            for x in y{
54                if *x==0{
55                    match self.color{
56                        PixelColors::Red=>print!("{}",RedScr),
57                        PixelColors::Blue=>print!("{}",BlueScr),
58                        PixelColors::Green=>print!("{}",GreenScr),
59                        PixelColors::Yellow=>print!("{}",YellowScr),
60                        PixelColors::Black=>print!("{}",BlackScr),
61                        PixelColors::Magenta=>print!("{}",MagentaScr),
62                        PixelColors::Cyan=>print!("{}",CyanScr),
63                        PixelColors::White=>print!("{}",WhiteScr),
64                    }
65                }else{
66                    match *x{
67                        1=>print!("{}",RedScr),
68                        2=>print!("{}",BlueScr),
69                        3=>print!("{}",GreenScr),
70                        4=>print!("{}",YellowScr),
71                        5=>print!("{}",BlackScr),
72                        6=>print!("{}",CyanScr),
73                        7=>print!("{}",MagentaScr),
74                        8=>print!("{}",WhiteScr),
75                        _=>print!("impossible"),
76                    }
77                }
78                print!("  ");
79                print!("{}",CLOSING_COLOR);
80            }
81            print!("\n");
82        }
83    }
84    ///needed in case anyone is realy into vecs....
85    pub fn attach_pixels(&mut self,shape:&Vec<Vec<u8>>,x:u8,y:u8)->Result<(),&str>{
86        if x>self.width{
87            return Err("out of bounds! width is too big");
88        }
89        if x>self.height{
90            return Err("out of bounds! height is too big");
91        }
92        let mut cor_x = (x) as usize;
93        let mut cor_y = y as usize;
94        for i in shape{
95            for j in i{
96               if *j>0{
97                    self.pixel_vec[cor_y][cor_x]=*j;
98               }else if *j>8{
99                return Err("no such color");
100               } 
101               cor_x+=1;
102            }
103            cor_y+=1;
104            cor_x = (x) as usize;
105        }
106        Ok(())
107    }
108    ///attach takes a drawn upon canvas and attaches it to the screen at a given point
109    /// #Examples
110    ///```
111    /// use termi_graphics::pixel_art::{Screen,PixelColors,Canvas};
112    /// use termi_graphics::shapes::line;
113    /// let mut screen1 = Screen::new(25,25,PixelColors::Red).unwrap();
114    /// let mut canvas1 = Canvas::new(5,5,PixelColors::Red).unwrap();
115    /// let ln1 = line(4,PixelColors::Green,false).unwrap();
116    /// canvas1.attach(&ln1,1,1).unwrap();
117    /// screen1.attach(&canvas1,10,10).unwrap();
118    ///```
119    ///creates a new 25x25 blank red screen
120    pub fn attach(&mut self,canvas:&Canvas,x:u8,y:u8)->Result<(),&str>{
121        if x>self.width{
122            return Err("out of bounds! width is too big");
123        }
124        if x>self.height{
125            return Err("out of bounds! height is too big");
126        }
127        let mut cor_x = (x) as usize;
128        let mut cor_y = y as usize;
129        let vec55 = &canvas.pixel_vec;
130        for i in vec55{
131            for j in i{
132               if *j>0{
133                    self.pixel_vec[cor_y][cor_x]=*j;
134               }else if *j>8{
135                return Err("no such color");
136               } 
137               cor_x+=1;
138            }
139            cor_y+=1;
140            cor_x = (x) as usize;
141        }
142        Ok(())
143    }
144    ///dittach removes only the cavases belongings from the screen, it works like an earaser that
145    ///you point to a certain spot and it earases in a fixed manor
146    /// #Examples
147    ///```
148    /// use termi_graphics::pixel_art::{Screen,PixelColors,Canvas};
149    /// use termi_graphics::shapes::line;
150    /// let mut screen1 = Screen::new(25,25,PixelColors::Red).unwrap();
151    /// let mut canvas1 = Canvas::new(5,5,PixelColors::Red).unwrap();
152    /// let ln1 = line(4,PixelColors::Green,false).unwrap();
153    /// canvas1.attach(&ln1,1,1).unwrap();
154    /// screen1.attach(&canvas1,10,10).unwrap();
155    /// screen1.dittach(&canvas1,10,10).unwrap();
156    ///```
157    pub fn dittach(&mut self,canvas:&Canvas,x:u8,y:u8)->Result<(),&str>{
158        if x>self.width{
159            return Err("out of bounds! width is too big");
160        }
161        if x>self.height{
162            return Err("out of bounds! height is too big");
163        }
164        let mut cor_x = x as usize;
165        let mut cor_y = y as usize;
166        let vec55 = &canvas.pixel_vec;
167        for i in vec55{
168            for j in i{
169               if *j>0{
170                    self.pixel_vec[cor_y][cor_x]=0;
171               }else if *j>8{
172                return Err("no such color");
173               } 
174               cor_x+=1;
175            }
176            cor_y+=1;
177            cor_x = x as usize;
178        }
179        Ok(())
180    }
181    ///dittach_pixels can remove a vec of pixels of the screen, again with the eareaser analogy
182    /// #Examples
183    ///```
184    /// use termi_graphics::pixel_art::{Screen,PixelColors,Canvas};
185    /// use termi_graphics::shapes::line;
186    /// let mut screen1 = Screen::new(25,25,PixelColors::Red).unwrap();
187    /// let mut canvas1 = Canvas::new(5,5,PixelColors::Red).unwrap();
188    /// let ln1 = line(4,PixelColors::Green,false).unwrap();
189    /// canvas1.attach(&ln1,1,1).unwrap();
190    /// screen1.attach(&canvas1,10,10).unwrap();
191    /// screen1.dittach_pixels(&ln1,11,11).unwrap();
192    ///```
193    pub fn dittach_pixels(&mut self,shape:&Vec<Vec<u8>>,x:u8,y:u8)->Result<(),&str>{
194        if x>self.width{
195            return Err("out of bounds! width is too big");
196        }
197        if x>self.height{
198            return Err("out of bounds! height is too big");
199        }
200        let mut cor_x = x as usize;
201        let mut cor_y = y as usize;
202        for i in shape{
203            for j in i{
204               if *j>0{
205                    self.pixel_vec[cor_y][cor_x]=0;
206               }else if *j>8{
207                return Err("no such color");
208               } 
209               cor_x+=1;
210            }
211            cor_y+=1;
212            cor_x = x as usize;
213        }
214        Ok(())
215    }
216}
217/// canvas struct,  to draw upon it and once it's done attach it to the screen
218pub struct Canvas{
219    width:u8,
220    height:u8,
221    color:PixelColors,
222    pixel_vec:Vec<Vec<u8>>,
223} 
224impl Canvas{
225    ///as the one with screen, canvas is a small screen.
226    pub fn new(width:u8,height:u8,color:PixelColors)->Option<Canvas>{
227        if height == 0 || width == 0{
228            return None;
229        }
230        let mut vec1 = Vec::new();
231        let mut pixel_vec = Vec::new();
232        for i in 0..height{
233           for j in 0..width{
234                &vec1.push(0);
235           } 
236           &pixel_vec.push(vec1);
237           vec1 = Vec::new();
238        }
239        let canvas1 = Canvas{width,height,color,pixel_vec,};
240        Some(canvas1)
241    }
242    ///you should only be able to print a canvas in debug
243    #[cfg(debug_assertions)]
244    pub fn print_canvas(&self){
245        for y in &self.pixel_vec{
246            for x in y{
247                if *x==0{
248                    match self.color{
249                        PixelColors::Red=>print!("{}",RedScr),
250                        PixelColors::Blue=>print!("{}",BlueScr),
251                        PixelColors::Green=>print!("{}",GreenScr),
252                        PixelColors::Yellow=>print!("{}",YellowScr),
253                        PixelColors::Black=>print!("{}",BlackScr),
254                        PixelColors::Magenta=>print!("{}",MagentaScr),
255                        PixelColors::Cyan=>print!("{}",CyanScr),
256                        PixelColors::White=>print!("{}",WhiteScr),
257                    }
258                }else{
259                    match *x{
260                        1=>print!("{}",RedScr),
261                        2=>print!("{}",BlueScr),
262                        3=>print!("{}",GreenScr),
263                        4=>print!("{}",YellowScr),
264                        5=>print!("{}",BlackScr),
265                        6=>print!("{}",CyanScr),
266                        7=>print!("{}",MagentaScr),
267                        8=>print!("{}",WhiteScr),
268                        _=>print!("impossible"),
269                    }
270                }
271                print!("  ");
272                print!("{}",CLOSING_COLOR);
273            }
274            print!("\n");
275        }
276    }
277    ///as the one in screen(attach_pixels)
278    pub fn attach(&mut self,shape:&Vec<Vec<u8>>,x:u8,y:u8)->Result<(),&str>{
279        if x>self.width{
280            return Err("out of bounds! width is too big");
281        }
282        if x>self.height{
283            return Err("out of bounds! height is too big");
284        }
285        let mut cor_x = (x) as usize;
286        let mut cor_y = y as usize;
287        for i in shape{
288            for j in i{
289               if *j>0{
290                    self.pixel_vec[cor_y][cor_x]=*j;
291               }else if *j>8{
292                return Err("no such color");
293               } 
294               cor_x+=1;
295            }
296            cor_y+=1;
297            cor_x = (x) as usize;
298        }
299        Ok(())
300    }
301    ///as the one in screen(dittach_pixels)
302    pub fn dittach(&mut self,shape:&Vec<Vec<u8>>,x:u8,y:u8)->Result<(),&str>{
303        if x>self.width{
304            return Err("out of bounds! width is too big");
305        }
306        if x>self.height{
307            return Err("out of bounds! height is too big");
308        }
309        let mut cor_x = x as usize;
310        let mut cor_y = y as usize;
311        for i in shape{
312            for j in i{
313               if *j>0{
314                    self.pixel_vec[cor_y][cor_x]=0;
315               }else if *j>8{
316                return Err("no such color");
317               } 
318               cor_x+=1;
319            }
320            cor_y+=1;
321            cor_x = x as usize;
322        }
323        Ok(())
324    }
325}