From d59705d46e44db6b42c4f6279e2e3b9283f9db76 Mon Sep 17 00:00:00 2001 From: Dmitry Fedotov Date: Tue, 25 Mar 2025 20:53:43 +0300 Subject: [PATCH] add color variations --- .gitignore | 2 ++ barnsley-fern.go | 68 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 8f6a2e4..c45b969 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ barnsley-fern +barnsley-fern-go +barnsley_fern.png diff --git a/barnsley-fern.go b/barnsley-fern.go index ff39457..611f7d0 100644 --- a/barnsley-fern.go +++ b/barnsley-fern.go @@ -8,6 +8,7 @@ import ( "image/png" "math/rand" "os" + "time" ) func createImage(h, v int) *image.RGBA { @@ -20,13 +21,61 @@ func createImage(h, v int) *image.RGBA { func fillBackground(img *image.RGBA, c color.Color) { rect := img.Bounds() - for x := 0; x < rect.Max.X; x++ { - for y := 0; y < rect.Max.Y; y++ { + for x := range rect.Max.X { + for y := range rect.Max.Y { img.Set(x, y, c) } } } +func newColorFunc(random bool, timed bool, rainbow bool) func(float64, float64) color.Color { + return func(x, y float64) color.Color { + var ( + r, g, b, a uint8 + ) + + switch { + case random: + // n := rand.Intn(3) + + // switch n { + // case 0: + // r = 255 + // case 1: + // g = 255 + // case 2: + // b = 255 + // } + + r = uint8(rand.Intn(256)) + g = uint8(rand.Intn(256)) + b = uint8(rand.Intn(256)) + a = 255 + + case timed: + n := time.Now().Nanosecond() % 256 // Nanosecond returns range [0, 999999999] + + r = uint8((n + 256) % 256) + g = uint8((n + 128) % 256) + b = uint8(n % 256) + + a = 255 + + case rainbow: + r = uint8(y * 256) + g = uint8(x * 256) + b = uint8(((y - x) - min(x, y)) * 256) + + a = 255 + + default: + r, g, b, a = 0, 153, 0, 255 + } + + return color.RGBA{r, g, b, a} + } +} + func min(a, b float64) float64 { if a > b { a = b @@ -34,7 +83,7 @@ func min(a, b float64) float64 { return a } -func drawBarnsleyFern(img *image.RGBA, c color.Color, dots int) { +func drawBarnsleyFern(img *image.RGBA, colorfunc func(float64, float64) color.Color, dots int) { var ( x, y, tmpx, tmpy, r, maxy, maxx, scale, yoffset, xoffset float64 ) @@ -68,7 +117,10 @@ func drawBarnsleyFern(img *image.RGBA, c color.Color, dots int) { } // рисуем точку x, y = tmpx, tmpy - img.Set(int(xoffset+x*scale), int(maxy-yoffset-y*scale), c) + + color := colorfunc(x, y) + + img.Set(int(xoffset+x*scale), int(maxy-yoffset-y*scale), color) } } @@ -78,6 +130,9 @@ func main() { h = flag.Int("h", 1920, "размер картинки по горизонтали") v = flag.Int("v", 1080, "размер картинки по вертикали") dots = flag.Int("d", 100000, "сколько точек рисовать") + rand = flag.Bool("rand", false, "использовать случайные цвета") + timed = flag.Bool("timed", false, "привязка к наносекундам времени исполнения") + rainbow = flag.Bool("rainbow", false, "привязка к координатам") img *image.RGBA f *os.File err error @@ -87,7 +142,10 @@ func main() { img = createImage(*h, *v) fillBackground(img, color.White) - drawBarnsleyFern(img, color.RGBA{0, 153, 0, 255}, *dots) + + colorfunc := newColorFunc(*rand, *timed, *rainbow) + + drawBarnsleyFern(img, colorfunc, *dots) f, err = os.Create(filename) if err != nil {