Skip to content

Commit e9ce61a

Browse files
committed
Fixup ints.
1 parent 1ff633d commit e9ce61a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

crates/processing_pyo3/src/color.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ use crate::math::{PyVec4, PyVecIter, hash_f32, PyVec3};
88

99
pub use processing::prelude::color::{ColorMode, ColorSpace};
1010

11-
pub(crate) fn parse_numeric(space: &ColorSpace, obj: &Bound<'_, PyAny>) -> PyResult<f32> {
11+
fn int_maxes(space: &ColorSpace) -> [f32; 4] {
12+
match space {
13+
ColorSpace::Srgb | ColorSpace::Linear => [255.0, 255.0, 255.0, 255.0],
14+
ColorSpace::Hsl | ColorSpace::Hsv | ColorSpace::Hwb => [360.0, 100.0, 100.0, 255.0],
15+
ColorSpace::Oklch => [100.0, 100.0, 360.0, 255.0],
16+
ColorSpace::Oklab => [100.0, 100.0, 100.0, 255.0],
17+
ColorSpace::Lab => [100.0, 100.0, 100.0, 255.0],
18+
ColorSpace::Lch => [100.0, 100.0, 360.0, 255.0],
19+
ColorSpace::Xyz => [100.0, 100.0, 100.0, 255.0],
20+
}
21+
}
22+
23+
/// Parse a Python int or float into an f32 for a given channel.
24+
pub(crate) fn parse_numeric(space: &ColorSpace, obj: &Bound<'_, PyAny>, ch: usize) -> PyResult<f32> {
1225
if let Ok(v) = obj.extract::<i64>() {
13-
return Ok(match space {
14-
ColorSpace::Srgb | ColorSpace::Linear => v as f32 / 255.0,
15-
_ => v as f32,
16-
});
26+
let native = space.default_maxes();
27+
let imax = int_maxes(space);
28+
return Ok(v as f32 / imax[ch] * native[ch]);
1729
}
1830
if let Ok(v) = obj.extract::<f64>() {
1931
return Ok(v as f32);
@@ -22,7 +34,7 @@ pub(crate) fn parse_numeric(space: &ColorSpace, obj: &Bound<'_, PyAny>) -> PyRes
2234
}
2335

2436
fn convert_channel(mode: &ColorMode, obj: &Bound<'_, PyAny>, ch: usize) -> PyResult<f32> {
25-
let v = parse_numeric(&mode.space, obj)?;
37+
let v = parse_numeric(&mode.space, obj, ch)?;
2638
Ok(mode.scale(v, ch))
2739
}
2840

crates/processing_pyo3/src/graphics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,20 +511,20 @@ impl Graphics {
511511
) -> PyResult<()> {
512512
let space = crate::color::ColorSpace::from_u8(mode)
513513
.ok_or_else(|| PyRuntimeError::new_err(format!("unknown color space: {mode}")))?;
514-
let parse = |obj: &Bound<'py, PyAny>| crate::color::parse_numeric(&space, obj);
514+
let parse = |obj: &Bound<'py, PyAny>, ch: usize| crate::color::parse_numeric(&space, obj, ch);
515515
let new_mode = match (max1, max2, max3, max_alpha) {
516516
// color_mode(MODE)
517517
(None, _, _, _) => ColorMode::with_defaults(space),
518518
// color_mode(MODE, max)
519-
(Some(m), None, _, _) => ColorMode::with_uniform_max(space, parse(m)?),
519+
(Some(m), None, _, _) => ColorMode::with_uniform_max(space, parse(m, 0)?),
520520
// color_mode(MODE, max1, max2, max3)
521521
(Some(m1), Some(m2), Some(m3), None) => {
522522
let defaults = space.default_maxes();
523-
ColorMode::new(space, parse(m1)?, parse(m2)?, parse(m3)?, defaults[3])
523+
ColorMode::new(space, parse(m1, 0)?, parse(m2, 1)?, parse(m3, 2)?, defaults[3])
524524
}
525525
// color_mode(MODE, max1, max2, max3, maxA)
526526
(Some(m1), Some(m2), Some(m3), Some(ma)) => {
527-
ColorMode::new(space, parse(m1)?, parse(m2)?, parse(m3)?, parse(ma)?)
527+
ColorMode::new(space, parse(m1, 0)?, parse(m2, 1)?, parse(m3, 2)?, parse(ma, 3)?)
528528
}
529529
_ => return Err(PyRuntimeError::new_err("expected 1, 2, 4, or 5 arguments")),
530530
};

0 commit comments

Comments
 (0)