بسم الله الرحمن الرحيم
------------
أقدم لكم مجهودي المتواضع كود بالجافا سكربت عبارة عن تحويل بين نظامي الألوان RGB و نظام الأوان Hex
مع إظهار اللون الموضح
و يعتبر هذا البرنامج مفيد جدا لمصممي الويب في تحويلاتهم للألوان حيث أن صفحات الويب لا تقبل إلا الشفرة السداسية عشر Hex
كما أنه سهل الاستخدام
**ملحوظة : هذا الكود هو مجهود شخصي خالص ولم ينقل من أي مكان آخر
عسى أن يعجبكم
<html>
<head>
<div align="center">
<h2>Color Converter</h2><h3>A JavaScript Code Programmed By M.A.K.</h3>
</div>
<hr>
<script language="javascript">
<!--
///////////////////////////////////////////////////////////////////////
function hexconverter(num)
{
orgnum=num;
var hexno="";
while (num>16)
{
hexno=tohex(num/16)+hexno;
num=(num-mod_16(num/16))/16;
}
if (num==16)
{
hexno="1"+tohex(num/16)+hexno;
}
else if (num< 16)
{
hexno=tohex(num/16)+hexno;
}
if (orgnum<16){hexno="0"+hexno;}
return hexno;
}
//******************************************************//
function mod_16(x)
{
while (x>1 || x==1)
{
x=x-1;
}
x=x*16;
return x;
}
function tohex(x)
{
var hexchange;
hexchange=mod_16(x);
if (hexchange==10){hexchange="A";}
else if (hexchange==11){hexchange="B";}
else if (hexchange==12){hexchange="C";}
else if (hexchange==13){hexchange="D";}
else if (hexchange==14){hexchange="E";}
else if (hexchange==15){hexchange="F";}
return hexchange;
}
//******************************************************//
//////////////////////////////////////////////////////////////////////
function conv_RGB_H()
{
xr=document.all.frmrgb.r;
xg=document.all.frmrgb.g;
xb=document.all.frmrgb.b;
xh=document.all.frmh.h;
xh.value="#"+hexconverter(xr.value)+hexconverter(xg.value)+hexconverter(xb.value);
var xscolor=document.getElementById("scolor");
xscolor.bgColor=xh.value;
}
//////////////////////////////////////////////////////////////////////
function conv_H_RGB()
{
xr=document.all.frmrgb.r;
xg=document.all.frmrgb.g;
xb=document.all.frmrgb.b;
xh=document.all.frmh.h;
xr.value=todec(1)*16+todec(2);
xg.value=todec(3)*16+todec(4);
xb.value=todec(5)*16+todec(6);
var xscolor=document.getElementById("scolor");
xscolor.bgColor=xh.value;
}
//******************************************************//
function todec(x)
{
if (xh.value.charAt(x)=='A' || xh.value.charAt(x)=='a'){return 10;}
else if (xh.value.charAt(x)=='B' || xh.value.charAt(x)=='b'){return 11;}
else if (xh.value.charAt(x)=='C' || xh.value.charAt(x)=='c'){return 12;}
else if (xh.value.charAt(x)=='D' || xh.value.charAt(x)=='d'){return 13;}
else if (xh.value.charAt(x)=='E' || xh.value.charAt(x)=='e'){return 14;}
else if (xh.value.charAt(x)=='F' || xh.value.charAt(x)=='f'){return 15;}
else {return parseFloat(xh.value.charAt(x));}
}
//////////////////////////////////////////////////////////////////////
//-->
</script>
</head>
<body>
<table align="center" width="80%" border="1" cellpadding="10px" cellspacing="10px" frame="below">
<tr>
<td align="center">
<h3><i>Enter RGB :</i></h3>
<form name="frmrgb" method="post">
<table border="1">
<tr><td> R
<td><input type="text" name="r" value="0" size="20" maxlength="3"
onblur="java script:
tst=document.all.frmrgb.r.value;
if(-1<tst && tst<256)
{}
else
{
window.alert('Enter a valid value for R ( 0 - 255)');
focus(document.all.frmrgb.r);
}"/>
<tr><td> G
<td><input type="text" name="g" value="0" size="20" maxlength="3"
onblur="java script:
tst=document.all.frmrgb.g.value;
if(-1<tst && tst<256)
{}
else
{
window.alert('Enter a valid value for G ( 0 - 255)');
focus(document.all.frmrgb.g);
}"/>
<tr><td> B
<td><input type="text" name="b" value="0" size="20" maxlength="3"
onblur="java script:
tst=document.all.frmrgb.b.value;
if(-1<tst && tst<256)
{}
else
{
window.alert('Enter a valid value for B ( 0 - 255)');
focus(document.all.frmrgb.B);
}"/>
<tr><td colspan="2"><input type="button" name="okrgb" value="Convert to Hex" onclick="conv_RGB_H()"/>
</table>
</form>
<td align="center">
<h3><i>Enter Hex Value :</i></h3>
<form name="frmh" method="post">
<table border="1">
<tr><td> Hex Value
<td><input type="text" name="h" value="#000000" size="20" maxlength="7"
onblur="java script:
tst=document.all.frmh.h.value;
if(tst.length<7)
{
window.alert('Enter a valid hexadecimal color');
focus(document.all.frmh.h);
}
else if(tst.charAt(0)!='#')
{
window.alert('Enter the pond # sign');
focus(document.all.frmh.h);
}
else if(tst.length==7 && tst.charAt(0)=='#')
{
for (i=1;i<8;i++)
{
if (tst.charAt(i)!=0 && tst.charAt(i)!='1' &&
tst.charAt(i)!='2' && tst.charAt(i)!='3' &&
tst.charAt(i)!='4' && tst.charAt(i)!='5' &&
tst.charAt(i)!='6' && tst.charAt(i)!='7' &&
tst.charAt(i)!='8' && tst.charAt(i)!='9' &&
tst.charAt(i)!='A' && tst.charAt(i)!='B' &&
tst.charAt(i)!='C' && tst.charAt(i)!='D' &&
tst.charAt(i)!='E' && tst.charAt(i)!='F' &&
tst.charAt(i)!='a' && tst.charAt(i)!='b' &&
tst.charAt(i)!='c' && tst.charAt(i)!='d' &&
tst.charAt(i)!='e' && tst.charAt(i)!='f')
{
window.alert('Enter a valid hexdecimal number\n\n(0,1,2,3,4,5,6,7,8,9,a[A],b,c[C],d[D],e[E],f[F])');
focus(document.all.frmh.h);
break;
}
}
}
"/>
<tr><td colspan="2"><input type="button" name="okh" value="Convert to RGB" onclick="conv_H_RGB()"/>
</table>
</form>
<tr>
<td id="scolor" colspan="2" align="center" bgcolor="#000000">
</table>
</body>
</html>
لا تنسونا من دعاء صالح..
بانتظار آرائكم !