OSDN Git Service

colors.RGB.name: once finding the closest color, do not cache it in the RGBToNames...
authorMichael Curran <mick@kulgan.net>
Fri, 17 Jun 2011 01:11:29 +0000 (11:11 +1000)
committerMichael Curran <mick@kulgan.net>
Fri, 17 Jun 2011 01:11:29 +0000 (11:11 +1000)
source/colors.py
user_docs/en/changes.t2t

index 7846bce..aad43d7 100644 (file)
@@ -66,11 +66,22 @@ class RGB(namedtuple('RGB',('red','green','blue'))):
        @property\r
        def name(self):\r
                foundName=RGBToNames.get(self,None)\r
-               if not foundName:\r
-                       closestRGB=sorted(RGBToNames.iterkeys(),key=lambda x: math.sqrt((abs(self.red-x.red)*0.3)**2+(abs(self.green-x.green)*0.59)**2+(abs(self.blue-x.blue)*0.11)**2))[0]\r
-                       foundName=RGBToNames[closestRGB]\r
-                       RGBToNames[self]=foundName\r
-               return foundName\r
+               if foundName:\r
+                       return foundName\r
+               foundName=RGBToNamesCache.get(self,None)\r
+               if foundName:\r
+                       return foundName\r
+               longestDistance=255.0\r
+               closestName=_("unknown color")\r
+               for possibleRGB,possibleName  in RGBToNames.iteritems():\r
+                       distance=math.sqrt(abs(self.red-possibleRGB.red)**2+abs(self.green-possibleRGB.green)**2+abs(self.blue-possibleRGB.blue)**2)\r
+                       if distance<longestDistance:\r
+                               longestDistance=distance\r
+                               closestName=possibleName\r
+               RGBToNamesCache[self]=closestName\r
+               return closestName\r
+\r
+RGBToNamesCache={}\r
 \r
 RGBToNames={\r
        #Standard 16 HTML 4 colors\r
index b01f9e1..3d34d5d 100644 (file)
@@ -93,7 +93,8 @@ Highlights of this release include major improvements concerning punctuation and
 - NVDA is  now able to read the recipiants in the To, CC and BCC fields in Microsoft Outlook. (#421)\r
 - Fixed the issue in NVDA's Voice Settings dialog where the value of sliders was sometimes not reported when changed. (#1411)\r
 - NVDA no longer fails to announce the new cell when moving in an Excel spreadsheet after cutting and pasting. (#1567)\r
-\r
+- NVDA no longer becomes worse at guessing color names the more colors it announces.\r
+  \r
 \r
 == Changes for Developers ==\r
 - Developer documentation can now be generated using SCons. See readme.txt at the root of the source distribution for details, including associated dependencies.\r