Color Palette Generator Using jQuery

By  on  

As I continue to learn jQuery, I think it's important that I begin by porting over scripts I've created using MooTools. One of those scripts is my Color Palette Generator script, which debuted on Eric Wendelin's blog. For those of you that missed it, my script analyzes all of the colors on the page (minus images) and builds a palette of colors. Here it is in some jQuery goodness.

The XHTML

<input type="button" id="get-colors" value="Get Colors" class="button" />
<div id="colors-wrapper"></div>

All we need to begin with is the button that triggers the palette generation and a DIV container that will hold all of the DIVs my jQuery creates.

The CSS

.dcolor		{ height:40px; }
.dtext		{  }
.dwrapper	{ width:200px; float:left; padding:10px; margin:0 20px 20px 0; border:1px solid #ccc; }

These CSS classes act as containers for the text DIV I generate and the color-displaying DIV I generate. Those two DIVs are held in one wrapping DIV.

The jQuery JavaScript

/* when the dom is ready */
$(document).ready(function() {
	$('#get-colors').click(function() {  
		
		//my colors array
		var colors = new Array();
		
		//get all elements
		$('*').each(function() {
			if($(this).css('background-color') && $(this).css('background-color') != 'transparent') { colors.push($(this).css('background-color')); }
			if($(this).css('color')) { colors.push($(this).css('color')); }
			if($(this).css('border-color')) { colors.push($(this).css('border-color')); }
		});
		
		//remove dupes and sort
		colors.sort();
		
		//create a color block for all of them
		jQuery.each(colors,function(it,value) {
			
			if(!$('div[rel=\'' + value + '\']').length)
			{
			
				//inject the wrapper
				var wrapper_id = 'w' + it;
				$('<div class="dwrapper" id="' + wrapper_id + '" rel="' + value + '"> </div>').appendTo('#colors-wrapper');
				
				//inject the color div
				$('<div class="dcolor" style="background-color:' + value + '"> </div>').appendTo('#' + wrapper_id);
				
				//inject text div
				$('<div class="text">' + value + '</div>').appendTo('#' + wrapper_id);
			}
			
		});
		
	});
});

When someone clicks the "Get Colors" button, I grab every element in the DOM and collect its color, background-color, and border-color. Once I've cycled through all of the elements, cycle through all of the colors and display them as DIVs inside my colors-wrapper element. You'll note that I utilized the rel attribute to prevent duplicates.

There's the jQuery Color Palette Generator for you!

Recent Features

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

Incredible Demos

  • By
    Check All/None Checkboxes Using MooTools

    There's nothing worse than having to click every checkbox in a list. Why not allow users to click one item and every checkbox becomes checked? Here's how to do just that with MooTools 1.2. The XHTML Note the image with the ucuc ID -- that...

  • By
    HTML5 Datalist

    One of the most used JavaScript widgets over the past decade has been the text box autocomplete widget.  Every JavaScript framework has their own autocomplete widget and many of them have become quite advanced.  Much like the placeholder attribute's introduction to markup, a frequently used...

Discussion

  1. bazztrap

    Can you include this script as part of GreeseMonkey .. that would be great

  2. I just coded up an example in jQuery on how to create div overlays over radio buttons to create a compact, interactive but simple color selector plug-in for jQuery. Feel free to check it out if it would be helpful :)

    http://blarnee.com/wp/jquery-colour-selector-plug-in-with-support-for-graceful-degradation/

  3. chris

    nice work. doesn’t grab the colors out of images included on the page. (this may not be possible with cross-browser javascript; just an observation.)

  4. Would love to see a version of this that converts the rgb results to hex.

  5. bonox

    Nice work, is it possible generate pallete from another web site?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!