-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttery.go
74 lines (59 loc) · 1.35 KB
/
buttery.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package buttery
import (
"image"
"math/rand"
"reflect"
)
// ReverseSlice performs an in-place swap in reverse order.
func ReverseSlice(s interface{}) {
size := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
for i, j := 0, size-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}
}
// ShuffleSlice randomizes the order of a slice.
func ShuffleSlice(s interface{}) {
size := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
rand.Shuffle(size, swap)
}
// GetDimensions reports the horizontal and vertical bounds of a GIF.
func GetDimensions(paletteds []*image.Paletted) (int, int) {
var xMin int
var xMax int
var yMin int
var yMax int
for _, paletted := range paletteds {
rect := paletted.Rect
rectXMin := rect.Min.X
rectXMax := rect.Max.X
rectYMin := rect.Min.Y
rectYMax := rect.Max.Y
if rectXMin < xMin {
xMin = rectXMin
}
if rectXMax > xMax {
xMax = rectXMax
}
if rectYMin < yMin {
yMin = rectYMin
}
if rectYMax > yMax {
yMax = rectYMax
}
}
return xMax - xMin, yMax - yMin
}
// GetPaletteSize queries the size of a GIF's color space.
func GetPaletteSize(paletteds []*image.Paletted) int {
var maxPaletteSize int
for _, paletted := range paletteds {
palette := paletted.Palette
paletteSize := len(palette)
if paletteSize > maxPaletteSize {
maxPaletteSize = paletteSize
}
}
return maxPaletteSize
}