How to get color value from gradient by percentage with javascript?

后端 未结 5 1128
离开以前
离开以前 2020-12-08 10:54

I have a fixed width div with gradient applied using css. I want to build slider based color picker based on this gradient.

When i drag the slider i calculate the pe

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 11:37

    There is a nice lib that handles variety of color manipulations chroma.js

    yarn add chroma-js
    

    And then

    import chroma from 'chroma-js';
    
    const f = chroma.scale(['yellow', 'red', 'black']);
    
    console.log( f(0.25).toString() );                    // #ff8000
    console.log( f(0.5).css('rgba') );                    // rgba(255,0,0,1)
    console.log( f(0.75).css() );                         // rgb(128,0,0)
    console.log( f(1).css() );                            // rgb(0,0,0)
    

提交回复
热议问题