## Step 1: Get Colors

First, some code eats the source image and selects colors, trying to pick “interesting” colors that occur a lot in an image. In this case, its version of interesting means saturated colors.

In the event that less than 32 colors are discovered, I shuffle those that it discovered as repeats to pad out the total color wells to 32. This is just an arbitrary choice on my part. It's how many stripes I wanted. Now, we should have a bunch of colors set aside:

• R G B
43,64,22
• R G B
75,104,54
• R G B
115,142,101
• R G B
14,27,4
• R G B
132,64,8
• R G B
191,129,58
• R G B
236,155,88
• R G B
222,173,85
• R G B
252,224,116
• R G B
139,11,12
• R G B
200,72,22
• R G B
233,103,45

### An aside: a little digital color theory

On a computer, the most common way of defining colors is a mixture of three primary colors: red, green, and blue (RGB). In theory, the combinations of these three colors can reproduce just about any color. Note: this is far from true, but for our purposes, let’s run with it.

Try looking at your computer screen or television with a magnifying glass (or a droplet of water) and you’ll suddenly see little red, green, and blue lights, and you’ll see how it works.

In our case, each “channel” of the color (RGB) has up to 256 shades, stored from 0-255, inclusive. So about 50% brightness red is the value 127/255*.

Each of the three channels has its own value of 0-255. All the channels on full brightness looks like white, all on zero looks like black. When the channels are near to the same value, it’s pretty much grey.

Imaginative readers will immediately grok that when the values are divergent you get richly-hued colors:

## Step 2: Permutation

My code takes advantage of this numerical representation of color to do variations on colors. It’s a simple algorithm: each initial color value is wiggled a bit, with some very constraints (so the change isn’t too jarring) and saved. There’s no attempt to preserve hue, brightness, or anything else. Each color has this done many times. I asked a mathematician what this is called, and he said it’s called a “random walk”.

We just have values so far, even though they started from colors — they're just numbers. Seen as text, the data looks like this:

ìX Þ­Use@¿:ég-ég- +@üàtüàt+@¿:üàtKh6seìXÈHQQìXÈHKh6¿:ég-@Þ­U@ìX á­Xrf @œ=èi-æh.,=ûãvþàq*?À7úÞrKh4tgëVÉJNOéUÊJNe3Â~8ëd+=Ý¯UB í[ â¬Yse \$ > œ}>èf-ée0,<þáyüâp,>Á9ýàpLj6vdêVÇJPNçVÊGQc3Ä;éa.@Þ¯VE ë] ß©\ub !>Œ{Açe-êg2);þã{ûäm+@Ÿ9ÿâsOh3xfëUÆHOMéSÇHPd0Á=æ_/B Þ¯XB ë[ Üš_xd "<¹{Cåh,ìf0,=ÿá{üæm,B¿8ÿãvMj3!uièWÄIPSê TÉISa.Ä>è_1E à¬VB ë] Û«auf \$9»z@æh*ïg2-;ÿázùåo)?Â8üãvKj6#tgêWÇHNSé¡RÊKSb0Ç@éb2D ß®UE ê^ Ú¬crc#7Ÿx=æh'ïj/ />üãzøäp+?Å8þãxNl8\$tjêXÄGQQë£PÇJS`0È@éd/Gá°TDè\ Ý«dud\$9œw?ãj)òl1,Aþåxûãm.BÂ6ÿà{Pm;!smíXÆJR Oé¢RÄJQc0ÅBçg0I à²SAë_ Ú«ewfAºz=äi*õl. -Düãwýåk+@Ã8ÿá|Oo9vlêYÃHO Më£RÁLRe1ÂBåe-H ãŽVB é_ Ý¬ftd@·{>äk-õn+0Dýãzüçi)?Å9ÿâyNr;vií[ÅGNJí£PÃNTd0ÁEãe+Kä·WEæ` Þ¯hteBŽy@æl+øq+ 3Dúà{ú

Yeah, you can't read it — it's not words, and not all values within 0-255 even map to normal letters — but you get the idea. These values can be mapped into colors, one by one, and they can be lined-up, and made into a stripe, like this:

## Step 3: Expanding the Stripes

Why 480 repetitions? Well, that goes back to 32. That was indeed arbitrary. What was NOT arbitrary was that as individual 1-pixel stripes, the twinkles don’t look very nice:

When I first made one of these, I wanted to SEE the variation, and you can’t really see that up there. So I expanded it, it so happens 10x, and then I could see. So now those 32 1-pixel lines become 32 strips 10px wide, or 320px. And when I had some prints made, the print sizes are 2:3 ratio, which let me to 480px. And here we are:

# Interesting Cumulative Effects

Through the repetition, the colors get more intense and garish. Every time. Compare the left side of a twinkle with the right. I didn’t set out for that to happen. It’s an emergent property of the way I wrote the random walk code; maybe it’s standard from a random walk. Remember that each “channel” of the color gets its own random change, so the colors can get wider and wider apart as the code loops. I like how the subtle/natural colors of a real-world photo start out on the left and get more techno as they proceed. This is just another happy accident.

Sometimes the colors get too artificial, but that’s just a particularity of this algorithm. I tend to like the colors in the middle, and sometimes I just want to zoom-in on one little area that has an unexpected combination of gorgeous complimentary colors in a little square of space.

### Coming soon... ?

It will be fun trying to encode my own sense of beauty into code and to find more cool algorithms. I'm also experimenting with moving “twinkles” and interactive stuff. Meanwhile, I hope that you like Twnkl.it as it is.