<s> """<STR_LIT>""" <EOL> from collections import namedtuple <EOL> import os , re <EOL> ColorInfo = namedtuple ( '<STR_LIT>' , <EOL> '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> Match = namedtuple ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> RGB = namedtuple ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> HSL = namedtuple ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> class NameThatColor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , colorfile = None ) : <EOL> import csv <EOL> self . color_info = [ ] <EOL> data = os . path . dirname ( __file__ ) + '<STR_LIT>' <EOL> reader = csv . reader ( open ( data ) ) <EOL> for hex_val , name in reader : <EOL> red , green , blue = self . rgb ( hex_val . strip ( ) ) <EOL> hue , saturation , lightness = self . hsl ( hex_val . strip ( ) ) <EOL> self . color_info . append ( ColorInfo ( hex_val . strip ( ) , name . strip ( ) , <EOL> red , green , blue , <EOL> hue , saturation , lightness ) ) <EOL> def name ( self , color ) : <EOL> """<STR_LIT>""" <EOL> color = color . upper ( ) <EOL> if not <NUM_LIT:3> < len ( color ) < <NUM_LIT:8> : <EOL> return Match ( "<STR_LIT>" , "<STR_LIT>" , False , color ) <EOL> elif len ( color ) % <NUM_LIT:3> == <NUM_LIT:0> : <EOL> color = "<STR_LIT:#>" + color <EOL> elif len ( color ) == <NUM_LIT:4> : <EOL> color = '<STR_LIT>' . join ( [ '<STR_LIT:#>' , <EOL> color [ <NUM_LIT:1> ] , color [ <NUM_LIT:1> ] , <EOL> color [ <NUM_LIT:2> ] , color [ <NUM_LIT:2> ] , <EOL> color [ <NUM_LIT:3> ] , color [ <NUM_LIT:3> ] ] ) <EOL> red , green , blue = self . rgb ( color ) <EOL> hue , saturation , lightness = self . hsl ( color ) <EOL> ndf1 = <NUM_LIT:0> <EOL> ndf2 = <NUM_LIT:0> <EOL> ndf = <NUM_LIT:0> <EOL> the_color = Match ( None , None , None , None ) <EOL> df = - <NUM_LIT:1> <EOL> for info in self . color_info : <EOL> if color == info . hex_value : <EOL> return Match ( info . hex_value , info . name , True , color ) <EOL> ndf1 = ( ( ( red - info . red ) ** <NUM_LIT:2> ) + <EOL> ( ( green - info . green ) ** <NUM_LIT:2> ) + <EOL> ( ( blue - info . blue ) ** <NUM_LIT:2> ) ) <EOL> ndf2 = ( ( ( hue - info . hue ) ** <NUM_LIT:2> ) + <EOL> ( ( saturation - info . saturation ) ** <NUM_LIT:2> ) + <EOL> ( ( lightness - info . lightness ) ** <NUM_LIT:2> ) ) <EOL> ndf = ndf1 + ndf2 * <NUM_LIT:2> <EOL> if not <NUM_LIT:0> < df < ndf : <EOL> df = ndf <EOL> the_color = info <EOL> if not the_color . name : <EOL> return Match ( "<STR_LIT>" , "<STR_LIT>" , False , color ) <EOL> else : <EOL> return Match ( the_color . hex_value , <EOL> the_color . name , <EOL> False , <EOL> color ) <EOL> def rgb ( self , color ) : <EOL> """<STR_LIT>""" <EOL> return RGB ( int ( color [ <NUM_LIT:1> : <NUM_LIT:3> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:3> : <NUM_LIT:5> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:5> : <NUM_LIT:7> ] , <NUM_LIT:16> ) ) <EOL> def hsl ( self , color ) : <EOL> """<STR_LIT>""" <EOL> red , green , blue = self . rgb ( color ) <EOL> red /= <NUM_LIT> <EOL> green /= <NUM_LIT> <EOL> blue /= <NUM_LIT> <EOL> min_color = min ( red , min ( green , blue ) ) <EOL> max_color = max ( red , max ( green , blue ) ) <EOL> delta = max_color - min_color <EOL> lightness = ( min_color + max_color ) / <NUM_LIT:2> <EOL> saturation = <NUM_LIT:0> <EOL> sat_mod = ( ( <NUM_LIT:2> * lightness ) if lightness < <NUM_LIT:0.5> else ( <NUM_LIT:2> - <NUM_LIT:2> * lightness ) ) <EOL> if <NUM_LIT:0> < lightness < <NUM_LIT:1> : <EOL> saturation = ( delta / sat_mod ) <EOL> hue = <NUM_LIT:0> <EOL> if delta > <NUM_LIT:0> : <EOL> if max_color == red and max_color != green : <EOL> hue += ( green - blue ) / delta <EOL> if max_color == green and max_color != blue : <EOL> hue += ( <NUM_LIT:2> + ( blue - red ) / delta ) <EOL> if max_color == blue and max_color != red : <EOL> hue += ( <NUM_LIT:4> + ( red - green ) / delta ) <EOL> hue /= <NUM_LIT:6> <EOL> return HSL ( int ( hue * <NUM_LIT:255> ) , <EOL> int ( saturation * <NUM_LIT:255> ) , <EOL> int ( lightness * <NUM_LIT:255> ) ) <EOL> def main ( ) : <EOL> import json <EOL> import argparse <EOL> output_choices = { <EOL> '<STR_LIT>' : lambda m : m . hex_value , <EOL> '<STR_LIT>' : lambda m : m . name , <EOL> '<STR_LIT>' : lambda m : m . exact , <EOL> '<STR_LIT>' : lambda m : m . original <EOL> } <EOL> format_choices = { <EOL> '<STR_LIT>' : lambda r : json . dumps ( r ) , <EOL> '<STR_LIT>' : lambda r : r <EOL> } <EOL> parser = argparse . ArgumentParser ( <EOL> description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT:-c>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT:target>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = "<STR_LIT>" , <EOL> nargs = '<STR_LIT:*>' , <EOL> choices = output_choices . keys ( ) , <EOL> default = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> dest = "<STR_LIT>" , <EOL> choices = format_choices . keys ( ) , <EOL> default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> Namer = NameThatColor ( args . colors_file ) <EOL> match = Namer . name ( args . target ) <EOL> result = { } <EOL> for choice in args . output : <EOL> result [ choice ] = output_choices [ choice ] ( match ) <EOL> print format_choices [ args . format ] ( result ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
