Generate a color scheme for your website with colr.org

A couple of weeks ago, when I was trying to redesign my site, I was trying to get some colors on my webpages since they always have been very minimalist and uncolored. But when it comes to my personal taste it is almost impossible to find colors that I really like. So I was googling around for help and I found colr.org. Although I despise the writing because of its flickrness the website itself is useful and fun to play with. You upload a picture (maybe thats why its so flickry) and the website, well in fact the javascript, generates color schemes out of you pictures. You can let it randomly pick colors from the image or you can pick them by yourself. You can compose whole color schemes and save them with catchy tags like »fresh«, »ocean« or »vagina«. If you search for »fiat« you will find my scheme which I generated from a friends photo which showed an old Fiat at night. There are a lot more sites out there which provide help with your color schemes but non of them worked like colr.org for me.

http://colr.org/

Technorati Tags:
, , , ,

YAJP – Yet Another Javascript Post

I just had to post this because I was trying for ten days to find this article again. You know what I mean, right? You know you found a webpage a week or two ago, you didn’t bookmark it, you’re not even sure if you browsed it with your newsreader, Firefox or something else. You don’t know how you found it and why. Somehow two weeks later you need this webpage again and you have no idea how to get it back. It is not in your browsers history anymore, its not in your delicous account and your search words for google are as general as »web, web 2.0, ajax, javascript, frameworks, browser, compatibility«. Through different combinations google threw finally the right URL on my screen – I had to search for »browser ajax framework compatibility«. Wasn’t too hard in the end but different combinations gave different results. I wasn’t even sure that I found it in the web. Therefore it deserves a trackback – just because I was so exited to find the link again. Maybe I should add it to delicious as well – just in case …

http://www.musingsfrommars.org/

The Javascript Sessions: Personal Experience

When I start learning a programming language, which is not very often, it always helps me to play around with stuff the minute I learned it. Sometimes reading and playing happens at the same time. Understanding the basic concepts is one part but the other one is using them. When I learned Perl I started to program a small address book application , when I learned Actionscript I started to program animations rather than animating them the Flash way. Now with Javascript I programmed something useless – useless for all of you guys but it helped me a lot learning the features of the language. Since I wanted to find out how all this Web 2.0 madness works I tried to play with every aspect of Javascript. My simple project is located here. What is it about? Well on the surface its an input field where you can enter an URL to a favicon.ico file. Once you entered the URL and pressed Return, the favicon of your choice appears in the 20 x 20 matrix below. But of course it is not that simple. When you enter the URL and press Return, the Javascript fires an asynchronous post request to a perl cgi which is checking if the favicon really exists. It is not possible to do that directly in Javascript because of a very useful security policy which says that Javascript is not allowed to connect to foreign domains unless you sign your Javascript file with a Root CA. Javascript can fire post and get requests but when you try that on a foreign domain you’ll get a »permission denied«. Firefox handles this one pretty strict, IE forbids it with the right security setting, Safari however ignores this policy completely. I haven’t checked Opera but from what I read it should give you some sort of warning as well. This one was very confusing since Safari allowed me to fire Get and Post requests to foreign domains. I should probably tell the webkit guys. Anyway, the cgi script is an open proxy which gets an URL and checks if it gets back a 200 HTTP Header. It doesn’t fetch the favicon, it is just looking if everything is fine. After that the cgi connects to a mysql database to look if the URL is already in it, if not it inserts the URL. The cgi gives back different error messages to the Javascript which are taken care of in the XMLHttpRequest callback function. This way the user gets feedback either if the URL is already existing or if it isn’t valid. This all happens in the background without any reloading which is the point of this whole AJAX mambojambo and it works great.

When you load the page there is a timeout of one second before the window.onload event fires a function which calls another cgi to request all existing URLs in the database. The query.pl queries the database and sends back a list to the XMLHttpRequest callback function. The function is splitting up the list of URLs and generates new IMG tags for each URL. This is what it does, but how does it really work?

All the favicons are objects. When the callback function of the initial database query gets back all the URLs and splits them up it creates a new Javascript object for every URL. The list also provides the database ID of the URL so every object has an ID, an URL, and a FaviconURL. The favicon objects also get a prototype function called “show”. This prototype function takes care of the HTML representation of the favicon object. It creates an a element which links to the Object.URL, within the a tag it creates an img tag which has the attribute »src« which links to the Object.FaviconURL, it also sets the id of the img tag to the Object.ID and in the end it attaches a couple of event handlers. The prototype function, in fact, looks like this:

Favicon.prototype.show = function() {
  var faviconContainer = document.createElement("a");
  faviconContainer.setAttribute("href", this.url);

  var favicon = document.createElement("img");
  favicon.setAttribute("id", this.id)
  favicon.setAttribute("src", this.faviconLink);
  favicon.setAttribute("class", "favs");

  favicon.onmouseover = function(oEvent) {
    Tooltip.create(this.id, FaviconObjects[this.id].url, oEvent);
  }

  favicon.onmousemove = function(oEvent) {
    Tooltip.move(oEvent);
  }

  favicon.onmouseout = function() {
    Tooltip.hide();
  }
  faviconContainer.appendChild(favicon);
  document.getElementById("wrapper").appendChild(faviconContainer);
}

The huge advantage is that you don’t have to take care of getting the right element references all the time. Especially when I attach the event handlers for the tooltip this comes in handy. Before I discovered prototype functions I told the tooltip to get the id via DOM from the HTML element which was terribly slow compared to this. And it was much more complicated as well.

Another nice thing is the FaviconObjects object. Here I used this object as an associative array to hold the references between the HTML objects and the Javascript objects. So with the id which is the same of the HTML and the Javascript world I can get instant access to either one of those. The callback function looks like this:

/*Callback Funtion for initial Pageload /getExistingIcons()/ */
function queryCallBack(sData) {
  var splitArray = new Array;
  var existingURLs = sData.split("\n");
  urlCounter = existingURLs.length-1;
  for (p=0; p<existingURLs.length-1; p++) {
    splitArray = existingURLs[p].split("_");
    FaviconObjects[splitArray[0]] = new Favicon(splitArray[0],
    splitArray[1]);
    FaviconObjects[splitArray[0]].show();
  }
}

So if you’re still there you can look at the entire javascript by clicking here. I commented all the functions so you should understand it pretty easily. I rewrote it after I finished all the all the small components to make it as readable and efficient as possible. If you have any annotations to it – let me know.

I learned to love objects, literals and anonymous functions as well as prototyping. This is why I fell in love with Javascript. Well and because most of the perl regexps work too.

Now I’d like you guys to fill the 20 x 20 Matrix with more favicons – during debugging I ended up adding already 124 favicons and in the end it was hard for me to come up with any more. When the matrix is filled I want to implement drag and drop and position coordinates which are also saved in the database. It would be nice to parse standard compliant favicons as well but I haven’t had enough time recently to get that working too. I guess I would have to do that in a cgi as well so this is more a question of Perl coding than Javascript voodoo.

As I said, I would be glad if more experienced Javascript coders would give comments on my code. So long.

// Not everything is escaped properly like backslash n so for the real Javascript check the linked file.

Technorati Tags:
, , ,

The Javascript Sessions: What's not in the books

After introducing some of books, I will now share my online resources. There are a lot of toolkits, frameworks or libraries out there, free to use, with powerful features or a lot less typing for you as benefit. The one that all these toolkits rely on is called »prototype«. It is an AJAX Framework which is providing a lot of useful functions to make your Javascript work a lot easier. One of my problems was to get the correct X and Y coordinates from a mouse event. The corresponding DOM properties would be »clientx« and »clienty« and everything worked fine in Firefox but Safari would show a different behavior. If you scrolled down on a website the clienty value that Safari would give you is the actual clienty + the distance you scrolled down – which made it real ugly to add a tooltip to the mouse cursor unless I had a browser detection … . This is where prototype.js came into play because it is providing stuff like this in neat functions. All I needed to do is adding prototype.js to my site. Of course I had to look for the right function as well but that was it. There was no need for me to write the browser detection which saved me a lot of time (This bug is already in Safaris / Webkit bugtracker). But it can do so much more. You can easily build XMLHttpObjects, get references of XHTML elements and and and … seriously it can do a lot. There is only one really bad thing about it – there is no official documentation. However some other people tried to fix that as good as possible. Jonathan Snook published on his site the very useful prototype.js cheat sheet and then there is a list of articles which cover the usage of prototype.js on prototypedoc.com. If you read the books which I mentioned in my previous post you will probably get around by looking directly into the prototype.js file.

One of the libraries / toolkits that rely on prototype.js is script.aculo.us. The name is weird but this piece of javascript gives you a handful of very pretty animated visual effects. In contrast to prototype scriptaculous is very well documented thanks to the use of a wiki. You can try out all the effects on the demo page. The basic visual effects are filed under »Combination Effects Demo«. Just to give you a tiny example of what you can do click here

Other, similar frameworks / toolkits are rico and dojo. Of course they have some unique distinguishable features but they also cover a few of scriptaculous.

One of the smaller toolkits is called »Lightbox« which provides a nice way of viewing pictures on websites. Instead of opening pop-ups it puts another layer over the website showing the picture. It looks really nice and is easy to implement. Go to the website and check out the demos.

Of course, wikipedia is another good resource to find the meaning of all these fancy web 2.0 related buzzwords.

The next post will cover my personal experience while investigating Javascript.

[Addition]

I just found a blog entry that lists a lot more Javascript frameworks than mine, if you’re interested go take a look.

Technorati Tags:
, , ,

The Javascript Sessions: The Books

Instead of writing one article of enormous length I decided to break it into pieces. It is all about Javascript which is catching my attention for quite a while now. In fact it is the number one reason for the lack of posts in my blog for the last couple of weeks. While I was getting into Javascript I wrote down a lot of stuff which I wanted to mention here in my Blog and I think its the best start to tell you what helped me getting started. First of all I read through three books. I use the web a lot to get the information I’m looking for but when it comes to programming there is nothing better than a good book so here is the book review:

The first book which I read was »Professional Javascript for Web Developers (published by Wrox Press)«. It is everything the title says. It tells you about Javascript in general, DOM Methods, Event Handling, RegExp, Style Manipulation, XMLHttpRequest and much more. No matter what your question about webpages and javascript is, there is a very good chance of getting the answer from this book.

The second book I want to point out is »Javascript: The Definite Guide 4th Edition (published by O’REILLY)« which gives you a very detailed, easy to swallow piece of Javascript. You learn a lot about Object Oriented Programming in Javascript and since everything in Javascript is an Object you should know about it as much as you can. The Book also provides a huge language reference which is pretty helpful too. The book is from 2004 so the DOM / Web part of the book is not really up to date but Oreilly announced the 5th Edition for August 2006 and is very likely that they will include a massive update on the web specific topics. Another plus of this book is Oreillys style of writing. As I said, its really pleasant to read, if you are interested.

Another book which I bought two weeks ago is »DOM Scripting: Web Design with JavaScript and the Document Object Model (published by Friends of ED)«. The Book instantly became a lot of more than positive feedback from all directions. I think it is a very good addition to the previous mentioned books because it takes all the theory and puts it into action. The book provides real examples which the reader can try out in order to play with it by himself. It is also easy to read, not too dry and accurate.

After reading these books you should have a pretty good idea about Javascript and its beauty. You should be a lot more familiar with objects, you will be hungry for prototype functions and anonymous functions and you will even know what »Unobtrusive Javascript« means and why it is good practice.

The next post will be about the online resources.

There you will find out how this stuff works

Technorati Tags:
, , ,