diff --git a/artist/artutil/util.go b/artist/artutil/util.go new file mode 100644 index 0000000..2858dde --- /dev/null +++ b/artist/artutil/util.go @@ -0,0 +1,64 @@ +// 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/canvas" +import "git.tebibyte.media/sashakoshka/tomo/shatter" + +// Fill fills the destination canvas with the given pattern. +func Fill (destination canvas.Canvas, source artist.Pattern) (updated image.Rectangle) { + source.Draw(destination, destination.Bounds()) + return destination.Bounds() +} + +// DrawClip lets you draw several subsets of a pattern at once. +func DrawClip ( + destination canvas.Canvas, + source artist.Pattern, + bounds image.Rectangle, + subsets ...image.Rectangle, +) ( + updatedRegion image.Rectangle, +) { + for _, subset := range subsets { + source.Draw(canvas.Cut(destination, subset), bounds) + updatedRegion = updatedRegion.Union(subset) + } + return +} + +// DrawShatter is like an inverse of DrawClip, drawing nothing in the areas +// specified by "rocks". +func DrawShatter ( + destination canvas.Canvas, + 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. +func AllocateSample (source artist.Pattern, width, height int) canvas.Canvas { + allocated := canvas.NewBasicCanvas(width, height) + 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 +} diff --git a/artist/color.go b/artist/color.go deleted file mode 100644 index 1729447..0000000 --- a/artist/color.go +++ /dev/null @@ -1,12 +0,0 @@ -package artist - -import "image/color" - -// 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 -} diff --git a/artist/pattern.go b/artist/pattern.go index c2caa5c..2f5bf4e 100644 --- a/artist/pattern.go +++ b/artist/pattern.go @@ -2,7 +2,6 @@ package artist import "image" import "git.tebibyte.media/sashakoshka/tomo/canvas" -import "git.tebibyte.media/sashakoshka/tomo/shatter" // Pattern is capable of drawing to a canvas within the bounds of a given // clipping rectangle. @@ -13,49 +12,3 @@ type Pattern interface { // using canvas.Cut() to draw only a specific subset of a pattern. Draw (destination canvas.Canvas, bounds image.Rectangle) } - -// Fill fills the destination canvas with the given pattern. -func Fill (destination canvas.Canvas, source Pattern) (updated image.Rectangle) { - source.Draw(destination, destination.Bounds()) - return destination.Bounds() -} - -// DrawClip lets you draw several subsets of a pattern at once. -func DrawClip ( - destination canvas.Canvas, - source Pattern, - bounds image.Rectangle, - subsets ...image.Rectangle, -) ( - updatedRegion image.Rectangle, -) { - for _, subset := range subsets { - source.Draw(canvas.Cut(destination, subset), bounds) - updatedRegion = updatedRegion.Union(subset) - } - return -} - -// DrawShatter is like an inverse of DrawClip, drawing nothing in the areas -// specified by "rocks". -func DrawShatter ( - destination canvas.Canvas, - source 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. -func AllocateSample (source Pattern, width, height int) canvas.Canvas { - allocated := canvas.NewBasicCanvas(width, height) - Fill(allocated, source) - return allocated -}