Skip to content

Instantly share code, notes, and snippets.

@vfig
Created June 29, 2022 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfig/092d48057606ae2f5361052c3d772d97 to your computer and use it in GitHub Desktop.
Save vfig/092d48057606ae2f5361052c3d772d97 to your computer and use it in GitHub Desktop.
fit 5th degree polynomial to color ramp values
import numpy as np
from numpy.polynomial import Polynomial
from numpy.polynomial.polynomial import polyval
# colorramp rgb values at steps of 1/16 from 0.0 to 1.0:
colors = [
(45,24,40),
(42,77,117),
(25,120,170),
(24,158,205),
(15,190,217),
(48,217,219),
(79,234,213),
(135,243,199),
(179,242,184),
(215,227,171),
(244,199,154),
(254,167,131),
(253,123,94),
(237,79,57),
(210,47,31),
(172,22,12),
(121,9,1),
]
# scale colors to 0.0-1.0 range:
x = [t/16.0 for t in range(17)]
yr = [c[0]/255.0 for c in colors]
yg = [c[1]/255.0 for c in colors]
yb = [c[2]/255.0 for c in colors]
# fit the curve:
degree = 5
pr = Polynomial.fit(x, yr, degree, window=[0,1])
pg = Polynomial.fit(x, yg, degree, window=[0,1])
pb = Polynomial.fit(x, yb, degree, window=[0,1])
# compare the output at the given x positions:
polyval(x,pr.coef)*255.0
polyval(x,pg.coef)*255.0
polyval(x,pb.coef)*255.0
# print the coefficients to use:
pr.coef
pg.coef
pb.coef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment