<s> from __future__ import division <EOL> import numpy as np <EOL> from copy import deepcopy <EOL> from . . ext . six import string_types <EOL> from . . util import logger <EOL> from . _color_dict import _color_dict <EOL> from . color_space import ( _hex_to_rgba , _rgb_to_hex , _rgb_to_hsv , <EOL> _hsv_to_rgb , _rgb_to_lab , _lab_to_rgb ) <EOL> def _string_to_rgb ( color ) : <EOL> """<STR_LIT>""" <EOL> if not color . startswith ( '<STR_LIT:#>' ) : <EOL> if color . lower ( ) not in _color_dict : <EOL> raise ValueError ( '<STR_LIT>' % color ) <EOL> color = _color_dict [ color ] <EOL> assert color [ <NUM_LIT:0> ] == '<STR_LIT:#>' <EOL> color = color [ <NUM_LIT:1> : ] <EOL> lc = len ( color ) <EOL> if lc in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> color = '<STR_LIT>' . join ( c + c for c in color ) <EOL> lc = len ( color ) <EOL> if lc not in ( <NUM_LIT:6> , <NUM_LIT:8> ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> color = np . array ( [ int ( color [ i : i + <NUM_LIT:2> ] , <NUM_LIT:16> ) / <NUM_LIT> for i in range ( <NUM_LIT:0> , lc , <NUM_LIT:2> ) ] ) <EOL> return color <EOL> def _user_to_rgba ( color , expand = True , clip = False ) : <EOL> """<STR_LIT>""" <EOL> if color is None : <EOL> color = np . zeros ( <NUM_LIT:4> , np . float32 ) <EOL> if isinstance ( color , string_types ) : <EOL> color = _string_to_rgb ( color ) <EOL> elif isinstance ( color , ColorArray ) : <EOL> color = color . rgba <EOL> elif isinstance ( color , ( list , tuple ) ) : <EOL> if any ( isinstance ( c , string_types ) for c in color ) : <EOL> color = [ _user_to_rgba ( c , expand = expand , clip = clip ) for c in color ] <EOL> if any ( len ( c ) > <NUM_LIT:1> for c in color ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> color = [ c [ <NUM_LIT:0> ] for c in color ] <EOL> color = np . atleast_2d ( color ) . astype ( np . float32 ) <EOL> if color . shape [ <NUM_LIT:1> ] not in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if expand and color . shape [ <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> color = np . concatenate ( ( color , np . ones ( ( color . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) ) , <EOL> axis = <NUM_LIT:1> ) <EOL> if color . min ( ) < <NUM_LIT:0> or color . max ( ) > <NUM_LIT:1> : <EOL> if clip : <EOL> color = np . clip ( color , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return color <EOL> def _array_clip_val ( val ) : <EOL> """<STR_LIT>""" <EOL> val = np . array ( val ) <EOL> if val . max ( ) > <NUM_LIT:1> or val . min ( ) < <NUM_LIT:0> : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> val [ ... ] = np . clip ( val , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> return val <EOL> class ColorArray ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , color = ( <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> ) , alpha = None , <EOL> clip = False , color_space = '<STR_LIT>' ) : <EOL> color = ( <NUM_LIT:0> , ) * <NUM_LIT:4> if color is None else color <EOL> if color_space == '<STR_LIT>' : <EOL> color = _hsv_to_rgb ( color ) <EOL> elif color_space != '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' + color_space ) <EOL> rgba = _user_to_rgba ( color , clip = clip ) <EOL> if alpha is not None : <EOL> rgba [ : , <NUM_LIT:3> ] = alpha <EOL> self . _rgba = None <EOL> self . rgba = rgba <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return deepcopy ( self ) <EOL> @ classmethod <EOL> def _name ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . __name__ <EOL> def __len__ ( self ) : <EOL> return self . _rgba . shape [ <NUM_LIT:0> ] <EOL> def __repr__ ( self ) : <EOL> nice_str = str ( tuple ( self . _rgba [ <NUM_LIT:0> ] ) ) <EOL> plural = '<STR_LIT>' <EOL> if len ( self ) > <NUM_LIT:1> : <EOL> plural = '<STR_LIT:s>' <EOL> nice_str += '<STR_LIT>' + str ( tuple ( self . rgba [ - <NUM_LIT:1> ] ) ) <EOL> return ( '<STR_LIT>' % ( self . _name ( ) , len ( self ) , <EOL> plural , nice_str ) ) <EOL> def __eq__ ( self , other ) : <EOL> return np . array_equal ( self . _rgba , other . _rgba ) <EOL> def __getitem__ ( self , item ) : <EOL> if isinstance ( item , tuple ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> subrgba = self . _rgba [ item ] <EOL> if subrgba . ndim == <NUM_LIT:1> : <EOL> assert len ( subrgba ) == <NUM_LIT:4> <EOL> elif subrgba . ndim == <NUM_LIT:2> : <EOL> assert subrgba . shape [ <NUM_LIT:1> ] in ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> return ColorArray ( subrgba ) <EOL> def __setitem__ ( self , item , value ) : <EOL> if isinstance ( item , tuple ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if isinstance ( value , ColorArray ) : <EOL> value = value . rgba <EOL> self . _rgba [ item ] = value <EOL> def extend ( self , colors ) : <EOL> """<STR_LIT>""" <EOL> colors = ColorArray ( colors ) <EOL> self . _rgba = np . vstack ( ( self . _rgba , colors . _rgba ) ) <EOL> return self <EOL> @ property <EOL> def rgba ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _rgba . copy ( ) <EOL> @ rgba . setter <EOL> def rgba ( self , val ) : <EOL> """<STR_LIT>""" <EOL> rgba = _user_to_rgba ( val , expand = False ) <EOL> if self . _rgba is None : <EOL> self . _rgba = rgba <EOL> else : <EOL> self . _rgba [ : , : rgba . shape [ <NUM_LIT:1> ] ] = rgba <EOL> @ property <EOL> def rgb ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _rgba [ : , : <NUM_LIT:3> ] . copy ( ) <EOL> @ rgb . setter <EOL> def rgb ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . rgba = val <EOL> @ property <EOL> def RGBA ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . _rgba * <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> @ RGBA . setter <EOL> def RGBA ( self , val ) : <EOL> """<STR_LIT>""" <EOL> val = np . atleast_1d ( val ) . astype ( np . float32 ) / <NUM_LIT:255> <EOL> self . rgba = val <EOL> @ property <EOL> def RGB ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . round ( self . _rgba [ : , : <NUM_LIT:3> ] * <NUM_LIT:255> ) . astype ( int ) <EOL> @ RGB . setter <EOL> def RGB ( self , val ) : <EOL> """<STR_LIT>""" <EOL> val = np . atleast_1d ( val ) . astype ( np . float32 ) / <NUM_LIT> <EOL> self . rgba = val <EOL> @ property <EOL> def alpha ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _rgba [ : , <NUM_LIT:3> ] <EOL> @ alpha . setter <EOL> def alpha ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . _rgba [ : , <NUM_LIT:3> ] = _array_clip_val ( val ) <EOL> @ property <EOL> def hex ( self ) : <EOL> """<STR_LIT>""" <EOL> return _rgb_to_hex ( self . _rgba ) <EOL> @ hex . setter <EOL> def hex ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . rgba = _hex_to_rgba ( val ) <EOL> @ property <EOL> def hsv ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _hsv <EOL> @ hsv . setter <EOL> def hsv ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . rgba = _hsv_to_rgb ( val ) <EOL> @ property <EOL> def _hsv ( self ) : <EOL> """<STR_LIT>""" <EOL> return _rgb_to_hsv ( self . _rgba [ : , : <NUM_LIT:3> ] ) <EOL> @ property <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _hsv [ : , <NUM_LIT:2> ] <EOL> @ value . setter <EOL> def value ( self , val ) : <EOL> """<STR_LIT>""" <EOL> hsv = self . _hsv <EOL> hsv [ : , <NUM_LIT:2> ] = _array_clip_val ( val ) <EOL> self . rgba = _hsv_to_rgb ( hsv ) <EOL> def lighter ( self , dv = <NUM_LIT:0.1> , copy = True ) : <EOL> """<STR_LIT>""" <EOL> color = self . copy ( ) if copy else self <EOL> color . value += dv <EOL> return color <EOL> def darker ( self , dv = <NUM_LIT:0.1> , copy = True ) : <EOL> """<STR_LIT>""" <EOL> color = self . copy ( ) if copy else self <EOL> color . value -= dv <EOL> return color <EOL> @ property <EOL> def lab ( self ) : <EOL> return _rgb_to_lab ( self . _rgba [ : , : <NUM_LIT:3> ] ) <EOL> @ lab . setter <EOL> def lab ( self , val ) : <EOL> self . rgba = _lab_to_rgb ( val ) <EOL> class Color ( ColorArray ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , color = '<STR_LIT>' , alpha = None , clip = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( color , ( list , tuple ) ) : <EOL> color = np . array ( color , np . float32 ) <EOL> rgba = _user_to_rgba ( color , clip = clip ) <EOL> if rgba . shape [ <NUM_LIT:0> ] != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if alpha is not None : <EOL> rgba [ : , <NUM_LIT:3> ] = alpha <EOL> self . _rgba = None <EOL> self . rgba = rgba . ravel ( ) <EOL> @ ColorArray . rgba . getter <EOL> def rgba ( self ) : <EOL> return super ( Color , self ) . rgba [ <NUM_LIT:0> ] <EOL> @ ColorArray . rgb . getter <EOL> def rgb ( self ) : <EOL> return super ( Color , self ) . rgb [ <NUM_LIT:0> ] <EOL> @ ColorArray . RGBA . getter <EOL> def RGBA ( self ) : <EOL> return super ( Color , self ) . RGBA [ <NUM_LIT:0> ] <EOL> @ ColorArray . RGB . getter <EOL> def RGB ( self ) : <EOL> return super ( Color , self ) . RGB [ <NUM_LIT:0> ] <EOL> @ ColorArray . alpha . getter <EOL> def alpha ( self ) : <EOL> return super ( Color , self ) . alpha [ <NUM_LIT:0> ] <EOL> @ ColorArray . hex . getter <EOL> def hex ( self ) : <EOL> return super ( Color , self ) . hex [ <NUM_LIT:0> ] <EOL> @ ColorArray . hsv . getter <EOL> def hsv ( self ) : <EOL> return super ( Color , self ) . hsv [ <NUM_LIT:0> ] <EOL> @ ColorArray . value . getter <EOL> def value ( self ) : <EOL> return super ( Color , self ) . value [ <NUM_LIT:0> ] <EOL> @ ColorArray . lab . getter <EOL> def lab ( self ) : <EOL> return super ( Color , self ) . lab [ <NUM_LIT:0> ] <EOL> @ property <EOL> def is_blank ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . rgba [ <NUM_LIT:3> ] == <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> nice_str = str ( tuple ( self . _rgba [ <NUM_LIT:0> ] ) ) <EOL> return ( '<STR_LIT>' % ( self . _name ( ) , nice_str ) ) </s>
