本文介绍了将十六进制转换为RGBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的小提琴 - jsbin/pitu/1/edit
我想尝试一个简单的hex到rgba转换。我曾经使用渲染颜色使用rgb作为默认,所以当使用farbtastic颜色选择器我通过抓取十六进制值生成的背景颜色(默认为rgb =简单转换)将十六进制值转换为rgb
I wanted to try an easy hex to rgba conversion. Ever browser I've used renders colors using rgb as default so when using the farbtastic color picker I'm converting the hex value to rgb by grabbing the background color the hex value generates (as rgb by default = simple conversion)
我试图将)符号替换为,1)没有工作,所以我去看看如何转换rgb到rgba将工作,我仍然有麻烦。
I tried replacing the ) symbol to , 1), but that didn't work so I went to just see how converting rgb to rgba would work, and I'm still having trouble.
jquery
$('.torgb').val($('#color').css('background-color')); $('.torgba').val().replace(/rgb/g,"rgba");目标
The goal
推荐答案//If you write your own code, remember hex color shortcuts (eg., #fff, #000) function hexToRgbA(hex){ var c; if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){ c= hex.substring(1).split(''); if(c.length== 3){ c= [c[0], c[0], c[1], c[1], c[2], c[2]]; } c= '0x'+c.join(''); return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)'; } throw new Error('Bad Hex'); } hexToRgbA('#fbafff') /* returned value: (String) rgba(251,175,255,1) */
更多推荐
将十六进制转换为RGBA
发布评论