alert(toRGB("#0000FF")); // 输出 rgb(0, 0, 255) alert(toRGB("invalid")); // 输出 invalid alert(toRGB("#G00")); // 输出 #G00
//如果没有太多要求 一行代码足矣。function toRGB(color){return /^#[a-f0-9]{6}$/i.test(color)?"rgb("+color.slice(1).replace(/([a-f0-9]){2}/gi,function($1){return parseInt($1,16)+","}).slice(0,-1)+")":color;}/*判断颜色是否符合#000000~#ffffff【符合】 拼接字符串拼接过程:首先取color第二位进行正则替换。将每两位字符转换为一个十进制数字和一个逗号,再去除末位逗号【不符合】 返回原字符*/
function toRGB(color) {
if (/^#[0-9a-f]{3}$/i.test(color)) {
return color
}
else if (/^#[0-9a-f]{6}$/i.test(color)) {
color = color.slice(1); // 去掉 #
var str = [];
color.replace(/\w{2}/gi, function ($1) { // g 全局
str.push(parseInt($1, 16))
});
return 'rgb(' + str.join(",") + ')'
}
else {
return 'invalid'
}
}
function toRGB(str){ if(str.substring(0,1)=="#"){ return "invalid"; }else if(str.length==4||str.length==7){ if(str.length==4){str=str+str.slice(1);} var n=str.slice(1); var arr=new Array(); arr[0]=parseInt(n.slice(0,2),16); arr[1]=parseInt(n.slice(2,4),16); arr[2]=parseInt(n.slice(4,6),16); var result="("+arr.join()+")"; return result; }else{ return "invalid"; } }
var toRGB = function(str) { var matches, result, len, redColorValue, greenColorValue, blueColorValue; //异常处理 if ((matches = str.match(/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\s*$/)) === null) { return result = "invalid"; } if ((len = matches[1].length) === 3) { return result = matches[0]; }else{ redColorValue = parseInt(matches[1].charAt(0) + matches[1].charAt(1),16); greenColorValue = parseInt(matches[1].charAt(2) + matches[1].charAt(3),16); blueColorValue = parseInt(matches[1].charAt(4) + matches[1].charAt(5),16); return result = "rgb(" + redColorValue +"," + greenColorValue + "," + blueColorValue + ")"; } };
function torgb(color) { if (typeof color != 'string') { return 'invalid'; } var regex = /^\s*#(?:([0-9a-fA-F]{3})|([0-9a-fA-F]{6}))\s*$/ var match = color.match(regex); if (!match) { return "invalid"; } var hex = typeof match[1] == 'undefined' ? match[2] : match[1]; if (!hex) { return 'invalid'; } var r = /.{2}/g; if (hex.length == 3) { r = /.{1}/g; } hex = hex.match(r); var o = []; for (var i = 0; i < hex.length; i++) { o.push(parseInt(hex.length == 3 ? hex[i] + hex[i] : hex[i], 16)); } var os = o.join(', '); os = 'rgb(' + os + ')'; return os; }
var parseColor = function( val ){
var r, g, b;
// 参数为RGB模式时不做进制转换,直接截取字符串即可
if( /rgb/.test(val) ){
var arr = val.match( /\d+/g );
r = parseInt( arr[0] );
g = parseInt( arr[1] );
b = parseInt( arr[2] );
}
// 参数为十六进制时需要做进制转换
else if( /#/.test(val) ){
var len = val.length;
// 非简写模式 #0066cc
if( len === 7 ){
r = parseInt( val.slice(1, 3), 16 );
g = parseInt( val.slice(3, 5), 16 );
b = parseInt( val.slice(5), 16 );
} else if(len==4){
return val;
}
}
else{
return val;
}
return "rgb("+r+","+g+","+b+")";
};
function toRGB(color){
var hh = “”;
var kk = [];
var reg =/^#[0-9a-zA-Z]{3})$/; // 设置正则规则
if(color==”invalid”) return “invalid”;// 如果无效返回
if(reg.test(color)) return color; // 如果是三为数表示,直接返回。
else {
for(var i=0; i
hh = color.substr(i,2);//substr() 从第 i 位开始截取 2 位字符
var cc = parseInt(hh,16);
kk.push(cc);
}
Return(“RGB(“+kk+”)”);
}
}
alert(toRGB("#0000FF")); // 输出 rgb(0, 0, 255)
alert(toRGB("invalid")); // 输出 invalid
alert(toRGB("#G00")); // 输出 #G00