Created simple bordered pattern

This commit is contained in:
2023-01-20 19:24:21 -05:00
parent befec471db
commit a71e726016
6 changed files with 53 additions and 20 deletions

24
artist/bordered.go Normal file
View File

@@ -0,0 +1,24 @@
package artist
import "image"
import "image/color"
// Bordered is a pattern with a border and a fill.
type Bordered struct {
Fill Pattern
Stroke
}
// AtWhen satisfies the Pattern interface.
func (pattern Bordered) AtWhen (x, y, width, height int) (c color.RGBA) {
outerBounds := image.Rectangle { Max: image.Point { width, height }}
innerBounds := outerBounds.Inset(pattern.Weight)
if (image.Point { x, y }).In (innerBounds) {
return pattern.Fill.AtWhen (
x - pattern.Weight,
y - pattern.Weight,
innerBounds.Dx(), innerBounds.Dy())
} else {
return pattern.Stroke.AtWhen(x, y, width, height)
}
}

View File

@@ -16,28 +16,28 @@ type borderInternal struct {
dx, dy int
}
// MultiBorder is a pattern that allows multiple borders of different lengths to
// be inset within one another. The final border is treated as a fill color, and
// its weight does not matter.
type MultiBorder struct {
// MultiBordered is a pattern that allows multiple borders of different lengths
// to be inset within one another. The final border is treated as a fill color,
// and its weight does not matter.
type MultiBordered struct {
borders []borderInternal
lastWidth, lastHeight int
maxBorder int
}
// NewMultiBorder creates a new MultiBorder pattern from the given list of
// NewMultiBordered creates a new MultiBordered pattern from the given list of
// borders.
func NewMultiBorder (borders ...Stroke) (multi *MultiBorder) {
func NewMultiBordered (borders ...Stroke) (multi *MultiBordered) {
internalBorders := make([]borderInternal, len(borders))
for index, border := range borders {
internalBorders[index].weight = border.Weight
internalBorders[index].stroke = border.Pattern
}
return &MultiBorder { borders: internalBorders }
return &MultiBordered { borders: internalBorders }
}
// AtWhen satisfies the Pattern interface.
func (multi *MultiBorder) AtWhen (x, y, width, height int) (c color.RGBA) {
func (multi *MultiBordered) AtWhen (x, y, width, height int) (c color.RGBA) {
if multi.lastWidth != width || multi.lastHeight != height {
multi.recalculate(width, height)
}
@@ -54,7 +54,7 @@ func (multi *MultiBorder) AtWhen (x, y, width, height int) (c color.RGBA) {
return
}
func (multi *MultiBorder) recalculate (width, height int) {
func (multi *MultiBordered) recalculate (width, height int) {
bounds := image.Rect (0, 0, width, height)
multi.maxBorder = 0
for index, border := range multi.borders {