Monday, February 4, 2008

The Amish Will Hate These Buttons

I've always heard that the Amish have an aversion to buttons, although it could just be a rumour. A quick check of Wikipedia shows that indeed, some Amish dress codes prohibit the use of buttons on clothing because of their (the buttons, not the Amish) association with the military and their potential for vanity. Well, OK. For those of you Amish who have accidentally stumbled across the least read blog on the planet, please divert your attention by clicking on any of the Google Ads to the right. We are about to get downright vain.

Same Old Song and Dance

I was recently in need of adding some buttons to a web page I was designing. They weren't actually the typical command buttons like cancel or submit. They were acting as special toolbar buttons on my page and needed to be very graphical. They were also going to need to change color to indicate disabled and enabled states. The application is international, so the buttons would have to scale horizontally to accommodate differing text widths.

We've all seen this trick a thousand times, and its been documented to death. Here is a link to a popular article on the subject. I opened Photoshop and began to "draw" the button I had envisioned. It was a rounded rectangle with some Photoshop effects applied to achieve a green gradient background with the obligatory button shading.

normal_button

layers_normal_button

Normally, I would copy this button and change the colors used in the gradient overlay to achieve a button with different colors for the different states of a button; rollover, disabled, and clicked. In the end, I would have four different button images and I would swap them out using a little JavaScript and CSS. (I could also opt to combine the images into a single large image and swap them using only CSS. There are a hundred ways to skin this cat.)

A New Perspective

I soon began to get bored with my options. I mean, how brain dead is it to keep swapping out these static images to respond to user interaction. Taking a cue from the Aqua Look and Feel and the work Kirill has done with the Substance Look and Feel, and the way every game console works, I realized that smooth transitions between button colors simulates the way natural light refracts on a normally translucent is simply cooler. A smooth transition from one color to the next can be achieved in HTML in two ways, AFAIK.
  1. Create two elements, one with the starting color and one with the ending color. Make the ending color element have an opacity of 0 and align it on top of the starting element. In response to mouse events, animate the opacity of the ending color element to achieve the effect.
  2. Using a single element, animate the background color from one color to another.

I think you might guess which approach I took. :-) Animating background colors from one value to another is a simple task in the RGB world.

rgb_transition

Each unique color is comprised of a unique combination of red, green, and blue values ranging from 0 to 255. An example starting color may be bright green with a CSS color setting of rgb(0, 255, 0), and an ending color of bright blue with a color value of rgb(0, 0, 255). In order to animate along the path shown in the graphic above, we perform a linear transition of each individual color component. For each step in the animation, red will range from 0 to 0; green from 255 to 0, and blue from 0 to 255. You can see that half-way through the animation, our color is rgb(0, 128, 128).

Color Cubes - A Peek Under the Covers

It's worth pointing out that the small graphic above is a very simplified 2D view of color space. The path denoted by the arrow doesn't even exist in the RGB color space (although it does exist in the HSB space).

color_cubeThink of the RGB space as a cube with one corner of the cube representing black (0, 0, 0) and the opposite corner is white (255, 255, 255). The three axis emanating from black are the red, blue and green axis. In the diagram below, I've filled in the left side of the cube with the representative color gradient.

green_blue_squareNext is a 2D view of the same side of the color cube. You can see that the path denoted by the line shows the color range that is traversed by our linear transition.

 
Let's see an example. If you press the transition button, the box on the right will animate between green and blue.

Your opinion may differ, but when this transition occurs, I would prefer that the color hues change and the saturation and value not change. This is achievable by converting from RGB to the HSL color model. Then the hue can be transitioned from one color to another without an artificial darkening or lightening step. Of course, the HSL color values will have to be converted back to RGB in order to apply them to the element. In CSS3, HSL is supported as a valid color model, but only Firefox 2 and Safari 3 offer support.

Finishing it Off

Now that we are achieving a smooth transition from one color to another, it is a relatively simple matter to compose a translucent image that sits over a div with a changing background color. The image will allow the background color to shine through with varying opacities. In order to pull this off, the image will have to be a PNG, of course.

It's a bit trickier to create these images in Photoshop which is mostly due to the way effects are applied in the tool. You will find yourself creating a lot of the affects as grayscale gradients on separate layers and varying amounts of opacity.

Here are shots of a portion of the new button with its background color and one without the background. On the translucent image, you can barely make out the subtle shading. It is most helpful to work with a background color to bring out the detail. When you save the image, save it without the background and as a transparent PNG.

new_button_trans The transparent image appears with several layers of gradients and borders. They're subtle, but they are there.

new_button The gradients show up nice against an opaque background.

layers_new_button Here are the Photoshop layers. Notice the Matte layer; this is the solid color in the corners. The matte is necessary for this particular example because the background color would show through otherwise. With a rectangular button, this would not be necessary.

Putting it all Together

Here are a couple examples. I am using the jQuery library to provide the color animation capabilities and event handling, but they can be performed with nearly any library or you can roll your own. Buttons that react to user events with smooth and elegant transitions are not just the realm of Flash and custom toolkits.

Who Needs Flash?

You can also add color transitions on mouse up and mouse down also. Try it out.

Wednesday, January 2, 2008

Sunday Funnies

I've been a big fan of Comic Life from Plasq since I first saw it a couple years ago. It almost made me reinstall Panther on my G4 PowerBook. I like all those kind of tools that let a person without true artistic ability fake it. You know, like Photoshop. I thought it would be cool to allow people to create their own comics, but allow them to embed the finished product on their blog or web page. This could be an image, or even better, a Google Map-type viewer. I also wanted to create the product as a web-based project. After all, that would be about the only way to differentiate the effort from Plasq's fine product. As I look at Plasq's web site today, I discovered that they now have a Windows version of their software. Even more incentive to try to build a web version. Such an undertaking would require some cutting edge Web 2.0 skills (I just can't bring myself to type skilz). The server architecture would have to be very interesting. I'm thinking of a JMS implementation in order to process events from the client side in an asynchronous manner. This would also allow me to leverage comet to improve the responsiveness and scalability of the application. This could be a very fun project. Of course it could get bogged down due to the browser's inability to perform certain tasks. I think I will try a few experiments to act as a proof of concept.
  1. Allow drag and drop for image files external to the web browser. This is the first gotcha that will require an impure solution. Browsers can't do this yet. Probably use a very slim Java plugin or something in Flash. I hate to do it though. If I end up sticking Flash in there, the inclination is to do all the image manipulating in flash also. Before you know it, the whole app is just a Flash app. That's no fun.
  2. Allow the user to right-click on an image and convert it to grayscale. Other image filters can come later.
  3. Demonstrate how an image can be masked. This is important in comics because the image usually doesn't bleed beyond the confines of its cell.
That's probably a good enough start. If I can accomplish those tasks and they work very well and are cross-browser, that may represent 50% of the hard work.

Sunday, December 16, 2007

Netbeans may get me to learn Ruby?

I wouldn't necessarily call them IDE wars, but the competition among IDE vendors is certainly making things easier for us developers. The only difficult thing we face now is choosing amongst them. The newest release on the block is NetBeans 6.0. I have tried various versions of NetBeans over the years, and 6.0 looks like a well-integrated version as opposed to a collection of haphazard plugins. They seem to be firing on all cylinders now with support for the latest frameworks, mobile development, and even non-Java languages like Ruby. They even include excellent Rails support. I can't wait to find a good excuse to try out Ruby on Rails. I plan to use it as a front end to a JPA backend. Check out this video to be amazed at the powerful combination this can achieve. What a nice way to introduce Rails into your Java enterprise. After all, it is all JVM bytecode (at least with JRuby), and while a consulting client may balk at native Ruby code, they may be more receptive to JRuby since it runs on the same platforms they are already using.

Eclipse: Friend or Foe

I have had a love/hate relationship with Eclipse over the years. I began my Java career like most folks; using a text editor (TextPad on Windows for me) and command line compilation. Despite this humble start and the mantra "real programmers use emacs", I was not a text editor bigot. My prior career with Pascal and the advantages of a real IDE like Borland's Delphi was not lost on me. So it came to pass that I began to develop Java on Symantec's Visual Cafe. I think my copy came with a book on the Java language. What was intriguing to me about this IDE was the first integrated debugger I had seen for Java, and it came with its own JVM. The VM allowed you to modify source code for a running application in real time and the changes were immediately reflected in the running application. You could program a red circle to bounce around the screen, and while it was running, you could change the color to blue. Only now, with the pressure of dynamic languages, is this productivity benefit being realized in modern Java environments by products like JavaRebel. My first Java customer was Victoria's Secret in Columbus, Ohio. There IDE of choice: Microsoft Visual Studio J++. Yup, and I loved it. I was developing programs for the Windows platform and I could make calls directly to COM objects. It compiled blazingly fast (for the times), and had a good debugger. My next Java customer was a Borland JBuilder shop, and it was very comforting for me to get back in touch with Borland. At that time, many of the popular IDEs had coallesced, and JBuilder became the top dog in only a couple years. They enjoyed that spot for a number of years until poor marketing, lack of focus, a failed J2EE container, shotgun acquisitions, and a bloated IDE and price point caused it all to crumble away. The advent of IntelliJ IDEA and the newly formed Eclipse Foundation soon crushed Borland where it stood. How could Borland justify $3,000 or more for an IDE when competitors were providing similar benefits for free or very little cost? The answer; they couldn't. Since that time, Borland attempted to sell their IDE products but considered the offers too low. So in 2006, Borland formed a wholly owned subsidiary named CodeGear to market and develop the products. Sometime in 2003, I noticed that my customer was spending about $60K per year on JBuilder licenses, and suggested they switch to IntelliJ IDEA for about $5K per year. After all, we weren't using any proprietary features of the JBuilder suite, and IDEA consisted of a code editor that blew JBuilder out of the water. So, I have been a happy IntelliJ user for many years now. Over the years and between customers, I lacked for a Java IDE that I could use for personal use. I usually got by using a beta version of IntelliJ, however I always stole a peek at the latest version of NetBeans or Eclipse. I had also investigated each of these products as a platform for Rich Client Applications. Still, I don't believe that I ever developed a fondness for either of these "alternative" IDE's as long as IDEA was only a mouse click away. That all changed this month. This month, I am at a customer's site who uses Eclipse. Not only Eclipse, but the Rational Software Architect suite built on Eclipse. RSA is a behemoth. The install on my notebook occupies over 7GB of disk space. Yes, you read that correctly. GIGABYTES! That's almost triple the size of my Windows XP operating system! To be fair, this includes Websphere, but come on. To add insult to injury, a single license of RSA costs almost $10,000. OMG! Despite the size of the install, the Eclipse version is 3.2, and it performs rather well. I don't believe you actually learn an IDE well until you are forced to use it. It took about a week for me to get comfortable with it, although even now I can't say I like it. I think it has all of the functionality that I need, or at least it can have it using plugins. Plugins can be a double-edged sword however. You have to know that you want one, you have to find it, you sometimes have to choose between several, then you have to hope it works well. I have tried the MyEclipse product in the past, and I think I will give it another shot now that I am more comfortable in the Eclipse IDE. It seems to have a nice starter set of plugins. I have noticed however, that even though they list support for some features, the forums tend to reflect that the support is still buggy. MyEclipse is supposed to come with good Websphere support and IBM offers a free ClearCase plugin. Since my current customer is not leveraging any other features of RSA, they could save $9,857 per developer by switching!