2023-04-29 08:11:00 +00:00
|
|
|
// Package artutil provides utility functions for working with graphical types
|
|
|
|
// defined in artist, canvas, and image.
|
|
|
|
package artutil
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
|
|
|
|
|
|
|
// Fill fills the destination canvas with the given pattern.
|
2023-04-30 17:45:21 +00:00
|
|
|
func Fill (destination artist.Canvas, source artist.Pattern) (updated image.Rectangle) {
|
2023-04-29 08:11:00 +00:00
|
|
|
source.Draw(destination, destination.Bounds())
|
|
|
|
return destination.Bounds()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawClip lets you draw several subsets of a pattern at once.
|
|
|
|
func DrawClip (
|
2023-04-30 17:45:21 +00:00
|
|
|
destination artist.Canvas,
|
2023-04-29 08:11:00 +00:00
|
|
|
source artist.Pattern,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
subsets ...image.Rectangle,
|
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
|
|
|
) {
|
|
|
|
for _, subset := range subsets {
|
2023-04-30 17:45:21 +00:00
|
|
|
source.Draw(artist.Cut(destination, subset), bounds)
|
2023-04-29 08:11:00 +00:00
|
|
|
updatedRegion = updatedRegion.Union(subset)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawShatter is like an inverse of DrawClip, drawing nothing in the areas
|
|
|
|
// specified by "rocks".
|
|
|
|
func DrawShatter (
|
2023-04-30 17:45:21 +00:00
|
|
|
destination artist.Canvas,
|
2023-04-29 08:11:00 +00:00
|
|
|
source artist.Pattern,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
rocks ...image.Rectangle,
|
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
|
|
|
) {
|
|
|
|
tiles := shatter.Shatter(bounds, rocks...)
|
|
|
|
return DrawClip(destination, source, bounds, tiles...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocateSample returns a new canvas containing the result of a pattern. The
|
|
|
|
// resulting canvas can be sourced from shape drawing functions. I beg of you
|
|
|
|
// please do not call this every time you need to draw a shape with a pattern on
|
|
|
|
// it because that is horrible and cruel to the computer.
|
2023-04-30 17:45:21 +00:00
|
|
|
func AllocateSample (source artist.Pattern, width, height int) artist.Canvas {
|
|
|
|
allocated := artist.NewBasicCanvas(width, height)
|
2023-04-29 08:11:00 +00:00
|
|
|
Fill(allocated, source)
|
|
|
|
return allocated
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hex creates a color.RGBA value from an RGBA integer value.
|
|
|
|
func Hex (color uint32) (c color.RGBA) {
|
|
|
|
c.A = uint8(color)
|
|
|
|
c.B = uint8(color >> 8)
|
|
|
|
c.G = uint8(color >> 16)
|
|
|
|
c.R = uint8(color >> 24)
|
|
|
|
return
|
|
|
|
}
|