From c0f7be2ebc6e47f283aa2230828b368890093c82 Mon Sep 17 00:00:00 2001 From: Amrit kumar Mahto Date: Mon, 13 Apr 2026 11:23:41 +0530 Subject: [PATCH 1/3] Improve RGB input handling in color conversion (support normalized and legacy ranges) --- src/rust/src/hardsubx/imgops.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/rust/src/hardsubx/imgops.rs b/src/rust/src/hardsubx/imgops.rs index 114b2e2b9..7911fc04d 100644 --- a/src/rust/src/hardsubx/imgops.rs +++ b/src/rust/src/hardsubx/imgops.rs @@ -1,6 +1,7 @@ use palette::{FromColor, Hsv, Lab, LinSrgb}; /// Convert RGB values to HSV color space. +/// Accepts RGB in [0.0, 1.0]. If values are > 1.0, they are assumed to be 0–255 and normalized. /// /// # Safety /// @@ -8,7 +9,14 @@ use palette::{FromColor, Hsv, Lab, LinSrgb}; /// - The references must remain valid for the duration of the function call #[no_mangle] pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V: &mut f32) { - let rgb = LinSrgb::new(R, G, B); + let max_val = R.max(G).max(B); + let (norm_r, norm_g, norm_b) = if max_val > 1.0 { + (R / 255.0, G / 255.0, B / 255.0) + } else { + (R, G, B) + }; + + let rgb = LinSrgb::new(norm_r, norm_g, norm_b); let hsv_rep = Hsv::from_color(rgb); @@ -18,6 +26,7 @@ pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V } /// Convert RGB values to Lab color space. +/// Accepts RGB in [0.0, 1.0]. If values are > 1.0, they are assumed to be 0–255 and normalized. /// /// # Safety /// @@ -25,8 +34,14 @@ pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V /// - The references must remain valid for the duration of the function call #[no_mangle] pub extern "C" fn rgb_to_lab(R: f32, G: f32, B: f32, L: &mut f32, a: &mut f32, b: &mut f32) { - // Normalize input RGB from 0-255 to 0.0-1.0 - let rgb = LinSrgb::new(R / 255.0, G / 255.0, B / 255.0); + let max_val = R.max(G).max(B); + let (norm_r, norm_g, norm_b) = if max_val > 1.0 { + (R / 255.0, G / 255.0, B / 255.0) + } else { + (R, G, B) + }; + + let rgb = LinSrgb::new(norm_r, norm_g, norm_b); // Convert from sRGB to Lab (D65 white point is default) let lab_rep = Lab::from_color(rgb); From d5d59be3d8b046f8f1bde1b960e468021ae545ca Mon Sep 17 00:00:00 2001 From: Amrit kumar Mahto Date: Mon, 13 Apr 2026 11:50:34 +0530 Subject: [PATCH 2/3] formate --- src/rust/src/hardsubx/imgops.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rust/src/hardsubx/imgops.rs b/src/rust/src/hardsubx/imgops.rs index 7911fc04d..ee2f0a6ad 100644 --- a/src/rust/src/hardsubx/imgops.rs +++ b/src/rust/src/hardsubx/imgops.rs @@ -15,7 +15,7 @@ pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V } else { (R, G, B) }; - + let rgb = LinSrgb::new(norm_r, norm_g, norm_b); let hsv_rep = Hsv::from_color(rgb); @@ -40,7 +40,7 @@ pub extern "C" fn rgb_to_lab(R: f32, G: f32, B: f32, L: &mut f32, a: &mut f32, b } else { (R, G, B) }; - + let rgb = LinSrgb::new(norm_r, norm_g, norm_b); // Convert from sRGB to Lab (D65 white point is default) From e6cd34efd77a410647fe96250898ee6590273b58 Mon Sep 17 00:00:00 2001 From: Amrit kumar Mahto Date: Mon, 4 May 2026 06:08:02 +0530 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20remove=20RGB=20auto-detection,=20nor?= =?UTF-8?q?malize=20to=200=E2=80=93255=20consistently?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rust/src/hardsubx/imgops.rs | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/rust/src/hardsubx/imgops.rs b/src/rust/src/hardsubx/imgops.rs index ee2f0a6ad..a733603f4 100644 --- a/src/rust/src/hardsubx/imgops.rs +++ b/src/rust/src/hardsubx/imgops.rs @@ -1,7 +1,7 @@ use palette::{FromColor, Hsv, Lab, LinSrgb}; /// Convert RGB values to HSV color space. -/// Accepts RGB in [0.0, 1.0]. If values are > 1.0, they are assumed to be 0–255 and normalized. +/// Accepts RGB in [0, 255] and normalizes to [0.0, 1.0] internally. /// /// # Safety /// @@ -9,14 +9,7 @@ use palette::{FromColor, Hsv, Lab, LinSrgb}; /// - The references must remain valid for the duration of the function call #[no_mangle] pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V: &mut f32) { - let max_val = R.max(G).max(B); - let (norm_r, norm_g, norm_b) = if max_val > 1.0 { - (R / 255.0, G / 255.0, B / 255.0) - } else { - (R, G, B) - }; - - let rgb = LinSrgb::new(norm_r, norm_g, norm_b); + let rgb = LinSrgb::new(R / 255.0, G / 255.0, B / 255.0); let hsv_rep = Hsv::from_color(rgb); @@ -26,7 +19,7 @@ pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V } /// Convert RGB values to Lab color space. -/// Accepts RGB in [0.0, 1.0]. If values are > 1.0, they are assumed to be 0–255 and normalized. +/// Accepts RGB in [0, 255] and normalizes to [0.0, 1.0] internally. /// /// # Safety /// @@ -34,14 +27,7 @@ pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V /// - The references must remain valid for the duration of the function call #[no_mangle] pub extern "C" fn rgb_to_lab(R: f32, G: f32, B: f32, L: &mut f32, a: &mut f32, b: &mut f32) { - let max_val = R.max(G).max(B); - let (norm_r, norm_g, norm_b) = if max_val > 1.0 { - (R / 255.0, G / 255.0, B / 255.0) - } else { - (R, G, B) - }; - - let rgb = LinSrgb::new(norm_r, norm_g, norm_b); + let rgb = LinSrgb::new(R / 255.0, G / 255.0, B / 255.0); // Convert from sRGB to Lab (D65 white point is default) let lab_rep = Lab::from_color(rgb); @@ -89,25 +75,25 @@ mod test { let (mut l, mut a, mut b) = (0.0, 0.0, 0.0); // Red (255, 0, 0) - rgb_to_lab(1.0, 0.0, 0.0, &mut l, &mut a, &mut b); + rgb_to_lab(255.0, 0.0, 0.0, &mut l, &mut a, &mut b); assert_eq!(l.floor(), 53.0); assert_eq!(a.floor(), 80.0); assert_eq!(b.floor(), 67.0); // Green (0, 255, 0) - rgb_to_lab(0.0, 1.0, 0.0, &mut l, &mut a, &mut b); + rgb_to_lab(0.0, 255.0, 0.0, &mut l, &mut a, &mut b); assert_eq!(l.floor(), 87.0); assert_eq!(a.floor(), -87.0); assert_eq!(b.floor(), 83.0); // Blue (0, 0, 255) - rgb_to_lab(0.0, 0.0, 1.0, &mut l, &mut a, &mut b); + rgb_to_lab(0.0, 0.0, 255.0, &mut l, &mut a, &mut b); assert_eq!(l.floor(), 32.0); assert_eq!(a.floor(), 79.0); assert_eq!(b.floor(), -108.0); // White (255, 255, 255) - rgb_to_lab(1.0, 1.0, 1.0, &mut l, &mut a, &mut b); + rgb_to_lab(255.0, 255.0, 255.0, &mut l, &mut a, &mut b); assert_eq!(l.floor(), 100.0); assert_eq!(a.floor(), 0.0); assert_eq!(b.floor(), 0.0);