Skip to content

Commit

Permalink
Rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Beinsezii committed Dec 30, 2023
1 parent 04c7f3e commit 271c11d
Showing 1 changed file with 73 additions and 25 deletions.
98 changes: 73 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ pub fn convert_space_alpha(from: Space, to: Space, pixel: &mut [f32; 4]) {
fn rm_paren<'a>(s: &'a str) -> &'a str {
if let (Some(f), Some(l)) = (s.chars().next(), s.chars().last()) {
if ['(', '[', '{'].contains(&f) && [')', ']', '}'].contains(&l) {
return &s[1..(s.len()-1)]
return &s[1..(s.len() - 1)];
}
}
s
Expand All @@ -556,37 +556,42 @@ fn rm_paren<'a>(s: &'a str) -> &'a str {
/// Ex: "0.2, 0.5, 0.6", "lch: 50 20 120" "oklab(0.2 0.6 90)"
///
/// Does not support alpha channel.
pub fn str2col(mut s: &str) -> Option<(Space, [f32; 3])>{
pub fn str2col(mut s: &str) -> Option<(Space, [f32; 3])> {
s = rm_paren(s.trim());
let mut space = Space::SRGB;

// Return hex if valid
if let Ok(irgb) = hex_to_irgb(s) {
return Some((space, irgb_to_srgb(irgb)))
return Some((space, irgb_to_srgb(irgb)));
}

let seps = [',', ':', ';'];

// Find Space at front then trim
if let Some(i) = s.find(|c: char| c.is_whitespace() || seps.contains(&c) || ['(', '[', '{'].contains(&c) ) {
if let Some(i) =
s.find(|c: char| c.is_whitespace() || seps.contains(&c) || ['(', '[', '{'].contains(&c))
{
if let Ok(sp) = Space::try_from(&s[..i]) {
space = sp;
s = rm_paren(s[i..].trim_start_matches(|c: char| c.is_whitespace() || seps.contains(&c)));
s = rm_paren(
s[i..].trim_start_matches(|c: char| c.is_whitespace() || seps.contains(&c)),
);
}
}

// Split by separators + whitespace and parse
let splits = s
.split(|c: char| c.is_whitespace() || seps.contains(&c))
.filter_map(|s| match s.is_empty(){
.filter_map(|s| match s.is_empty() {
true => None,
false => Some(s.parse::<f32>().ok()),
}).collect::<Vec<Option<f32>>>();
})
.collect::<Vec<Option<f32>>>();

// Return floats if all 3 valid
if splits.len() == 3 {
if let (Some(a), Some(b), Some(c)) = (splits[0], splits[1], splits[2]) {
return Some((space, [a, b, c]))
return Some((space, [a, b, c]));
}
}

Expand Down Expand Up @@ -721,8 +726,8 @@ pub extern "C" fn xyz_to_jzazbz(pixel: &mut [f32; 3]) {

lms.iter_mut().for_each(|e| {
let v = *e / 10000.0;
let v = (PQEOTF_C1 + PQEOTF_C2 * spowf(v, PQEOTF_M1))
/ (1.0 + PQEOTF_C3 * spowf(v, PQEOTF_M1));
let v =
(PQEOTF_C1 + PQEOTF_C2 * spowf(v, PQEOTF_M1)) / (1.0 + PQEOTF_C3 * spowf(v, PQEOTF_M1));
*e = spowf(v, JZAZBZ_P);
});

Expand Down Expand Up @@ -950,7 +955,6 @@ pub extern "C" fn jzazbz_to_xyz(pixel: &mut [f32; 3]) {
*c = 10000.0 * spowf(v, 1.0 / PQEOTF_M1);
});


*pixel = matmul3t(lms, JZAZBZ_M1_INV);

pixel[0] = (pixel[0] + (JZAZBZ_B - 1.0) * pixel[2]) / JZAZBZ_B;
Expand Down Expand Up @@ -1173,7 +1177,14 @@ mod tests {
reference_space: Space,
reference: &[[f32; 3]],
) {
conv_cmp_full(input_space, input, reference_space, reference, 1e-2, &[0, 7])
conv_cmp_full(
input_space,
input,
reference_space,
reference,
1e-2,
&[0, 7],
)
}
// ### Comparison FNs ### }}}

Expand Down Expand Up @@ -1385,7 +1396,10 @@ mod tests {
// ### Str2Col ### {{{
#[test]
fn str2col_base() {
assert_eq!(str2col("0.2, 0.5, 0.6"), Some((Space::SRGB, [0.2, 0.5, 0.6])))
assert_eq!(
str2col("0.2, 0.5, 0.6"),
Some((Space::SRGB, [0.2, 0.5, 0.6]))
)
}

#[test]
Expand All @@ -1395,7 +1409,10 @@ mod tests {

#[test]
fn str2col_base_lop() {
assert_eq!(str2col("0.2,0.5, 0.6"), Some((Space::SRGB, [0.2, 0.5, 0.6])))
assert_eq!(
str2col("0.2,0.5, 0.6"),
Some((Space::SRGB, [0.2, 0.5, 0.6]))
)
}

#[test]
Expand All @@ -1405,17 +1422,26 @@ mod tests {

#[test]
fn str2col_base_bare_fat() {
assert_eq!(str2col(" 0.2 0.5 0.6 "), Some((Space::SRGB, [0.2, 0.5, 0.6])))
assert_eq!(
str2col(" 0.2 0.5 0.6 "),
Some((Space::SRGB, [0.2, 0.5, 0.6]))
)
}

#[test]
fn str2col_base_paren() {
assert_eq!(str2col("(0.2 0.5 0.6)"), Some((Space::SRGB, [0.2, 0.5, 0.6])))
assert_eq!(
str2col("(0.2 0.5 0.6)"),
Some((Space::SRGB, [0.2, 0.5, 0.6]))
)
}

#[test]
fn str2col_base_paren2() {
assert_eq!(str2col("{ 0.2 : 0.5 : 0.6 }"), Some((Space::SRGB, [0.2, 0.5, 0.6])))
assert_eq!(
str2col("{ 0.2 : 0.5 : 0.6 }"),
Some((Space::SRGB, [0.2, 0.5, 0.6]))
)
}

#[test]
Expand All @@ -1440,37 +1466,58 @@ mod tests {

#[test]
fn str2col_lch() {
assert_eq!(str2col("lch(50, 30, 160)"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch(50, 30, 160)"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
fn str2col_lch_space() {
assert_eq!(str2col("lch 50, 30, 160"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch 50, 30, 160"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
fn str2col_lch_colon() {
assert_eq!(str2col("lch:50:30:160"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch:50:30:160"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
fn str2col_lch_semicolon() {
assert_eq!(str2col("lch;50;30;160"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch;50;30;160"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
fn str2col_lch_mixed() {
assert_eq!(str2col("lch; (50,30,160)"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch; (50,30,160)"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
fn str2col_lch_mixed2() {
assert_eq!(str2col("lch(50; 30; 160)"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch(50; 30; 160)"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
fn str2col_lch_mixed3() {
assert_eq!(str2col("lch (50 30 160)"), Some((Space::LCH, [50.0, 30.0, 160.0])))
assert_eq!(
str2col("lch (50 30 160)"),
Some((Space::LCH, [50.0, 30.0, 160.0]))
)
}

#[test]
Expand All @@ -1480,7 +1527,8 @@ mod tests {

#[test]
fn str2space_base() {
let pix = str2space("oklch : 0.62792590, 0.25768453, 29.22319405", Space::SRGB).expect("STR2SPACE_BASE FAIL");
let pix = str2space("oklch : 0.62792590, 0.25768453, 29.22319405", Space::SRGB)
.expect("STR2SPACE_BASE FAIL");
let reference = [1.00000000, 0.00000000, 0.00000000];
pix_cmp(&[pix], &[reference], 1e-3, &[]);
}
Expand Down

0 comments on commit 271c11d

Please sign in to comment.