-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
48 lines (42 loc) · 1.11 KB
/
util.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
package main
import (
"image"
"strings"
"github.com/BurntSushi/xgbutil/xgraphics"
)
// vpCenter inspects the canvas and image geometry, and determines where the
// origin of the image should be painted into the canvas.
// If the image is bigger than the canvas, this is always (0, 0).
// If the image is the same size, then it is also (0, 0).
// If a dimension of the image is smaller than the canvas, then:
// x = (canvas_width - image_width) / 2 and
// y = (canvas_height - image_height) / 2
func vpCenter(ximg *xgraphics.Image, canWidth, canHeight int) image.Point {
xmargin, ymargin := 0, 0
if ximg.Bounds().Dx() < canWidth {
xmargin = (canWidth - ximg.Bounds().Dx()) / 2
}
if ximg.Bounds().Dy() < canHeight {
ymargin = (canHeight - ximg.Bounds().Dy()) / 2
}
return image.Point{xmargin, ymargin}
}
// basename retrieves the basename of a file path.
func basename(fName string) string {
if lslash := strings.LastIndex(fName, "/"); lslash != -1 {
fName = fName[lslash+1:]
}
return fName
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}