Add colors in Rust (#78)

* 🎨 Add colors in Rust

* 🌿 Use an option for the properties and #[repr(C)]

*  Remove WASM dependency on document.

* 😎 Wrap Fill and stroke in a style struct.

* 📦 Use crate::Color

* Merge Add transactions for temporary modifications to the document

* Run cargo fmt

* Color without a 'U'
This commit is contained in:
0HyperCube
2021-04-21 22:25:06 +01:00
committed by Keavon Chambers
parent 2849b99b59
commit 46c9ef02ca
26 changed files with 689 additions and 407 deletions

View File

@@ -0,0 +1,29 @@
use super::style;
use super::LayerData;
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Circle {
shape: kurbo::Circle,
style: style::PathStyle,
}
impl Circle {
pub fn new(center: impl Into<kurbo::Point>, radius: f64, style: style::PathStyle) -> Circle {
Circle {
shape: kurbo::Circle::new(center, radius),
style,
}
}
}
impl LayerData for Circle {
fn render(&self) -> String {
format!(
r#"<circle cx="{}" cy="{}" r="{}" {} />"#,
self.shape.center.x,
self.shape.center.y,
self.shape.radius,
self.style.render(),
)
}
}

View File

@@ -0,0 +1,87 @@
use crate::{DocumentError, LayerId};
use super::{Layer, LayerData, LayerDataTypes};
#[derive(Debug, Clone, PartialEq)]
pub struct Folder {
next_assignment_id: LayerId,
pub layer_ids: Vec<LayerId>,
layers: Vec<Layer>,
}
impl LayerData for Folder {
fn render(&self) -> String {
self.layers
.iter()
.filter(|layer| layer.visible)
.map(|layer| layer.data.render())
.fold(String::with_capacity(self.layers.len() * 30), |s, n| s + "\n" + &n)
}
}
impl Folder {
pub fn add_layer(&mut self, layer: Layer, insert_index: isize) -> Option<LayerId> {
let mut insert_index = insert_index as i128;
if insert_index < 0 {
insert_index = self.layers.len() as i128 + insert_index as i128 + 1;
}
if insert_index <= self.layers.len() as i128 && insert_index >= 0 {
self.layers.insert(insert_index as usize, layer);
self.layer_ids.insert(insert_index as usize, self.next_assignment_id);
self.next_assignment_id += 1;
Some(self.next_assignment_id - 1)
} else {
None
}
}
pub fn remove_layer(&mut self, id: LayerId) -> Result<(), DocumentError> {
let pos = self.layer_ids.iter().position(|x| *x == id).ok_or(DocumentError::LayerNotFound)?;
self.layers.remove(pos);
self.layer_ids.remove(pos);
Ok(())
}
/// Returns a list of layers in the folder
pub fn list_layers(&self) -> &[LayerId] {
self.layer_ids.as_slice()
}
pub fn layer(&self, id: LayerId) -> Option<&Layer> {
let pos = self.layer_ids.iter().position(|x| *x == id)?;
Some(&self.layers[pos])
}
pub fn layer_mut(&mut self, id: LayerId) -> Option<&mut Layer> {
let pos = self.layer_ids.iter().position(|x| *x == id)?;
Some(&mut self.layers[pos])
}
pub fn folder(&self, id: LayerId) -> Option<&Folder> {
match self.layer(id) {
Some(Layer {
data: LayerDataTypes::Folder(folder), ..
}) => Some(&folder),
_ => None,
}
}
pub fn folder_mut(&mut self, id: LayerId) -> Option<&mut Folder> {
match self.layer_mut(id) {
Some(Layer {
data: LayerDataTypes::Folder(folder), ..
}) => Some(folder),
_ => None,
}
}
}
impl Default for Folder {
fn default() -> Self {
Self {
layer_ids: vec![],
layers: vec![],
next_assignment_id: 0,
}
}
}

View File

@@ -0,0 +1,30 @@
use super::style;
use super::LayerData;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Line {
shape: kurbo::Line,
style: style::PathStyle,
}
impl Line {
pub fn new(p0: impl Into<kurbo::Point>, p1: impl Into<kurbo::Point>, style: style::PathStyle) -> Line {
Line {
shape: kurbo::Line::new(p0, p1),
style,
}
}
}
impl LayerData for Line {
fn render(&self) -> String {
format!(
r#"<line x1="{}" y1="{}" x2="{}" y2="{}" {} />"#,
self.shape.p0.x,
self.shape.p0.y,
self.shape.p1.x,
self.shape.p1.y,
self.style.render(),
)
}
}

View File

@@ -0,0 +1,54 @@
pub mod style;
pub mod circle;
pub use circle::Circle;
pub mod line;
pub use line::Line;
pub mod rect;
pub use rect::Rect;
pub mod shape;
pub use shape::Shape;
pub mod folder;
pub use folder::Folder;
pub trait LayerData {
fn render(&self) -> String;
}
#[derive(Debug, Clone, PartialEq)]
pub enum LayerDataTypes {
Folder(Folder),
Circle(Circle),
Rect(Rect),
Line(Line),
Shape(Shape),
}
impl LayerDataTypes {
pub fn render(&self) -> String {
match self {
Self::Folder(f) => f.render(),
Self::Circle(c) => c.render(),
Self::Rect(r) => r.render(),
Self::Line(l) => l.render(),
Self::Shape(s) => s.render(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Layer {
pub visible: bool,
pub name: Option<String>,
pub data: LayerDataTypes,
}
impl Layer {
pub fn new(data: LayerDataTypes) -> Self {
Self { visible: true, name: None, data }
}
}

View File

@@ -0,0 +1,30 @@
use super::style;
use super::LayerData;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
shape: kurbo::Rect,
style: style::PathStyle,
}
impl Rect {
pub fn new(p0: impl Into<kurbo::Point>, p1: impl Into<kurbo::Point>, style: style::PathStyle) -> Rect {
Rect {
shape: kurbo::Rect::from_points(p0, p1),
style,
}
}
}
impl LayerData for Rect {
fn render(&self) -> String {
format!(
r#"<rect x="{}" y="{}" width="{}" height="{}" {} />"#,
self.shape.min_x(),
self.shape.min_y(),
self.shape.width(),
self.shape.height(),
self.style.render(),
)
}
}

View File

@@ -0,0 +1,25 @@
use crate::shape_points;
use super::style;
use super::LayerData;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Shape {
shape: shape_points::ShapePoints,
style: style::PathStyle,
}
impl Shape {
pub fn new(center: impl Into<kurbo::Point>, extent: impl Into<kurbo::Vec2>, sides: u8, style: style::PathStyle) -> Shape {
Shape {
shape: shape_points::ShapePoints::new(center, extent, sides),
style,
}
}
}
impl LayerData for Shape {
fn render(&self) -> String {
format!(r#"<polygon points="{}" {} />"#, self.shape, self.style.render(),)
}
}

View File

@@ -0,0 +1,56 @@
use crate::color::Color;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Fill {
color: Color,
}
impl Fill {
pub fn new(color: Color) -> Self {
Self { color }
}
pub fn render(&self) -> String {
format!("fill: #{};", self.color.as_hex())
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Stroke {
color: Color,
width: f32,
}
impl Stroke {
pub fn new(color: Color, width: f32) -> Self {
Self { color, width }
}
pub fn render(&self) -> String {
format!("stroke: #{};stroke-width:{};", self.color.as_hex(), self.width)
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct PathStyle {
stroke: Option<Stroke>,
fill: Option<Fill>,
}
impl PathStyle {
pub fn new(stroke: Option<Stroke>, fill: Option<Fill>) -> Self {
Self { stroke, fill }
}
pub fn render(&self) -> String {
format!(
"style=\"{}{}\"",
match self.fill {
Some(fill) => fill.render(),
None => String::new(),
},
match self.stroke {
Some(stroke) => stroke.render(),
None => String::new(),
},
)
}
}