Eric Wendelin's Blog

Create a Color Palette Using CSS and MooTools 1.2

This entry was authored by highly-respected blogger and friend David Walsh. Learn more about David


As you can see from my site’s lack of design (davidwalsh.name), I’m about 90% programmer and 10% designer. As someone that’s not a designer, I’m really grateful for websites like ColourLovers — websites that provide you palettes of colors that look good together. Let’s pretend for a moment that I do have a good design and I want others to know my palette. MooTools 1.2 has made that a reality.

Step 1: The XHTML

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

I’ve created two elements to begin with: a DIV that will serve as a wrapper for the color palette and a button that will trigger the color palette’s creation.

Step 2: The CSS

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

The above CSS simply formats the elements that are needed to create the palette.

Step 3: The Moo 1.2 Javascript

//once the dom is ready
window.addEvent('domready', function() {

	//do it!
	$('get-colors').addEvent('click', function() {
		//starting the colors array
		var colors = [];
		var disallows = ['transparent'];

		//for every element
		$$('*').each(function(el) {
			//record colors!
			colors.include(el.getStyle('color'));
			colors.include(el.getStyle('background-color'));
			el.getStyle('border-color').split(' ').each(function(c) {
				colors.include(c);
			});
		});

		//sort the colors
		colors.sort();

		//empty wrapper
		$('colors-wrapper').empty();

		//for every color…
		colors.each(function(val,i) {

			//if it's good
			if(!disallows.contains(val))
			{

				//create wrapper div
				var wrapper = new Element('div', {
					'class':'dwrapper'
				});

				//create color div, inject
				var colorer = new Element('div', {
					'class':'dcolor',
					'style': 'background:' + val
				});
				colorer.inject(wrapper);

				//alert(val);

				//create text div, inject
				var texter = new Element('div', {
					'class':'dtext',
					'text':val
				});
				texter.inject(wrapper);

				//inject wrapper
				wrapper.inject($('colors-wrapper'));
			}
		});
	});
});

Once the DOM is ready, we start listening for a click on our “Get Colors” button. When the button is clicked, we store every element’s background color, border color, and text color. Once we have every color in our array, we sort the array and clear the wrapper DIV. For every color we’ve found, we create a DIV that displays the color and it’s hex value. That’s it!

The result? A sweet javascript script giving you a color palette of the current page. Click here to build a color palette of my example page!

Note: This script doesn’t accommodate images.

David WalshDavid Walsh is a Senior Web Developer residing in Madison, WI. He spends most of his day getting a monitor tan coding PHP, CSS, MooTools, and XHTML. When not tinkering with the newest version of Moo, David uses soccer, Rock Band, and movies to dull his coding pains.

Popularity: 8% [?]

19 Responses so far

  1. David Walsh May 15th, 2008 7:29 am

    Thank you for the opportunity Eric!

  2. Eric Wendelin May 15th, 2008 10:18 am

    Of course David! Great stuff!

  3. Mark May 15th, 2008 10:21 am

    Cool. :) Now if you could only query colourlovers and have it suggest similar and contrasting colors.

  4. David Walsh May 15th, 2008 10:28 am

    @Mark: Interesting idea. I’ll keep that in mind.

  5. Mark May 15th, 2008 10:59 am

    Could you “cURL” a site and pull the colors?

  6. Eric Wendelin May 15th, 2008 11:19 am

    @Mark:
    Frankly, you can pull colors from any page using some javascript as above (without MooTools). cURL is probably overkill here but yes it can be done.

  7. Mark May 15th, 2008 11:33 am

    Well, you can’t put javascript on someone else’s site unless downloaded the html and css files to your computer and inserted the Javascript. This method wouldn’t be very automated.

    I was thinking more on the lines like creating a function that goes out and looks at any site and returns with a palette of colors. I was assuming curl would be the best for the “going out and getting” part of the function.

    Am I missing something?

  8. Eric Wendelin May 15th, 2008 11:59 am

    @Mark:
    You’re right that cURL is the best way for automation. You can, of course, use Firebug to execute the javaScript needed if you don’t care about automation.

    Something like that would be very, very cool. :)

  9. David Walsh May 15th, 2008 4:09 pm

    @Eric & Mark: You could cURL it but that would be an extremely difficult process because you don’t get the “true” DOM. You’d have to parse the output to find inline styles. You’d also have to parse the doc to get links to stylesheets and then parse the stylesheets to find colors. I’ve got it on my list to figure out how to get the palette of other websites.

  10. Eric Wendelin May 15th, 2008 4:22 pm

    You’re right, David. Some uber-sweet regex and scripting could probably whip this up. :)

  11. John May 16th, 2008 7:33 am

    If you use Firefox, install the Colorzilla 2 Beta (http://www.iosart.com/firefox/colorzilla/). You can then right click on the button on the bottom left hand side of any page and choose “Webpage DOM Color Analyzer…” and it will show a window with a list of all colors from the current sites DOM.

  12. Eric Wendelin May 16th, 2008 8:20 am

    @John:
    Nice! Actually I tried out Colorzilla 2 Beta and it messed with my other extensions. This was a couple months ago, however, so it may be more stable.

    Thank you very much for the tip!

  13. David Walsh May 16th, 2008 8:52 am

    @John: For an individual’s purposes, that’s great. But if a user doesn’t use Fox, they can’t get your colors. With this system they can.

  14. hairulazami May 26th, 2008 2:06 am

    helo.. i like your website.. simple and so cute :) thanks for the color palet tutorial

  15. mrbiotech July 11th, 2008 9:47 am

    Thanks for sharing!

    BTW: Your site design RAUX!!! Model of elegance!

  16. Eric Wendelin July 11th, 2008 9:50 am

    @hairulazami and mrbiotech:
    Thanks for the compliments. Funny you should mention it because I’m in the process of redesigning it…

Trackbacks

Leave a reply