Skip to content

Instantly share code, notes, and snippets.

@alexflint
Last active August 29, 2015 14:03
Show Gist options
  • Save alexflint/92747ef026601a75f8b1 to your computer and use it in GitHub Desktop.
Save alexflint/92747ef026601a75f8b1 to your computer and use it in GitHub Desktop.
RED_KEYS = ((0., 0.),
(0.35, 0.),
(0.66, 1.),
(0.89, 1.),
(1., 0.5))
GREEN_KEYS = ((0., 0.),
(0.125, 0.),
(0.375, 1.),
(0.64, 1.),
(0.91, 0.),
(1., 0.))
BLUE_KEYS = ((0., 0.5),
(0.11, 1.),
(0.34, 1.),
(0.65, 0.),
(1., 0.))
def interpolate(x, keys):
assert 0 <= x <= 1
assert keys[0][0] == 0
assert keys[-1][0] == 1
for i in range(len(keys)):
if x < keys[i][0] or i == len(keys)-1:
ax, ay = float(x) - keys[i-1][0], keys[i-1][1]
bx, by = keys[i][0] - float(x), keys[i][1]
return (bx*ay + ax*by) / (ax+bx)
def get_color_normalized(x):
red = interpolate(x, RED_KEYS)
green = interpolate(x, GREEN_KEYS)
blue = interpolate(x, BLUE_KEYS)
return (red, green, blue)
def clamp(x, xmin, xmax):
return min(xmax, max(xmin, x))
def get_color(x, xmin, xmax):
x = float(clamp(x, xmin, xmax))
return get_color_normalized((x - xmin) / (xmax - xmin))
def get_color_for_rss(rss):
return get_color(rss, -100, -30)
if __name__ == '__main__':
print get_color_for_rss(-110)
print get_color_for_rss(-100)
print get_color_for_rss(-34)
print get_color_for_rss(-30)
print get_color_for_rss(-20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment