<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<head>
<title> 判断两种颜色值是否为相似颜色 </title>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta name="author" content="枫岩,
[email protected]">
<meta name="copyright" content="http://www.cnlei.com" />
<link type="text/css" rel="stylesheet" href="" />
</head>
<body>
<script type="text/javascript">
//Title : 判断两种颜色值是否为相似颜色
//Author : CnLei
//WebSite : http://www.cnlei.com
//真正应用时,请将alert()提示信息注释掉或删除掉
function isSimilarColor(sHexColorA,sHexColorB,nOffset){
this.offsetNum=Math.abs(nOffset);
this.offsetNum>255?this.offsetNum=this.offsetNum-256:"";
var arrNumA=[parseInt(sHexColorA.substring(0,2),16),
parseInt(sHexColorA.substring(2,4),16),
parseInt(sHexColorA.substring(4,6),16)];
var arrNumB=[parseInt(sHexColorB.substring(0,2),16),
parseInt(sHexColorB.substring(2,4),16),
parseInt(sHexColorB.substring(4,6),16)];
for (var i=0;i<arrNumA.length;i++){
if(Math.abs(arrNumA[i]-arrNumB[i])>this.offsetNum){
alert("不相似\n第"+i+"组的差值为:"+(arrNumA[i]-arrNumB[i])+",\n超过了最大误差:"+this.offsetNum);
return false;
};
}
alert("相似色\n"+sHexColorA+"\n"+sHexColorB)
return true;
}
isSimilarColor("FFFFFF","F0FFFE",10);
</script>
</body>
</html>