HEX to RGB Converter

前端 未结 3 711
一整个雨季
一整个雨季 2021-01-19 19:43

I have the JavaScript for converting a HEX value to RGB but I was wondering if I could use jQuery to call the function and insert the HTML?

Here\'s the JavaScript;

3条回答
  •  醉话见心
    2021-01-19 20:17

    valid hex colors can have 3 or 6 characters after the '#'

    function hexToRgb(hex){
        if(/^#([a-f0-9]{3}){1,2}$/.test(hex)){
            if(hex.length== 4){
                hex= '#'+[hex[1], hex[1], hex[2], hex[2], hex[3], hex[3]].join('');
            }
            var c= '0x'+hex.substring(1);
            return 'rgb('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+')';
        }
    }
    

提交回复
热议问题