Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/des/cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ pub enum CatColorMap {
Auto,
/// A categorical color map that uses a predefined set of colors indexed by string categories
Strings(HashMap<String, style::series::Color>),
/// A categorical color map that uses a predefined set of colors indexed by integer categories
Integers(HashMap<i64, style::series::Color>),
}

/// A colormap that interpret string values as literal colors using `Rgb8::parse` and integer values as RGBA 32 bit colors.
Expand Down
14 changes: 0 additions & 14 deletions src/drawing/cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ impl ColorMapBuild for des::cmap::ColorMap {
.iter()
.map(|(cat_val, color)| (cat_val.clone(), *color))
.collect(),
des::cmap::CatColorMap::Integers(..) => {
todo!("Integer category color map is not implemented yet")
}
};
Some(Arc::new(CatColorMapImpl {
hash: self.hash(bounds),
Expand Down Expand Up @@ -133,9 +130,6 @@ impl ColorMapBuild for des::cmap::ColorMap {
.iter()
.map(|(cat_val, color)| (cat_val.clone(), *color))
.collect(),
des::cmap::CatColorMap::Integers(..) => {
todo!("Integer category color map is not implemented yet")
}
};
Ok(Arc::new(CatColorMapImpl {
hash: self.hash(bounds),
Expand Down Expand Up @@ -417,14 +411,6 @@ fn hash_cat_cmap(cat: &des::cmap::CatColorMap) -> u64 {
}
hasher.finish()
}
des::cmap::CatColorMap::Integers(map) => {
let mut hasher = DefaultHasher::new();
for (cat_val, color) in map.iter() {
cat_val.hash(&mut hasher);
color.hash(&mut hasher);
}
hasher.finish()
}
}
}

Expand Down
82 changes: 28 additions & 54 deletions src/sd/cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use plotive_base::Rgb8;
use serde::Deserializer;
use serde::de::{Error, IntoDeserializer, SeqAccess};
use serde::de::{Error, SeqAccess};
use serde::ser::{SerializeMap, SerializeSeq};

use crate::des::cmap::{self, CatColorMap, ColorMap, LerpColorMap, LerpMethod};
Expand Down Expand Up @@ -63,11 +63,15 @@ where
where
E: serde::de::Error,
{
if let Ok(color) = value.parse::<style::series::Color>() {
if let Ok(t) = T::deserialize(serde::de::value::StrDeserializer::<E>::new(value)) {
Ok(ColorNoneT::T(t))
} else if let Ok(color) = value.parse::<style::series::Color>() {
Ok(ColorNoneT::Color(color))
} else {
let t = T::deserialize(value.into_deserializer())?;
Ok(ColorNoneT::T(t))
Err(E::custom(format!(
"expected a color or one of the LerpColorMap fields, got: {}",
value
)))
}
}

Expand All @@ -94,14 +98,6 @@ where
}
}

/// Helper type deserialize either a string or an integer as a key for a categorical color map.
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
enum CatKey {
String(String),
Integer(i64),
}

impl<'de> serde::de::Deserialize<'de> for ColorMap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -158,19 +154,9 @@ impl<'de> serde::de::Deserialize<'de> for ColorMap {

let mut is_cats = false;
let mut cats: HashMap<String, style::series::Color> = HashMap::new();
let mut icats: HashMap<i64, style::series::Color> = HashMap::new();

while let Some(key) = map.next_key::<CatKey>()? {
let str_key = match key {
CatKey::String(s) => s,
CatKey::Integer(i) => {
let val = map.next_value()?;
icats.insert(i, val);
is_cats = true;
continue;
}
};
match str_key.as_str() {

while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"method" if !is_cats => {
let val: ColorNoneT<LerpMethod> = map.next_value()?;
match val {
Expand Down Expand Up @@ -241,35 +227,30 @@ impl<'de> serde::de::Deserialize<'de> for ColorMap {
}
_ => {
let val: style::series::Color = map.next_value()?;
cats.insert(str_key, val);
cats.insert(key, val);
is_cats = true;
}
}
}

if is_cats {
if is_lerp {
return Err(A::Error::custom(
"Can't mix categorical and lerp color map fields",
));
}
if !icats.is_empty() && !cats.is_empty() {
return Err(A::Error::custom(
"Can't mix integer and string keys in categorical color map",
));
}
if !icats.is_empty() {
return Ok(CatColorMap::Integers(icats).into());
} else {
return Ok(CatColorMap::Strings(cats).into());
}
if is_cats && is_lerp {
println!("method: {:?}", method);
println!("cmap: {:?}", cmap);
println!("scale: {:?}", scale);
println!("stops: {:?}", stops);
println!("cats: {:?}", cats);
return Err(A::Error::custom(
"Can't mix categorical and lerp color map fields",
));
}
if is_lerp {
return Ok(
lerp_color_map_from_fields::<A::Error>(method, cmap, stops, scale)?.into(),
);

if is_cats {
Ok(CatColorMap::Strings(cats).into())
} else if is_lerp {
Ok(lerp_color_map_from_fields::<A::Error>(method, cmap, stops, scale)?.into())
} else {
Err(A::Error::custom("Missing fields for ColorMap"))
}
Err(A::Error::custom("Missing fields for ColorMap"))
}
}

Expand Down Expand Up @@ -613,13 +594,6 @@ impl serde::Serialize for CatColorMap {
}
state.end()
}
CatColorMap::Integers(cmap) => {
let mut state = serializer.serialize_map(Some(cmap.len() + 1))?;
for (key, value) in cmap {
state.serialize_entry(&key.to_string(), value)?;
}
state.end()
}
}
}
}
Expand Down
Loading
Loading