Mix named colors with xcolor bang specs
This commit is contained in:
1 parent
69dc0a4863
commit
8f429b892b
4 files changed
+41
-6
No files matched your search
+37
-2
@@ -1863,7 +1863,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
.map(str::trim_start)
|
||||
.unwrap_or("");
|
||||
if let Some((name, _)) = braced_group(rest) {
|
||||
geometry.page_color = named_color(name.trim(), &geometry.colors);
|
||||
geometry.page_color = color_spec(name.trim(), &geometry.colors);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -5681,6 +5681,33 @@ fn define_color(line: &str) -> Option<(String, [u8; 3])> {
|
||||
Some((name.trim().to_string(), rgb))
|
||||
}
|
||||
|
||||
fn color_spec(spec: &str, custom: &[(String, [u8; 3])]) -> Option<[u8; 3]> {
|
||||
let parts: Vec<&str> = spec.split('!').collect();
|
||||
if parts.len() == 1 {
|
||||
return named_color(spec.trim(), custom);
|
||||
}
|
||||
if parts.len() > 3 {
|
||||
return None;
|
||||
}
|
||||
let first = named_color(parts[0].trim(), custom)?;
|
||||
let percent: u8 = parts[1].trim().parse().ok()?;
|
||||
let second = if parts.len() == 3 {
|
||||
named_color(parts[2].trim(), custom)?
|
||||
} else {
|
||||
[255, 255, 255]
|
||||
};
|
||||
let first_weight = percent.min(100) as u32;
|
||||
let second_weight = 100 - first_weight;
|
||||
let mix = |left: u8, right: u8| {
|
||||
((left as u32 * first_weight + right as u32 * second_weight + 50) / 100) as u8
|
||||
};
|
||||
Some([
|
||||
mix(first[0], second[0]),
|
||||
mix(first[1], second[1]),
|
||||
mix(first[2], second[2]),
|
||||
])
|
||||
}
|
||||
|
||||
fn named_color(name: &str, custom: &[(String, [u8; 3])]) -> Option<[u8; 3]> {
|
||||
if let Some((_, rgb)) = custom.iter().rev().find(|(known, _)| known == name) {
|
||||
return Some(*rgb);
|
||||
@@ -5754,7 +5781,7 @@ fn split_textcolor(text: &str, custom: &[(String, [u8; 3])]) -> Vec<(Option<[u8;
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
match named_color(name, custom) {
|
||||
match color_spec(name, custom) {
|
||||
Some(color) => {
|
||||
if !plain.is_empty() {
|
||||
out.push((None, std::mem::take(&mut plain)));
|
||||
@@ -9363,6 +9390,14 @@ a & b
|
||||
assert_eq!(body.trim(), "Gap 4.5pt.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn textcolor_mixes_named_colors() {
|
||||
let json = preview_json("\\textcolor{red!50!blue}{Purple} \\textcolor{red!25}{Tint}")
|
||||
.expect("preview");
|
||||
assert!(json.contains(r##"fill=\"#800080\""##), "{json}");
|
||||
assert!(json.contains(r##"fill=\"#ffbfbf\""##), "{json}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn definecolor_models_produce_rgb_values() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user