Skip to content

Commit 89e65a3

Browse files
committed
Fixup ints.
1 parent 780e358 commit 89e65a3

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
@@ -510,20 +510,20 @@ impl Graphics {
510510
) -> PyResult<()> {
511511
let space = crate::color::ColorSpace::from_u8(mode)
512512
.ok_or_else(|| PyRuntimeError::new_err(format!("unknown color space: {mode}")))?;
513-
let parse = |obj: &Bound<'py, PyAny>| crate::color::parse_numeric(&space, obj);
513+
let parse = |obj: &Bound<'py, PyAny>, ch: usize| crate::color::parse_numeric(&space, obj, ch);
514514
let new_mode = match (max1, max2, max3, max_alpha) {
515515
// color_mode(MODE)
516516
(None, _, _, _) => ColorMode::with_defaults(space),
517517
// color_mode(MODE, max)
518-
(Some(m), None, _, _) => ColorMode::with_uniform_max(space, parse(m)?),
518+
(Some(m), None, _, _) => ColorMode::with_uniform_max(space, parse(m, 0)?),
519519
// color_mode(MODE, max1, max2, max3)
520520
(Some(m1), Some(m2), Some(m3), None) => {
521521
let defaults = space.default_maxes();
522-
ColorMode::new(space, parse(m1)?, parse(m2)?, parse(m3)?, defaults[3])
522+
ColorMode::new(space, parse(m1, 0)?, parse(m2, 1)?, parse(m3, 2)?, defaults[3])
523523
}
524524
// color_mode(MODE, max1, max2, max3, maxA)
525525
(Some(m1), Some(m2), Some(m3), Some(ma)) => {
526-
ColorMode::new(space, parse(m1)?, parse(m2)?, parse(m3)?, parse(ma)?)
526+
ColorMode::new(space, parse(m1, 0)?, parse(m2, 1)?, parse(m3, 2)?, parse(ma, 3)?)
527527
}
528528
_ => return Err(PyRuntimeError::new_err("expected 1, 2, 4, or 5 arguments")),
529529
};

0 commit comments

Comments
 (0)