Calculate color HEX having 2 colors and percent/position

前端 未结 4 495
野性不改
野性不改 2020-12-25 13:52

Is it possible to calculate a color in a middle of a gradient?

var color1 = \'FF0000\';
var color2 = \'00FF00\';

// 50% between the two colors, should retur         


        
4条回答
  •  余生分开走
    2020-12-25 14:31

    I can't comment on the answer above, so I write it here:

    I found out that in the Javascript substring method the to parameter index is not included in the returned string. That means:

    var string = "test";
    //index:      0123
    alert(string.substring(1,3));
    
    //will alert es and NOT est
    

    Edit: So it should be:

    parseInt(color1.substring(0,2), 16);
    parseInt(color1.substring(2,4), 16);
    

    and

    parseInt(color1.substring(4,6), 16);
    

提交回复
热议问题