dont clobber colors when introducing a new one

This commit is contained in:
Dustin Carlino 2018-06-21 10:42:30 -07:00
parent e7e334a460
commit dde825fa56
2 changed files with 10 additions and 29 deletions

View File

@ -6,7 +6,7 @@ use graphics::types::Color;
use rand;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Error, ErrorKind, Read, Write};
use std::io::{Error, Read, Write};
use strum::IntoEnumIterator;
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, EnumIter, PartialOrd, Ord)]
@ -51,32 +51,23 @@ impl ColorScheme {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let scheme: ColorScheme = serde_json::from_str(&contents).unwrap();
let mut scheme: ColorScheme = serde_json::from_str(&contents).unwrap();
for color in Colors::iter() {
if !scheme.map.contains_key(&color) {
return Err(Error::new(
ErrorKind::Other,
format!("no color for {:?} defined", color),
));
println!(
"No color for {:?} defined, initializing with a random one",
color
);
scheme
.map
.insert(color, [rand::random(), rand::random(), rand::random(), 1.0]);
}
}
Ok(scheme)
}
pub fn random_colors() -> ColorScheme {
let mut scheme = ColorScheme {
map: BTreeMap::new(),
};
for color in Colors::iter() {
scheme
.map
.insert(color, [rand::random(), rand::random(), rand::random(), 1.0]);
}
scheme
}
pub fn get(&self, c: Colors) -> Color {
// TODO make sure this isn't slow; maybe back this with an array
*self.map.get(&c).unwrap()

View File

@ -118,7 +118,7 @@ impl UI {
color_picker: ColorPicker::new(),
canvas: Canvas::new(),
cs: ColorScheme::random_colors(),
cs: ColorScheme::load("color_scheme").unwrap(),
};
match savestate::load("editor_state") {
@ -137,16 +137,6 @@ impl UI {
}
}
match ColorScheme::load("color_scheme") {
Ok(scheme) => {
println!("Loaded previous color_scheme");
ui.cs = scheme;
}
Err(err) => {
println!("Couldn't load color_scheme: {}", err);
}
}
// TODO or make a custom event for zoom change
let old_zoom = -1.0;
let new_zoom = ui.canvas.cam_zoom;