Dynamicaly generate a Web Safe Color palette with JavaScript

Generating web safe color palette can be easily done using JavaScript. Web safe palette contains 256 colors that we can come up with by mixing some basic colors.

Here is the code

//first lets start with the table that will contain the palette
t='';

//next we define the array of basic colors including black, white and gray
c=new Array('00','CC','33','66','99','FF');

//now we will iterate through colors. Notice how number 6 corresponds to number of items in the array c
for(i=0;i<6;i++){
 for(j=0;j<6;j++){

 // each row will have 6 colors
  t +='';
   for(k=0;k<6;k++){

    //this creates hex code for each color
    l=c[i]+c[j]+c[k];

    //now lets create table cell for each color
    t+='';
   }
  t +='';
 }
}

//now let's display the palette 
document.write(t+'
#'+l+'
');

That's it. Pretty simple!

Here is the example

 

Do you like this or find it useful? Drop me a note or treat me to a double-espresso from my favorite coffee shop.

    Comments

    No Comments

    Add Comment | Contact me