# Fractals/color mandelbrot
## Coloring algorithms

In addition to plotting the set, a variety of algorithms have been developed to
* efficiently color the set in an aesthetically pleasing way
* show structures of the data (scientific visualisation)
general coloring algorithms: ( feature of the pixel -> color )
* discrete gradient:
** last iteration (integer value) = Level Set Method = LSM
* continous gradient: takes normalised position (floating point number in [0.0, 1.0] range) as an input and gives color as an output, not related with fractals, but used in general computer graphic; $T:scalarvalue\to colorvalue$

### Passing iterations into a color directly

One thing we may want to consider is avoiding having to deal with a palette or color blending at all. There are actually a handful of methods we can leverage to generate smooth, consistent coloring by constructing the color on the spot.
Here
* v refers to a normalized exponentially mapped cyclic iter count
* f(v) refers to the sRGB transfer function
A naive method for generating a color in this way is by directly scaling v to 255 and passing it into RGB as such
One flaw with this is that RGB is non-linear due to gamma; consider linear sRGB instead. Going from RGB to sRGB uses an inverse companding function on the channels. This makes the gamma linear, and allows us to properly sum the colors for sampling.

### Continuous (smooth) coloring

The escape time algorithm is popular for its simplicity. However, it creates bands of color, which, as a type of aliasing, can detract from an image's aesthetic value. This can be improved using an algorithm known as "normalized iteration count", which provides a smooth transition of colors between iterations. The algorithm associates a real number $\nu$ with each value of z by using the connection of the iteration number with the potential function. This function is given by
    $\phi (z)=\lim _{n\to \infty }(\log |z_{n}|/P^{n}),$
where z$_n$ is the value after n iterations and P is the power for which z is raised to in the Mandelbrot set equation (z$_{n+1}$ = z$_n$$^P$ + c, P is generally 2).
If we choose a large bailout radius N (e.g., 10$^{100}$), we have that
    $\log |z_{n}|/P^{n}=\log(N)/P^{\nu (z)}$
for some real number $\nu (z)$, and this is
    $\nu (z)=n-\log _{P}(\log |z_{n}|/\log(N)),$
and as n is the first iteration number such that |z$_n$| > N, the number we subtract from n is in the interval [0, 1).
For the coloring we must have a cyclic scale of colors (constructed mathematically, for instance) and containing H colors numbered from 0 to H − 1 (H = 500, for instance). We multiply the real number $\nu (z)$ by a fixed real number determining the density of the colors in the picture, take the integral part of this number modulo H, and use it to look up the corresponding color in the color table.
For example, modifying the above pseudocode and also using the concept of w:linear interpolation would yield

### Exponentially mapped and cyclic iterations

Typically when we render a fractal, the range of where colors from a given palette appear along the fractal is static. If we desire to offset the location from the border of the fractal, or adjust their palette to cycle in a specific way, there are a few simple changes we can make when taking the final iteration count before passing it along to choose an item from our palette.
When we have obtained the iteration count, we can make the range of colors non-linear. Raising a value normalized to the range [0,1] to a power n, maps a linear range to an exponential range, which in our case can nudge the appearance of colors along the outside of the fractal, and allow us to bring out other colors, or push in the entire palette closer to the border.
where:
* i is our iteration count after bailout
* max_i is our iteration limit
* S is the exponent we are raising iters to
* N is the number of items in our palette.
This scales the iter count non-linearly and scales the palette to cycle approximately proportionally to the zoom.
We can then plug v into whatever algorithm we desire for generating a color.

### HSV coloring

HSV Coloring can be accomplished by mapping iter count from [0,max_iter) to [0,360), taking it to the power of 1.5, and then modulo 360.
We can then simply take the exponentially mapped iter count into the value and return
This method applies to HSL as well, except we pass a saturation of 50% instead.
where:
    $h=({\frac {i}{max_{i}}}360)^{1.5}\ mod\ 360$

### LCH coloring

One of the most perceptually uniform coloring methods involves passing in the processed iter count into LCH. If we utilize the exponentially mapped and cyclic method above, we can take the result of that into the Luma and Chroma channels. We can also exponentially map the iter count and scale it to 360, and pass this modulo 360 into the hue.
x∈Q+s$_i$=(i/max$_i$)$^x$v=1.0−cos$^2$(πs$_i$)L=75−(75v)C=28+(75−75v)H=(360s$_i$)$^{1.5}$mod360{\textstyle {\begin{array}{lcl}x&\in &\mathbb {Q+} \\s_{i}&=&(i/max_{i})^{\mathbf {x} }\\v&=&1.0-cos^{2}(\pi s_{i})\\L&=&75-(75v)\\C&=&28+(75-75v)\\H&=&(360s_{i})^{1.5}{\bmod {3}}60\end{array}}}
One issue we wish to avoid here is out-of-gamut colors. This can be achieved with a little trick based on the change in in-gamut colors relative to luma and chroma. As we increase luma, we need to decrease chroma to stay within gamut.
* LCH gradient
* Example of exponentially mapped cyclic LCH coloring without shading
* Exponential Cyclic Coloring in LCH color space with shading

### Histogram coloring

A more complex coloring method involves using a histogram which pairs each pixel with said pixel's maximum iteration count before escape/bailout. This method will equally distribute colors to the same overall area, and, importantly, is independent of the maximum number of iterations chosen.
This algorithm has four passes. The first pass involves calculating the iteration counts associated with each pixel (but without any pixels being plotted). These are stored in an array: IterationCounts[x][y], where x and y are the x and y coordinates of said pixel on the screen respectively.
The first step of the second pass is to create an array of size n, which is the maximum iteration count: NumIterationsPerPixel. Next, one must iterate over the array of pixel-iteration count pairs, IterationCounts, and retrieve each pixel's saved iteration count, i, via e.g. i = IterationCounts[x][y]. After each pixel's iteration count i is retrieved, it is necessary to index the NumIterationsPerPixel by i and increment the indexed value (which is initially zero) -- e.g. NumIterationsPerPixel[i] = NumIterationsPerPixel[i] + 1 .
The third pass iterates through the NumIterationsPerPixel array and adds up all the stored values, saving them in total. The array index represents the number of pixels that reached that iteration count before bailout.
After this, the fourth pass begins and all the values in the IterationCounts array are indexed, and, for each iteration count i, associated with each pixel, the count is added to a global sum of all the iteration counts from 1 to i in the NumIterationsPerPixel array . This value is then normalized by dividing the sum by the total value computed earlier.
Finally, the computed value is used, e.g. as an index to a color palette.
This method may be combined with the smooth coloring method below for more aesthetically pleasing images.

### Muency

color From the Mandelbrot Set Glossary and Encyclopedia, by Robert Munafo, (c) 1987-2023
Java script version by Albert Lobo

## See also

* computing the Mandelbrot Set Exterior
* coloring the dynamic plane and th Julia and the Fatou sets
* general coloring algorithms: ( feature of the pixel -> color ): $T:scalarvalue\to colorvalue$

## References

* ↑ García, Francisco; Ángel Fernández; Javier Barrallo; Luis Martín. "Coloring Dynamical Systems in the Complex Plane" (PDF). Archived (PDF) from the original on 30 November 2019. Retrieved 2008-01-21. {{cite journal}}: Cite journal requires |journal= (help)
* ↑ Linas Vepstas. "Renormalizing the Mandelbrot Escape". Archived from the original on 14 February 2020. Retrieved 11 February 2020.
* ↑ "Newbie: How to map colors in the Mandelbrot set?". www.fractalforums.com. May 2007. Archived from the original on 9 September 2022. Retrieved February 11, 2020.