colours.py

Fri, 18 Sep 2020 21:05:55 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 18 Sep 2020 21:05:55 +0300
changeset 131
9814dbbd92cb
parent 11
b42788f5d0a9
child 145
fde18c4d6784
child 146
3555679d276b
permissions
-rw-r--r--

added unknown subfile test

8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
1 #!/usr/bin/env python3
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
2 class Colour:
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
3 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
4 Colour interface. Supports LDConfig colours and direct colours.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
5 For LDConfig colours to work, load_colours must be called first.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
6 '''
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
7 def __init__(self, index):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
8 if not isinstance(index, int):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
9 index = int(index, 0)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
10 self.index = index
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
11 def __str__(self):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
12 if self.is_direct_colour:
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
13 # write direct colours with hex codes
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
14 return '0x%07X' % self.index
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
15 else:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
16 return str(self.index)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
17 def __repr__(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
18 try:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
19 return 'colours.' + colours_inverse_dict[self.index]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
20 except KeyError:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
21 return str.format('Colour({!r})', self.index)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
22 @property
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
23 def is_direct_colour(self):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
24 return self.index >= 0x2000000
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
25 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
26 def is_ldconfig_colour(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
27 return self.index in ldconfig_colour_data
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
28 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
29 def name(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
30 if self.is_ldconfig_colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
31 return ldconfig_colour_data[self.index]['name']
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
32 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
33 return str(self)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
34 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
35 def face_colour(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
36 if self.is_ldconfig_colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
37 return ldconfig_colour_data[self.index]['value']
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
38 elif self.is_direct_colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
39 return '#%06X' % (self.index & 0xffffff)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
40 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
41 return '#000000'
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
42 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
43 def is_valid(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
44 return self.is_ldconfig_colour or self.is_direct_colour
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
45 def __eq__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
46 return self.index == other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
47 def __lt__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
48 return self.index < other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
49 def __le__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
50 return self.index <= other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
51 def __gt__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
52 return self.index > other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
53 def __ge__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
54 return self.index >= other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
55
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
56 def parse_ldconfig_ldr_line(line):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
57 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
58 Parses a single LDConfig.ldr line.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
59
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
60 Returns:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
61 · a dict for a successful parsed colour.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
62 · None for empty lines and comments.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
63
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
64 Raises an error for invalid lines.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
65 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
66 line = line.strip()
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
67 import re
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
68 def parse_tag(tag):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
69 match = re.search(tag + r'\s+([^ ]+)', line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
70 if match:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
71 return match.group(1)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
72 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
73 raise KeyError(str.format('Tag {} not found', tag))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
74 # parse 0 !COLOUR and get the name
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
75 match = re.search(r'^0\s+!COLOUR\s([^ ]+)', line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
76 if not match:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
77 raise ValueError('Bad LDConfig.ldr line: ' + line)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
78 # replace underscores for readability
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
79 name = match.group(1).replace('_', ' ')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
80 # parse tags
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
81 code = int(parse_tag('CODE'))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
82 value = parse_tag('VALUE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
83 edge = parse_tag('EDGE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
84 return {
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
85 'name': name,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
86 'code': code,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
87 'value': value,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
88 'edge': edge,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
89 }
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
90
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
91 def parse_ldconfig_ldr(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
92 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
93 Parses LDConfig.ldr and returns pairs
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
94 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
95 for line in ldconfig_ldr:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
96 line = line.strip()
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
97 if line.startswith('0 !COLOUR'):
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
98 colour = parse_ldconfig_ldr_line(line)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
99 yield (colour['code'], colour)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
100
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
101 class colours:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
102 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
103 LDConfig colour namespace, exists for interactive mode and for
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
104 Colour.__repr__ to return something pretty.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
105 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
106 pass
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
107
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
108 # LDConfig lookup tables
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
109 colours_inverse_dict = {}
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
110 ldconfig_colour_data = {}
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
111
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
112 def load_colours(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
113 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
114 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
115 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
116 global ldconfig_colour_data
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
117 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
118 for index, colour in ldconfig_colour_data.items():
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
119 identifier = colour['name'].replace(' ', '_').lower()
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
120 setattr(colours, identifier, Colour(index))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
121 colours_inverse_dict[index] = identifier
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
122
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
123 # Interactive mode support (pass LDConfig.ldr path as a command-line argument)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
124 if __name__ == '__main__':
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
125 from sys import argv
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
126 with open(argv[1]) as ldconfig_ldr:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
127 load_colours(ldconfig_ldr)

mercurial