Piet: A Language of Colors

Piet Mondrian - Collection of MoMA

This programming language uses colors instead of text and punctuation to add and perform other tasks.

Most programming languages you’ve learned probably look like what you think of when you hear “language”. They’re made up of words and numbers and symbols and punctuation. They’re read from the top of the file on down.

Do programming languages have to look like this, though? Our written languages are restricted by how we speak. A computer doesn’t need to care about whether or not the symbols are pronounceable or make sense to a human ear or eye. That’s where esoteric languages come into play!

Esoteric programming languages, sometimes abbreviated to esolangs, are programming languages that push the boundaries of what it even means to be a language. Esoteric languages can consist of nothing but emoji, or have the code snake around the page in a 2-d way, or be made up entirely of whitespace! Some of them might look a little bit more like a programming language you’re used to but just be weird, like a language where every statement reads like a parody of shakespeare or an early ‘00s lolcat image macro.

The language we’ll be looking at is called Piet, and it’s a language where programs are pictures!

Piet was created by David Morgan-Mar a few years ago and is a language where programs are big bright pictures made up of regions of solid colors. The idea is that Piet programs should look a little like the paintings of Piet Mondrian yet be able to do anything a more normal programming language can do.

The specification for Piet is available on its website: http://www.dangermouse.net/esoteric/piet.html. There’s also an excellent Piet programming tutorial at http://homepages.vub.ac.be/~diddesen/piet/index.html. In the rest of this piece, we’ll help get you started using Piet and over the first conceptual hurdles.

When getting started learning Piet, I recommend using the online interpreter found at http://zobier.net/piet/. There are other interpreters linked from the Piet homepage and in a GitHub repo listed at the end of the article.

There’s three basic pieces that need to be explained in Piet:

  • How the interpreter knows what command to run
  • How the interpreter knows what to do next
  • How the interpreter knows the program is finished

When the entire program is regions of solid color, how does the program know what to do? Piet chooses commands based not on color but on changes in color. Every time the interpreter moves from one color region to another, a command will be run. The interpreter looks at both the hue, whether it’s red, green, blue, etc., and the lightness which is how bright or dark that color is. You also can’t generally use any color. It’s just 18 specific colors: the light, dark, and medium versions of red, yellow, green, cyan, blue, and magenta.

The specification gives the exact allowed colors in hexidecimal form, so red is #FF0000, light red is #FFC0C0, and dark red is #C00000. If you haven’t seen the hexidecimal form of colors, the idea is that you read something like #FFC0C0 as a triple of FF, C0, and C0 representing the proportion of red to green to blue that makes up the color. A really good site for getting an intuition about the hex forms of colors is http://www.color-hex.com/.

So a neat consequence of how Piet calculates what command to run next is that there’s many possible programs that all do the same thing, just with different colors. For example, both of the following programs add 2 and 2 then print it out.

One of the things I like about the online interpreter is that if you select a color it shows you what color to choose next to get the command you want. In this screenshot, I have the middle shade of blue selected.

The actual commands available in Piet are pretty limited. You don’t have variables, you have a stack.

If you’re unfamiliar with what a stack is, picture a big pile of books or plates: the two obvious things you can do with it are (1) put something on top of the stack and (2) take the top thing off the stack. Computer scientists generally call putting something on the stack “pushing” and taking the top thing off the stack “popping”.

In Piet you can push numbers onto the stack, pop them off the stack, do arithmetic with numbers on the stack, read and print out simple data and manipulate which color block is chosen next. It may not sound like it, but that actually is powerful enough to write any program you want.

Now, how does the Piet interpreter move around the picture? The interpreter keeps track of two pieces of data: the direction pointer (DP) and the codel chooser (CC). The direction pointer chooses what direction to “move” and what edge of the region to look at and the codel chooser is used to resolve which end of that edge. So, for example, if the interpreter is in the green region in the program below and the DP is pointing to the right, then if the CC is set to “left” it will move to the yellow region otherwise it’ll move to the blue region. As a small note, in the online interpreter “left” means the CC has a value of 0 and “right” means the CC has a value of 1. For the DP, “up” is 0 and it follows clockwise til “left” is 3.

White space is skipped over entirely and moving through white to get to a color region means that you move to the new region but no command is executed.

Black, on the other hand, cannot be moved through at all. Instead if the DP & CC combination means that the interpreter would move into a black region or against the edge of the image, then first the CC is flipped from left-to-right or visa versa. If it’s still stuck then the DP is rotated clockwise. So instead of trying to move right, it moves down or instead of trying to move left, it moves up.

Now we get to the final question: how does a program end? A program ends if all eight possible combinations of DP and CC are blocked. So going back to our tiny addition program

That last odd looking bit with the red surrounded by black is how we terminate the program. It’s actually a little tricky to ensure that you’ve stopped the program. As a challenge, try to figure out what goes wrong if we just have a single change like this:

Learn More

My Piet interpreters

https://github.com/clarissalittler/piet-interpreters

Piet homepage

http://www.dangermouse.net/esoteric/piet.html

Online Piet interpreter

http://zobier.net/piet/

Piet programming tutorial

http://homepages.vub.ac.be/~diddesen/piet/index.html

Piet Mondrian abstract paintings

https://www.wikiart.org/en/piet-mondrian/by-genre/abstract

An online directory of colors by hex code

http://www.color-hex.com/

Also In The October 2017 Issue

Building and creating your tools with the Minecraft toolbox helps you survive the game.

Sensors give robots the senses humans have.

30+ ideas for all age holiday gifts, from books to apps to board games to VR and more.

There might be a reason that too-real robot and video game character creeps you out.

This programming language uses colors instead of text and punctuation to add and perform other tasks.

Knowing how passwords are cracked can help you create better passwords.

There are a number of strategies teachers (plus parents and students) can take to learn programming.

This project uses conductive thread to create a glove to activates your phone.

Software programming does neat things with language, in this case, mixing capital letters.

This Scratch game has lots of ways you can customize the game play. No cats were harmed in the making of this article either.

Links from the bottom of all the October 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for October 2017.