Skip to content

Commit 2f9f7d2

Browse files
Add constant gradients
1 parent 196acff commit 2f9f7d2

File tree

4 files changed

+1109
-6
lines changed

4 files changed

+1109
-6
lines changed

palette/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ categories = ["graphics", "multimedia::images", "no-std"]
1515
build = "build/main.rs"
1616

1717
[features]
18-
default = ["named_from_str", "std"]
18+
default = ["named_from_str", "named_gradients", "std"]
1919
named_from_str = ["named", "phf", "phf_codegen", "std"]
2020
named = []
21+
named_gradients = ["std"]
2122
random = ["rand"]
2223
serializing = ["serde", "std"]
2324

palette/build/named.rs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
use std::fs::File;
22

3-
#[cfg(feature = "named")]
43
pub fn build() {
5-
use std::io::{BufRead, BufReader, Write};
64
use std::path::Path;
75

86
let out_dir = ::std::env::var("OUT_DIR").unwrap();
97
let dest_path = Path::new(&out_dir).join("named.rs");
8+
let mut writer = File::create(dest_path).expect("couldn't create named.rs");
9+
build_colors(&mut writer);
10+
let dest_path = Path::new(&out_dir).join("named_gradients.rs");
11+
let mut writer = File::create(dest_path).expect("couldn't create named_gradients.rs");
12+
build_gradients(&mut writer);
13+
}
14+
15+
#[cfg(feature = "named")]
16+
pub fn build_colors(writer: &mut File) {
17+
use std::io::{BufRead, BufReader, Write};
1018

1119
let reader =
1220
BufReader::new(File::open("build/svg_colors.txt").expect("could not open svg_colors.txt"));
13-
let mut writer = File::create(dest_path).expect("couldn't create named.rs");
1421
let mut entries = vec![];
1522

1623
for line in reader.lines() {
@@ -40,7 +47,56 @@ pub fn build() {
4047
entries.push((name.to_owned(), name.to_uppercase()));
4148
}
4249

43-
gen_from_str(&mut writer, &entries)
50+
gen_from_str(writer, &entries)
51+
}
52+
53+
#[cfg(feature = "named_gradients")]
54+
pub fn build_gradients(writer: &mut File) {
55+
use std::io::{BufRead, BufReader, Write};
56+
57+
let reader =
58+
BufReader::new(File::open("build/svg_gradients_mpl.txt").expect("could not open svg_gradients_mpl.txt"));
59+
60+
let mut line_iter = reader.lines();
61+
while let Some(Ok(line)) = line_iter.next(){
62+
//empty lines are allowed
63+
if line.is_empty() {continue;}
64+
let mut parts = line.split_whitespace();
65+
//every line should have the same info: name type number_of_colors [\n red green blue]^number_of_colors
66+
let name = parts.next().expect("couldn't get the color name");
67+
let color_type = parts.next().expect("couldn't get the type of the colors");
68+
//we assume that color_type is a rgb type
69+
let color_type = format!("crate::rgb::{}", color_type);
70+
let number_of_colors : usize = parts.next().expect("couldn't get the number of colors")
71+
.parse().unwrap_or_else(|_| panic!("couldn't parse the number of colors for color {}", name));
72+
writeln!(writer, "/// New matplotlib colormap by Nathaniel J. Smith, Stefan van der Walt, and (in the case of viridis) Eric Firing.").unwrap();
73+
writeln!(writer, "/// This gradient is perfectly perceptually-uniform, both in regular form and also when converted to black-and-white.").unwrap();
74+
//writeln!(writer, "/// The colormap is released under the CC0 license public domain dedication.").unwrap();
75+
write!(writer,
76+
"pub const {0}: crate::gradient::Gradient<{1}, [(f32,{1});{2}]> = crate::gradient::Gradient([",
77+
name.to_uppercase(), color_type, number_of_colors).unwrap();
78+
for i in 0..number_of_colors {
79+
let color = line_iter
80+
.next()
81+
.unwrap_or_else(|| panic!("less lines than stated colors in gradient {}", name))
82+
.unwrap_or_else(|_| panic!("couldn't read the {}th line of color {}", i, name));
83+
let mut rgb = color.split(",");
84+
let red: f32 = rgb
85+
.next()
86+
.and_then(|r| r.trim().parse().ok())
87+
.unwrap_or_else(|| panic!("couldn't get the {}th red-value for {}", i, name));
88+
let green: f32 = rgb
89+
.next()
90+
.and_then(|r| r.trim().parse().ok())
91+
.unwrap_or_else(|| panic!("couldn't get the {}th green-value for {}", i, name));
92+
let blue: f32 = rgb
93+
.next()
94+
.and_then(|r| r.trim().parse().ok())
95+
.unwrap_or_else(|| panic!("couldn't get the {}th blue-value for {}", i, name));
96+
write!(writer, "({:.10},{}{{red: {}, green: {}, blue: {}, standard: ::core::marker::PhantomData}}),", (i as f32/number_of_colors as f32), color_type, red, green, blue).unwrap();
97+
}
98+
write!(writer, "]);\n").unwrap();
99+
}
44100
}
45101

46102
#[cfg(feature = "named_from_str")]
@@ -60,8 +116,12 @@ fn gen_from_str(writer: &mut File, entries: &[(String, String)]) {
60116
}
61117

62118
#[cfg(not(feature = "named"))]
63-
pub fn build() {}
119+
pub fn build_colors(_writer: &mut File) {}
64120

65121
#[allow(unused)]
66122
#[cfg(not(feature = "named_from_str"))]
67123
fn gen_from_str(_writer: &mut File, _entries: &[(String, String)]) {}
124+
125+
#[allow(unused)]
126+
#[cfg(not(feature = "named_gradients"))]
127+
pub fn build_gradients(_writer: &mut File) {}

0 commit comments

Comments
 (0)