From 28c436c296427a059e06253618d8518988cf0899 Mon Sep 17 00:00:00 2001 From: Ewen Le Bihan Date: Thu, 2 May 2024 15:25:55 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Wasm-bin=20Color=20enum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/color.rs | 59 +++++++++++++++++++++++++++++++++----------------- src/web.rs | 14 ++++++++---- web/index.html | 28 ++++++++++++------------ 3 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/color.rs b/src/color.rs index 4d1ef03..9f3c38a 100644 --- a/src/color.rs +++ b/src/color.rs @@ -5,9 +5,10 @@ use std::{ path::PathBuf, }; -use itertools::Itertools; use serde::Deserialize; +use wasm_bindgen::prelude::*; +#[wasm_bindgen] #[derive(Debug, Clone, Copy, PartialEq)] pub enum Color { Black, @@ -50,6 +51,43 @@ impl From<&str> for Color { } } +impl Color { + pub fn to_string(self, mapping: &ColorMapping) -> String { + match self { + Color::Black => mapping.black.to_string(), + Color::White => mapping.white.to_string(), + Color::Red => mapping.red.to_string(), + Color::Green => mapping.green.to_string(), + Color::Blue => mapping.blue.to_string(), + Color::Yellow => mapping.yellow.to_string(), + Color::Orange => mapping.orange.to_string(), + Color::Purple => mapping.purple.to_string(), + Color::Brown => mapping.brown.to_string(), + Color::Cyan => mapping.cyan.to_string(), + Color::Pink => mapping.pink.to_string(), + Color::Gray => mapping.gray.to_string(), + } + } + + pub fn name(&self) -> String { + match self { + Color::Black => "black", + Color::White => "white", + Color::Red => "red", + Color::Green => "green", + Color::Blue => "blue", + Color::Yellow => "yellow", + Color::Orange => "orange", + Color::Purple => "purple", + Color::Brown => "brown", + Color::Cyan => "cyan", + Color::Pink => "pink", + Color::Gray => "gray", + } + .to_string() + } +} + #[derive(Debug, Deserialize, Clone)] pub struct ColorMapping { pub black: String, @@ -203,22 +241,3 @@ impl ColorMapping { } } } - -impl Color { - pub fn to_string(self, mapping: &ColorMapping) -> String { - match self { - Color::Black => mapping.black.to_string(), - Color::White => mapping.white.to_string(), - Color::Red => mapping.red.to_string(), - Color::Green => mapping.green.to_string(), - Color::Blue => mapping.blue.to_string(), - Color::Yellow => mapping.yellow.to_string(), - Color::Orange => mapping.orange.to_string(), - Color::Purple => mapping.purple.to_string(), - Color::Brown => mapping.brown.to_string(), - Color::Cyan => mapping.cyan.to_string(), - Color::Pink => mapping.pink.to_string(), - Color::Gray => mapping.gray.to_string(), - } - } -} diff --git a/src/web.rs b/src/web.rs index 7ce0fbe..271cb9c 100644 --- a/src/web.rs +++ b/src/web.rs @@ -5,12 +5,18 @@ use crate::{Canvas, Color, ColorMapping, Fill}; #[wasm_bindgen(start)] pub fn main() -> Result<(), JsValue> { - render_image(0.0, "black")?; + render_image(0.0, Color::Black)?; Ok(()) } +// Can't bind Color.name directly, see https://github.com/rustwasm/wasm-bindgen/issues/1715 #[wasm_bindgen] -pub fn render_image(opacity: f32, color: &str) -> Result<(), JsValue> { +pub fn color_name(c: Color) -> String { + c.name() +} + +#[wasm_bindgen] +pub fn render_image(opacity: f32, color: Color) -> Result<(), JsValue> { let mut canvas = Canvas::default_settings(); canvas.colormap = ColorMapping { black: "#ffffff".into(), @@ -29,7 +35,7 @@ pub fn render_image(opacity: f32, color: &str) -> Result<(), JsValue> { canvas.set_grid_size(4, 4); - let mut layer = canvas.random_layer(&color); + let mut layer = canvas.random_layer(&color.name()); layer.paint_all_objects(Fill::Translucent(color.into(), opacity)); canvas.add_or_replace_layer(layer); @@ -39,7 +45,7 @@ pub fn render_image(opacity: f32, color: &str) -> Result<(), JsValue> { let output = document.create_element("div")?; output.set_class_name("frame"); - output.set_attribute("data-color", color)?; + output.set_attribute("data-color", &color.name())?; output.set_inner_html(&canvas.render(&vec!["*"], false)); body.append_child(&output)?; Ok(()) diff --git a/web/index.html b/web/index.html index 6b628dd..eea5758 100644 --- a/web/index.html +++ b/web/index.html @@ -7,12 +7,12 @@