Programming in Color (fixed)

Todays programming pathology is programs as art.

Start with a really simple stack based language, add in a crazy way of encoding instructions using color, and you end up with a masterpiece of beautiful insanity. It's not too exciting from a purely computational point of view, but the programs are really great to look at. Yes, it's a pathological language with truly beautiful source code!

*(The original version of this post had some trouble because I linked to the original images in-place,
which the owner of the Piet webpage had blocked. I didn't realize he didn't want links. I've since downloaded
the images, coverted them to jpegs, and posted them here. I initially thought that the problem with the images was formats, which is what I originally said in this explanation. It's not the image format, but the linking; but converting the files to jpeg and uploading them removed the links that caused the problem.)*

Here's the complete list of
instructions (without control flow):

1. **Push**: Push a value onto the stack.
2. **Pop**: discard the top value from the stack.
3. **Add**: Take the top two values off of the stack, add them, and push the result.
4. **Subtract**: like add, but subtract the values.
5. **Multiply**: like add, but multiply.
6. **Divide**: like add, but divide.
7. **Mod**: like add, by modulo.
8. **Not**: pop the top value off the stack; if it's 0, push 1, otherwise push 0.
9. **Greater**: pop the top two values off the stack, push 1 if the second value on the
stack is greater than the top, otherwise, push 0.
10. **Roll**: pop the top two values of the stack. Call the top one "count", and the second
one "depth". Now, take the top "depth" set of values on the stack, and rotate them by taking
the value on top, and putting it on the bottom. Do the rotation count times.
11. **In**: read a value from input and push it on the stack.
12. **Out**: pop a value from the stack and print it.

So far, seems pretty trivial, right? Standard stack based language, nothing unusual.

Ok. So, now let's start getting weird. We're looking at a two dimensional language, so the control flow operators are all based on *direction*. There are two things that control the flow of the program: the direction of motion, and the direction of selection. The direction of motion describes where the pointer goes on a step; the direction of selection determines where it looks if it needs more information than it has under the instruction pointer. So the control flow operations are:

1. **Switch**: pop the top stack value *n*, and switch the selection direction *n* times.
2. **Rotate**: pop the top stack value *n*, and rotate the motion direction 90 degrees clockwise
*n* times.

Ok, getting a bit weird now... But still, overall, pretty mundane. This isn't a particularly
interesting model of computation, nothing exciting about it. Certainly nothing do beat, say,
Snusp for the beauty of 2d programming.

Now... Here's where it gets *really* interesting. How are instructions encoded? By *color*. The program source is a PNG image!

Yes, todays language is called "[Piet][piet]", in honor of Piet Mondrian, a modern abstract artist, because programs are intended to look as much like a painting by Mondrian as possible. There are twenty colors in Piet that have semantic significance, given in the table below:

#FFC0C0
light red
#FFFFC0
light yellow
#C0FFC0
light green
#C0FFFF
light cyan
#C0C0FF
light blue
#FFC0FF
light magenta
#FF0000
red
#FFFF00
yellow
#00FF00
green
#00FFFF
cyan
#0000FF
blue
#FF00FF
magenta
#C00000
dark red
#C0C000
dark yellow
#00C000
dark green
#00C0C0
dark cyan
#0000C0
dark blue
#C000C0
dark magenta
#FFFFFF white #000000 black

Most commands are given by the color transition between the pixel under the instruction pointer, and the pixel next to it in the selection direction. You'll definitely need to keep that table handy to see how the instructions work.

So, let's look at how we encode instructions. We take the current pixel, and the pixel next to it in the selection direction, and consider their *difference* in the two dimensions given in the color table: hue, and darkness:

* **Add**: +1 hue; no change in lightness.
* **Divide**: +2 hue; no change in lightness.
* **Greater**: +3 hue; no change in lightness.
* **Dup**: +4 hue; no change in lightness.
* **In** (input character): +5 hue; no change in lightness.
* **Push**: No change in hue, +1 darkness.
* **Subtract**: +1 hue, +1 darkness.
* **Mod**: +2 hue, +1 darkness.
* **Rotate**: +3 hue, +1 darkness.
* **Roll**: +4 hue, +1 darkness.
* **Out** (output number): +5 hue, +1 darkness.
* **Pop**: No change in hue, +2 darkness.
* **Multiply**: +1 hue, +2 darkness.
* **Not**: +2 hue, +2 darkness.
* **Switch**: +3 hue, +2 darkness.
* **In** (input number): +4 hue, +2 darkness.
* **Out** (output character): +5 hue, +2 darkness.

Numbers in Piet come from the *size* of the color blocks. The number of pixels in the block correspond to a number. So, for example, a block of 6 light yellow pixels followed by a yellow pixel will push the number 6 onto a stack.

Black and white are special colors - white is a separator; white pixels are just skipped over as if they weren't there. Black pixels are blocks; if the program hits a black pixel, it will try switching the selection directions (ignoring white pixels); if that is also black, it will try rotating the motion direction +90 degrees. If it can't find any non-black pixels using an sequence of alternating SWITCH/ROTATE instructions, the program will halt.

So now, finally, you can look at a few nifty examples of [Piet programs][programs]. To make them easier to read, the Piet programs are scaled up, so the pixels are actually represented by largish squares. As usual, we'll start with "Hello World":

i-d5c69738accfbc52c9ae791915e7354d-Piet_hello_big.jpg

That *is* hello world. Each of the large color blocks is one of the characters; the narrow lines are stack manipulations and output statements; and the little black arrowhead toward the top left captures the pointer and forces the program to halt.

Moving on, here's a much fancier "Hello world":
i-32186be14a3d879b98276ab3d5609453-hw3-5.jpg

One that really meets the goal of looking like a Mondrian painting, this program prints
out "Piet":

i-7fa13d814a7c19d41d26945f2dd5980c-Piet-4.jpg

My personal favorite Piet program; this one prints out the alphabet; and the author provided
an [annotated version][annote] of it to help you understand how it works! Here's the program:

i-454241ff5d911f198702f5593be9d38a-alpha_filled_big.jpg

One last brilliant Piet program, which even the guy who *designed* Piet can't figure out... It's the towers of Hanoi in Piet!

i-b42ac95db44d57d13424e8747c70e341-hanoibig.jpg

[annote]: http://www.dangermouse.net/esoteric/piet/alpha_filled_trace.png
[programs]: http://www.dangermouse.net/esoteric/piet/samples.html
[piet]: http://www.dangermouse.net/esoteric/piet.html

More like this

I thought that it would be fun to stick with the "stack-based" theme of last week's pathological post, but this time, to pick an utterly pointlessly twisted stack based language, but one that would be appreciated by the mascot of one of my fellow ScienceBlogs. Orac, this one's for you! :-) Our…
Todays bit of programming insanity is a bit of a novelty: it's an object-oriented programming language called Glass, with an interpreter available here. So far in all of my Friday Pathological Programming columns, I haven't written about a single object-oriented language. But Glass is something…
Today's a mighty cool example of bizzare language design, called GammaPlex In terms of language design, it's nothing particularly special: it's yet another stack language with a befunge-like graphical syntax. What's unusual about GammaPlex is that it's strongly focused on graphics. It's got built…
In light of the recent posts and discussions about multidimensional numbers,today's pathological language is Recurse, a two-dimensional language - like Befunge, sort of. But I find it more interesting in its own peculiar little way. It's actually a function-oriented two-dimensional language where…

programs as art? you're not too far from reviewing Shakespeare (programs as poetry), are you? :)

By Joe Shelby (not verified) on 03 Nov 2006 #permalink

Hmm. I'm not getting any of the Piet pictures. I can see the color table, but that's probably just plain HTML tables. ^_^

I run Avant, an IE6 variant.

By Xanthir, FCD (not verified) on 03 Nov 2006 #permalink

I'm not seeing the Piet pictures either (and I fun Firefox). When I pull an image URL out of the page source and try it directly, it works.

Ye freaking gods, that's bizarre....

By the way, regarding ALPACA, I've found that the entire mine.nu domain seems to be empty and reclaimed by its provider. So where the heck *can* we get it?

By David Harmon (not verified) on 03 Nov 2006 #permalink

David:

Unfortunately, the catseye site, where all of Chris Pressey's languages are hosted, has a long history of unreliability. I'd
put a copy up here, except that it was in one of the unrecoverable directories from *my* dead computer. A lot of
the esolangs are cached on the esolang wiki, esolangs.org.

Incidentally, (catseye.)mb.ca is also gone.

Regarding Piet, I guess those hue and lightness values wrap around the table?

By David Harmon (not verified) on 03 Nov 2006 #permalink

Right, no pictures with firefox. But I ran Safari on the page, and then the missing pictures show up as question marks. Right clicking on one, I could choose to open the picture in a separate window, and then I got a whole little web page, basically saying that he does not allow other web sites to link to his pictures "as if they were their own". Seems that Mark has violated a policy of the owner of the pictures, inadvertently I am sure, but still ...

No such luck --the esolang.org link (catseye.webhop.net) goes through dyndns.com (again...) to the mine.nu address. And Chris Pressey's user page points to webhop.net.

By David Harmon (not verified) on 03 Nov 2006 #permalink

Feh. Perhaps Mr. Pressey should archive his languages at esolang?

By David Harmon (not verified) on 03 Nov 2006 #permalink

Incidentally, the "commented alphabet" program link also leads to DM's 403 page.

While poking around the Esoteric Programming Languages WebRing, I stumbled across Whirl, which might be a candidate for your series: http://www.bigzaphod.org/whirl

By David Harmon (not verified) on 03 Nov 2006 #permalink

A nice new meaning of 'The art of Computer Programming'!
A similar exercise has been made converting music notation into colours. 'Warm' colours for 'warm' (harmonic) intervals and 'cold' colours for the more dissonant intervals. It makes beautiful paintings out of Bach fugues and Chopin etudes, and sort of repetitive fractal images from Irish folk songs.

Note that "annotated alphabet print" program link also goes to warning page. I had to disable referrers in FireFox to see it.

By Jos'h Fuller (not verified) on 04 Nov 2006 #permalink

This is like, so groovey! I wish there was a programming language with little hearts, and smilys in the zeroes!

By Haley Mills (not verified) on 04 Nov 2006 #permalink

Piet has featured in an ARG (Alternative Reality Game) called Perplxcity (perplexcityDOTcom), it was truly a fun card to solve :-)

By Martijn Verburg (not verified) on 05 Nov 2006 #permalink