0

Ive got two data points which are hex codes and im trying to find to set up a system to automatically find x amount of colours between the two colors and adding them to a list for later use. My current way of going about it is converting the values to RGB and calculating the difference between the R,G and B values then multiplying the difference by x up to the amount of colours I want but this is not working.

import colormap
import math

limit = 5
upper_color,lower_color = '#ff05ff','#380238'

color_code = []
ur,ug,ub = colormap.hex2rgb(upper_color)
lr,lg,lb = colormap.hex2rgb(lower_color)

for i in range(limit):
  r_dist = ur-lr
  g_dist = ug-lg
  b_dist = ub-lb
  
  color_code.append(colormap.rgb2hex(math.floor(abs((r_dist*(1+i/10)))),
                                     math.floor(abs((g_dist*(1+i/10)))),
                                     math.floor(abs((b_dist*(1+i/10))))))

  • Please provide the error that you are getting. – D.L Jun 17 '21 at 12:59
  • @D.L: It is just unexpected output, not errors. As you see, the math is not correct – Giacomo Catenazzi Jun 17 '21 at 13:03
  • 2
    The problem: math: Try with simple numbers, and you see: you forgot to add the lr, lg, lb to the result. Dist is just the difference part – Giacomo Catenazzi Jun 17 '21 at 13:05
  • 1
    Choosing colors between two endpoints is a trickier problem than it appears, because of the way the eye responds to light and colors. The linear interpolation you're attempting here is simple and often generates acceptable results, but to see some alternatives have a look at [Color gradient algorithm](https://stackoverflow.com/q/22607043/5987). – Mark Ransom Jun 17 '21 at 13:12
  • I actually get this error with `import colormap`... https://stackoverflow.com/questions/43469163/how-to-install-colormap-using-pip-for-spyder-python-3-5-on-windows – D.L Jun 17 '21 at 13:31

0 Answers0