Skip to content

Commit

Permalink
[commands] fix colors and graph height
Browse files Browse the repository at this point in the history
  • Loading branch information
Ionizing committed Aug 10, 2024
1 parent 659f79d commit 4850e83
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 49 deletions.
10 changes: 5 additions & 5 deletions src/commands/band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ use crate::{
commands::common::{
write_array_to_txt,
RawSelection,
CustomColor,
}
};

Expand All @@ -75,7 +74,7 @@ struct Selection {
ispins: Vec<usize>,
iatoms: Vec<usize>,
iorbits: Vec<usize>,
color: Option<CustomColor>,
color: Option<String>,
}


Expand Down Expand Up @@ -509,7 +508,7 @@ impl Band {
assert_eq!(kpath.len(), nkpoints); // cropped_eigvals[ispin, ikpoint, iband]

let rand_color = RawSelection::get_random_color();
let color = selection.color.clone().unwrap_or(rand_color);
let color = selection.color.clone().unwrap_or(rand_color.into());
let marker = plotly::common::Marker::new().color(color);

for ispin in 0 .. nspin {
Expand Down Expand Up @@ -794,6 +793,7 @@ impl OptProcess for Band {
.tick_text(klabels)
.zero_line(true)
)
.height(960)
.legend(plotly::layout::Legend::new().item_sizing(ItemSizing::Constant));

Self::plot_boundaries(&mut layout, &kxs);
Expand Down Expand Up @@ -876,7 +876,7 @@ impl OptProcess for Band {


// save data
info!("Writing Bandstructure to {:?}", &self.htmlout);
info!("Writing Bandstructure to {:?}", &htmlout);

for is in 0 .. nspin {
let spin_label = match (is_ncl, nspin, is) {
Expand All @@ -897,7 +897,7 @@ impl OptProcess for Band {
write_array_to_txt(&fname, data_ref, "kpath(in_2pi) band-levels(nkpoints_x_nbands)")?;
}

fs::write(&self.htmlout, plot.to_html())?;
plot.write_html(&htmlout);

if self.to_inline_html {
info!("Printing inline html to stdout ...");
Expand Down
46 changes: 14 additions & 32 deletions src/commands/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::{
io::Write,
fs,
path::Path,
panic::catch_unwind,
fmt::Write as _,
};

use regex::Regex;
use serde::{
Serialize,
Deserialize,
Expand All @@ -16,13 +16,7 @@ use anyhow::{
Result,
Context,
};
use plotly::common::{
ColorScalePalette,
color::{
Color,
ColorWrapper,
},
};
use plotly::common::ColorScalePalette;
use ndarray::Array1;

use crate::types::{
Expand Down Expand Up @@ -65,16 +59,6 @@ const NAMED_COLORS: &[&str] = &[
];


#[derive(Debug, Clone)]
pub struct CustomColor (ColorWrapper);

impl Color for CustomColor {
fn to_color(&self) -> ColorWrapper {
self.0.clone()
}
}


const PALETTES: &[&str] = &[
"blackbody", "bluered", "blues", "cividis", "earth",
"electric", "greens", "greys", "hot", "jet",
Expand Down Expand Up @@ -275,20 +259,18 @@ bail!("[DOS]: Invalid spin component selected: `{}`, available components are `u
}

// Parse the color to this curve.
pub fn parse_color(input: &str) -> Result<CustomColor> {
if NAMED_COLORS.contains(&input.to_ascii_lowercase().as_ref()) {
Ok(CustomColor(ColorWrapper::S(input.to_owned())))
} else {
let ret = catch_unwind(|| {
input.to_color()
});
pub fn parse_color(input: &str) -> Result<String> {
let re_rgb = Regex::new("^#(?:[0-9a-fA-F]{3}){1,2}$").unwrap().is_match(input);
let re_argb = Regex::new("^#(?:[0-9a-fA-F]{3,4}){1,2}$").unwrap().is_match(input);

if let Ok(c) = ret {
Ok(CustomColor(c))
} else {
bail!("The input color is neither a named color nor a valid hex code.
let input_lowercase = input.to_lowercase();

if NAMED_COLORS.contains(&input_lowercase.as_ref()) ||
re_rgb || re_argb {
Ok(input_lowercase)
} else {
bail!("The input color is neither a named color nor a valid hex code.
See \"https://developer.mozilla.org/en-US/docs/Web/CSS/color_value for availed named colors.\"");
}
}
}

Expand All @@ -301,12 +283,12 @@ See \"https://developer.mozilla.org/en-US/docs/Web/CSS/color_value for availed n
}
}

pub fn get_random_color() -> CustomColor {
pub fn get_random_color() -> &'static str {
use rand::Rng;

let mut rng = rand::thread_rng();
let id = rng.gen_range(0 .. NAMED_COLORS.len());
Self::parse_color(NAMED_COLORS[id]).unwrap()
NAMED_COLORS[id]
}

}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/dos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use crate::{
commands::common::{
RawSelection,
write_array_to_txt,
CustomColor,
}
};

Expand All @@ -47,7 +46,7 @@ struct Selection {
ikpoints: Vec<usize>,
iatoms: Vec<usize>,
iorbits: Vec<usize>,
color: Option<CustomColor>,
color: Option<String>,
factor: f64,
}

Expand Down Expand Up @@ -545,11 +544,12 @@ impl OptProcess for Dos {
.zero_line(true)
.range_slider(plotly::layout::RangeSlider::new().visible(true))
.range(xlim)
);
)
.height(960);
plot.set_layout(layout);

info!("Writing DOS plot to {:?}", htmlout);
fs::write(&htmlout, plot.to_html())?;
plot.write_html(&htmlout);

info!("Writing raw DOS data to {:?}", txtout);
let label = labels.join(" ");
Expand Down
5 changes: 3 additions & 2 deletions src/commands/tdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ I suggest you provide `gamma_half` argument to avoid confusion.");
.x_axis(plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("Energy (eV)"))
.fixed_range(false))
.hover_mode(plotly::layout::HoverMode::X);
.hover_mode(plotly::layout::HoverMode::X)
.height(960);

plot.set_layout(layout);

// Write html
info!("Writing plot to {:?}", &self.htmlout);
fs::write(&self.htmlout, plot.to_html())?;
plot.write_html(&self.htmlout);

if self.to_inline_html {
info!("Printing inline HTML to stdout ...");
Expand Down
6 changes: 3 additions & 3 deletions src/commands/wav1d.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::fs;

use clap::Args;
use log::{
Expand Down Expand Up @@ -228,12 +227,13 @@ I suggest you provide `gamma_half` argument to avoid confusion.");
.title(plotly::common::Title::with_text("E-E<sub>f</sub> (eV)"))
.zero_line(true))
.x_axis(plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("Distance (Å)")));
.title(plotly::common::Title::with_text("Distance (Å)")))
.height(960);
plot.set_layout(layout);

plot.use_local_plotly();
info!("Writing to {:?}", self.htmlout);
fs::write(&self.htmlout, plot.to_html())?;
plot.write_html(&self.htmlout);

let comment = dat.iter()
.map(|(_, l, _)| l.clone())
Expand Down
6 changes: 3 additions & 3 deletions src/commands/workfunc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::path::PathBuf;
use clap::Args;
use rayon;
Expand Down Expand Up @@ -127,11 +126,12 @@ impl OptProcess for Workfunc {
.zero_line(true))
.x_axis(plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("Distance (A)"))
.zero_line(true));
.zero_line(true))
.height(960);
plot.set_layout(layout);

info!("Writing to {:?}", self.htmlout);
fs::write(&self.htmlout, plot.to_html())?;
plot.write_html(&self.htmlout);

if self.show {
plot.show();
Expand Down

0 comments on commit 4850e83

Please sign in to comment.