From be0b3aa41fd22171dc1909dae91350f334bc8205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Sat, 30 Dec 2017 21:29:24 +0100 Subject: [PATCH] Move color and rectangle structs to separate module --- src/color.rs | 22 +++++++++++++++++++++ src/main.rs | 54 ++++------------------------------------------------ src/rect.rs | 20 +++++++++++++++++++ 3 files changed, 46 insertions(+), 50 deletions(-) create mode 100644 src/color.rs create mode 100644 src/rect.rs diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..82e2f87 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,22 @@ +/// Color struct. +/// +/// Represents a color with RGB values from 0 to 255. +impl Color { + + /// Constructor. + /// + /// The Red, Green and Blue values must be between 0 and 255. + pub fn from(r: u8, g: u8, b: u8) -> Color { + Color { + r, + g, + b, + } + } + + /// Get a hexadecimal representation of the color, + /// such as `FFFFFF` for white and `FF0000` for red. + pub fn as_hex(&self) -> String { + format!("{:02X}{:02X}{:02X}", self.r, self.g, self.b) + } +} diff --git a/src/main.rs b/src/main.rs index 99cbe3c..f56fc1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate clap; extern crate image; mod app; +mod color; +mod rect; use std::io::Error; use std::io::prelude::*; @@ -19,6 +21,8 @@ use clap::{Arg, ArgMatches, App}; use image::{DynamicImage, FilterType, Pixel}; use app::*; +use color::Color; +use rect::Rect; @@ -546,53 +550,3 @@ impl PixClient { // Ok(buffer) // } } - - - -/// Color structure. -#[derive(Copy, Clone)] -struct Color { - r: u8, - g: u8, - b: u8, -} - -impl Color { - /// Create a new color instance - pub fn from(r: u8, g: u8, b: u8) -> Color { - Color { - r, - g, - b, - } - } - - /// Get a hexadecimal representation of the color, - /// such as `FFFFFF` for white and `FF0000` for red. - pub fn as_hex(&self) -> String { - format!("{:02X}{:02X}{:02X}", self.r, self.g, self.b) - } -} - - - -/// Rectangle struct. -#[derive(Copy, Clone)] -pub struct Rect { - // TODO: Make these properties private - pub x: u32, - pub y: u32, - pub w: u32, - pub h: u32, -} - -impl Rect { - pub fn from(x: u32, y: u32, w: u32, h: u32) -> Rect { - Rect { - x, - y, - w, - h, - } - } -} diff --git a/src/rect.rs b/src/rect.rs new file mode 100644 index 0000000..5d18a50 --- /dev/null +++ b/src/rect.rs @@ -0,0 +1,20 @@ +/// Rectangle struct. +#[derive(Copy, Clone)] +pub struct Rect { + // TODO: Make these properties private + pub x: u32, + pub y: u32, + pub w: u32, + pub h: u32, +} + +impl Rect { + pub fn from(x: u32, y: u32, w: u32, h: u32) -> Rect { + Rect { + x, + y, + w, + h, + } + } +}