Color gradient in elixir

Hello everyone,

I am trying to calculate linear gradient in Elixir.

Something like this

def linear_gradient(percentage, start_color, stop_color), do: color

I was initially thinking of transforming colors to rgb, and calculate percentage for each canal, but I am not sure it will work that way.

Is there someone who can point me to how css linear gradient are calculated? I would like to implement something similar in Elixir. The idea is to visually represent percentages.

Thanks for taking time.

1 Like

Hi @kokolegorille, I don’t know if this is what you want (never had do do something similar before), but I found this spec, see if it helps: CSS Images Module Level 3.


Thank You for your answer, I knew it was not as simple as I thought it would be :slight_smile:

I will read the reference and try to convert this to Elixir.

1 Like

I would consider converting the start and end points to the LAB colour space and then doing the interpolation. LAB separates luminance (the L) which you want to be constant from the colour axes (AB) which you can then interpolate.


Thank You for your answer, I will try to convert rgb to lab, then do interpolation as You propose :slight_smile:

1 Like

I don’t know if will help in this case but I’ve found HSL hsl() - CSS: Cascading Style Sheets | MDN to be a nice colour format to manipulate programmatically


Yes, HSV is also good because it’s a perceptual colour space (as is LAB). I suggested LAB because it’s quite simple to interpolate between two points (a1,b1 to a2,b2) keeping L (luminance) constant. But HSL or HSV would be good alternatives.