-
Notifications
You must be signed in to change notification settings - Fork 8
/
borders.go
51 lines (45 loc) · 1.41 KB
/
borders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package imageutil
import (
"image"
"image/color"
"image/draw"
)
// PaintBorder colorizes a border of size `width` in the existing
// canvas, overwriting any colors in that space.
func PaintBorder(img draw.Image, clr color.Color, width uint) {
if img == nil || width == 0 {
return
}
PaintColor(img, clr, RectangleBorderXMin(img.Bounds(), width))
PaintColor(img, clr, RectangleBorderXMax(img.Bounds(), width))
PaintColor(img, clr, RectangleBorderYMin(img.Bounds(), width))
PaintColor(img, clr, RectangleBorderYMax(img.Bounds(), width))
}
func RectangleBorderXMin(rect image.Rectangle, pixels uint) image.Rectangle {
maxY := rect.Min.Y + int(pixels)
if maxY > rect.Max.Y {
maxY = rect.Max.Y
}
return image.Rect(rect.Min.X, rect.Min.Y, rect.Max.X, maxY)
}
func RectangleBorderXMax(rect image.Rectangle, pixels uint) image.Rectangle {
minY := rect.Max.Y - int(pixels)
if minY < rect.Min.Y {
minY = rect.Min.Y
}
return image.Rect(rect.Min.X, minY, rect.Max.X, rect.Max.Y)
}
func RectangleBorderYMin(rect image.Rectangle, pixels uint) image.Rectangle {
maxX := rect.Min.X + int(pixels)
if maxX > rect.Max.X {
maxX = rect.Max.X
}
return image.Rect(rect.Min.X, rect.Min.Y, maxX, rect.Max.Y)
}
func RectangleBorderYMax(rect image.Rectangle, pixels uint) image.Rectangle {
minX := rect.Max.X - int(pixels)
if minX < rect.Min.X {
minX = rect.Min.X
}
return image.Rect(minX, rect.Min.Y, rect.Max.X, rect.Max.Y)
}