plotters icon indicating copy to clipboard operation
plotters copied to clipboard

fix: wrap hue in HSLColor + add from_degrees

Open zxq82lm opened this issue 3 months ago • 0 comments

This branch fixes the behavior of HSLColor in Plotters when the hue value is outside the [0,1] range.

Fix

  • Hue is now wrapped into [0,1) instead of clamped.
  • Added a convenience constructor:
HSLColor::from_degrees(h_deg, s, l)
  • Unit tests ensure correct RGB for 0°, 120°, and 240°.

The fix was tested with the following example code, originally reported in this issue https://github.com/plotters-rs/plotters/issues/696:

use plotters::prelude::*;
use plotters::style::HSLColor;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let root = BitMapBackend::new("hsl_test_output3.png", (200, 200)).into_drawing_area();
    root.fill(&WHITE)?;

    // sweep hue 0° (red) -> 240° (blue) horizontally
    for x in 0..200 {
        let hue = 240.0 * x as f64 / 200.0;
        let color = HSLColor(hue, 1.0, 0.5);
        for y in 0..200 {
            root.draw_pixel((x as i32, y as i32), &color)?;
        }
    }

    root.present()?;
    Ok(())
}

Closes #696

zxq82lm avatar Oct 03 '25 03:10 zxq82lm