1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import random |
---|
4 | |
---|
5 | class Color(object): |
---|
6 | """Represents a color for the keyboard""" |
---|
7 | def __init__(self, red=None, green=None, blue=None, rgb=None): |
---|
8 | # Expects integer values, no floats |
---|
9 | if rgb is None: |
---|
10 | rgb = (red, green, blue) |
---|
11 | else: |
---|
12 | rgb = tuple(rgb) |
---|
13 | if None in rgb: |
---|
14 | raise Exception("bad value in %r" % rgb) |
---|
15 | for name, value in zip(['red' , 'green', 'blue'], rgb): |
---|
16 | if not 0 <= value < 256: |
---|
17 | raise Exception("Bad value %s for %s" % (value, name)) |
---|
18 | self.rgb = rgb |
---|
19 | |
---|
20 | @classmethod |
---|
21 | def blend(cls, c1, c2, transparency): |
---|
22 | """Given two colors, return a color based on a weighted combination""" |
---|
23 | blended = [a+b for a, b in zip([(1 - transparency) * x for x in c1.rgb], |
---|
24 | [transparency * x for x in c2.rgb])] |
---|
25 | return cls(rgb=blended) |
---|
26 | |
---|
27 | @classmethod |
---|
28 | def average(cls, colors): |
---|
29 | """Return the mean color among the given colors""" |
---|
30 | return cls(rgb=[sum(x)/len(colors) for x in zip(*[c.rgb for c in colors])]) |
---|
31 | |
---|
32 | @classmethod |
---|
33 | def fromstring(cls, hexstring): |
---|
34 | """Convert a 6-digit hex string into a Color object""" |
---|
35 | r = int(hexstring[0:2], 16) |
---|
36 | g = int(hexstring[2:4], 16) |
---|
37 | b = int(hexstring[4:6], 16) |
---|
38 | return cls(r, g, b) |
---|
39 | |
---|
40 | @classmethod |
---|
41 | def random8(cls, exclude=None): |
---|
42 | """Returns a random color from the 8 basic colors, optionally excluding |
---|
43 | a choice. |
---|
44 | """ |
---|
45 | while True: |
---|
46 | candidate = cls(rgb=[random.choice([0,255]) for _n in range(3)]) |
---|
47 | if not exclude or candidate != exclude: |
---|
48 | return candidate |
---|
49 | |
---|
50 | def __str__(self): |
---|
51 | """6-hex-digit representation of the color |
---|
52 | |
---|
53 | This is the representation used by ckb-daemon |
---|
54 | """ |
---|
55 | return '%02x%02x%02x' % self.rgb |
---|
56 | |
---|
57 | def __cmp__(self, other): |
---|
58 | return cmp(self.rgb, other.rgb) |
---|
59 | |
---|
60 | def __eq__(self, other): |
---|
61 | return self.rgb == other.rgb |
---|
62 | |
---|
63 | def __hash__(self): |
---|
64 | return hash(self.rgb) |
---|
65 | |
---|
66 | |
---|
67 | class Colors(object): |
---|
68 | """Pre-defined colors""" |
---|
69 | WHITE = Color.fromstring('ffffff') |
---|
70 | GREY = Color.fromstring('808080') |
---|
71 | DARK_GREY = Color.fromstring('404040') |
---|
72 | BLACK = Color.fromstring('000000') |
---|
73 | |
---|
74 | RED = Color.fromstring('ff0000') |
---|
75 | GREEN = Color.fromstring('00ff00') |
---|
76 | BLUE = Color.fromstring('0000ff') |
---|
77 | PURPLE = Color.fromstring('ff00ff') |
---|
78 | YELLOW = Color.fromstring('ffff00') |
---|
79 | CYAN = Color.fromstring('00ffff') |
---|
80 | DARKCYAN = Color.fromstring('004040') |
---|
81 | ORANGE = Color.fromstring('ff8000') |
---|