704 # uses a singleton for efficiency |
704 # uses a singleton for efficiency |
705 global _namedColors |
705 global _namedColors |
706 if _namedColors is not None: return _namedColors |
706 if _namedColors is not None: return _namedColors |
707 from . import colors |
707 from . import colors |
708 _namedColors = {} |
708 _namedColors = {} |
709 for (name, value) in list(colors.__dict__.items()): |
709 for name, value in colors.__dict__.items(): |
710 if isinstance(value, Color): |
710 if isinstance(value, Color): |
711 _namedColors[name] = value |
711 _namedColors[name] = value |
712 |
712 |
713 return _namedColors |
713 return _namedColors |
714 |
714 |
718 mode=1 return a string description |
718 mode=1 return a string description |
719 mode=2 return (distance, colorName) |
719 mode=2 return (distance, colorName) |
720 ''' |
720 ''' |
721 namedColors = getAllNamedColors() |
721 namedColors = getAllNamedColors() |
722 closest = (10, None, None) #big number, name, color |
722 closest = (10, None, None) #big number, name, color |
723 for (name, color) in list(namedColors.items()): |
723 for name, color in namedColors.items(): |
724 distance = colorDistance(aColor, color) |
724 distance = colorDistance(aColor, color) |
725 if distance < closest[0]: |
725 if distance < closest[0]: |
726 closest = (distance, name, color) |
726 closest = (distance, name, color) |
727 if mode<=1: |
727 if mode<=1: |
728 s = 'best match is %s, distance %0.4f' % (closest[1], closest[0]) |
728 s = 'best match is %s, distance %0.4f' % (closest[1], closest[0]) |
872 UNDEF = [] |
872 UNDEF = [] |
873 progress = 1 |
873 progress = 1 |
874 assigned = {} |
874 assigned = {} |
875 while kw and progress: |
875 while kw and progress: |
876 progress = 0 |
876 progress = 0 |
877 for k, v in list(kw.items()): |
877 for k, v in kw.items(): |
878 if isinstance(v,(tuple,list)): |
878 if isinstance(v,(tuple,list)): |
879 c = list(map(lambda x,UNDEF=UNDEF: toColor(x,UNDEF),v)) |
879 c = list(map(lambda x,UNDEF=UNDEF: toColor(x,UNDEF),v)) |
880 if isinstance(v,tuple): c = tuple(c) |
880 if isinstance(v,tuple): c = tuple(c) |
881 ok = UNDEF not in c |
881 ok = UNDEF not in c |
882 else: |
882 else: |
887 del kw[k] |
887 del kw[k] |
888 progress = 1 |
888 progress = 1 |
889 |
889 |
890 if kw: raise ValueError("Can't convert\n%s" % str(kw)) |
890 if kw: raise ValueError("Can't convert\n%s" % str(kw)) |
891 getAllNamedColors() |
891 getAllNamedColors() |
892 for k, c in list(assigned.items()): |
892 for k, c in assigned.items(): |
893 globals()[k] = c |
893 globals()[k] = c |
894 if isinstance(c,Color): _namedColors[k] = c |
894 if isinstance(c,Color): _namedColors[k] = c |
895 |
895 |
896 def Whiter(c,f): |
896 def Whiter(c,f): |
897 '''given a color combine with white as c*f w*(1-f) 0<=f<=1''' |
897 '''given a color combine with white as c*f w*(1-f) 0<=f<=1''' |