See the full code in action here. I made it
so that you can click the image to toggle the interactivity on and off, this way you can pause it on a specific
color phase if you want.

*Möbius transformations* are rational functions of the form $f(z)=\frac{az+b}{cz+d}$ with $ad-bc \neq 0$
(If this condition didn't hold then the function would be a constant). These functions act on the extended complex
plane, or *Riemann sphere*, $\mathbb{C}\cup \{\infty\}$. The Riemann sphere is denoted $\hat{\mathbb{C}}$ for brevity.
The whole reason that this object is called the Riemann sphere is that if you
stereographically project the sphere onto the extended complex plane, apply a Möbius transformation,
then perform an inverse stereographic projection, it amounts to a rotation and translation of the sphere!

A simple example of a Möbius transformation is $z\mapsto \frac{1}{z}$, which is equivalent to $z\mapsto
\frac{\bar{z}}{|z|^2}$. Scaling a point by the inverse square of its distance to the origin is an *inversion*
of the unit circle. It gets the name "inversion" because it turns the unit disk inside out, and leaves the
boundary unchanged in the same way that $x\mapsto \frac{1}{x}$ turns the positive real numbers inside out while
leaving $1$ unchanged. The $\bar{z}$ part of this map amounts to a reflection across the real axis. Via stereographic
projection, we can visualize this transformation by imagining the unit sphere centered at the origin rotating by
$\pi$ radians across the real axis. If this is difficult to understand, check out
this video, which supplies an excellent
visual intuition for how these transformations work.

Möbius transformations form a group under function composition. In fact, it is not difficult to show that this group is
isomorphic to the group of $2\times 2$ complex matrices quotiented by the group of scalar multiples (If you multiply every
coefficient in a Möbius transformation by a non-zero number, you get the same Möbius transformation). This group is called
$PSL(2, \mathbb{C})$.

Since all Möbius transformations are holomorphic and have non-vanishing derivatives, they are all conformal, meaning
they always preserve angles. From the relationship with stereographic projection, we can deduce that Möbius transformations
always take circles to circles (Note that lines are circles in the extended complex plane). It's also not difficult to show
that Möbius transformations are *triply transitive*, meaning that if we specify how a triple $(z_1,\, z_2,\, z_3)$
of complex numbers is mapped to another triple $(w_1,\, w_2,\, w_3)$ then we get ourselves a *unique* Möbius transformation!

I could go on all day about the interesting properties of Möbius transformations. They even pop up in my special relativity article.
However, what we're really interested in for the discussion of this fractal is *Kleinian groups*. These are simply discrete
subgroups of the *Möbius group* (the group of all Möbius transformations).

Over 2000 years ago, Apollonius of Perga
discovered that if you start with three pairwise tangent circles and begin iteratively drawing circles which are tangent to
three pre-existing circles, you get a neat desgin.
This "neat design" is a fractal called the *Apollonian gasket*. It turns out that this marvelous shape is also the *limit set*
of a Kleinian group. In simple terms, this means that every Kleinian group has a gasket which is invariant under the application of
any element of the group.

It turns out that there is a simple way to render Apollonian gaskets using circle inversions. Pick four pairwise tangent circles, call them
primitive circles. We can construct an Apollonian gasket by inverting across each of these circles. A point which never falls outside
of any of the circles is in the gasket, while a point that eventually falls out of the circles is not in the gasket. Too see why this
works, notice that any triple of the four primitive circles specifies three points of tangency. The three points of tangency can be used to define
a circle which will, clearly, never fall outside any of the primitive circles under iterated inversion. There are 4 such circles.
Three of them will be the initial circles for the Apollonian gasket and one will be a circle which encapsulates the other three.
Since circle inversion preserves circle tangency, iterated inversion will give us the Apollonian gasket of the initial circles!
I encourage you to draw the situation out if you find it difficult to understand.

As you may have noticed, this fractal is *not* an Apollonian gasket. It's an experimental fractal! It's constructed via the process
that I described in the previous paragraph, except that the primitive circles are allowed to overlap. The mouse coordinates are changing the radii
of two of the circles.

This might seem like a lot to take in. Indeed, this is an area of math that I wish I knew more about. That
being said, a popular text about Möbius transformations and the novelties that arise from them is Indra's Pearls.