2023-01-14 18:08:55 -07:00
|
|
|
package artist
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
|
|
|
|
2023-01-20 17:07:16 -07:00
|
|
|
// Stroke represents a stoke that has a weight and a pattern.
|
|
|
|
type Stroke struct {
|
2023-01-14 18:08:55 -07:00
|
|
|
Weight int
|
2023-01-20 17:07:16 -07:00
|
|
|
Pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
type borderInternal struct {
|
|
|
|
weight int
|
|
|
|
stroke Pattern
|
2023-01-14 18:08:55 -07:00
|
|
|
bounds image.Rectangle
|
|
|
|
dx, dy int
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:24:21 -07:00
|
|
|
// 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 {
|
2023-01-20 17:07:16 -07:00
|
|
|
borders []borderInternal
|
2023-01-14 18:08:55 -07:00
|
|
|
lastWidth, lastHeight int
|
|
|
|
maxBorder int
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:24:21 -07:00
|
|
|
// NewMultiBordered creates a new MultiBordered pattern from the given list of
|
2023-01-14 19:01:00 -07:00
|
|
|
// borders.
|
2023-01-20 17:24:21 -07:00
|
|
|
func NewMultiBordered (borders ...Stroke) (multi *MultiBordered) {
|
2023-01-20 17:07:16 -07:00
|
|
|
internalBorders := make([]borderInternal, len(borders))
|
|
|
|
for index, border := range borders {
|
|
|
|
internalBorders[index].weight = border.Weight
|
|
|
|
internalBorders[index].stroke = border.Pattern
|
|
|
|
}
|
2023-01-20 17:24:21 -07:00
|
|
|
return &MultiBordered { borders: internalBorders }
|
2023-01-14 18:08:55 -07:00
|
|
|
}
|
|
|
|
|
2023-01-14 19:01:00 -07:00
|
|
|
// AtWhen satisfies the Pattern interface.
|
2023-01-20 17:24:21 -07:00
|
|
|
func (multi *MultiBordered) AtWhen (x, y, width, height int) (c color.RGBA) {
|
2023-01-14 18:08:55 -07:00
|
|
|
if multi.lastWidth != width || multi.lastHeight != height {
|
|
|
|
multi.recalculate(width, height)
|
|
|
|
}
|
|
|
|
point := image.Point { x, y }
|
|
|
|
for index := multi.maxBorder; index >= 0; index -- {
|
|
|
|
border := multi.borders[index]
|
|
|
|
if point.In(border.bounds) {
|
2023-01-20 17:07:16 -07:00
|
|
|
return border.stroke.AtWhen (
|
2023-01-14 18:08:55 -07:00
|
|
|
point.X - border.bounds.Min.X,
|
|
|
|
point.Y - border.bounds.Min.Y,
|
|
|
|
border.dx, border.dy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:24:21 -07:00
|
|
|
func (multi *MultiBordered) recalculate (width, height int) {
|
2023-01-14 18:08:55 -07:00
|
|
|
bounds := image.Rect (0, 0, width, height)
|
|
|
|
multi.maxBorder = 0
|
|
|
|
for index, border := range multi.borders {
|
|
|
|
multi.maxBorder = index
|
|
|
|
multi.borders[index].bounds = bounds
|
|
|
|
multi.borders[index].dx = bounds.Dx()
|
|
|
|
multi.borders[index].dy = bounds.Dy()
|
2023-01-20 17:07:16 -07:00
|
|
|
bounds = bounds.Inset(border.weight)
|
2023-01-14 18:08:55 -07:00
|
|
|
if bounds.Empty() { break }
|
|
|
|
}
|
|
|
|
}
|