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
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
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
42
43 VISIBLE = 64
44 LOW_COLORATION = 4
45 LOW_SATURATION = 192
46 HIGH_SATURATION = 240
47
48
49 if r >= WORD or g >= WORD or b >= WORD:
50 r = int(r / WORD)
51 g = int(g / WORD)
52 b = int(b / WORD)
53
54
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
62
63
64
65
66
67
68
69
70
71
72
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
83 if r > 250:
84 return AEConstants.COLOR_MAP[63]
85 elif r > 200:
86 return AEConstants.COLOR_MAP[41]
87 elif r > 96:
88 return AEConstants.COLOR_MAP[21]
89 else:
90 return AEConstants.COLOR_MAP[42]
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
103 r = r >> 6 << 4
104 g = g >> 6 << 2
105 b = b >> 6
106 rgb = r+g+b
107 name = AEConstants.COLOR_MAP[rgb]
108 return name
109
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
133 tag.append(lang[0])
134 except IndexError:
135 pass
136 try:
137
138 t = lang[1].split('.')[0]
139 except IndexError:
140 pass
141 else:
142
143 if len(tag) == 0:
144 tag.append(t)
145 tag.append(t)
146
147 return '-'.join(tag).lower()
148