Skip to content

Commit 670a404

Browse files
committed
Cleanup.
1 parent 1960c1f commit 670a404

File tree

2 files changed

+40
-90
lines changed

2 files changed

+40
-90
lines changed

crates/processing_pyo3/src/color.rs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use std::hash::{Hash, Hasher};
2-
31
use bevy::color::{
4-
Alpha, Color, ColorToComponents, Gray, Hsla, Hsva, Hue, Hwba, Laba, Lcha, LinearRgba,
5-
Luminance, Mix, Oklaba, Oklcha, Saturation, Srgba, Xyza, color_difference::EuclideanDistance,
2+
Alpha, Color, Gray, Hsla, Hsva, Hue, Hwba, Laba, Lcha, LinearRgba, Luminance, Mix, Oklaba,
3+
Oklcha, Saturation, Srgba, Xyza, color_difference::EuclideanDistance,
64
};
75
use pyo3::{exceptions::PyTypeError, prelude::*, types::PyTuple};
86

@@ -253,9 +251,7 @@ impl PyColor {
253251
}
254252

255253
fn mix(&self, other: &Self, t: f32) -> Self {
256-
let a = to_srgba(&self.0);
257-
let b = to_srgba(&other.0);
258-
Self(Color::Srgba(a.mix(&b, t)))
254+
Self(self.0.mix(&other.0, t))
259255
}
260256

261257
fn lerp(&self, other: &Self, t: f32) -> Self {
@@ -291,14 +287,11 @@ impl PyColor {
291287
}
292288

293289
fn saturation(&self) -> f32 {
294-
let h: Hsla = self.0.into();
295-
h.saturation
290+
self.0.saturation()
296291
}
297292

298293
fn with_saturation(&self, saturation: f32) -> Self {
299-
let mut h: Hsla = self.0.into();
300-
h.saturation = saturation;
301-
Self(Color::Hsla(h))
294+
Self(self.0.with_saturation(saturation))
302295
}
303296

304297
fn is_fully_transparent(&self) -> bool {
@@ -412,58 +405,15 @@ impl ColorLike {
412405
}
413406

414407
pub(crate) fn extract_color(args: &Bound<'_, PyTuple>) -> PyResult<Color> {
415-
match args.len() {
416-
0 => Err(PyTypeError::new_err("color requires at least 1 argument")),
417-
1 => {
418-
let first = args.get_item(0)?;
419-
if let Ok(c) = first.extract::<PyRef<PyColor>>() {
420-
return Ok(c.0);
421-
}
422-
if let Ok(s) = first.extract::<String>() {
423-
return parse_hex(&s);
424-
}
425-
if let Ok(v) = first.extract::<PyRef<PyVec4>>() {
426-
return Ok(Color::srgba(v.0.x, v.0.y, v.0.z, v.0.w));
427-
}
428-
let v = extract_component(&first)?;
429-
Ok(Color::srgba(v, v, v, 1.0))
430-
}
431-
2 => {
432-
let v = extract_component(&args.get_item(0)?)?;
433-
let a = extract_component(&args.get_item(1)?)?;
434-
Ok(Color::srgba(v, v, v, a))
435-
}
436-
3 => {
437-
let r = extract_component(&args.get_item(0)?)?;
438-
let g = extract_component(&args.get_item(1)?)?;
439-
let b = extract_component(&args.get_item(2)?)?;
440-
Ok(Color::srgba(r, g, b, 1.0))
441-
}
442-
4 => {
443-
let r = extract_component(&args.get_item(0)?)?;
444-
let g = extract_component(&args.get_item(1)?)?;
445-
let b = extract_component(&args.get_item(2)?)?;
446-
let a = extract_component(&args.get_item(3)?)?;
447-
Ok(Color::srgba(r, g, b, a))
448-
}
449-
_ => Err(PyTypeError::new_err("color takes 1-4 arguments")),
450-
}
408+
PyColor::py_new(args).map(|c| c.0)
451409
}
452410

453-
pub fn parse_hex(s: &str) -> PyResult<Color> {
411+
fn parse_hex(s: &str) -> PyResult<Color> {
454412
Srgba::hex(s)
455413
.map(|srgba| Color::Srgba(srgba))
456414
.map_err(|e| PyTypeError::new_err(format!("invalid hex color: {e}")))
457415
}
458416

459-
pub fn make_color(args: &Bound<'_, PyTuple>) -> PyResult<PyColor> {
460-
PyColor::py_new(args)
461-
}
462-
463-
pub fn make_hex(s: &str) -> PyResult<PyColor> {
464-
parse_hex(s).map(PyColor)
465-
}
466-
467417
#[cfg(test)]
468418
mod tests {
469419
use super::*;

crates/processing_pyo3/src/lib.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,26 @@ mod mewnala {
158158

159159
#[pyfunction]
160160
#[pyo3(signature = (*args))]
161-
fn vec2(args: &Bound<'_, PyTuple>) -> PyResult<crate::math::PyVec2> {
162-
crate::math::PyVec2::py_new(args)
161+
fn vec2(args: &Bound<'_, PyTuple>) -> PyResult<PyVec2> {
162+
PyVec2::py_new(args)
163163
}
164164

165165
#[pyfunction]
166166
#[pyo3(signature = (*args))]
167-
fn vec3(args: &Bound<'_, PyTuple>) -> PyResult<crate::math::PyVec3> {
168-
crate::math::PyVec3::py_new(args)
167+
fn vec3(args: &Bound<'_, PyTuple>) -> PyResult<PyVec3> {
168+
PyVec3::py_new(args)
169169
}
170170

171171
#[pyfunction]
172172
#[pyo3(signature = (*args))]
173-
fn vec4(args: &Bound<'_, PyTuple>) -> PyResult<crate::math::PyVec4> {
174-
crate::math::PyVec4::py_new(args)
173+
fn vec4(args: &Bound<'_, PyTuple>) -> PyResult<PyVec4> {
174+
PyVec4::py_new(args)
175175
}
176176

177177
#[pyfunction]
178178
#[pyo3(signature = (*args))]
179-
fn quat(args: &Bound<'_, PyTuple>) -> PyResult<crate::math::PyQuat> {
180-
crate::math::PyQuat::py_new(args)
179+
fn quat(args: &Bound<'_, PyTuple>) -> PyResult<PyQuat> {
180+
PyQuat::py_new(args)
181181
}
182182
}
183183

@@ -186,77 +186,77 @@ mod mewnala {
186186
use super::*;
187187

188188
#[pymodule_export]
189-
use super::super::color::PyColor;
189+
use crate::color::PyColor;
190190

191191
#[pyfunction]
192192
#[pyo3(signature = (*args))]
193-
fn color(args: &Bound<'_, PyTuple>) -> PyResult<super::super::color::PyColor> {
194-
super::super::color::PyColor::py_new(args)
193+
fn color(args: &Bound<'_, PyTuple>) -> PyResult<PyColor> {
194+
PyColor::py_new(args)
195195
}
196196

197197
#[pyfunction]
198-
fn hex(s: &str) -> PyResult<super::super::color::PyColor> {
199-
super::super::color::make_hex(s)
198+
fn hex(s: &str) -> PyResult<PyColor> {
199+
PyColor::from_hex(s)
200200
}
201201

202202
#[pyfunction]
203203
#[pyo3(signature = (r, g, b, a=1.0))]
204-
fn srgb(r: f32, g: f32, b: f32, a: f32) -> super::super::color::PyColor {
205-
super::super::color::PyColor::from_srgb(r, g, b, a)
204+
fn srgb(r: f32, g: f32, b: f32, a: f32) -> PyColor {
205+
PyColor::from_srgb(r, g, b, a)
206206
}
207207

208208
#[pyfunction]
209209
#[pyo3(signature = (r, g, b, a=1.0))]
210-
fn linear(r: f32, g: f32, b: f32, a: f32) -> super::super::color::PyColor {
211-
super::super::color::PyColor::from_linear(r, g, b, a)
210+
fn linear(r: f32, g: f32, b: f32, a: f32) -> PyColor {
211+
PyColor::from_linear(r, g, b, a)
212212
}
213213

214214
#[pyfunction]
215215
#[pyo3(signature = (h, s, l, a=1.0))]
216-
fn hsla(h: f32, s: f32, l: f32, a: f32) -> super::super::color::PyColor {
217-
super::super::color::PyColor::from_hsla(h, s, l, a)
216+
fn hsla(h: f32, s: f32, l: f32, a: f32) -> PyColor {
217+
PyColor::from_hsla(h, s, l, a)
218218
}
219219

220220
#[pyfunction]
221221
#[pyo3(signature = (h, s, v, a=1.0))]
222-
fn hsva(h: f32, s: f32, v: f32, a: f32) -> super::super::color::PyColor {
223-
super::super::color::PyColor::from_hsva(h, s, v, a)
222+
fn hsva(h: f32, s: f32, v: f32, a: f32) -> PyColor {
223+
PyColor::from_hsva(h, s, v, a)
224224
}
225225

226226
#[pyfunction]
227227
#[pyo3(signature = (h, w, b, a=1.0))]
228-
fn hwba(h: f32, w: f32, b: f32, a: f32) -> super::super::color::PyColor {
229-
super::super::color::PyColor::from_hwba(h, w, b, a)
228+
fn hwba(h: f32, w: f32, b: f32, a: f32) -> PyColor {
229+
PyColor::from_hwba(h, w, b, a)
230230
}
231231

232232
#[pyfunction]
233233
#[pyo3(signature = (l, a_axis, b_axis, alpha=1.0))]
234-
fn oklab(l: f32, a_axis: f32, b_axis: f32, alpha: f32) -> super::super::color::PyColor {
235-
super::super::color::PyColor::from_oklab(l, a_axis, b_axis, alpha)
234+
fn oklab(l: f32, a_axis: f32, b_axis: f32, alpha: f32) -> PyColor {
235+
PyColor::from_oklab(l, a_axis, b_axis, alpha)
236236
}
237237

238238
#[pyfunction]
239239
#[pyo3(signature = (l, c, h, a=1.0))]
240-
fn oklch(l: f32, c: f32, h: f32, a: f32) -> super::super::color::PyColor {
241-
super::super::color::PyColor::from_oklch(l, c, h, a)
240+
fn oklch(l: f32, c: f32, h: f32, a: f32) -> PyColor {
241+
PyColor::from_oklch(l, c, h, a)
242242
}
243243

244244
#[pyfunction]
245245
#[pyo3(signature = (l, a_axis, b_axis, alpha=1.0))]
246-
fn lab(l: f32, a_axis: f32, b_axis: f32, alpha: f32) -> super::super::color::PyColor {
247-
super::super::color::PyColor::from_lab(l, a_axis, b_axis, alpha)
246+
fn lab(l: f32, a_axis: f32, b_axis: f32, alpha: f32) -> PyColor {
247+
PyColor::from_lab(l, a_axis, b_axis, alpha)
248248
}
249249

250250
#[pyfunction]
251251
#[pyo3(signature = (l, c, h, a=1.0))]
252-
fn lch(l: f32, c: f32, h: f32, a: f32) -> super::super::color::PyColor {
253-
super::super::color::PyColor::from_lch(l, c, h, a)
252+
fn lch(l: f32, c: f32, h: f32, a: f32) -> PyColor {
253+
PyColor::from_lch(l, c, h, a)
254254
}
255255

256256
#[pyfunction]
257257
#[pyo3(signature = (x, y, z, a=1.0))]
258-
fn xyz(x: f32, y: f32, z: f32, a: f32) -> super::super::color::PyColor {
259-
super::super::color::PyColor::from_xyz(x, y, z, a)
258+
fn xyz(x: f32, y: f32, z: f32, a: f32) -> PyColor {
259+
PyColor::from_xyz(x, y, z, a)
260260
}
261261
}
262262

0 commit comments

Comments
 (0)