Package Task :: Package Tools :: Module Utils
[hide private]
[frames] | no frames]

Source Code for Module Task.Tools.Utils

  1  ''' 
  2  Defines L{Tools} that function as a set of utilities. 
  3   
  4  @author: Brett Clippingdale 
  5  @author: Peter Parente 
  6  @organization: IBM Corporation 
  7  @copyright: Copyright (c) 2005, 2007 IBM Corporation 
  8  @license: The BSD License 
  9   
 10  All rights reserved. This program and the accompanying materials are made  
 11  available under the terms of the BSD license which accompanies 
 12  this distribution, and is available at 
 13  U{http://www.opensource.org/licenses/bsd-license.php} 
 14  ''' 
 15   
 16  import Base, AEConstants 
 17   
18 -class Utils(Base.TaskTools):
19 ''' 20 Provides utility methods that assist L{Perk} developers in processing 21 accessibility information, but do not touch accessible objects, do output, 22 handle input, manage the LSR system, etc. 23 '''
24 - def getColorString(self, val):
25 ''' 26 Tries to map a given RGB string (if in form "u,u,u") to a nearby color 27 name. 28 29 @param val: Representation of an RGB color in form "u,u,u" 30 @type val: string 31 @return: Localized color name if val param is valid format, otherwise None 32 @rtype: string 33 ''' 34 # if color value an RGB 3-tuple, map it to a string value 35 try: 36 r, g, b = val.split(',') 37 r, g, b = int(r), int(g), int(b) 38 except (ValueError, TypeError, AttributeError): 39 return val 40 WORD = 256 # 8-bit word size 41 42 VISIBLE = 64 43 LOW_COLORATION = 4 44 LOW_SATURATION = 192 45 HIGH_SATURATION = 240 46 47 # normalize values into three 8-bit values to reprent RGB color 48 if r >= WORD or g >= WORD or b >= WORD: 49 r = int(r / WORD) # can't guarantee apps will have modulo 256 color 50 g = int(g / WORD) 51 b = int(b / WORD) 52 53 # if all values of RGB 3-tuple have low saturation, amplify them 54 while r < VISIBLE and g < VISIBLE and b < VISIBLE and \ 55 (r > LOW_COLORATION or g > LOW_COLORATION or b > LOW_COLORATION): 56 r = r << 1 57 g = g << 1 58 b = b << 1 59 60 # attenuation algo #1 61 ## if all values of RGB 3-tuple have high saturation, attenuate them 62 #while r >= LOW_SATURATION and g >= LOW_SATURATION and b >= LOW_SATURATION and \ 63 #(r < HIGH_SATURATION or g < HIGH_SATURATION or b < HIGH_SATURATION): 64 #print "high saturation: shifting rgb from:", r, g, b 65 #r = r >> 1 66 #g = g >> 1 67 #b = b >> 1 68 #print "shifted rgb to:" , r, g, b 69 70 # attenuation algo #2 71 # if all values of RGB 3-tuple have high saturation, attenuate them 72 if r >= LOW_SATURATION and g >= LOW_SATURATION and b >= LOW_SATURATION and \ 73 (r < HIGH_SATURATION or g < HIGH_SATURATION or b < HIGH_SATURATION): 74 if r < g and r < b: 75 r = r - 64 76 elif g < r and g < b: 77 g = g - 64 78 elif b < r and b < g: 79 b = b - 64 80 elif r == g == b: 81 # assign levels of grey based on saturation 82 if r > 250: 83 return AEConstants.COLOR_MAP[63] #'white' 84 elif r > 200: 85 return AEConstants.COLOR_MAP[41] # light grey 86 elif r > 96: 87 return AEConstants.COLOR_MAP[21] # medium grey 88 else: 89 return AEConstants.COLOR_MAP[42] # dark grey 90 pass 91 elif r == g: 92 r = r - 64 93 g = g - 64 94 elif r == b: 95 r = r - 64 96 b = b - 64 97 elif g == b: 98 g = g - 64 99 b = b - 64 100 101 # place high 2-bits of each color into bitfield: rrggbb for value 0..63 102 r = r >> 6 << 4 103 g = g >> 6 << 2 104 b = b >> 6 105 rgb = r+g+b # add into 6-bit bitfield (rrggbb) for integer value 0..63 106 name = AEConstants.COLOR_MAP[rgb] # get the color value from dictionary 107 return name
108
109 - def convertPOSIXToIANA(self, lang):
110 ''' 111 Converts a POSIX language string to a IANA language tag following RFC 4646 112 U{http://www.ietf.org/rfc/rfc4646.txt}, the format recommended for 113 L{AEOutput} device language settings. 114 115 The POSIX format is language[_territory][.codeset] 116 117 Some examples include "en", "fr_CA", "en_US.UTF-8". Ambiguous conversions 118 such as "zh_CN" are allowed, but do not pinpoint specific spoken dialects 119 (i.e. Mandarin, Cantonese, Pinyin, Taiwanese Mandarin, etc.) The code set 120 is currently ignored, though may serve as a hint for the dialect in the 121 future. 122 123 @param lang: POSIX locale string 124 @type lang: string 125 @return: IANA language tag 126 @rtype: string 127 ''' 128 tag = [] 129 lang = lang.split('_') 130 try: 131 # if first element is there, it's the language code 132 tag.append(lang[0]) 133 except IndexError: 134 pass 135 try: 136 # if the second element is there, it's the territory code 137 t = lang[1].split('.')[0] 138 except IndexError: 139 pass 140 else: 141 # if there was no language code, backfill using the territory 142 if len(tag) == 0: 143 tag.append(t) 144 tag.append(t) 145 # make lowercase for comparison 146 return '-'.join(tag).lower()
147