#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Provide a class to represent colors with HSL (Hue, Saturation, Value).'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsimportcolorsysfromtypingimportTYPE_CHECKING# Bokeh importsfrom..util.deprecationimportdeprecatedfrom.colorimportColorifTYPE_CHECKING:from.rgbimportRGB#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('HSL',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------
[docs]classHSL(Color):''' Represent colors by specifying their Hue, Saturation, and lightness. Alpha values may also optionally be provided. Otherwise, alpha values default to 1. .. warning:: HSL is deprecated as of Bokeh 2.3.1 and will be removed in a future release. Use RGB or named colors instead. '''
[docs]def__init__(self,h:float,s:float,l:float,a:float=1.0)->None:''' Args: h (int) : The Hue, in [0, 360] s (int) : The Saturation, in [0, 1] l (int) : The lightness, in [0, 1] a (float, optional) : An alpha value for this color in [0, 1] (default: 1.0) '''deprecated((2,3,1),"HSL()","RGB() or named colors")self.h=hself.s=sself.l=lself.a=a
[docs]defcopy(self)->HSL:''' Return a copy of this color value. Returns: :class:`~bokeh.colors.hsl.HSL` '''returnHSL(self.h,self.s,self.l,self.a)
[docs]@classmethoddeffrom_hsl(cls,value:HSL)->HSL:''' Copy an HSL color from another HSL color value. Args: value (HSL) : The HSL color to copy. Returns: :class:`~bokeh.colors.hsl.HSL` '''returnvalue.copy()
[docs]@classmethoddeffrom_rgb(cls,value:RGB)->HSL:''' Create an HSL color from an RGB color value. Args: value (:class:`~bokeh.colors.rgb.RGB`) : The RGB color to convert. Returns: :class:`~bokeh.colors.hsl.HSL` '''returnvalue.to_hsl()
[docs]defto_css(self)->str:''' Generate the CSS representation of this HSL color. Returns: str, ``"hsl(...)"`` or ``"hsla(...)"`` '''ifself.a==1.0:returnf"hsl({self.h}, {self.s*100}%, {self.l*100}%)"else:returnf"hsla({self.h}, {self.s*100}%, {self.l*100}%, {self.a})"
[docs]defto_hsl(self)->HSL:''' Return a HSL copy for this HSL color. Returns: :class:`~bokeh.colors.hsl.HSL` '''returnself.copy()
[docs]defto_rgb(self)->RGB:''' Return a corresponding :class:`~bokeh.colors.rgb.RGB` color for this HSL color. Returns: :class:`~bokeh.colors.rgb.RGB` '''from.rgbimportRGB# prevent circular importr,g,b=colorsys.hls_to_rgb(float(self.h)/360,self.l,self.s)returnRGB(round(r*255),round(g*255),round(b*255),self.a)
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------