-
Notifications
You must be signed in to change notification settings - Fork 3
/
color_map.py
48 lines (39 loc) · 1.31 KB
/
color_map.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
# use hsv color to represent numbers
# translated from:
# https://stackoverflow.com/questions/46928277/trying-to-convert-integer-range-to-rgb-color
def transitionOfHueRange(percentage, startHue, endHue):
hue = ((percentage * (endHue - startHue)) + startHue) / 360
saturation = 1.0
lightness = 0.5
return hslColorToRgb(hue, saturation, lightness)
def hslColorToRgb(hue, saturation, lightness):
if saturation == 0.0:
# The color is achromatic (has no color)
# Thus use its lightness for a grey-scale color
grey = percToColor(lightness)
return (grey, grey, grey)
if lightness < 0.5:
q = lightness * (1 + saturation)
else:
q = lightness + saturation - lightness * saturation
p = 2 * lightness - q
oneThird = 1.0 / 3
red = percToColor(hueToRgb(p, q, hue + oneThird))
green = percToColor(hueToRgb(p, q, hue))
blue = percToColor(hueToRgb(p, q, hue - oneThird))
return (red, green, blue)
def hueToRgb( p, q, t):
if t < 0:
t += 1
if t > 1:
t -= 1
if t < 1.0 / 6:
return p + (q - p) * 6 * t
if t < 1.0 / 2:
return q
if t < 2.0 / 3:
return p + (q - p) * (2.0 / 3 - t) * 6
return p
def percToColor(percentage):
return percentage
#return round(percentage * 255)