<s> import numpy as np <EOL> class ColorMapCreator : <EOL> """<STR_LIT>""" <EOL> Xn , Yn , Zn = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> transM = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> def __init__ ( self , RGB1 , RGB2 , numColors = <NUM_LIT> , divide = <NUM_LIT> , <EOL> method = "<STR_LIT>" , filename = "<STR_LIT>" ) : <EOL> self . numColors = numColors <EOL> assert np . mod ( numColors , <NUM_LIT:2> ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> knownMethods = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert method in knownMethods , "<STR_LIT>" <EOL> if method == knownMethods [ <NUM_LIT:0> ] : <EOL> self . colorMap = self . generateColorMap ( RGB1 , RGB2 , divide ) <EOL> elif method == knownMethods [ <NUM_LIT:1> ] : <EOL> self . colorMap = self . generateColorMapLab ( RGB1 , RGB2 , divide ) <EOL> if filename == "<STR_LIT>" : <EOL> for c in self . colorMap : <EOL> pass <EOL> else : <EOL> with open ( filename , "<STR_LIT:w>" ) as f : <EOL> for c in self . colorMap : <EOL> f . write ( "<STR_LIT>" . format ( c [ <NUM_LIT:0> ] , c [ <NUM_LIT:1> ] , c [ <NUM_LIT:2> ] ) ) <EOL> def rgblinear ( self , RGB ) : <EOL> """<STR_LIT>""" <EOL> RGBlinear = np . zeros ( ( <NUM_LIT:3> , ) ) <EOL> for i , value in enumerate ( RGB ) : <EOL> value = float ( value ) / <NUM_LIT> <EOL> if value > <NUM_LIT> : <EOL> value = ( ( value + <NUM_LIT> ) / <NUM_LIT> ) ** <NUM_LIT> <EOL> else : <EOL> value = value / <NUM_LIT> <EOL> RGBlinear [ i ] = value * <NUM_LIT> <EOL> return RGBlinear <EOL> def sRGB ( self , RGBlinear ) : <EOL> """<STR_LIT>""" <EOL> RGB = np . zeros ( ( <NUM_LIT:3> , ) ) <EOL> for i , value in enumerate ( RGBlinear ) : <EOL> value = float ( value ) / <NUM_LIT> <EOL> if value > <NUM_LIT> : <EOL> value = ( <NUM_LIT> * np . power ( value , <NUM_LIT:1.> / <NUM_LIT> ) ) - <NUM_LIT> <EOL> else : <EOL> value = value * <NUM_LIT> <EOL> RGB [ i ] = round ( value * <NUM_LIT> ) <EOL> return RGB <EOL> def rgb2xyz ( self , RGB ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( self . rgblinear ( RGB ) , self . transM ) <EOL> def xyz2rgb ( self , XYZ ) : <EOL> """<STR_LIT>""" <EOL> return self . sRGB ( np . dot ( XYZ , np . array ( np . matrix ( self . transM ) . I ) ) ) <EOL> def rgb2Lab ( self , RGB ) : <EOL> """<STR_LIT>""" <EOL> X , Y , Z = ( self . rgb2xyz ( RGB ) ) . tolist ( ) <EOL> def f ( x ) : <EOL> limit = <NUM_LIT> <EOL> if x > limit : <EOL> return np . power ( x , <NUM_LIT:1.> / <NUM_LIT> ) <EOL> else : <EOL> return <NUM_LIT> * x + <NUM_LIT> / <NUM_LIT> <EOL> L = <NUM_LIT> * ( f ( Y / self . Yn ) - ( <NUM_LIT> / <NUM_LIT> ) ) <EOL> a = <NUM_LIT> * ( f ( X / self . Xn ) - f ( Y / self . Yn ) ) <EOL> b = <NUM_LIT> * ( f ( Y / self . Yn ) - f ( Z / self . Zn ) ) <EOL> return np . array ( [ L , a , b ] ) <EOL> def Lab2rgb ( self , Lab ) : <EOL> """<STR_LIT>""" <EOL> L , a , b = Lab . tolist ( ) <EOL> def finverse ( x ) : <EOL> xlim = <NUM_LIT> <EOL> a = <NUM_LIT> <EOL> b = <NUM_LIT> / <NUM_LIT> <EOL> ylim = a * xlim + b <EOL> if x > ylim : <EOL> return np . power ( x , <NUM_LIT:3> ) <EOL> else : <EOL> return ( x - b ) / a <EOL> X = self . Xn * finverse ( ( a / <NUM_LIT> ) + ( L + <NUM_LIT> ) / <NUM_LIT> ) <EOL> Y = self . Yn * finverse ( ( L + <NUM_LIT> ) / <NUM_LIT> ) <EOL> Z = self . Zn * finverse ( ( L + <NUM_LIT> ) / <NUM_LIT> - ( b / <NUM_LIT> ) ) <EOL> return self . xyz2rgb ( np . array ( [ X , Y , Z ] ) ) <EOL> def Lab2Msh ( self , Lab ) : <EOL> """<STR_LIT>""" <EOL> L , a , b = Lab . tolist ( ) <EOL> M = np . sqrt ( np . sum ( np . power ( Lab , <NUM_LIT:2> ) ) ) <EOL> s = np . arccos ( L / M ) <EOL> h = np . arctan2 ( b , a ) <EOL> return np . array ( [ M , s , h ] ) <EOL> def Msh2Lab ( self , Msh ) : <EOL> """<STR_LIT>""" <EOL> M , s , h = Msh . tolist ( ) <EOL> L = M * np . cos ( s ) <EOL> a = M * np . sin ( s ) * np . cos ( h ) <EOL> b = M * np . sin ( s ) * np . sin ( h ) <EOL> return np . array ( [ L , a , b ] ) <EOL> def rgb2Msh ( self , RGB ) : <EOL> """<STR_LIT>""" <EOL> return self . Lab2Msh ( self . rgb2Lab ( RGB ) ) <EOL> def Msh2rgb ( self , Msh ) : <EOL> """<STR_LIT>""" <EOL> return self . Lab2rgb ( self . Msh2Lab ( Msh ) ) <EOL> def adjustHue ( self , MshSat , Munsat ) : <EOL> """<STR_LIT>""" <EOL> Msat , ssat , hsat = MshSat . tolist ( ) <EOL> if Msat >= Munsat : <EOL> return hsat <EOL> else : <EOL> hSpin = ssat * np . sqrt ( Munsat ** <NUM_LIT:2> - Msat ** <NUM_LIT:2> ) / ( Msat * np . sin ( ssat ) ) <EOL> if hsat > - np . pi / <NUM_LIT:3> : <EOL> return hsat + hSpin <EOL> else : <EOL> return hsat - hSpin <EOL> def interpolateColor ( self , RGB1 , RGB2 , interp ) : <EOL> """<STR_LIT>""" <EOL> Msh1 = self . rgb2Msh ( RGB1 ) <EOL> M1 , s1 , h1 = Msh1 . tolist ( ) <EOL> Msh2 = self . rgb2Msh ( RGB2 ) <EOL> M2 , s2 , h2 = Msh2 . tolist ( ) <EOL> if ( s1 > <NUM_LIT> ) and ( s2 > <NUM_LIT> ) and ( np . abs ( h1 - h2 ) > np . pi / <NUM_LIT> ) : <EOL> Mmid = max ( [ M1 , M2 , <NUM_LIT> ] ) <EOL> if interp < <NUM_LIT:0.5> : <EOL> M2 = Mmid <EOL> s2 = <NUM_LIT:0.> <EOL> h2 = <NUM_LIT:0.> <EOL> interp = <NUM_LIT:2> * interp <EOL> else : <EOL> M1 = Mmid <EOL> s1 = <NUM_LIT:0.> <EOL> h1 = <NUM_LIT:0.> <EOL> interp = <NUM_LIT:2> * interp - <NUM_LIT:1.> <EOL> if ( s1 < <NUM_LIT> ) and ( s2 > <NUM_LIT> ) : <EOL> h1 = self . adjustHue ( np . array ( [ M2 , s2 , h2 ] ) , M1 ) <EOL> elif ( s2 < <NUM_LIT> ) and ( s1 > <NUM_LIT> ) : <EOL> h2 = self . adjustHue ( np . array ( [ M1 , s1 , h1 ] ) , M2 ) <EOL> MshMid = ( <NUM_LIT:1> - interp ) * np . array ( [ M1 , s1 , h1 ] ) + interp * np . array ( [ M2 , s2 , h2 ] ) <EOL> return self . Msh2rgb ( MshMid ) <EOL> def generateColorMap ( self , RGB1 , RGB2 , divide ) : <EOL> """<STR_LIT>""" <EOL> scalars = np . linspace ( <NUM_LIT:0.> , <NUM_LIT:1.> , self . numColors ) <EOL> RGBs = np . zeros ( ( self . numColors , <NUM_LIT:3> ) ) <EOL> for i , s in enumerate ( scalars ) : <EOL> RGBs [ i , : ] = self . interpolateColor ( RGB1 , RGB2 , s ) <EOL> return RGBs / divide <EOL> def generateColorMapLab ( self , RGB1 , RGB2 , divide ) : <EOL> """<STR_LIT>""" <EOL> Lab1 = self . rgb2Lab ( RGB1 ) <EOL> Lab2 = self . rgb2Lab ( RGB2 ) <EOL> LabWhite = np . array ( [ <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ) <EOL> Lab = np . zeros ( ( self . numColors , <NUM_LIT:3> ) ) <EOL> RGBs = np . zeros ( ( self . numColors , <NUM_LIT:3> ) ) <EOL> N2 = np . floor ( self . numColors / <NUM_LIT> ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> Lab [ <NUM_LIT:0> : N2 + <NUM_LIT:1> , i ] = np . linspace ( Lab1 [ i ] , LabWhite [ i ] , N2 + <NUM_LIT:1> ) <EOL> Lab [ N2 : , i ] = np . linspace ( LabWhite [ i ] , Lab2 [ i ] , N2 + <NUM_LIT:1> ) <EOL> for i , l in enumerate ( Lab ) : <EOL> RGBs [ i , : ] = self . Lab2rgb ( l ) <EOL> return RGBs / divide <EOL> def diverge_map ( RGB1 = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) , RGB2 = np . array ( [ <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ] ) , <EOL> numColors = <NUM_LIT> ) : <EOL> colormap = ColorMapCreator ( RGB1 , RGB2 , numColors = numColors ) <EOL> colormap . colorMap = np . clip ( colormap . colorMap , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> cdict = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> inds = np . linspace ( <NUM_LIT:0.> , <NUM_LIT:1.> , numColors ) <EOL> for ii , ind in enumerate ( inds ) : <EOL> cdict [ '<STR_LIT>' ] . append ( [ ind , colormap . colorMap [ ii , <NUM_LIT:0> ] , <EOL> colormap . colorMap [ ii , <NUM_LIT:0> ] ] ) <EOL> cdict [ '<STR_LIT>' ] . append ( [ ind , colormap . colorMap [ ii , <NUM_LIT:1> ] , <EOL> colormap . colorMap [ ii , <NUM_LIT:1> ] ] ) <EOL> cdict [ '<STR_LIT>' ] . append ( [ ind , colormap . colorMap [ ii , <NUM_LIT:2> ] , <EOL> colormap . colorMap [ ii , <NUM_LIT:2> ] ] ) <EOL> from matplotlib . colors import LinearSegmentedColormap <EOL> mycmap = LinearSegmentedColormap ( '<STR_LIT>' , cdict ) <EOL> return mycmap </s>
