colours.py

Thu, 21 Dec 2017 12:27:16 +0200

author
Santeri Piippo
date
Thu, 21 Dec 2017 12:27:16 +0200
changeset 8
303c51137cb2
parent 4
ldraw.py@8eb83f200486
child 11
b42788f5d0a9
permissions
-rw-r--r--

Added ldconfig.ldr support

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:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
77 # failed, check if it's an empty line or comment:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
78 if not line or line.startswith('0'):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
79 return None
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
80 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
81 # failed too, thus the line is bad
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
82 raise ValueError('Bad LDConfig.ldr line')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
83 # replace underscores for readability
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
84 name = match.group(1).replace('_', ' ')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
85 # parse tags
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
86 code = int(parse_tag('CODE'))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
87 value = parse_tag('VALUE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
88 edge = parse_tag('EDGE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
89 return {
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
90 'name': name,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
91 'code': code,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
92 'value': value,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
93 'edge': edge,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
94 }
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
95
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
96 def parse_ldconfig_ldr(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
97 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
98 Parses LDConfig.ldr and returns pairs
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
99 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
100 for line in ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
101 colour = parse_ldconfig_ldr_line(line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
102 if colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
103 yield (colour['code'], colour)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
104
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
105 class colours:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
106 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
107 LDConfig colour namespace, exists for interactive mode and for
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
108 Colour.__repr__ to return something pretty.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
109 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
110 pass
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
111
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
112 # LDConfig lookup tables
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
113 colours_inverse_dict = {}
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
114 ldconfig_colour_data = {}
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
115
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
116 def load_colours(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
117 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
118 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
119 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
120 global ldconfig_colour_data
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
121 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
122 for index, colour in ldconfig_colour_data.items():
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
123 identifier = colour['name'].replace(' ', '_').lower()
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
124 setattr(colours, identifier, Colour(index))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
125 colours_inverse_dict[index] = identifier
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
126
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
127 # Interactive mode support (pass LDConfig.ldr path as a command-line argument)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
128 if __name__ == '__main__':
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
129 from sys import argv
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
130 with open(argv[1]) as ldconfig_ldr:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
131 load_colours(ldconfig_ldr)

mercurial