Package AccessEngine :: Package AccessEngineAPI :: Module Utils
[hide private]
[frames] | no frames]

Source Code for Module AccessEngine.AccessEngineAPI.Utils

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