Kip New and improved gradient generator. Now almost as good as Photoshop!

You may recall that the gradients I use on this site (in the background images on the photos pages, and in the header above each comment) are dynamically generated. I made some significant improvements to the code over the holiday weekend, which I will discuss here.

First, the minor things. I’ve added support for If-Modified-Since header, so that no cached image is retrieve and no content is returned to the browser if the user has the image cached. In doing this I learned that PHP’s filectime does not actually return the time the file was created; rather, it returns the last time it was changed. I don’t really understand the difference between this and filemtime (time the file was modified), as they both seem to always return the same time. Oh well.

Next, I added support for 3-character colors (like 000 for black and f00 for red). This just makes sense if you’re used to dealing with them in CSS.

And finally, the big improvement is an implementation of error diffusion.1 Why did I need to do this? In some very gradual gradients, you would see bands of individual colors. Eight bits per channel just isn’t quite enough.  For example, look at this gradient from 0x888888 to 0x444444:

Gradient from 0x888888 to 0x444444, with no dithering

You should be able to see vertical bands somewhere in that gradient. If not, it probably has to do with your monitor’s gamma settings or something. In any case, what I do to prevent this is keep a running sum of how far off each pixel is from its “true” color. When the absolute value of that sum is greater than 1, I adjust the color of the next pixel by one level in the appropriate direction to compensate. This gives a much smoother gradient:

Gradient from 0x888888 to 0x444444, with no dithering

This actually gives better results than Paint.NET and Paint Shop Pro. Here’s a close-up look at what that looks like, with pixel colors exaggerated:

Close-up of gradient with dithering

It’s good, though it’s not quite as good as what you get in Photoshop:

Close-up of gradient generated in Photoshop

Clearly the Photoshop guys are doing some kind of subpixel shading, since there are slightly colored pixels in a monochrome gradient. I guess they know what they’re doing.

Lastly, here is my PHP gradient generator source code, if you want to look under the covers and/or utilize the code.

1 Technically, it’s what I thought error diffusion was. But I read the Wikipedia article on error diffusion when writing this post, and now I’m pretty sure that what I did isn’t actually error diffusion. It’s kind of a similar process that I came up with all on my own.
No Comments | Add Comment
Kip Grayson arrives

Yesterday morning Grayson Matthew Robinson—the handsome little boy shown below—came into our lives:

Stephanie holds Grayson for the first time

The last time I managed to write about Emma’s birth within a few hours, but this time around I guess I’m a little late. Mark my words, someday when Grayson gets rebellious and I ask him, “Son, why did you drop out of school and get all those tattoos and start a Metallica cover band? I mean come on, no one liked Metallica songs even when Metallica performed them, and even before Lars Ulrich had that gender-reassignment surgery. The only people who ever liked Metallica are in their eighties now. It’s just a poor decision.” And he’ll reply “I did it because I wanted to be first. I always got second best, Dad! Look at the day I was born, you couldn’t even be bothered to write a blog post about it! Emma got a blog post. Emma got to pierce her nose. Emma got to go to Harvard.” Then I’ll tell him, “Son, the only reason you didn’t get a blog post that day was because I was so busy falling in love with you.” Then he’ll say “eww gay.” That’s just how teenagers talk.1

All kidding aside, I actually was somewhat concerned that I would never be able to love him as much as I love Emma. But I quickly learned that your love for your children isn’t meted out in discrete quantities from a finite supply. I already feel like I have just as much love for Grayson as I do for Emma. It is very surreal. It seems to be multiplied somehow when I see how much Emma loves her brother too. The video below shows just that, with Emma holding Grayson for the first time.

It has surprised me just how much I can already tell that Grayson has a different personality than Emma. For one thing, he loves being held much more than Emma did. Even as a baby, being held didn’t do much to console Emma. She’s never been much of a snuggler (except when she’s sick). Grayson, on the other hand, is very content to be held. He’s also generally quieter than Emma was at this point, though it remains to be seen if that trend will continue. Emma I’m sure was a little extra fussy because (as you may recall) she was born with a black eye.

In any case, I hope you’ll join me in welcoming Grayson into the world. You can find pictures from his first two hours of life on this very website. I’m sure more will be coming later, but probably not until after we are back home from the hospital.

1 Alternate ending: “Hey, I told people you were born on Facebook!” “What’s Facebook?” “It was this website that everyone sent all their personal information to. It ultimately turned out to be owned by a Russian spy.”
No Comments
RSS feeds: Kip's - Stephanie's - Both