Skip to content

Commit

Permalink
feat: image/imageutil/padding: add IsPaddingFuncWhite()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Nov 18, 2024
1 parent 303ec46 commit 43f7a99
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions image/imageutil/padding/padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ import (

type IsPaddingFunc func(c color.Color) bool

// CreateIsPaddingFuncSimple returns a function that fulfills the
// `IsPaddingFunc` inteface.
func CreateIsPaddingFuncSimple(paddingColor color.Color) func(testColor color.Color) bool {
return func(testColor color.Color) bool {
return colors.Equal(paddingColor, testColor)
}
}

// IsPaddingFuncWhite returns a function that fulfills the `IsPaddingFunc`
// interface. The `unused` parameter is present to fulfill the interface
// requirements but is not used otherwise.
func IsPaddingFuncWhite() func(unused color.Color) bool {
return CreateIsPaddingFuncSimple(color.White)
}

func AddPaddingUniform(im image.Image, paddingWidth uint, paddingColor color.Color) image.Image {
out := image.NewRGBA(image.Rect(0, 0, im.Bounds().Dx()+int(2*paddingWidth), im.Bounds().Dy()+int(2*paddingWidth)))
draw.Draw(out, out.Bounds(), &image.Uniform{paddingColor}, image.Point{}, draw.Src)
Expand All @@ -28,6 +37,9 @@ func AddPaddingUniform(im image.Image, paddingWidth uint, paddingColor color.Col
}

func NonPaddingRectangle(im image.Image, isPadding IsPaddingFunc) image.Rectangle {
if isPadding == nil {
isPadding = IsPaddingFuncWhite()
}
topP, rightP, bottomP, leftP := PaddingWidths(im, isPadding)
return image.Rect(
int(leftP)+im.Bounds().Min.X,
Expand Down

0 comments on commit 43f7a99

Please sign in to comment.