Skip to content

Commit

Permalink
New nodes: "Clamp", "To U32", and "To U64" (GraphiteEditor#2087)
Browse files Browse the repository at this point in the history
* New nodes: "Clamp", "To U32", and "To U64"

* Add name
  • Loading branch information
Keavon authored Nov 2, 2024
1 parent 3f17e83 commit 4c9ab2d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions node-graph/gcore/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ fn random<U: num_traits::float::Float>(
result * (max - min) + min
}

// To u32
#[node_macro::node(name("To u32"), category("Math: Numeric"))]
fn to_u32<U: num_traits::float::Float>(_: (), #[implementations(f64, f32)] value: U) -> u32 {
value.to_u32().unwrap()
}

// To u64
#[node_macro::node(name("To u64"), category("Math: Numeric"))]
fn to_u64<U: num_traits::float::Float>(_: (), #[implementations(f64, f32)] value: U) -> u64 {
value.to_u64().unwrap()
}

// Round
#[node_macro::node(category("Math: Numeric"))]
fn round<U: num_traits::float::Float>(_: (), #[implementations(f64, f32)] value: U) -> U {
Expand Down Expand Up @@ -202,6 +214,23 @@ fn max<T: core::cmp::PartialOrd>(_: (), #[implementations(f64, &f64, f32, &f32,
}
}

// Clamp
#[node_macro::node(category("Math: Numeric"))]
fn clamp<T: core::cmp::PartialOrd>(
_: (),
#[implementations(f64, &f64, f32, &f32, u32, &u32, &str)] value: T,
#[implementations(f64, &f64, f32, &f32, u32, &u32, &str)] min: T,
#[implementations(f64, &f64, f32, &f32, u32, &u32, &str)] max: T,
) -> T {
if value < min {
min
} else if value > max {
max
} else {
value
}
}

// Equals
#[node_macro::node(category("Math: Logic"))]
fn equals<U: core::cmp::PartialEq<T>, T>(
Expand Down

0 comments on commit 4c9ab2d

Please sign in to comment.