**Note: **This post creates some cool colors, but to see something truly stunning, check out dln385’s images, inspired by these posts.

In my last post, I detailed an algorithm for generating images using regular expressions. However, I found them a tad boring and mundane, so in this post I’m going to add colors to the images.

We follow exactly the same algorithm for choosing where to color the pixels, but we need a method to decide what color to paint. To accomplish this, we introduce three colors channels *C, M, Y *which are basically a long string, composed of the same alphabet as the areas (0, 1, 2, 3).

For each string *S* (area) do the following:

- If
*S* matches the regular expression, mark it as painted.
- Define a blank color CMY(0, 0, 0).
- For each color channel, xor the string accompanying the channel, and if it matches the regular expression, change the color channel from 0.0 to 1.0.

Let’s do an example. Let *R* be the regular expression *(1+2+3)*0(1+3)*0(0+1+2+3)**.

Let the following be the color channels *C,M,Y*:

**C – **032102
**M – **033122
**Y – **132212

If we test the string *S = 330100*, we find it matches, so we must paint it.

Calculate *S XOR C = *330100 XOR 032102 = 302002, which matches the regular expression, so the cyan color channel is 1.0.

Calculate *S XOR M = 330100 XOR 033122 = 303022, *which also matches the regular expression, so the magenta color channel is also 1.0.

Calculate *S XOR Y = 330100 XOR 132212 = 202321*, which does **not **match the regular expression, so the yellow color channel is 0.0.

This means we paint the area, marked by the string 330100 with the CMY-color (1, 1, 0).

Here is a visual example to showcase this new method:

You can also do *AND* instead of *XOR*, it produces images like the following:

It’s obviously less visually stunning, but still interesting. Here is *OR* instead:

It’s also less visually diverse, but still cool to look at.

### Like this:

Like Loading...

*Related*

This is such a cool idea! Inspired by dln385’s results, I’ve built an online version in JavaScript: http://sjoerdvisscher.handcraft.com/regexfractal.html