Ejemplos en Javascript

Editor de colores para el BGcolor

Este script permite elegir un color de fondo que, a diferencia de los ejemplos examinados hasta ahora, se carga en una ventanita independiente. Hay asimismo un conversor de valores hexadecimales.

<HTML>
<HEAD>

<TITLE>Ejemplos Javascript: ejemplo pr???¡ctico </TITLE>


<SCRIPT LANGUAGE="JavaScript">
<!-- Hide Me
////////////////////////////////////////////////////////////////////////////
// JavaScript RGB to Hexidecimal Color Converter --     Version 1.0 -- 4/23/97
//
// -- Convert RGB values to hexidecimal values on the client side --
// Written by Mike Brittain (brittaim@lafayette.edu)
//
//      Copyright (c) Eyeful Web Enterprises, 1997
//      All Rights Reserved.
//     
// *FREEWARE* - This script may be distributed freely (without charge) as long as the above comments remain intact.
//     
// Visit Eyeful Web Enterprises at: http://www.lafayette.edu/~brittaim/
//
////////////////////////////////////////////////////////////////////////////

var bgcolor = "#000000";
var RGB = new Array(256);
var k = 0;
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");

for (i = 0; i < 16; i++) {     
        for (j = 0; j < 16; j++) {             
                RGB[k] = hex[i] + hex[j];
                k++;
        }
}

function dropLeadingZeros(num){
        while (num.charAt(0) == "0") {
                newTerm = num.substring(1, num.length);
                num = newTerm;
        }
        if (num == "")
                num = "0";
        return num;
}

function convert(form){
        while ((form.red.value > 255) || (form.red.value < 0)) {
                alert("All RGB values must be between 0 and 255.")
                form.red.value = 0;
                form.red.focus();
        }
        while ((form.green.value > 255) || (form.green.value < 0)) {
                alert("All RGB values must be between 0 and 255.")
                form.green.value = 0;
                form.green.focus();
        }              
        while ((form.blue.value > 255) || (form.blue.value < 0)) {
                alert("All RGB values must be between 0 and 255.")
                form.blue.value = 0;
                form.blue.focus();
        }
        if (form.red.value == "")
                form.red.value = 0;
        if (form.green.value == "")
                form.green.value = 0;
        if (form.blue.value == "")
                form.blue.value = 0;
        form.red.value = dropLeadingZeros(form.red.value);
        form.green.value = dropLeadingZeros(form.green.value);
        form.blue.value = dropLeadingZeros(form.blue.value);
        rr = RGB[form.red.value]
        gg = RGB[form.green.value]
        bb = RGB[form.blue.value]
        bgcolor = "#" + rr + gg + bb;  
        form.hex.value = bgcolor;
        tinywindow('small');
        colorWindow();
}

function colorWindow(){
        colorwindow.document.bgColor = bgcolor;
        colorwindow.document.write(' ');
        colorwindow.window.focus();
}

var colorwindow = null;
var winopts = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=75,height=75";
function tinywindow(windowname) {
        colorwindow = open("", windowname, winopts);
}
// End Hide -->
</SCRIPT>


</HEAD>

<BODY bgcolor="white" onLoad="tinywindow('small'); colorWindow();">

 <CENTER>
<FORM METHOD=post>
<TABLE WIDTH=500 BORDER=1>
<TR>
<TD ALIGN=center COLSPAN=3>
<FONT SIZE="+2" FACE="arial, helvetica"><B>JavaScript RGB to Hexidecimal Color Converter</B></FONT>
<BR>
<FONT SIZE="-1" FACE="arial, helvetica">(For use with Netscape 3 or 4 only.)</FONT>
</TD>
</TR>
<TR>
<TD ALIGN=right>
<BR><B><FONT COLOR="#808000">R:</FONT></B>
<INPUT TYPE=text NAME="red" SIZE=3 MAXLENGTH=3 onFocus="this.select()">
<BR><B><FONT COLOR="#808000">G:</FONT></B>
<INPUT TYPE=text NAME="green" SIZE=3 MAXLENGTH=3 onFocus="this.select()">
<BR><B><FONT COLOR="#808000">B:</FONT></B> <INPUT TYPE=text NAME="blue" SIZE=3 MAXLENGTH=3 onFocus="this.select()">
</TD>
<TD VALIGN=bottom ALIGN=center>
<INPUT TYPE=button NAME=button1 VALUE="Convert!" onClick="convert(this.form);">
</TD>
<TD WIDTH=300> </TD>
</TR>
<TR>
<TD COLSPAN=2 ALIGN=right>
<HR NOSHADE SIZE=1><B>HEX:</B>
<INPUT TYPE=text NAME="hex" SIZE=12 MAXLENGTH=12 onFocus="this.select()">
</TD>
<TD></TD>
</TR>
</TABLE>
</FORM>
</CENTER>




</body>
</html>

Background y fondo