From fbe40fb8669f47b9f9066dd48bd2e1e589242fa6 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 8 Nov 2020 17:33:15 +0000 Subject: [PATCH] dedupe palette function --- TODO.md | 1 - src/lib.rs | 28 ++++++++++++---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/TODO.md b/TODO.md index 7f6a639..33f4741 100644 --- a/TODO.md +++ b/TODO.md @@ -5,4 +5,3 @@ * noise reduction (remove lonely pixels) * implement Atkinson and Bayer dithering options * stats for added room (number of tiles) -* dedupe "palette from custom colours" functionality diff --git a/src/lib.rs b/src/lib.rs index 4f4aa3a..41f584c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,19 +185,23 @@ fn image_to_base64(image: &DynamicImage) -> String { format!("data:image/png;base64,{}", base64::encode(&bytes)) } +fn palette_from(bg: &bitsy_parser::Colour, fg: &bitsy_parser::Colour) -> bitsy_parser::Palette { + bitsy_parser::Palette { + id: "0".to_string(), + name: None, + colours: vec![ + bg.clone(), fg.clone(), bitsy_parser::Colour { red: 0, green: 0, blue: 0 } + ], + } +} + fn render_preview(state: &State) -> DynamicImage { let mut buffer = state.image.as_ref().unwrap().clone().into_rgba(); let palette = match &state.palette { SelectedPalette::None => bitsy_parser::mock::game_default().palettes[0].clone(), SelectedPalette::Existing { id } => state.game.as_ref().unwrap().get_palette(id).unwrap().clone(), - SelectedPalette::New { background, foreground } => bitsy_parser::Palette { - id: "0".to_string(), - name: None, - colours: vec![ - background.clone(), foreground.clone(), bitsy_parser::Colour { red: 0, green: 0, blue: 0 } - ], - }, + SelectedPalette::New { background, foreground } => palette_from(background, foreground), }; let colour_map = crate::ColourMap::from(&palette); @@ -258,15 +262,7 @@ pub fn add_room() -> String { SelectedPalette::None => bitsy_parser::mock::game_default().palettes[0].id.clone(), SelectedPalette::Existing { id } => id.clone(), SelectedPalette::New { background, foreground } => { - game.add_palette(bitsy_parser::Palette { - id: "0".to_string(), - name: None, - colours: vec![ - background.clone(), - foreground.clone(), - bitsy_parser::Colour { red: 0, green: 0, blue: 0 } - ], - }) + game.add_palette(palette_from(background, foreground)) }, });