I recently created a fun little weekend project (well, an MLK Day project to be more specific). It's a simple survey to query users about what colors come to mind when they think about abstract ideas. It's called What Color Is...? The original motivation behind the site was to come up with an idea for my daughter's science fair project. She sees letters and numbers as colors (something called grapheme-color synesthesia). I came up with the idea to devise a simple test to see what colors, if any, most people see when they think about an abstract idea. From there I naturally began thinking about how it could be turned into a slick online survey. It was an interesting little project that had a number of minor technical challenges along the way.
A Touch of Eye-candy: Colorify jQuery Plugin
For the design of the site, I chose a basic gray jQuery UI theme
(Smoothness). I did this so as to not distract from the main point of
the survey by putting too much color into the surrounding UI. I did
feel like adding a little bit of fun color into the UI though. I
decided that when I would put in some multicolored text in the page
titles. To achieve this, I ended up creating a simple jQuery plugin
that takes any element and assigns a string of random colors to the
letters in the text. I called it "colorify":
(function($){
$.fn.colorify = function(){
var cify = function(s){
var rgb = function(){
var i = Math.floor(Math.random() * 3);
var r = function(n){
return n == i ? 0 : Math.floor(Math.random() * 256);
}
return "rgb(" + r(0) + "," + r(1) + "," + r(2) + ")";
}
var s2 = "";
for (var i = 0; i < s.length; i++) {
s2 += "<span style=\"color:" + rgb() + ";\">" + s.substr(i, 1) + "</span>";
}
return s2;
}
this.html(cify(this.html()));
};
})(jQuery);
This goes through each letter in the text, gets a random RGB
value and assigns it to the letter by wrapping a span tag with a style
parameter around the letter. It's used like any simple jQuery plugin:
$("#wcititle").colorify();
Which creates colored text like this:
What Color Is...?
One interesting thing about this is that my first attempt at the function resulted in some colors that were just too light to show up well on a white background. That's an obvious problem when getting completely random colors. The solution I found was to randomly choose one of the 3 color components (R, G, or B) and set that to 0 (full saturation). This preserves the generation of bright colors without making them too light.
I wouldn't call this a very useful plugin -- I'll probably never use it for another project. But it was kind of fun to write. Check it out in action at What Color Is...?
Posted on January 31, 2011 4:44:37 PM EST by David Hammond