<s> from math import e <EOL> class Profile ( object ) : <EOL> def __init__ ( self , color , meanSat , meanHue , meanVal , meanSqSat , meanSqHue , meanSqVal ) : <EOL> self . color = color <EOL> self . meanSat = meanSat <EOL> self . meanHue = meanHue <EOL> self . meanVal = meanVal <EOL> self . meanSqSat = meanSqSat <EOL> self . meanSqHue = meanSqHue <EOL> self . meanSqVal = meanSqVal <EOL> def probability ( self , h , s , v , hOff ) : <EOL> h -= hOff <EOL> hWeight = float ( self . meanSqSat ) / ( max ( self . meanSqHue , <NUM_LIT:1> ) ) <EOL> vWeight = float ( self . meanSqVal ) / ( max ( self . meanSqVal , <NUM_LIT:1> ) ) <EOL> sWeight = <NUM_LIT:1.> <EOL> weightSum = hWeight + sWeight + vWeight <EOL> hWeight = hWeight / weightSum <EOL> sWeight = sWeight / weightSum <EOL> vWeight = vWeight / weightSum <EOL> hWeight = <NUM_LIT:1.> <EOL> sWeight = vWeight = <NUM_LIT:0.> <EOL> devsH = ( ( h - self . meanHue ) ** <NUM_LIT:2> ) / max ( <NUM_LIT:1.> , self . meanSqHue ) <EOL> devsS = ( ( s - self . meanSat ) ** <NUM_LIT:2> ) / max ( <NUM_LIT:1.> , self . meanSqSat ) <EOL> devsV = ( ( v - self . meanVal ) ** <NUM_LIT:2> ) / max ( <NUM_LIT:1.> , self . meanSqVal ) <EOL> prob = <NUM_LIT:0> <EOL> prob += hWeight * ( e ** ( - abs ( devsH ) ) ) <EOL> prob += sWeight * ( e ** ( - abs ( devsS ) ) ) <EOL> prob += vWeight * ( e ** ( - abs ( devsV ) ) ) <EOL> return prob <EOL> class Profiles ( object ) : <EOL> def __init__ ( self ) : <EOL> with open ( '<STR_LIT>' ) as file : <EOL> data = eval ( file . read ( ) ) <EOL> self . colorProfiles = [ ] <EOL> self . hueOffset = <NUM_LIT:0> <EOL> self . rgbOffset = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> for color in data : <EOL> profile = [ ] <EOL> profile . append ( color ) <EOL> sats = [ i [ <NUM_LIT:0> ] for i in data [ color ] ] <EOL> hues = [ i [ <NUM_LIT:1> ] for i in data [ color ] ] <EOL> vals = [ i [ <NUM_LIT:2> ] for i in data [ color ] ] <EOL> meanSat = float ( sum ( sats ) ) / len ( sats ) <EOL> meanHue = float ( sum ( hues ) ) / len ( hues ) <EOL> meanVal = float ( sum ( vals ) ) / len ( vals ) <EOL> sqsSat = [ ( sat - meanSat ) ** <NUM_LIT:2> for sat in sats ] <EOL> meanSqSat = float ( sum ( sqsSat ) ) / len ( sqsSat ) <EOL> sqsHue = [ ( hue - meanHue ) ** <NUM_LIT:2> for hue in hues ] <EOL> meanSqHue = float ( sum ( sqsHue ) ) / len ( sqsHue ) <EOL> sqsVal = [ ( val - meanVal ) ** <NUM_LIT:2> for val in vals ] <EOL> meanSqVal = float ( sum ( sqsVal ) ) / len ( sqsVal ) <EOL> self . colorProfiles . append ( Profile ( color , meanSat , meanHue , meanVal , <EOL> meanSqSat , meanSqHue , meanSqVal ) ) <EOL> def getColor ( self , h , s , v ) : <EOL> maxProb = - <NUM_LIT:1> <EOL> maxProfile = None <EOL> for profile in self . colorProfiles : <EOL> prob = profile . probability ( h , s , v , self . hueOffset ) <EOL> if prob > maxProb : <EOL> maxProfile = profile <EOL> maxProb = prob <EOL> return maxProfile . color <EOL> def colorByHSV ( hue , sat , val ) : <EOL> return profiles . getColor ( hue , sat , val ) <EOL> def colorByRGB ( * args ) : <EOL> if len ( args ) == <NUM_LIT:4> : <EOL> ( rgb , h , s , v ) = args <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> ( rgb , hsv ) = args <EOL> ( h , s , v ) = hsv <EOL> ( blue , green , red ) = rgb <EOL> ( blueOff , greenOff , redOff ) = profiles . rgbOffset <EOL> red += redOff <EOL> green += greenOff <EOL> blue += blueOff <EOL> green = float ( max ( green , <NUM_LIT:1> ) ) <EOL> red = float ( max ( red , <NUM_LIT:1> ) ) <EOL> blue = float ( max ( blue , <NUM_LIT:1> ) ) <EOL> if blue / red > <NUM_LIT:2> and blue / green > <NUM_LIT:2> : <EOL> return '<STR_LIT>' <EOL> elif green / red > <NUM_LIT:2> : <EOL> return '<STR_LIT>' <EOL> if h > <NUM_LIT> or h < <NUM_LIT:6> : <EOL> return '<STR_LIT>' <EOL> elif h < <NUM_LIT:20> and s < <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> elif h < <NUM_LIT:20> : <EOL> return '<STR_LIT>' <EOL> elif h < <NUM_LIT:50> : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> profiles = Profiles ( ) <EOL> def updateWhite ( rgb ) : <EOL> ( red , green , blue ) = rgb <EOL> avg = ( red + green + blue ) / <NUM_LIT:3> <EOL> profiles . rgbOffset = ( avg - red , avg - green , avg - blue ) </s>
