同事提供的题目(Taobao)
alert(toRGB("#0000FF")); //返回rgb(0,0,255)
alert(toRGB("#00F")); //返回rgb(0,0,255)
alert(toRGB("#FF0000")); //返回rgb(255,0,0)
alert(toRGB("taobao")); //返回rgb(taobao)
alert(toRGB("#G00")); //返回rgb(#G00)
function toRGB(color){
//第有一字符有#号
if(color.indexOf('#')==0){
//判断长度
if(color.length==4){
//取出字母
r = color.match(/([\da-z])/ig);
}else{
//取出字母
r = color.match(/([\da-z]{2})/ig);
}
//返回
return 'rgb('+formatHEX(r) +')';
}else{
return 'rgb('+color+')';
}
}
//转化色彩数组转为10进制
function formatHEX(x){
var html=[];
for(i in x){
html[i] = parseInt(x[i], 16);
if(isNaN(html[i])){
return '#'+x.join('');
}
}
return html.join(',');
}
Tags: color, js, 转化
This entry was posted
on 星期六, 十月 10th, 2009 at 16:10 and is filed under 默认分类.
You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.