Create Linear Gradient for a given HEX value in Javascript

Viewed 3358

I am using a linear gradient background image to paint a DIV background.

background-image: linear-gradient(to bottom,  #cdeb8f 0%,#a5c955 100%); 

Linear gradient looks good better than a simple background color. Is it possible to create this kind of linear gradients by javascript for a given HEX color?

I mean I want to pass a HEX color value to a function as parameter and it should return me a lighter or darker HEX color so that I can create the above good looking gradient.

Given as above, if I pass

#cdeb8f 

it should return me

#a5c955

I converted above HEX values into Decimals and found that they have a 1.24 ratio. I then used a function to convert passed HEX into Decimal, multiplying or dividing it by 1.24 and convert back to HEX. Return HEX color did not create a meaningful linear gradient with the passed HEX color.

The main problem is to keep the above proportion so that the linear gradient does not look weird.

1 Answers
Related