Smallest working QR Code

I’ve been playing a bit with QR code generators, and tried to generated a very small one for this homepage. This is the result:

The file is 21×21 pixels, and has just 178 165 bytes. It works with my cellphone from a distance of about 7cm, and gets you to http://martin.ankerl.com/.

How did I create it?

  • Create very short URL to the link you want to create the QR code for. I’ve used j.mp with the customize link feature, and played a bit with special characters like _ until I got a working link.
  • Create a QR code for this link, with low error code correction. This generator has this option.
  • Open the file with your favourite image editor, cut off the border, resize it by factor 1/4 with resample to get a QR code where each black dot is one pixel wide.
  • Save as .png, without any additional information.
  • use IrfanView’s PNGOUT, and afterwards optipng -o9 to recompress the picture.

Can you make a smaller QR code with working weblink?

Have fun!

Cleverness Considered Harmful

I have just read this nice quote at the stackoverflow question “Why is cleverness considered harmful in programming by some people?“:

Fools ignore complexity; pragmatists suffer it; experts avoid it; geniuses remove it.
— Alan Perlis

Which reminds me of a little code piece I have written recently. I’ve recently tried to implement a small, little parser for a very simple custom data format, in C++. To do this, I have tried several approaches:

1. Boost.Spirit

Since we use Boost in our projects, I have started reading about Boost.Spirit, and took some time to decipher the tutorials which contains code like this:

After half an hour I got annoyed because it simply is too much effort. I don’t care how well thought out the library is and how powerful it is, it is simply unusable. Maybe I am too stupid, but I am sure that even when I manage to understand it enough to write a decent parser, half a year later I can never debug my code again: it’s simply too clever.

2. Coco

I’ve ditched Boost.Spirit, and tried to use Coco. I am unfamiliar with this but have seen a colleague use it, so I gave it a try. I was reading the documentation, which looks nice but has 42 pages and since I am a lazy bastard I stopped right there because I just want to get something working, and quickly.

3. Hand Written Large Switch

I have ditched Cocomo, and started to write my own, very simple code that basically looked like this:

After just 10 minutes I got a minimal parser that worked good enough and was extremly readable and understandable code. Everybody with basic C++ understanding can skim over this code and get it. The number of WTF’s per minute when reading this code is close to zero. I am very happy with this approach, and it really rocks because it is so dead simple.

You can rightfully say that a simple switch is very inflexible, and not extensible. You are right, but who cares? Almost all code that I have seen that was planed ahead for flexibility that you might need, gets too complicated because what you planed ahead for might never be needed; even worse: most of the time you need flexibility that you cannot know in advance, it only becomes apparent when you have something and running and use it for a while.

Final Words

Back to the original quote, based on my experience I would extend it a bit:

Ignorants add complexity; fools ignore complexity; pragmatists suffer it; experts avoid it; geniuses remove it.
— Martin Ankerl

If you find this interesting you might also like consider reading the Three Laws of Software Development.

What do you think?

svn-shortlog — Compact & Beautiful Subversion Changelog

At work we periodically have short developer meetings to discuss what has happened in the last month. To do this, we go through the bugs in our issue tracking system, and the subversion commits in our repository. Unfortunately, getting an overview of the subversion commits was rather cumbersome, and we could not find any efficient tool to do this. Hence, svn-shortlog was born.

This is an attempt to format the subversion log of a one-month period in the following way:

  • Beautiful HTML output.
  • Compact representation of lots of information
  • Usable with a not-so color rich beamer.
  • Fully automatic.

Usage

  1. Install Ruby (both 1.8 or 1.9 should work).
  2. Download svn-shortlog.rb.
  3. Open svn-shortlog.rb with your favourite text editor, and configure the config section according to your needs.
  4. Doubleclick svn-shortlog.rb
  5. Open the generated changelog_....html file with your favourite browser.

Sample Output

Here is a sample output of one month of boost commits into trunk, taken from the public repository. The output is quite information dense, a quick description is in the screenshot:

All commits are structured by user, then by date. Each commit is on one line. You can click each line to see the full information related to a commit.

Issues

Ideas, suggestions, problems? Please post them as a comment here, at the bug tracker.

Credits

This tool is based on the idea from my colleague Christoph Heindl and inspired by Linus’ Kernel shortlog and Gmail.

How to Generate Random Colors Programmatically

Creating random colors is actually more difficult than it seems. The randomness itself is easy, but aesthetically pleasing randomness is more difficult. For a little project at work I needed to automatically generate multiple background colors with the following properties:

  • Text over the colored background should be easily readable
  • Colors should be very distinct
  • The number of required colors is not initially known

Naïve Approach

The first and simplest approach is to create random colors by simply using a random number between [0, 256[ for the R, G, B values. I have created a little Ruby script to generate sample HTML code:

The generated output looks like this:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

As you can see this is quite suboptimal. Some letters are hard to read because the background is too dark (B, Q, S), other colors look very similar (F, R).

Using HSV Color Space

HSV_cylinder_smallLet’s fix the too dark / too bright problem first. A convenient way to do this is to not use the RGB color space, but HSV (Hue, Saturation, Value). Here you get equally bright and colorful colors by using a fixed value for saturation and value, and just modifying the hue.

Based on the description provided by the wikipedia article on conversion from HSV to RGB I have implemented a converter:

Using the generator and fixed values for saturation and value:

returns something like this:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Much better. The text is easily readable, and all colors have a similar brightness. Unfortunately, since we have limited us to less colors now, the difference between the randomly generated colors is even less than in the first approach.

Golden Ratio

Using just rand() to choose different values for hue does not lead to a good use of the whole color spectrum, it simply is too random.

distribution-random

Here I have generated 2, 4, 8, 16, and 32 random values and printed them all on a scale. Its easy to see that some values are very tightly packed together, which we do not want.

Lo and behold, some mathematician has discovered the Golden Ratio more than 2400 years ago. It has lots of interesting properties, but for us only one is interesting:

[…] Furthermore, it is a property of the golden ratio, Φ, that each subsequent hash value divides the interval into which it falls according to the golden ratio!
Bruno R. Preiss, P.Eng.

Using the golden ratio as the spacing, the generated values look like this:

distribution-goldenratio

Much better! The values are very evenly distributed, regardless how many values are used. Also, the algorithm for this is extremely simple. Just add 1/Φ and modulo 1 for each subsequent color.

The final result:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You can see that the first few values are very different, and the difference decreases as more colors are added (Z and E are already quite similar). Anyways, this is good enough for me.

And because it is so beautiful, here are some more colors 😉
s=0.99, v=0.99, s=0.25, h=0.8, and s=0.3, v=0.99

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Have fun!
Martin

How To: Download Any Flash Video with flashrip in Ubuntu

Downloading flash videos in Linux was already not too difficult, but thanks to flashrip, it has gotten very easy. Here is a little demo how it works:



Once installed, you basically use one click to get a video preview and then a prompt with the filename to save the file. The script works by looking into the newest flash files in your /tmp folder, and creates a hardlink to the save destination. When the video has fully loaded, you can close the browser window. The temp file will get deleted, and the linked copy will remain.

Installing flashrip

Open a terminal like gnome-terminal or konsole, and run these commands:

Now all thats left to do is to create a link in your gnome panel for ease of use: Right click the gnome panel, “Add to panel…”, choose “Custom Application Launcher…”. Choose a proper name, and a command like this:

For the command, replace the second parameter with the default location where you want to save the ripped videos (you have to use the full path here!)

I have tested this in Ubuntu, but it should work on any linux where gnome is installed.

Have fun!