-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
70 lines (59 loc) · 1.36 KB
/
grid.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
package waveform
import (
"image"
"image/color"
)
// Stack is a simple image.Image implementation that stacks other
// image.Image's on top of each other vertically.
//
// The width of the Stack is of the widest image given. The height
// of the Stack is the height of each image added up.
//
// Points that fall outside of the image.Image return an undefined
// color.Color, therefore it is suggested to only use images of the
// same width.
type Stack struct {
width, height int
imgs []image.Image
}
// NewStack creates a new Stack from the images passed in.
func NewStack(imgs ...image.Image) image.Image {
var w, h int
for i := range imgs {
h += imgs[i].Bounds().Max.Y
x := imgs[i].Bounds().Max.X
if x > w {
w = x
}
}
return &Stack{
imgs: imgs,
width: w,
height: h,
}
}
// Bounds implements the image.Image method
func (s *Stack) Bounds() image.Rectangle {
return image.Rectangle{
Min: image.Point{0, 0},
Max: image.Point{s.width, s.height},
}
}
// At implements the image.Image method
func (s *Stack) At(x, y int) color.Color {
var passedY int
for i := range s.imgs {
b := s.imgs[i].Bounds().Max.Y + passedY
if y > b {
passedY = b
continue
}
y = y - passedY
return s.imgs[i].At(x, y)
}
return nil
}
// ColorModel implements the image.Image method
func (s *Stack) ColorModel() color.Model {
return color.RGBAModel
}