2023-08-14 13:04:04 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/png"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/fogleman/gg"
|
|
|
|
)
|
|
|
|
|
2023-09-05 08:58:35 +00:00
|
|
|
func AddStatusIndicatorToImage(inputImage []byte, innerColor color.Color, indicatorSize, indicatorBorder, indicatorCenterToEdge float64) ([]byte, error) {
|
2023-08-14 13:04:04 +00:00
|
|
|
// decode the input image
|
|
|
|
img, _, err := image.Decode(bytes.NewReader(inputImage))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the dimensions of the image
|
|
|
|
width := img.Bounds().Max.X
|
|
|
|
height := img.Bounds().Max.Y
|
|
|
|
|
2023-09-05 08:58:35 +00:00
|
|
|
indicatorOuterRadius := (indicatorSize / 2) + indicatorBorder
|
2023-08-14 13:04:04 +00:00
|
|
|
|
|
|
|
// calculate the center point
|
2023-09-05 08:58:35 +00:00
|
|
|
x := float64(width) - indicatorCenterToEdge
|
|
|
|
y := float64(height) - indicatorCenterToEdge
|
2023-08-14 13:04:04 +00:00
|
|
|
|
|
|
|
// create a new gg.Context instance
|
|
|
|
dc := gg.NewContext(width, height)
|
|
|
|
dc.DrawImage(img, 0, 0)
|
|
|
|
|
|
|
|
// Loop through each pixel in the hole and set it to transparent
|
|
|
|
dc.SetColor(color.Transparent)
|
2023-09-05 08:58:35 +00:00
|
|
|
for i := x - indicatorOuterRadius; i <= x+indicatorOuterRadius; i++ {
|
|
|
|
for j := y - indicatorOuterRadius; j <= y+indicatorOuterRadius; j++ {
|
|
|
|
if math.Pow(i-x, 2)+math.Pow(j-y, 2) <= math.Pow(indicatorOuterRadius, 2) {
|
2023-08-14 13:04:04 +00:00
|
|
|
dc.SetPixel(int(i), int(j))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw inner circle
|
2023-09-05 08:58:35 +00:00
|
|
|
dc.DrawCircle(x, y, indicatorOuterRadius-indicatorBorder)
|
2023-08-14 13:04:04 +00:00
|
|
|
dc.SetColor(innerColor)
|
|
|
|
dc.Fill()
|
|
|
|
|
|
|
|
// encode the modified image as PNG and return as []byte
|
|
|
|
var outputImage bytes.Buffer
|
|
|
|
err = png.Encode(&outputImage, dc.Image())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return outputImage.Bytes(), nil
|
|
|
|
}
|