# Celebrate National Coloring Book Day with Wolfram (and Four Crayons)

August 2, 2016 — Zach Littrell, Technical Content Writer, Technical Communications and Strategy Group

Happy National Coloring Book Day! When my coworkers suggested that I write a blog post celebrating this colorful occasion, I was, frankly, tickled pink by the idea. Coloring is a fun, therapeutic activity for anyone of any age who can color inside the lines—or occasionally just a little outside, if they’re more like me. And as the newest member of the Wolfram Blog team, I wanted to see in what fun ways I could add a little color to the Wolfram Blog.

While looking through Wolfram|Alpha’s massive collection of popular curves, from Pokémon to ALF to Stephen Wolfram, I realized that all of the images built into the Wolfram Knowledgebase would be great for coloring. So, I figured, why not make my own Wolfram coloring book in Mathematica? Carpe colores!

Each of the popular curves in the Knowledgebase can be accessed as an Entity in the Wolfram Language and comes with a wide variety of properties, including their parametric equations. But there’s no need to plot them yourself—they also conveniently come with an "Image" property already included:

And with a few lines of code, the Wolfram Language can get rid of the axes and thicken the lines to make them just right for a coloring book page:

I encourage you to take this function, plop it in a notebook, create some coloring book pages, print them out, and spoil the coloring connoisseur in your life—especially if that person is yourself.

But that’s not enough for me: I want to make an interactive interface so I can color these popular curves directly in Mathematica. Ideally what I would like is to click any region of an image, and every pixel within that region gets colored, saving me from the stress of coloring within the lines. In other words, once I have loaded a popular curve, I need to segment the image into individual regions that can be colored. With the MorphologicalComponents function, I can do just that:

To capture the mouse location for each mouse click, I embed my image in a ClickPane. I can then identify which region is clicked by checking the mouse location with MorphologicalComponents and replacing its current color with whatever the currently selected color is. There are tons of different methods I could have used to implement this sort of “flood fill” algorithm in the Wolfram Language, just like how there is more than one way to color the same picture.

I wrapped up this functionality in a simple GUI to select from the available popular curves, and I had a ball trying out my little program and coloring the library of curves my way. The notebook with my code is included in this post, so feel free to try it out!

It’s a fast-paced world nowadays, though. I understand if you’re too busy to arduously fill out a coloring book page. That doesn’t mean you have to miss out on the fun! You can always just ask Wolfram|Alpha for a popular curve, randomly colored. Sure, the coloring may look anywhere between unconventional and absurd, but it’s fun to see how Wolfram|Alpha ends up coloring your favorite Futurama character or historical figure:

While I’m ruminating on computational coloring, it would be a real shame not to mention one of the most important coloring problems of all: the four-color theorem, one of the first computer-assisted proofs and a surprisingly difficult problem to solve. Appel and Haken proved in 1977 that any planar map can be colored so that no two regions sharing a border (bigger than a point) have the same color, using only four colors. In other words, you can color a geographic map so each country is a different color than its neighbors using just red, green, blue, and yellow, like the map of Europe below. That box of 100 different crayons may be nifty, but four is really all you need:

A fun challenge you can try is finding a four-coloring for a coloring book page. But to be true to the computational roots of the four-color problem, you can always be clever and use the Wolfram Language to find a four-coloring for you. To accomplish this, I adapted a method that can also be used to four-color a geographic map in the Wolfram Language. Given four colors—let’s say purple, blue, pink, and green—they can be mapped to pairs of Booleans, like the following:

I can enforce that two regions, A and B, don’t have the same color by creating a Boolean equation with Xor that checks that their respective Boolean pairs aren’t the exact same, using the convention that the color for A is represented by (x[A],y[A]):

And I can do a simple test to see if regions A and B are neighbors by using the Wolfram Language’s built-in morphological tools. With Dilation, I expand region A up, down, left, and right a couple of pixels and see if there’s any overlap with B. The Wolfram Language can then find a four-coloring solution for all equations for each pair of neighbors with FindInstance. The complete code for four-coloring is in the included notebook, so I encourage you to try it out and find four-colorings for your favorite curves!

And besides saving you money on crayons, finding four-colorings has practical applications in real-world scheduling problems, where you’re assigning people or things to time slots instead of colors to regions. So feel free to brag while coloring today that you’re really just exploring difficult mathematical problems in a relaxing, colorful manner.

There are many more different ways to chromatically express yourself in the Wolfram Language that I didn’t even touch on. With advanced image processing functions, machine learning tools, and data about famous artwork in the Wolfram Knowledgebase, the sky’s the limit. I hope you have a relaxing Coloring Book Day, and feel free to share any ideas or questions you have about coloring with the Wolfram Language!

RELATED POSTS