This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/artist/patterns/texture.go

42 lines
1.3 KiB
Go
Raw Normal View History

2023-02-25 23:41:16 +00:00
package patterns
2023-01-09 06:03:19 +00:00
import "image"
2023-02-25 23:41:16 +00:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-02-25 23:41:16 +00:00
// Texture is a pattern that tiles the content of a canvas both horizontally and
// vertically.
type Texture struct {
2023-02-25 23:41:16 +00:00
canvas.Canvas
}
2023-02-25 23:41:16 +00:00
// Draw tiles the pattern's canvas within the clipping bounds. The minimum
// points of the pattern's canvas and the destination canvas will be lined up.
func (pattern Texture) Draw (destination canvas.Canvas, clip image.Rectangle) {
2023-02-27 17:48:44 +00:00
realBounds := destination.Bounds()
bounds := clip.Canon().Intersect(realBounds)
2023-02-25 23:41:16 +00:00
if bounds.Empty() { return }
dstData, dstStride := destination.Buffer()
srcData, srcStride := pattern.Buffer()
srcBounds := pattern.Bounds()
2023-02-27 21:38:33 +00:00
point := image.Point { }
for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ {
for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ {
srcPoint := point.Sub(realBounds.Min).Add(srcBounds.Min)
dstIndex := point.X + point.Y * dstStride
2023-02-25 23:41:16 +00:00
srcIndex :=
2023-02-27 21:38:33 +00:00
wrap(srcPoint.X, srcBounds.Min.X, srcBounds.Max.X) +
wrap(srcPoint.Y, srcBounds.Min.Y, srcBounds.Max.Y) * srcStride
2023-02-25 23:41:16 +00:00
dstData[dstIndex] = srcData[srcIndex]
}}
}
2023-02-25 23:41:16 +00:00
func wrap (value, min, max int) int {
difference := max - min
value = (value - min) % difference
if value < 0 { value += difference }
return value + min
}