Skip to content

Commit

Permalink
Added Jzazbz and Jzczhz
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jul 20, 2024
1 parent 103c4ea commit c93b2c7
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["src/app"] }

[package]
name = "colorutils-rs"
version = "0.4.17"
version = "0.5.0"
edition = "2021"
description = "High performance utilities for color format handling and conversion."
readme = "README.md"
Expand Down
11 changes: 10 additions & 1 deletion src/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Hsl {
}

impl Hsl {
#[inline]
pub fn new(h: u16, s: u16, l: u16) -> Hsl {
Hsl {
h: h as f32,
Expand All @@ -26,14 +27,17 @@ impl Hsl {
}
}

#[inline]
pub fn from_components(h: f32, s: f32, l: f32) -> Hsl {
Hsl { h, s, l }
}

pub fn from_rgb(rgb: &Rgb<u8>) -> Hsl {
#[inline]
pub fn from_rgb(rgb: Rgb<u8>) -> Hsl {
rgb2hsl(rgb.r, rgb.g, rgb.b)
}

#[inline]
pub fn to_rgb8(&self) -> Rgb<u8> {
let c = (1f32 - (2f32 * self.l - 1f32).abs()) * self.s;
let x = c * (1f32 - ((self.h / 60f32) % 2f32 - 1f32).abs());
Expand All @@ -60,23 +64,28 @@ impl Hsl {
}
}

#[inline]
pub fn to_rgb(&self) -> Rgb<u8> {
self.to_rgb8()
}

#[inline]
pub fn get_saturation(&self) -> u16 {
((self.s * 100f32) as u16).min(100u16)
}

#[inline]
pub fn get_lightness(&self) -> u16 {
((self.l * 100f32) as u16).min(100u16)
}

#[inline]
pub fn get_hue(&self) -> u16 {
(self.h as u16).min(360)
}
}

#[inline]
fn rgb2hsl(o_r: u8, o_g: u8, o_b: u8) -> Hsl {
let r = o_r as f32 / 255f32;
let g = o_g as f32 / 255f32;
Expand Down
3 changes: 2 additions & 1 deletion src/hsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static HSV_U8_SCALE: f32 = 1f32 / 255f32;
static HSV_PERCENTAGE_SCALE: f32 = 1f32 / 100f32;

impl Hsv {
#[inline]
pub fn new(h: u16, s: u16, l: u16) -> Hsv {
Hsv {
h: h as f32,
Expand All @@ -33,7 +34,7 @@ impl Hsv {
Hsv { h, s, v }
}
#[inline]
pub fn from(rgb: &Rgb<u8>) -> Hsv {
pub fn from(rgb: Rgb<u8>) -> Hsv {
let (h, s, v) = rgb_to_hsv(
rgb.r as f32 * HSV_U8_SCALE,
rgb.g as f32 * HSV_U8_SCALE,
Expand Down
2 changes: 1 addition & 1 deletion src/image_to_xyz_lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn channels_to_xyz<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool, cons
}
}
XyzTarget::XYZ => {
let xyz = Xyz::from_rgb(&rgb, &matrix, transfer_function);
let xyz = Xyz::from_rgb(rgb, &matrix, transfer_function);
unsafe {
ptr.write_unaligned(xyz.x);
ptr.add(1).write_unaligned(xyz.y);
Expand Down
2 changes: 1 addition & 1 deletion src/image_xyza_laba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn channels_to_xyz_with_alpha<const CHANNELS_CONFIGURATION: u8, const TARGET: u8
}
}
XyzTarget::XYZ => {
let xyz = Xyz::from_rgb(&rgb, &matrix, transfer_function);
let xyz = Xyz::from_rgb(rgb, &matrix, transfer_function);
unsafe {
dst_store.write_unaligned(xyz.x);
dst_store.add(1).write_unaligned(xyz.y);
Expand Down
10 changes: 9 additions & 1 deletion src/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ impl Lab {
}

impl Lab {
pub fn from_rgb(rgb: &Rgb<u8>) -> Self {
/// Converts to CIE Lab from Rgb
#[inline]
pub fn from_rgb(rgb: Rgb<u8>) -> Self {
let xyz = Xyz::from_srgb(rgb);
let x = xyz.x * 100f32 / 95.047f32;
let y = xyz.y * 100f32 / 100f32;
Expand All @@ -62,6 +64,8 @@ impl Lab {
}

impl Lab {
/// Converts CIE Lab into Rgb
#[inline]
pub fn to_rgb8(&self) -> Rgb<u8> {
let y = (self.l + 16.0) / 116.0;
let x = self.a * (1f32 / 500f32) + y;
Expand Down Expand Up @@ -93,18 +97,22 @@ impl Lab {
Xyz::new(x / 100f32, y / 100f32, z / 100f32).to_srgb()
}

/// Converts CIE Lab into Rgb
#[inline]
pub fn to_rgb(&self) -> Rgb<u8> {
self.to_rgb8()
}
}

impl EuclideanDistance for Lab {
#[inline]
fn euclidean_distance(&self, other: Lab) -> f32 {
(self.l - other.l).hypot3(self.a - other.a, self.b - other.b)
}
}

impl TaxicabDistance for Lab {
#[inline]
fn taxicab_distance(&self, other: Self) -> f32 {
(self.a - other.a).hypot(self.b - other.b) + (self.l - other.l).abs()
}
Expand Down
14 changes: 9 additions & 5 deletions src/luv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ pub(crate) const LUV_CUTOFF_FORWARD_Y: f32 = (6f32 / 29f32) * (6f32 / 29f32) * (
pub(crate) const LUV_MULTIPLIER_FORWARD_Y: f32 = (29f32 / 3f32) * (29f32 / 3f32) * (29f32 / 3f32);
pub(crate) const LUV_MULTIPLIER_INVERSE_Y: f32 = (3f32 / 29f32) * (3f32 / 29f32) * (3f32 / 29f32);
impl Luv {
pub fn from_rgb(rgb: &Rgb<u8>) -> Self {
/// Converts RGB to CIE Luv
#[inline]
pub fn from_rgb(rgb: Rgb<u8>) -> Self {
let xyz = Xyz::from_srgb(rgb);
let [x, y, z] = [xyz.x, xyz.y, xyz.z];
let den = x + 15.0 * y + 3.0 * z;
Expand All @@ -94,8 +96,10 @@ impl Luv {
Luv { l, u, v }
}

pub fn from_rgba(rgba: &Rgba<u8>) -> Self {
Luv::from_rgb(&rgba.to_rgb())
//
#[inline]
pub fn from_rgba(rgba: Rgba<u8>) -> Self {
Luv::from_rgb(rgba.to_rgb())
}

pub fn to_rgb(&self) -> Rgb<u8> {
Expand Down Expand Up @@ -129,11 +133,11 @@ impl Luv {
}

impl LCh {
pub fn from_rgb(rgb: &Rgb<u8>) -> Self {
pub fn from_rgb(rgb: Rgb<u8>) -> Self {
LCh::from_luv(Luv::from_rgb(rgb))
}

pub fn from_rgba(rgba: &Rgba<u8>) -> Self {
pub fn from_rgba(rgba: Rgba<u8>) -> Self {
LCh::from_luv(Luv::from_rgba(rgba))
}

Expand Down
10 changes: 8 additions & 2 deletions src/neon/hsv_to_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,14 @@ pub unsafe fn neon_hsv_u16_to_image<
let pixel_set = uint8x8x3_t(r_chan, g_chan, b_chan);
vst3_u8(dst_ptr.add(cx * channels), pixel_set);
}
ImageConfiguration::Rgba => {}
ImageConfiguration::Bgra => {}
ImageConfiguration::Rgba => {
let pixel_set = uint8x8x4_t(r_chan, g_chan, b_chan, vdup_n_u8(0));
vst4_u8(dst_ptr.add(cx * channels), pixel_set);
}
ImageConfiguration::Bgra => {
let pixel_set = uint8x8x4_t(b_chan, g_chan, r_chan, vdup_n_u8(0));
vst4_u8(dst_ptr.add(cx * channels), pixel_set);
}
ImageConfiguration::Bgr => {
let pixel_set = uint8x8x3_t(b_chan, g_chan, r_chan);
vst3_u8(dst_ptr.add(cx * channels), pixel_set);
Expand Down
14 changes: 7 additions & 7 deletions src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@ impl Rgb<u8> {
/// Converts rgb to XYZ
#[inline]
pub fn to_xyz(&self, matrix: &[[f32; 3]; 3], transfer_function: TransferFunction) -> Xyz {
Xyz::from_rgb(self, matrix, transfer_function)
Xyz::from_rgb(*self, matrix, transfer_function)
}

/// Converts rgb to HSL
#[inline]
pub fn to_hsl(&self) -> Hsl {
Hsl::from_rgb(self)
Hsl::from_rgb(*self)
}

/// Converts rgb to HSV
#[inline]
pub fn to_hsv(&self) -> Hsv {
Hsv::from(self)
Hsv::from(*self)
}

/// Converts rgb to CIELAB
#[inline]
pub fn to_lab(&self) -> Lab {
Lab::from_rgb(self)
Lab::from_rgb(*self)
}

/// Converts rgb to CIELUV
#[inline]
pub fn to_luv(&self) -> Luv {
Luv::from_rgb(self)
Luv::from_rgb(*self)
}

/// Converts rgb to CIELCH
#[inline]
pub fn to_lch(&self) -> LCh {
LCh::from_rgb(self)
LCh::from_rgb(*self)
}

/// Converts rgb to RGB f32
Expand All @@ -79,7 +79,7 @@ impl Rgb<u8> {
/// Converts rgb to S-shaped sigmoidized components
#[inline]
pub fn to_sigmoidal(&self) -> Sigmoidal {
Sigmoidal::from_rgb(self)
Sigmoidal::from_rgb(*self)
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ impl Rgba<f16> {
}

impl Rgba<f32> {
#[inline]
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Rgba<f32> {
return Rgba { r, g, b, a };
}

#[inline]
pub fn from_rgb(r: f32, g: f32, b: f32) -> Rgba<f32> {
return Rgba { r, g, b, a: 1f32 };
}
}

impl Rgba<u8> {
#[inline]
pub fn new(r: u8, g: u8, b: u8, a: u8) -> Rgba<u8> {
return Rgba { r, g, b, a };
}

#[inline]
pub fn from_rgb(r: u8, g: u8, b: u8) -> Rgba<u8> {
return Rgba {
r,
Expand All @@ -60,6 +64,7 @@ impl Rgba<u8> {
};
}

#[inline]
pub fn to_rgb(&self) -> Rgb<u8> {
Rgb {
r: self.r,
Expand Down Expand Up @@ -161,6 +166,7 @@ impl Rgb565 {
}

impl ToRgba8 for Rgb565 {
#[inline]
fn to_rgba8(&self) -> Rgba<u8> {
let red8 = ((self.rgb565 & 0b1111100000000000) >> 8) as u8;
let green8 = ((self.rgb565 & 0b11111100000) >> 3) as u8;
Expand All @@ -173,6 +179,7 @@ static SCALE_RGB565_5BIT: f32 = 1f32 / 31f32;
static SCALE_RGB565_6BIT: f32 = 1f32 / 63f32;

impl ToRgbaF16 for Rgb565 {
#[inline]
fn to_rgba_f16(&self) -> Rgba<f16> {
let red5 = (self.rgb565 & 0b1111100000000000) as f32 * SCALE_RGB565_5BIT;
let green6 = (self.rgb565 & 0b11111100000) as f32 * SCALE_RGB565_6BIT;
Expand Down
5 changes: 3 additions & 2 deletions src/sigmoidal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ fn inverse_sigmoidal(x: f32) -> f32 {
}

impl Sigmoidal {
#[inline]
pub fn new(sr: f32, sg: f32, sb: f32) -> Self {
Sigmoidal { sr, sg, sb }
}

#[inline]
pub fn from_rgb(rgb: &Rgb<u8>) -> Self {
pub fn from_rgb(rgb: Rgb<u8>) -> Self {
let normalized = rgb.to_rgb_f32();
Sigmoidal::new(
to_sigmoidal(normalized.r),
Expand All @@ -68,7 +69,7 @@ impl Sigmoidal {
impl From<Rgb<u8>> for Sigmoidal {
#[inline]
fn from(value: Rgb<u8>) -> Self {
Sigmoidal::from_rgb(&value)
Sigmoidal::from_rgb(value)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/xyz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Xyz {

/// This functions always use sRGB transfer function and Rec.601 primaries with D65 White point
#[inline]
pub fn from_srgb(rgb: &Rgb<u8>) -> Self {
pub fn from_srgb(rgb: Rgb<u8>) -> Self {
Xyz::from_rgb(rgb, &SRGB_TO_XYZ_D65, TransferFunction::Srgb)
}

Expand All @@ -63,7 +63,7 @@ impl Xyz {
/// * `transfer_function` - Transfer functions for current colorspace
#[inline]
pub fn from_rgb(
rgb: &Rgb<u8>,
rgb: Rgb<u8>,
matrix: &[[f32; 3]; 3],
transfer_function: TransferFunction,
) -> Self {
Expand Down

0 comments on commit c93b2c7

Please sign in to comment.