tylerreisinger / prisma

A powerful color representation, manipulation and conversion library that aims to be easy to use.
MIT License
21 stars 5 forks source link

"entered unreachable code" panic on converting HSL color to RGB with hue outside range 0…360 #22

Open mcclure opened 6 months ago

mcclure commented 6 months ago

Running with Rust 1.77.2 on Windows 10, I ran this code :

let color = [r, g, b];
let color = prisma::Rgb::new(color[0], color[1], color[2]);
let mut color = prisma::Hsv::<f32,angular_units::Deg<_>>::from_color(&color);
color.set_hue( color.hue() + angular_units::Deg(90.0) );
let color = prisma::Rgb::from_color(&color);
let color = [color.red(), color.green(), color.blue()];

In other words, I created a color from existing r,g,b values and then attempted to rotate its hue 90 degrees.

On the line let color = prisma::Rgb::from_color(&color); I got:

thread 'main' panicked at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:29
1:18:
internal error: entered unreachable code
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\std\src\panicking.rs:647
   1: core::panicking::panic_fmt
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\core\src\panicking.rs:72
   2: core::panicking::panic
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library\core\src\panicking.rs:144
   3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
   3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291         3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291         3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291       3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291       3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291     3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291     3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291    3: prisma::hsv::impl$15::from_color<f32,angular_units::Deg<f32> >
             at C:\Users\Andi\.cargo\registry\src\index.crates.io-6f17d22bba15001f\prisma-0.1.1\src\hsv.rs:291 
   4: video::main
             at .\src\main.rs:188
   5: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\core\src\ops\function.rs:250
   6: core::hint::black_box
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\library\core\src\hint.rs:334
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

However, if I change the rotation line to:

color.set_hue( ( color.hue() + angular_units::Deg(90.0) ) % angular_units::Deg(360.0) )

It runs without problems.

Expected behavior

tylerreisinger commented 6 months ago

Unreachable code is definitely not appropriate. I'll look into making sure angular quantities properly wrap if going outside the standard domain.