-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.py
More file actions
84 lines (77 loc) · 3.41 KB
/
Copy pathcolor.py
File metadata and controls
84 lines (77 loc) · 3.41 KB
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
78
79
80
81
82
83
84
from PIL import Image, ImageDraw
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def getColor(bounds, scale, value):
width = bounds[1] - bounds[0]
color = [0,0,0]
for i in range(0, len(scale)-1):
if (value - bounds[0])<=width*scale[i+1][1]:
for j in range(0,3):
color[j] = (value-(bounds[0]+ (scale[i][1])*width)) / (width*(scale[i+1][1] - scale[i][1]))
if (scale[i][0][j] < scale[i+1][0][j]):
color[j] = scale[i][0][j] + (abs(scale[i][0][j] - scale[i+1][0][j]) * color[j])
elif (scale[i][0][j] > scale[i+1][0][j]):
color[j] = scale[i][0][j] - (abs(scale[i][0][j] - scale[i+1][0][j]) * color[j])
else:
color[j] = scale[i][0][j]
return rgb_to_hex((color[0],color[1],color[2]))
def getInd(bounds, scale, value, rnd):
width = bounds[1] - bounds[0]
color = [0,0,0]
rounders = []
n = 12
for j in range(0,n+1):
rounders.append([getColor([0.,float(n)],scale,float(j),0),float(j)/float(n)])
for i in range(0, len(rounders)-1):
#print (value - bounds[0])/width,rounders[i+1][1]
if (value - bounds[0])/width<=rounders[i+1][1]:
return i+1
return len(rounders)
def getRound(bounds, value, n):
width = bounds[1] - bounds[0]
for i in range(0, n):
if (value - bounds[0])/width<=(i+1.)*(1./(n-1.)):
return i+1
return len(rounders)
def oldGetColor(bounds, scale, value, rnd):
width = bounds[1] - bounds[0]
color = [0,0,0]
if (rnd==1):
rounders = []
n = 12
for j in range(0,n+1):
rounders.append([getColor([0.,float(n)],scale,float(j),0),float(j)/float(n)])
for i in range(0, len(rounders)-1):
if (value - bounds[0])/width<=rounders[i+1][1]:
return rounders[i+1][0]
return (scale[-1][0][0],scale[-1][0][1],scale[-1][0][0])
else:
for i in range(0, len(scale)-1):
if (value - bounds[0])<=width*scale[i+1][1]:
for j in range(0,3):
color[j] = (value-(bounds[0]+ (scale[i][1])*width)) / (width*(scale[i+1][1] - scale[i][1]))
if (scale[i][0][j] < scale[i+1][0][j]):
color[j] = scale[i][0][j] + (abs(scale[i][0][j] - scale[i+1][0][j]) * color[j])
elif (scale[i][0][j] > scale[i+1][0][j]):
color[j] = scale[i][0][j] - (abs(scale[i][0][j] - scale[i+1][0][j]) * color[j])
else:
color[j] = scale[i][0][j]
return (color[0],color[1],color[2])
def getScale(bounds,scale,height,width,rnd,rot=0):
plane = Image.new("RGB",(width,height), (0,0,0))
draw = ImageDraw.Draw(plane)
if (rot==0):
for j in range(0,height):
color = oldGetColor(bounds,scale,bounds[0]+((height-j)*(bounds[1]-bounds[0])/height),rnd)
color = tuple(int(x) for x in color)
draw.line([0,j,int(width),j],fill=color)
else:
for j in range(0,width):
color = oldGetColor(bounds,scale,bounds[0]+((width-j)*(bounds[1]-bounds[0])/width),rnd)
color = tuple(int(x) for x in color)
draw.line([j,0,j,int(height)],fill=color)
return plane