92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/png"
|
|
"math/rand"
|
|
"os"
|
|
|
|
iu "git.nkpl.cc/twocookedfaggots/imageutils"
|
|
)
|
|
|
|
func square(side int) image.Rectangle { return image.Rect(0, 0, side, side) }
|
|
|
|
func Circles(b image.Rectangle, radius int) chan iu.Circle {
|
|
ch := make(chan iu.Circle)
|
|
go func() {
|
|
defer close(ch)
|
|
for {
|
|
x, y := rand.Int()%b.Dx(), rand.Int()%b.Dy()
|
|
dxdy := b.Dx() * b.Dy()
|
|
r := rand.Int() % (dxdy / (dxdy / radius))
|
|
ch <- iu.Circle{image.Point{x, y}, r}
|
|
}
|
|
}()
|
|
return ch
|
|
}
|
|
|
|
var Opaque, Transparent = color.Alpha{0xff}, color.Alpha{0}
|
|
|
|
type xor struct{ dst, src image.Image }
|
|
|
|
func (t xor) ColorModel() color.Model { return t.dst.ColorModel() }
|
|
func (t xor) Bounds() image.Rectangle { return t.src.Bounds() }
|
|
func (t xor) At(x, y int) color.Color {
|
|
dstColor := t.ColorModel().Convert(t.dst.At(x, y))
|
|
srcColor := t.ColorModel().Convert(t.src.At(x, y))
|
|
|
|
|
|
if ((dstColor == color.Opaque) || (srcColor == color.Opaque)) &&
|
|
!((dstColor == color.Opaque) && (srcColor == color.Opaque)) {
|
|
return color.Opaque
|
|
}
|
|
return color.Transparent
|
|
}
|
|
|
|
var exchange = color.ModelFunc(
|
|
func(c color.Color) color.Color {
|
|
if color.Alpha16Model.Convert(c) == color.Transparent{
|
|
return color.Opaque
|
|
}
|
|
return color.Transparent
|
|
},
|
|
)
|
|
|
|
type Exchange struct { image.Image }
|
|
|
|
func (e Exchange) ColorModel() color.Model { return color.Alpha16Model }
|
|
func (e Exchange) Bounds() image.Rectangle { return e.Image.Bounds() }
|
|
func (e Exchange) At(x, y int) color.Color {
|
|
c := e.ColorModel().Convert(e.Image.At(x, y))
|
|
return exchange.Convert(c)
|
|
}
|
|
|
|
func main() {
|
|
canvas := image.NewAlpha16(square(1024))
|
|
circles := Circles(canvas.Bounds(), 1024/7)
|
|
|
|
// draw n circles
|
|
for i := 0; i < 500; i++ {
|
|
c := <-circles
|
|
|
|
draw.Draw(canvas, c.Bounds(), xor{c, canvas}, c.Bounds().Min, draw.Src)
|
|
/*
|
|
draw.DrawMask(
|
|
canvas, canvas.Bounds(), &image.Uniform{color.White},
|
|
image.Point{}, xor{c, canvas}, image.Point{}, draw.Src,
|
|
)
|
|
*/
|
|
}
|
|
/*
|
|
newCanvas := image.NewRGBA(canvas.Bounds())
|
|
draw.Draw(newCanvas, newCanvas.Bounds(), &image.Uniform{color.RGBA{255, 255, 0, 255}}, image.ZP, draw.Over)
|
|
draw.Draw(newCanvas, newCanvas.Bounds(), canvas, image.ZP, draw.Over)
|
|
*/
|
|
err := png.Encode(os.Stdout, canvas)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|