returning image.Image instead of returning a type Pair

This commit is contained in:
potassium 2024-05-10 12:33:54 +03:00
parent 56208ff04d
commit 04c781c03a

145
concat.go
View File

@ -10,7 +10,7 @@ type Side int
const ( const (
Left Side = iota Left Side = iota
Right Right
Up Up
Down Down
) )
@ -29,101 +29,98 @@ func (p pair) Bounds() image.Rectangle {
b2 = p.second.Bounds() b2 = p.second.Bounds()
) )
rect := image.Rectangle{} point := image.Point{}
switch p.side { switch p.side {
case Left: case Left:
fallthrough fallthrough
case Right: case Right:
rect = image.Rectangle{ point = image.Point{
image.ZP, X: b1.Dx() + b2.Dx(),
image.Point{ Y: max(b1.Dy(), b2.Dy()),
X: b1.Dx() + b2.Dx(),
Y: max(b1.Dy(), b2.Dy()),
},
} }
case Up: case Up:
fallthrough fallthrough
case Down: case Down:
rect = image.Rectangle{ point = image.Point{
image.ZP, X: max(b1.Dx(), b2.Dx()),
image.Point{ Y: b1.Dy() + b2.Dy(),
X: max(b1.Dx(), b2.Dx()),
Y: b1.Dy() + b2.Dy(),
},
} }
} }
return rect return image.Rectangle{
image.ZP,
point,
}
} }
func (p pair) At(x, y int) color.Color { func (p pair) At(x, y int) color.Color {
rgbaImg := image.NewRGBA(p.Bounds()) img := image.NewRGBA(p.Bounds())
point := image.Point{}
switch p.side { switch p.side {
case Left: case Left:
draw.Draw( p.first, p.second = p.second, p.first
rgbaImg, rgbaImg.Bounds(), fallthrough
p.second,
image.ZP, draw.Src,
)
draw.Draw(
rgbaImg, p.first.Bounds(),
p.first,
image.Point{
p.second.Bounds().Dx(),
0,
},
draw.Src,
)
case Right: case Right:
draw.Draw( point = image.Point{-p.first.Bounds().Dx(), 0}
rgbaImg, rgbaImg.Bounds(),
p.first,
image.ZP, draw.Src,
)
draw.Draw(
rgbaImg, p.first.Bounds(),
p.second,
image.Point{
p.first.Bounds().Dx(),
0,
},
draw.Over,
)
case Up: case Up:
draw.Draw( p.first, p.second = p.second, p.first
rgbaImg, rgbaImg.Bounds(), fallthrough
p.first,
image.ZP, draw.Src,
)
draw.Draw(
rgbaImg, p.Bounds(),
p.second,
image.Point{
0,
p.first.Bounds().Dy(),
},
draw.Src,
)
case Down: case Down:
draw.Draw( point = image.Point{0, -p.first.Bounds().Dy()}
rgbaImg, rgbaImg.Bounds(),
p.second,
image.ZP, draw.Src,
)
draw.Draw(
rgbaImg, p.Bounds(),
p.first,
image.Point{
0,
p.second.Bounds().Dy(),
},
draw.Src,
)
} }
return rgbaImg.At(x, y) // draw main
draw.Draw(
img, img.Bounds(),
p.first,
image.ZP, draw.Src,
)
// draw second
draw.Draw(
img, p.Bounds(),
p.second,
point,
draw.Src,
)
return img.At(x, y)
}
func render(p pair) image.Image {
img := image.NewRGBA(p.Bounds())
point := image.Point{}
switch p.side {
case Left:
p.first, p.second = p.second, p.first
fallthrough
case Right:
point = image.Point{-p.first.Bounds().Dx(), 0}
case Up:
p.first, p.second = p.second, p.first
fallthrough
case Down:
point = image.Point{0, -p.first.Bounds().Dy()}
}
// draw main
draw.Draw(
img, img.Bounds(),
p.first,
image.ZP, draw.Src,
)
// draw second
draw.Draw(
img, p.Bounds(),
p.second,
point,
draw.Src,
)
return img
} }
// Concat concatenates second image on a given side. // Concat concatenates second image on a given side.
func Concat(i1, i2 image.Image, s Side) image.Image { func Concat(i1, i2 image.Image, s Side) image.Image {
return pair{i1, i2, s} return render(pair{i1, i2, s})
} }