colours.py

Thu, 26 Aug 2021 19:37:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 19:37:00 +0300
changeset 148
8f621aa4cfd7
parent 147
bec55b021ae7
permissions
-rw-r--r--

Update license year

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):
145
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
18 return str.format('Colour({!r})', self.index)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
19 @property
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
20 def is_direct_colour(self):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
21 return self.index >= 0x2000000
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
22 def __eq__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
23 return self.index == other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
24 def __lt__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
25 return self.index < other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
26 def __le__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
27 return self.index <= other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
28 def __gt__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
29 return self.index > other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
30 def __ge__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
31 return self.index >= other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
32
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
33 def parse_ldconfig_ldr_line(line):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
34 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
35 Parses a single LDConfig.ldr line.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
36
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
37 Returns:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
38 · a dict for a successful parsed colour.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
39 · None for empty lines and comments.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
40
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
41 Raises an error for invalid lines.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
42 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
43 line = line.strip()
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
44 import re
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
45 def parse_tag(tag):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
46 match = re.search(tag + r'\s+([^ ]+)', line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
47 if match:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
48 return match.group(1)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
49 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
50 raise KeyError(str.format('Tag {} not found', tag))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
51 # parse 0 !COLOUR and get the name
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
52 match = re.search(r'^0\s+!COLOUR\s([^ ]+)', line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
53 if not match:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
54 raise ValueError('Bad LDConfig.ldr line: ' + line)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
55 # replace underscores for readability
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
56 name = match.group(1).replace('_', ' ')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
57 # parse tags
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
58 code = int(parse_tag('CODE'))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
59 value = parse_tag('VALUE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
60 edge = parse_tag('EDGE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
61 return {
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
62 'name': name,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
63 'code': code,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
64 'value': value,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
65 'edge': edge,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
66 }
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
67
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
68 def parse_ldconfig_ldr(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
69 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
70 Parses LDConfig.ldr and returns pairs
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
71 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
72 for line in ldconfig_ldr:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
73 line = line.strip()
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
74 if line.startswith('0 !COLOUR'):
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
75 colour = parse_ldconfig_ldr_line(line)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
76 yield (colour['code'], colour)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
77
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
78 def load_colours(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
79 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
80 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter.
145
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
81 Returns a lookup table
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
82 '''
147
bec55b021ae7 Merge commit
Teemu Piippo <teemu@hecknology.net>
parents: 146 145
diff changeset
83 return dict(parse_ldconfig_ldr(ldconfig_ldr))

mercurial