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