Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/rust/src/hardsubx/imgops.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
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
///
/// - `H`, `S`, `V` must be valid mutable references to f32 values
/// - 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);

Expand All @@ -18,15 +26,22 @@ 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
///
/// - `L`, `a`, `b` must be valid mutable references to f32 values
/// - 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);
Expand Down
Loading