Skip to content
Advertisement

How to create a color picker without a canvas? [closed]

I would like to create a custom color picker without a canvas or JavaScript library. I have found a few websites that use a canvas, but is it possible to do it with just a background image? Here is my code:

JavaScript
JavaScript

Advertisement

Answer

The question uses linear gradients to fill the color picker chart. This gives colors in the x direction (but not the y) and as the gradient is linear we can work out the color which the mouse is over by doing a linear interpolation between the color that starts the section it is in (e.g. red) and the color that ends the section it is in (orange) depending on how far along the section the mouse is.

The components we are going to use are:

JavaScript

Note we are working in rgb because of the choice of a linear 1 dimensional color chart and then converting to hsl and hex so all 3 types of color can be shown.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement