colours.py

changeset 145
fde18c4d6784
parent 11
b42788f5d0a9
child 147
bec55b021ae7
equal deleted inserted replaced
144:eb4c767522ac 145:fde18c4d6784
13 # write direct colours with hex codes 13 # write direct colours with hex codes
14 return '0x%07X' % self.index 14 return '0x%07X' % self.index
15 else: 15 else:
16 return str(self.index) 16 return str(self.index)
17 def __repr__(self): 17 def __repr__(self):
18 try: 18 return str.format('Colour({!r})', self.index)
19 return 'colours.' + colours_inverse_dict[self.index]
20 except KeyError:
21 return str.format('Colour({!r})', self.index)
22 @property 19 @property
23 def is_direct_colour(self): 20 def is_direct_colour(self):
24 return self.index >= 0x2000000 21 return self.index >= 0x2000000
25 @property
26 def is_ldconfig_colour(self):
27 return self.index in ldconfig_colour_data
28 @property
29 def name(self):
30 if self.is_ldconfig_colour:
31 return ldconfig_colour_data[self.index]['name']
32 else:
33 return str(self)
34 @property
35 def face_colour(self):
36 if self.is_ldconfig_colour:
37 return ldconfig_colour_data[self.index]['value']
38 elif self.is_direct_colour:
39 return '#%06X' % (self.index & 0xffffff)
40 else:
41 return '#000000'
42 @property
43 def is_valid(self):
44 return self.is_ldconfig_colour or self.is_direct_colour
45 def __eq__(self, other): 22 def __eq__(self, other):
46 return self.index == other.index 23 return self.index == other.index
47 def __lt__(self, other): 24 def __lt__(self, other):
48 return self.index < other.index 25 return self.index < other.index
49 def __le__(self, other): 26 def __le__(self, other):
96 line = line.strip() 73 line = line.strip()
97 if line.startswith('0 !COLOUR'): 74 if line.startswith('0 !COLOUR'):
98 colour = parse_ldconfig_ldr_line(line) 75 colour = parse_ldconfig_ldr_line(line)
99 yield (colour['code'], colour) 76 yield (colour['code'], colour)
100 77
101 class colours:
102 '''
103 LDConfig colour namespace, exists for interactive mode and for
104 Colour.__repr__ to return something pretty.
105 '''
106 pass
107
108 # LDConfig lookup tables
109 colours_inverse_dict = {}
110 ldconfig_colour_data = {}
111 78
112 def load_colours(ldconfig_ldr): 79 def load_colours(ldconfig_ldr):
113 ''' 80 '''
114 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter. 81 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter.
82 Returns a lookup table
115 ''' 83 '''
116 global ldconfig_colour_data
117 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr)) 84 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr))
118 for index, colour in ldconfig_colour_data.items(): 85 for index, colour in ldconfig_colour_data.items():
119 identifier = colour['name'].replace(' ', '_').lower() 86 identifier = colour['name'].replace(' ', '_').lower()
120 setattr(colours, identifier, Colour(index)) 87 return ldconfig_colour_data
121 colours_inverse_dict[index] = identifier
122 88
123 # Interactive mode support (pass LDConfig.ldr path as a command-line argument) 89 # Interactive mode support (pass LDConfig.ldr path as a command-line argument)
124 if __name__ == '__main__': 90 if __name__ == '__main__':
125 from sys import argv 91 from sys import argv
126 with open(argv[1]) as ldconfig_ldr: 92 with open(argv[1]) as ldconfig_ldr:

mercurial