// Written by xojoc . Public domain. package main import ( "github.com/lucasb-eyer/go-colorful" "image" "image/color" "image/png" "math" "math/cmplx" "os" ) const ( w = 700 h = 500 maxi = 50 ) func xhex(s string) colorful.Color { c, err := colorful.Hex(s) if err != nil { panic(err) } return c } func main() { img := image.NewRGBA(image.Rect(0, 0, w, h)) palette1 := []colorful.Color{ xhex("#ceecf7"), xhex("#5c87b2"), xhex("#7ea1bf"), xhex("#55708d"), xhex("#14222f"), } palette2 := []colorful.Color{ xhex("#757577"), xhex("#546169"), xhex("#2D333F"), xhex("#902165"), xhex("#A30B3A"), } ncolors1 := len(palette1) ncolors2 := len(palette2) // Where the mandelbrot set intersects the x,y axis. setx := -2.5 sety := -1.0 // width/height of the set. setw := 3.5 seth := 2.0 for y := 0; y < h; y++ { for x := 0; x < w; x++ { // Scale in the range (-2.5,1.0) for x and (-1.0,1.0) for y. c := complex(float64(x)/float64(w)*setw+setx, float64(y)/float64(h)*seth+sety) z := 0i i := 0 for ; cmplx.Abs(z) < 1<<16 && i < maxi; i++ { z = cmplx.Pow(z, 2) + c } var idx float64 if i < maxi { idx = float64(i) - math.Log2(math.Log2(cmplx.Abs(z))) } palette := palette1 ncolors := ncolors1 if imag(z) < 0 { palette = palette2 ncolors = ncolors2 } c1 := palette[int(idx)%ncolors] c2 := palette[int(idx+1)%ncolors] _, f := math.Modf(idx) if i == maxi { img.Set(x, y, color.Black) } else { img.Set(x, y, c1.BlendLab(c2, f)) } } } png.Encode(os.Stdout, img) }