#-----------------------------------------------------------------------------# 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 base class for representing color values.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromabcimportABCMeta,abstractmethodfromtypingimportTYPE_CHECKING,Type,TypeVar## Bokeh importsifTYPE_CHECKING:from.hslimportHSLfrom.rgbimportRGB#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('Color',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------Self=TypeVar("Self",bound="Color")
[docs]classColor(metaclass=ABCMeta):''' A base class for representing color objects. '''def__repr__(self)->str:returnself.to_css()
[docs]@staticmethoddefclamp(value:float,maximum:float|None=None)->float:''' Clamp numeric values to be non-negative, an optionally, less than a given maximum. Args: value (float) : A number to clamp. maximum (float, optional) : A max bound to to clamp to. If None, there is no upper bound, and values are only clamped to be non-negative. (default: None) Returns: float '''value=max(value,0)ifmaximumisnotNone:returnmin(value,maximum)else:returnvalue
[docs]@abstractmethoddefcopy(self:Self)->Self:''' Copy this color. *Subclasses must implement this method.* '''raiseNotImplementedError
[docs]defdarken(self:Self,amount:float)->Self:''' Darken (reduce the luminance) of this color. Args: amount (float) : Amount to reduce the luminance by (clamped above zero) Returns: Color '''hsl=self.to_hsl()hsl.l=self.clamp(hsl.l-amount)returnself.from_hsl(hsl)
[docs]@classmethod@abstractmethoddeffrom_hsl(cls:Type[Self],value:HSL)->Self:''' Create a new color by converting from an HSL color. *Subclasses must implement this method.* Args: value (HSL) : A color to convert from HSL Returns: Color '''raiseNotImplementedError
[docs]@classmethod@abstractmethoddeffrom_rgb(cls:Type[Self],value:RGB)->Self:''' Create a new color by converting from an RGB color. *Subclasses must implement this method.* Args: value (:class:`~bokeh.colors.rgb.RGB`) : A color to convert from RGB Returns: Color '''raiseNotImplementedError
[docs]deflighten(self:Self,amount:float)->Self:''' Lighten (increase the luminance) of this color. Args: amount (float) : Amount to increase the luminance by (clamped above zero) Returns: Color '''hsl=self.to_hsl()hsl.l=self.clamp(hsl.l+amount,1)returnself.from_hsl(hsl)
[docs]@abstractmethoddefto_css(self)->str:''' Return a CSS representation of this color. *Subclasses must implement this method.* Returns: str '''raiseNotImplementedError
[docs]@abstractmethoddefto_hsl(self)->HSL:''' Create a new HSL color by converting from this color. *Subclasses must implement this method.* Returns: HSL '''raiseNotImplementedError
[docs]@abstractmethoddefto_rgb(self)->RGB:''' Create a new HSL color by converting from this color. *Subclasses must implement this method.* Returns: :class:`~bokeh.colors.rgb.RGB` '''raiseNotImplementedError
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------