<s> """<STR_LIT>""" <EOL> import colorsys <EOL> from sanpera . _api import ffi , lib <EOL> from sanpera . exception import magick_try <EOL> from sanpera . imagemagick import HAS_HDRI <EOL> if HAS_HDRI : <EOL> def _clamp ( ch ) : <EOL> return ch <EOL> else : <EOL> def _clamp ( ch ) : <EOL> if ch < <NUM_LIT:0> : <EOL> return <NUM_LIT:0.> <EOL> if ch > <NUM_LIT:1> : <EOL> return <NUM_LIT:1.> <EOL> return ch <EOL> class BaseColor ( object ) : <EOL> """<STR_LIT>""" <EOL> COLOR_WHEEL = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ] <EOL> def __init__ ( self ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> @ property <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> hslself = self . hsl ( ) <EOL> l = hslself . lightness <EOL> if l <= <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> return "<STR_LIT>" <EOL> elif l < <NUM_LIT:0.1> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT> : <EOL> shade = "<STR_LIT>" <EOL> elif l < <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> s = hslself . saturation <EOL> if s < <NUM_LIT> : <EOL> if shade : <EOL> return shade + "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> elif s < <NUM_LIT> : <EOL> sat = "<STR_LIT>" <EOL> else : <EOL> sat = "<STR_LIT>" <EOL> h = hslself . hue <EOL> colors = len ( self . COLOR_WHEEL ) <EOL> wheel_position = ( h * colors + <NUM_LIT:0.5> / colors ) % colors <EOL> wheel_index = int ( wheel_position ) <EOL> hue = self . COLOR_WHEEL [ wheel_index ] <EOL> if wheel_position - wheel_index > <NUM_LIT:0.5> : <EOL> hue += "<STR_LIT:->" + self . COLOR_WHEEL [ ( wheel_index + <NUM_LIT:1> ) % colors ] <EOL> color = hue <EOL> if sat : <EOL> color = sat + "<STR_LIT:U+0020>" + color <EOL> if shade : <EOL> color = shade + "<STR_LIT:U+0020>" + color <EOL> return color <EOL> @ property <EOL> def opacity ( self ) : <EOL> return self . _opacity <EOL> @ property <EOL> def extra_channels ( self ) : <EOL> return self . _extra_channels <EOL> def rgb ( self ) : <EOL> raise NotImplementedError <EOL> def hsl ( self ) : <EOL> raise NotImplementedError <EOL> @ classmethod <EOL> def parse ( cls , name ) : <EOL> """<STR_LIT>""" <EOL> pixel = ffi . new ( "<STR_LIT>" ) <EOL> with magick_try ( ) as exc : <EOL> success = lib . QueryColorDatabase ( name . encode ( '<STR_LIT:ascii>' ) , pixel , exc . ptr ) <EOL> if not success : <EOL> raise ValueError ( "<STR_LIT>" . format ( name ) ) <EOL> return cls . _from_pixel ( pixel ) <EOL> @ classmethod <EOL> def coerce ( cls , value ) : <EOL> if isinstance ( value , cls ) : <EOL> return value <EOL> return cls . parse ( value ) <EOL> @ classmethod <EOL> def _from_pixel ( cls , pixel ) : <EOL> """<STR_LIT>""" <EOL> array = ffi . new ( "<STR_LIT>" , <NUM_LIT:4> ) <EOL> lib . sanpera_pixel_to_doubles ( pixel , array ) <EOL> return RGBColor . _from_c_array ( array ) <EOL> def _populate_pixel ( self , pixel ) : <EOL> """<STR_LIT>""" <EOL> rgb = self . rgb ( ) <EOL> lib . sanpera_pixel_from_doubles ( pixel , rgb . _array ) <EOL> def _populate_magick_pixel ( self , pixel ) : <EOL> """<STR_LIT>""" <EOL> rgb = self . rgb ( ) <EOL> lib . sanpera_magick_pixel_from_doubles ( pixel , rgb . _array ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , BaseColor ) : <EOL> return NotImplemented <EOL> left = self . rgb ( ) <EOL> right = other . rgb ( ) <EOL> return ( <EOL> left . _red == right . _red and <EOL> left . _green == right . _green and <EOL> left . _blue == right . _blue and <EOL> left . _opacity == right . _opacity ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class RGBColor ( BaseColor ) : <EOL> def __init__ ( self , red , green , blue , opacity = <NUM_LIT:1.0> , _array = None ) : <EOL> self . _red = red <EOL> self . _green = green <EOL> self . _blue = blue <EOL> self . _opacity = opacity <EOL> self . _extra_channels = ( ) <EOL> if _array is None : <EOL> self . _array = ffi . new ( "<STR_LIT>" , [ red , green , blue , opacity ] ) <EOL> else : <EOL> self . _array = _array <EOL> @ classmethod <EOL> def _from_c_array ( cls , array ) : <EOL> return cls ( * array , _array = array ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . _red , self . _green , self . _blue , self . description , self . _opacity * <NUM_LIT:100> ) <EOL> def __mul__ ( self , factor ) : <EOL> return RGBColor ( <EOL> self . _red * factor , <EOL> self . _green * factor , <EOL> self . _blue * factor , <EOL> self . _opacity ) <EOL> def __rmul__ ( self , factor ) : <EOL> return self . __mul__ ( factor ) <EOL> def __add__ ( self , other ) : <EOL> return RGBColor ( <EOL> self . _red + other , <EOL> self . _green + other , <EOL> self . _blue + other , <EOL> self . _opacity ) <EOL> def __sub__ ( self , other ) : <EOL> return self + ( - other ) <EOL> def clamped ( self ) : <EOL> return RGBColor ( <EOL> _clamp ( self . _red ) , <EOL> _clamp ( self . _green ) , <EOL> _clamp ( self . _blue ) , <EOL> _clamp ( self . _opacity ) ) <EOL> @ property <EOL> def red ( self ) : <EOL> return self . _red <EOL> @ property <EOL> def green ( self ) : <EOL> return self . _green <EOL> @ property <EOL> def blue ( self ) : <EOL> return self . _blue <EOL> @ property <EOL> def alpha ( self ) : <EOL> return self . _opacity <EOL> def rgb ( self ) : <EOL> return self <EOL> def hsl ( self ) : <EOL> hue , light , sat = colorsys . rgb_to_hls ( <EOL> _clamp ( self . _red ) , _clamp ( self . _green ) , _clamp ( self . _blue ) ) <EOL> return HSLColor ( hue , sat , light , self . _opacity ) <EOL> class CMYKColor ( BaseColor ) : <EOL> def __init__ ( self , cyan , magenta , yellow , black , alpha = <NUM_LIT:1.0> ) : <EOL> self . cyan = cyan <EOL> self . magenta = magenta <EOL> self . yellow = yellow <EOL> self . black = black <EOL> self . alpha = alpha <EOL> def rgb ( self ) : <EOL> return RGBColor ( <EOL> <NUM_LIT:1> - self . cyan - self . black , <EOL> <NUM_LIT:1> - self . magenta - self . black , <EOL> <NUM_LIT:1> - self . yellow - self . black , <EOL> self . alpha ) <EOL> class HSLColor ( BaseColor ) : <EOL> def __init__ ( self , hue , saturation , lightness , opacity = <NUM_LIT:1.0> ) : <EOL> if hue == <NUM_LIT:1.0> : <EOL> hue = <NUM_LIT:0.0> <EOL> self . _hue = hue <EOL> self . _saturation = saturation <EOL> self . _lightness = lightness <EOL> self . _opacity = opacity <EOL> self . _extra_channels = ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . _hue , self . _saturation , self . _lightness , self . description , self . _opacity * <NUM_LIT:100> ) <EOL> @ property <EOL> def hue ( self ) : <EOL> return self . _hue <EOL> @ property <EOL> def saturation ( self ) : <EOL> return self . _saturation <EOL> @ property <EOL> def lightness ( self ) : <EOL> return self . _lightness <EOL> def rgb ( self ) : <EOL> r , g , b = colorsys . hls_to_rgb ( self . _hue , self . _lightness , self . _saturation ) <EOL> return RGBColor ( r , g , b , self . _opacity ) <EOL> def hsl ( self ) : <EOL> return self <EOL> class GrayColor ( BaseColor ) : <EOL> def __init__ ( self , value , alpha = <NUM_LIT:1.0> ) : <EOL> self . value = value <EOL> self . alpha = alpha <EOL> def rgb ( self ) : <EOL> return RGBColor ( self . value , self . value , self . value , self . alpha ) <EOL> def hsl ( self ) : <EOL> return HSLColor ( <NUM_LIT:0> , <NUM_LIT:0> , self . value , self . alpha ) </s>
