add color variations

This commit is contained in:
Dmitry Fedotov
2025-03-25 20:53:43 +03:00
parent 8d99b9b26d
commit d59705d46e
2 changed files with 65 additions and 5 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
barnsley-fern barnsley-fern
barnsley-fern-go
barnsley_fern.png

View File

@@ -8,6 +8,7 @@ import (
"image/png" "image/png"
"math/rand" "math/rand"
"os" "os"
"time"
) )
func createImage(h, v int) *image.RGBA { 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) { func fillBackground(img *image.RGBA, c color.Color) {
rect := img.Bounds() rect := img.Bounds()
for x := 0; x < rect.Max.X; x++ { for x := range rect.Max.X {
for y := 0; y < rect.Max.Y; y++ { for y := range rect.Max.Y {
img.Set(x, y, c) 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 { func min(a, b float64) float64 {
if a > b { if a > b {
a = b a = b
@@ -34,7 +83,7 @@ func min(a, b float64) float64 {
return a 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 ( var (
x, y, tmpx, tmpy, r, maxy, maxx, scale, yoffset, xoffset float64 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 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, "размер картинки по горизонтали") h = flag.Int("h", 1920, "размер картинки по горизонтали")
v = flag.Int("v", 1080, "размер картинки по вертикали") v = flag.Int("v", 1080, "размер картинки по вертикали")
dots = flag.Int("d", 100000, "сколько точек рисовать") dots = flag.Int("d", 100000, "сколько точек рисовать")
rand = flag.Bool("rand", false, "использовать случайные цвета")
timed = flag.Bool("timed", false, "привязка к наносекундам времени исполнения")
rainbow = flag.Bool("rainbow", false, "привязка к координатам")
img *image.RGBA img *image.RGBA
f *os.File f *os.File
err error err error
@@ -87,7 +142,10 @@ func main() {
img = createImage(*h, *v) img = createImage(*h, *v)
fillBackground(img, color.White) 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) f, err = os.Create(filename)
if err != nil { if err != nil {