Ray's Test Pages - User Changeable Options - Colours

For a while now I've been looking at ways to brighten up the design of the site. In the end I decided that this site will be one of the most user configurable on the Internet. I want the visitor, if they want, to be able to choose their own menu type, background image or colour, and font type and colour. I want them to be able to choose if they want the change in style to apply to the site while they are visiting just for this session or to remain persistent and remain the same each time they visit it.

Another main heading

Colours

Because variables are going to be set and reset we need some defaults to start with. When the page is loaded we need to call the default script colours in the body tag. The body tag now looks like this

<body onload="defaults()">

Another sub heading

The defaults JavaScript function looks like this

<script language="JavaScript">
// *** Set the colour and image defaults ***
function defaults(){
if (imgnum==0) {document.body.style.backgroundImage = "url(trnsprnt.gif)"};
if (BGclr=="") {document.bgColor="#COCOCO"};
}
</script>

What I'd like to do is allow the user to change the text colour, heading text colour, emphasized text (for quotes and code), link colours (active and visited), menu text colour and menu background colour. I'd also like the visitor not to be able to select something like black on black.

I'll try a table first. The background colours can be added programmatically or literally, I'll try literally first

                                           
                                           
                                           
                                           

The cells all have a HTML coding of <td width="4%" bgcolor="#CCFFFF" onclick="ChngClr('CCFFFF')">&nbsp;</td>

The function ChngClr is

<script language="JavaScript">
function ChngClr(clrnum){
document.body.style.backgroundImage = "url(trnsprnt.gif)";
document.bgColor=clrnum;
}
</script>

Notice that the transparent background GIF must be applied for the background colours to work.

This is fine except that every cell must be coded differently. There is some pattern to the codes so it shouldn't be too difficult to create a table, fill the cells and create the onclick link function.

There are a couple of other ways to display these colours. I could use an image map but I don't fancy having to type in all the coordinates. Another way is to use a slider control.

The radio buttons below lets the user decide which particular element is changed. When a colour is clicked on the javascript funtion checks which of the radio buttons is checked.

The function ChngClr now becomes

<script language="JavaScript">
// *** depending on which radio button is checked, change the colour of that element ***
function ChngClr(clrnum){
if (document.clrchk.clrelement[0].checked==true) {
document.body.style.backgroundImage = "url(trnsprnt.gif)";
document.bgColor=clrnum};
if (document.clrchk.clrelement[1].checked==true) {document.fgColor=clrnum};
}
</script>

Now to this programmatically. The JavaScript function creates a table and fills each cell with a colour and makes each of them clickable.

<script language="JavaScript">
// *** Draw a colour table ***
function JSclrtable(){

// Create array to hold the colour hex values
var hexclr = new Array ("00", "33", "66", "99", "CC", "FF");

document.write("<div align='center'>");
document.write("<center>");
document.write("<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='50%' id='AutoNumber4'>");
document.write('<tr>');

for (red=0; red<=5; red++){
document.write('</TR>');
document.write('<tr>');
for (grn=0; grn<=5; grn++){
for (blu=0; blu<=5; blu++){
document.write('<TD BGCOLOR="#' + hexclr[red] + hexclr[grn] + hexclr[blu] + '"');
document.write('onclick="ChngClr(\'' + hexclr[red] + hexclr[grn] + hexclr[blu] + '\')">&nbsp;</td>');
}
}
}
document.write('</tr>');
document.write("</table>");
document.write("</center>");
document.write("</div>");

document.write("<div align='center'>");
document.write("<center>");
document.write("<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='50%' id='AutoNumber4'>");
document.write('<tr>');

for (count=0; count<=5; count++){
document.write('<TD BGCOLOR="#' + hexclr[count] + hexclr[count] + hexclr[count] + '"');
document.write('onclick="ChngClr(\'' + hexclr[count] + hexclr[count] + hexclr[count] + '\')">&nbsp;</td>');
}
document.write('</tr>');
document.write("</table>");
document.write("</center>");
document.write("</div>");
}

Background All Text Main Text Headings Sub Heads Emphasized Text
Menu Text Menu Background Link V Link A Link

The next method I want to try is to create a form that lets the user choose from any of the colours. The three colour buttons Red, Green and Blue sets that colour value to 255 and makes the others go to 0. The hue buttons, White and Black forces all values to FFFFFF or 000000. The buttons marked |< and >| makes that particular colour value go to 0 or 255. By pressing the buttons marked < or > causes a timer to start that increments or decrements the colour value once every half second. The code to do this is pretty long so anyone who is interested will be better off looking at the source code.

The page originally had a table cell to show the changing colours from the above buttons. Unfortunately neither Mozilla nor Netscape like manipulating the background colours of these from Javascript. The table was replaced with a button which can change colour in all browsers.

Suppose the user wants to use a particular colour. I'll give them the opportunity to type in the hex for that number and validate it. This can be done by using the onclick event of a submit button. The JavaScript will take each digit in turn and check that it between 0 and F inclusive and 6 characters long, the maxlength property in the form is set so that only 6 characters can be entered anyway.

Please enter your colour's hex value #

The validation code is

<script language="JavaScript">
// *** check the input hex colour value ***
function valhex(){
var validchars="1234567890ABCDEF";
var userclr=document.gethex.usernum.value;
var txtlen = userclr.length;
var usrerr=0
if (txtlen != 6) {usrerr = 1;}
for (count = 0; count<=6; count++){
    var digit = userclr.charAt(count).toUpperCase();
    if (validchars.indexOf(digit) == -1){usrerr=1;}
}
if (usrerr !=0) {
    alert("The value you entered is not correct");
    document.gethex.usernum.focus();}
else {ChngClr(userclr);}
}
</script>

Using if (document.clrchk.clrelement[3].checked==true) {document.getElementById("Emph").style.color=clrnum;} and then in the body using <H4 ID="Emph"> then only the first instance is found and changed.

If you've any suggestions, help, advice, bits of code, whatever please email me at brisray@yahoo.co.uk

Images - the page where the best of the image selection code ends up

Test Page - where most of the testing is done

Colours - Page 2 - page for testing CSS and other groupings

Other Websites

Webtek Systems - what my site (or anybody else's) must NOT look like

This page created 2nd December 2003, last updated 31st December 2003


GoStats stats counter