Skip to content

Commit

Permalink
Merge pull request #10 from mockersf/std-and-glam-features
Browse files Browse the repository at this point in the history
std and glam features
  • Loading branch information
kabergstrom authored Nov 13, 2021
2 parents e5bb9a1 + 791cc8c commit 9663bf5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
8 changes: 5 additions & 3 deletions build/kolor-64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ num-traits = { version = "^0.2.14", optional = true, default-features = false }
glam = { version = "0.17.1", default-features = false, optional = true }

[features]
default = ["color-matrices", "f64", "std", "glam"]
default = ["color-matrices", "f64", "std-glam"]
serde1 = ["serde", "glam/serde"]
color-matrices = []
f64 = []
std = ["glam/std"]
libm = ["num-traits", "num-traits/libm", "glam/libm"]
std = []
std-glam = ["std", "glam/std"]
libm = ["num-traits", "num-traits/libm"]
libm-glam = ["libm", "glam/libm"]
8 changes: 5 additions & 3 deletions build/kolor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ num-traits = { version = "^0.2.14", optional = true, default-features = false }
glam = { version = "0.17.1", default-features = false, optional = true }

[features]
default = ["color-matrices", "f32", "std", "glam"]
default = ["color-matrices", "f32", "std-glam"]
serde1 = ["serde", "glam/serde"]
color-matrices = []
f32 = []
std = ["glam/std"]
libm = ["num-traits", "num-traits/libm", "glam/libm"]
std = []
std-glam = ["std", "glam/std"]
libm = ["num-traits", "num-traits/libm"]
libm-glam = ["libm", "glam/libm"]
14 changes: 11 additions & 3 deletions kolor/src/details/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ mod math {
#[cfg(not(feature = "glam"))]
mod math {
use crate::FType;
use std::ops::{Add, Div, Mul, MulAssign, Sub};
#[cfg(all(not(feature = "std"), feature = "libm"))]
use core::ops::{Add, Div, Mul, MulAssign, Sub};
#[cfg(all(not(feature = "std"), feature = "libm"))]
use num_traits::Float;

#[cfg(all(not(feature = "libm"), feature = "std"))]
use std::ops::{Add, Div, Mul, MulAssign, Sub};

#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -111,9 +113,15 @@ mod math {
pub fn dot(self, other: Self) -> FType {
self.x * other.x + self.y * other.y + self.z * other.z
}

pub fn abs_diff_eq(self, other: Self, max_abs_diff: FType) -> bool {
(self.x - other.x).abs() <= max_abs_diff
&& (self.y - other.y).abs() <= max_abs_diff
&& (self.z - other.z).abs() <= max_abs_diff
}
}

impl Cuberoot for Vec3 {
impl super::Cuberoot for Vec3 {
#[inline]
fn cbrt(&self) -> Self {
Self::new(self.x.cbrt(), self.y.cbrt(), self.z.cbrt())
Expand Down
22 changes: 16 additions & 6 deletions kolor/src/details/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,17 +786,27 @@ mod test {
let inverse_val = crate::details::transform::pq::ST_2084_PQ_eotf_float(pq_val);
assert!(
(value - inverse_val).abs() < 0.001,
"pq_val {} inverse {}",
"pq_val {:?} inverse {:?}",
value,
inverse_val
);
// validate inverse matrices
let value = Vec3::new(0.5, 0.5, 0.5);
let result = ICtCp::ICtCp_From_PQ_INVERSE * (ICtCp::ICtCp_From_PQ * value);
assert!(value.abs_diff_eq(result, 0.0001), "{} != {}", value, result);
assert!(
value.abs_diff_eq(result, 0.0001),
"{:?} != {:?}",
value,
result
);

let result = ICtCp::ICtCp_LMS_INVERSE * (ICtCp::ICtCp_LMS * value);
assert!(value.abs_diff_eq(result, 0.0001), "{} != {}", value, result);
assert!(
value.abs_diff_eq(result, 0.0001),
"{:?} != {:?}",
value,
result
);

let to = |value: Vec3| from_conv.convert(to_conv.convert(value));
let from = |value: Vec3| from_conv.convert(to_conv.convert(value));
Expand All @@ -806,7 +816,7 @@ mod test {
let allowed_error = 0.002;
assert!(
value.abs_diff_eq(result, allowed_error),
"{} != {}",
"{:?} != {:?}",
value,
result
);
Expand All @@ -815,7 +825,7 @@ mod test {
let result = from(to(value));
assert!(
value.abs_diff_eq(result, allowed_error),
"{} != {}",
"{:?} != {:?}",
value,
result
);
Expand All @@ -824,7 +834,7 @@ mod test {
let result = from(to(value));
assert!(
value.abs_diff_eq(result, allowed_error),
"{} != {}",
"{:?} != {:?}",
value,
result
);
Expand Down
11 changes: 9 additions & 2 deletions kolor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,15 @@
//! Functions in [details::xyz] can be used to create conversion matrices to/from an RGB color space
//! given a set of primaries and a white point.
//!
//! # no_std support
//! kolor supports `no_std` by disabling the default-enabled `std` feature and enabling the `libm` feature.
//! # no_std and glam support
//! kolor is using by default `std` and `glam`, but both can be disabled separately or together with the
//! folowing features:
//!
//! | |`std`|`no_std`|
//! |-|-|-|
//! |`glam`|`std-glam`|`libm-glam`|
//! |no `glam`|`std`|`libm`|
//!

#![cfg_attr(not(feature = "std"), no_std)]

Expand Down
2 changes: 1 addition & 1 deletion matrix-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ license = "MIT OR Apache-2.0"

[dependencies]
kolor-64 = { version = "0.1.7", path = "../build/kolor-64", default-features = false, features = [
"std",
"std-glam",
"f64"
] }

0 comments on commit 9663bf5

Please sign in to comment.