-
Notifications
You must be signed in to change notification settings - Fork 0
/
colour.py
77 lines (58 loc) · 2.47 KB
/
colour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import xlwt
class ColourGradient:
def __init__(self, minimum, maximum, interval, book):
self.levels = {}
self.minimum = minimum
self.maximum = maximum
dataRange = maximum - minimum
steps = int(dataRange / interval) + 1
if (steps >= 4):
steps_4 = steps / 4
else:
steps_4 = 1
for i in range(steps):
if (i <= steps_4):
red = 255
elif (i > steps_4 and i <= steps_4 * 2):
red = 255 - (255 / steps_4) * (i - steps_4)
elif (i > steps_4 * 2 and i <= steps_4 * 3):
red = (255 / 2 / steps_4) * (i - steps_4 * 2)
elif i < steps:
red = (255 / 2) - (255 / 2 / steps_4) * (i - steps_4 * 3)
else:
red = 0
if (i <= steps_4):
green = (255 / steps_4) * i
elif (i > steps_4 and i <= steps_4 * 2):
green = 255 - (255 / steps_4) * (i - steps_4)
elif (i > steps_4 * 2 and i <= steps_4 * 3):
green = (255 / steps_4) * (i - steps_4 * 2)
else:
green = 255
if (i <= steps_4):
blue = 0
elif (i > steps_4 and i <= steps_4 * 2):
blue = 0 + (255 / steps_4) * (i - steps_4)
elif i < steps:
blue = 255 - (255 / steps_4 / 2) * (i - steps_4 * 2)
else:
blue = 0
red = abs(red)
green = abs(green)
blue = abs(blue)
if (red > 255): red = 255
if (green > 255): green = 255
if (blue > 255): blue = 255
value = self.roundValue(minimum + i * interval)
excelIndex = 8 + i
colourName = "custom_colour_%d" % excelIndex
xlwt.add_palette_colour(colourName, excelIndex)
book.set_colour_RGB(excelIndex, red, green, blue)
style = xlwt.easyxf('pattern: pattern solid, fore_colour %s' % colourName, num_format_str='0%')
self.levels[value] = (red, green, blue, value, excelIndex, colourName, style)
def roundValue(self, value):
return round(value, 2)
def getStyle(self, value):
value = max(self.minimum, value)
value = min(self.maximum, value)
return self.levels[self.roundValue(value)][6]