@@ -357,7 +357,67 @@ def __call__(self, n: int) -> Sequence[RGBHexColor | None]:
357357@dataclass
358358class hue_pal (_discrete_pal ):
359359 """
360- Utility for making hue palettes for color schemes.
360+ Make a hue palette in HCL colour space
361+
362+ Parameters
363+ ----------
364+ h : float | tuple[float, float]
365+ If a float, it is the first hue value, in the range `[0, 360]`.
366+ The range of the palette will be `[first, first + 360)`.
367+
368+ If a tuple, it is the range `[first, last)` of the hues.
369+ c : float
370+ The chroma value, in the range [0, 100].
371+ l : float
372+ The lightness value, in the range [0, 100].
373+ direction : 1 | -1
374+ The order of colours in the scale. If -1 the order
375+ of colours is reversed. The default is 1.
376+
377+ Returns
378+ -------
379+ palette : Callable
380+ A function that takes an integer and returns a that number of
381+ colours from the HCL colour space.
382+
383+ Examples
384+ --------
385+ >>> hue_pal()(5)
386+ ['#f8766d', '#a3a500', '#00bf7d', '#00b0f6', '#e76bf3']
387+ >>> hue_pal((100, 300))(5)
388+ ['#88ac00', '#00be67', '#00becd', '#3fa1ff', '#e36ef6']
389+ """
390+
391+ h : float | tuple [float , float ] = 15
392+ c : float = 100
393+ l : float = 65
394+ direction : Literal [1 , - 1 ] = 1
395+
396+ def __post_init__ (self ):
397+ # Ensure a proper range for the palette
398+ if isinstance (self .h , tuple ):
399+ self ._hue_range = self .h
400+ else :
401+ self ._hue_range = self .h , self .h + 360
402+
403+ def __call__ (self , n : int ) -> Sequence [RGBHexColor ]:
404+ h = self ._hue_range
405+
406+ # Make a hue range that is functionally zero wrap around and
407+ # and cover the entire hue space.
408+ # h = (0, 360) == (360, 360) == (720, 360)
409+ # h = (200, 200) == (200, 200+360) == (200, 200+720)
410+ if (h [1 ] - h [0 ]) % 360 < 1 :
411+ h = h [0 ] % 360 , (h [1 ] - 360 / n ) % 360
412+
413+ hues = np .linspace (h [0 ], h [1 ], n )[:: self .direction ] % 360
414+ return [hsluv .lch_to_hex ((self .l , self .c , h )) for h in hues ]
415+
416+
417+ @dataclass
418+ class hls_pal (_discrete_pal ):
419+ """
420+ Make a hue palette in HLS or HSLUV colour space
361421
362422 Parameters
363423 ----------
@@ -384,9 +444,9 @@ class hue_pal(_discrete_pal):
384444
385445 Examples
386446 --------
387- >>> hue_pal ()(5)
447+ >>> hls_pal ()(5)
388448 ['#db5f57', '#b9db57', '#57db94', '#5784db', '#c957db']
389- >>> hue_pal (color_space='hsluv')(5)
449+ >>> hls_pal (color_space='hsluv')(5)
390450 ['#e0697e', '#9b9054', '#569d79', '#5b98ab', '#b675d7']
391451 """
392452
@@ -399,7 +459,7 @@ def __post_init__(self):
399459 h , l , s = self .h , self .l , self .s
400460 if not all (0 <= val <= 1 for val in (h , l , s )):
401461 msg = (
402- "hue_pal expects values to be between 0 and 1. "
462+ "hls_pal expects values to be between 0 and 1. "
403463 f"I got { h = } , { l = } , { s = } "
404464 )
405465 raise ValueError (msg )
0 commit comments