Skip to content

Conversation

zxq82lm
Copy link

@zxq82lm zxq82lm commented Oct 3, 2025

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 #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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] HSLColor always renders as solid red, ignoring hue adjustments
1 participant