OSDN Git Service

Added a 'colors' module, which contains an 'RGB' type for storing RGB color values...
authorMichael Curran <mick@kulgan.net>
Wed, 15 Sep 2010 06:03:07 +0000 (16:03 +1000)
committerMichael Curran <mick@kulgan.net>
Wed, 15 Sep 2010 06:03:07 +0000 (16:03 +1000)
The known color names are stored in a 'RGBToNames' dictionary, and the color names are translatable.
So far the standard 16 HTML colors have been added to the dictionary.
      FindColorName first looks for an exact match, but if not successful it locates the closest color buy sorting the known colors by their distance from the given color, in the 3 dimensional color space. It also takes the standard human eye color biasing in to account (e.g. humans think that green is brighter than blue). It also caches the color with the found name in the dictionary for quickeraccess next time.

source/colors.py [new file with mode: 0644]

diff --git a/source/colors.py b/source/colors.py
new file mode 100644 (file)
index 0000000..0d87477
--- /dev/null
@@ -0,0 +1,39 @@
+#colors.py\r
+#A part of NonVisual Desktop Access (NVDA)\r
+#Copyright (C) 2006-2008 NVDA Contributors <http://www.nvda-project.org/>\r
+#This file is covered by the GNU General Public License.\r
+#See the file COPYING for more details.\r
+\r
+from collections import namedtuple\r
+import math\r
+\r
+RGB=namedtuple('RGB',('red','green','blue'))\r
+\r
+RGBToNames={\r
+       RGB(0x00,0x00,0x00):_('black'),\r
+       RGB(0x00,0x80,0x00):_('green'),\r
+       RGB(0xc0,0xc0,0xc0):_('light grey'),\r
+       RGB(0x00,0xff,0x00):_('lime'),\r
+       RGB(0x80,0x80,0x80):_('grey'),\r
+       RGB(0x80,0x80,0x00):_('olive'),\r
+       RGB(0xff,0xff,0xff):_('white'),\r
+       RGB(0xff,0xff,0x00):_('yellow'),\r
+       RGB(0x80,0x00,0x00):_('dark red'),\r
+       RGB(0x00,0x00,0xa0):_('navy blue'),\r
+       RGB(0xff,0x00,0x00):_('red'),\r
+       RGB(0x00,0x00,0xff):_('blue'),\r
+       RGB(0x80,0x00,0x80):_('purple'),\r
+       RGB(0x00,0x80,0x80):_('teal'),\r
+       RGB(0xff,0x00,0xff):_('fuchsia'),\r
+       RGB(0x00,0xff,0xff):_('aqua'),\r
+}\r
+\r
+def findColorName(rgb):\r
+       foundName=RGBToNames.get(rgb,None)\r
+       if not foundName:\r
+               closestRGB=sorted(RGBToNames.iterkeys(),key=lambda x: math.sqrt((abs(rgb.red-x.red)*0.3)**2+(abs(rgb.green-x.green)*0.59)**2+(abs(rgb.blue-x.blue)*0.11)**2))[0]\r
+               foundName=RGBToNames[closestRGB]\r
+               RGBToNames[rgb]=foundName\r
+       return foundName\r
+\r
+\r