Better display

This commit is contained in:
PeronTheDuck 2020-01-21 20:49:15 -03:00
parent 6a0c53a86a
commit 7c1b805f1a
3 changed files with 148 additions and 41 deletions

44
src/graphic/color.rs Normal file
View File

@ -0,0 +1,44 @@
#[derive(Copy, Clone, Debug)]
pub struct Color {
r: f64,
g: f64,
b: f64,
}
impl std::convert::From<(u8, u8, u8)> for Color {
fn from(color: (u8, u8, u8)) -> Self {
Color {
r: (color.0 as f64) / 255.0,
g: (color.1 as f64) / 255.0,
b: (color.2 as f64) / 255.0,
}
}
}
impl std::convert::From<(f64, f64, f64)> for Color {
fn from(color: (f64, f64, f64)) -> Self {
Color {
r: color.0,
g: color.1,
b: color.2,
}
}
}
impl std::convert::From<i32> for Color {
fn from(num: i32) -> Self {
let num = num & 0x00_FF_FF_FF; // Remove alpha
Color {
r: (num >> 16) as f64 / 255.0,
g: (num >> 8) as f64 / 255.0,
b: (num) as f64 / 255.0,
}
}
}
impl std::convert::Into<(f64, f64, f64)> for &Color {
fn into(self) -> (f64, f64, f64) {
(self.r, self.g, self.b)
}
}
impl std::default::Default for Color {
fn default() -> Self {
Color::from(0x00_00_00)
}
}

View File

@ -1,14 +1,12 @@
mod color;
mod palette;
pub use color::Color;
pub struct Image {
pub pixels: std::boxed::Box<[Color]>,
pub width: usize,
pub height: usize,
}
#[derive(Copy, Clone, Debug)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Image {
pub fn new() -> Self {
let width: usize = 16;
@ -21,9 +19,19 @@ impl Image {
height,
}
}
pub fn draw(&self, cr: &cairo::Context) {
println!("Started drawing");
cr.scale(self.width as f64, self.height as f64);
pub fn draw(&self, cr: &cairo::Context, widget_size: (i32, i32)) {
//println!("Started drawing");
/* println!("Info: ");
println!("Image {}x{}", w, h);
println!("Widget {}x{}", widget_width, widget_height);
println!("Pixel {}x{}", pixel_width, pixel_height); */
let w = self.width as f64;
let h = self.height as f64;
let widget_width = widget_size.0 as f64;
let widget_height = widget_size.1 as f64;
let pixel_width = 1. / w;
let pixel_height = 1. / h;
cr.scale(widget_width, widget_height);
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.paint();
for y in 0..self.height {
@ -35,41 +43,13 @@ impl Image {
.into();
cr.set_source_rgb(r, g, b);
cr.rectangle(
x as f64 / self.width as f64,
y as f64 / self.height as f64,
1.,
1.,
x as f64 * pixel_width,
y as f64 * pixel_height,
pixel_width,
pixel_height,
);
cr.fill();
}
}
}
}
impl std::convert::From<(u8, u8, u8)> for Color {
fn from(color: (u8, u8, u8)) -> Self {
Color {
r: color.0,
g: color.1,
b: color.2,
}
}
}
impl std::convert::From<i32> for Color {
fn from(num: i32) -> Self {
let num = num & 0x00_FF_FF_FF; // Remove alpha
Color {
r: (num >> 16) as u8,
g: (num >> 8) as u8,
b: (num) as u8,
}
}
}
impl std::convert::Into<(f64, f64, f64)> for &Color {
fn into(self) -> (f64, f64, f64) {
(
(self.r as f64) / 255.0,
(self.g as f64) / 255.0,
(self.b as f64) / 255.0,
)
}
}

83
src/graphic/palette.rs Normal file
View File

@ -0,0 +1,83 @@
use super::Color;
/* pub static palette: [Color; 16] = [
Color {
r: 0x00,
g: 0x00,
b: 0x00,
},
Color {
r: 0x7F,
g: 0x00,
b: 0x00,
},
Color {
r: 0x00,
g: 0x7F,
b: 0x00,
},
Color {
r: 0x7F,
g: 0x7F,
b: 0x00,
},
Color {
r: 0x00,
g: 0x00,
b: 0x7F,
},
Color {
r: 0x7F,
g: 0x00,
b: 0x7F,
},
Color {
r: 0x00,
g: 0x7F,
b: 0x7F,
},
Color {
r: 0x7F,
g: 0x7F,
b: 0x7F,
},
Color {
r: 0x55,
g: 0x55,
b: 0x55,
},
Color {
r: 0xFF,
g: 0x55,
b: 0x55,
},
Color {
r: 0x55,
g: 0xFF,
b: 0x55,
},
Color {
r: 0xFF,
g: 0xFF,
b: 0x55,
},
Color {
r: 0x55,
g: 0x55,
b: 0xFF,
},
Color {
r: 0xFF,
g: 0x55,
b: 0xFF,
},
Color {
r: 0x55,
g: 0xFF,
b: 0xFF,
},
Color {
r: 0xFF,
g: 0xFF,
b: 0xFF,
},
]; */