weiro/services/imgedit/shadow.go

36 lines
1.1 KiB
Go
Raw Normal View History

package imgedit
import (
"image"
"image/color"
"github.com/disintegration/imaging"
)
func makeBoxShadow(maskImg image.Image, shadowColor color.Color, sigma float64, shadowMargin, offsetY int) image.Image {
w, h := maskImg.Bounds().Dx(), maskImg.Bounds().Dy()
cr, cg, cb, _ := shadowColor.RGBA()
cr8, cg8, cb8 := uint8(cr>>8), uint8(cg>>8), uint8(cb>>8)
// New box image
backing := image.NewNRGBA(image.Rect(0, 0, w+shadowMargin*2, h+shadowMargin*2+offsetY))
newImg := image.NewNRGBA(image.Rect(0, 0, w+shadowMargin*2, h+shadowMargin*2+offsetY))
for x := 0; x < w+shadowMargin*2; x++ {
for y := 0; y < h+shadowMargin*2; y++ {
var c = color.NRGBA{R: 255, G: 255, B: 255, A: 0}
if x >= shadowMargin-4 && y >= shadowMargin-4 && x <= w+shadowMargin+4 && y <= h+shadowMargin+4 {
_, _, _, a := maskImg.At(x-shadowMargin, y-shadowMargin).RGBA()
c = color.NRGBA{R: cr8, G: cg8, B: cb8, A: uint8(a >> 8)}
}
backing.SetNRGBA(x, y, color.NRGBA{R: 255, G: 255, B: 255, A: 0})
newImg.SetNRGBA(x, y+offsetY, c)
}
}
// Blur
blurredImage := imaging.Blur(newImg, sigma)
backing = imaging.OverlayCenter(backing, blurredImage, 0.6)
return backing
}