-
Notifications
You must be signed in to change notification settings - Fork 1
/
strip.go
56 lines (45 loc) · 1.02 KB
/
strip.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
package web
import (
"io"
"github.com/ajstarks/svgo"
)
// A Pixel is a single RGB pixel.
type Pixel struct {
R uint8
G uint8
B uint8
}
// A Strip is a collection of Pixel.
type Strip struct {
Number int
Pixels []Pixel
}
// RenderStripSVG renders a SVG for the specified strips.
func RenderStripSVG(strips []Strip, w io.Writer) error {
const (
pixelWidth = 4
pixelHeight = 8
stripPadding = 2
)
// Identify the longest strip (they should all be the same, but...)
longestStrip := 0
for i := range strips {
if l := len(strips[i].Pixels); l > longestStrip {
longestStrip = l
}
}
canvas := svg.New(w)
canvas.Start(pixelWidth*longestStrip, (pixelHeight+stripPadding)*len(strips))
yOffset := 0
for i := range strips {
strip := &strips[i]
for p := range strip.Pixels {
pixel := &strip.Pixels[p]
color := canvas.RGB(int(pixel.R), int(pixel.G), int(pixel.B))
canvas.Rect(p*pixelWidth, yOffset, pixelWidth, pixelHeight, color)
}
yOffset += pixelHeight + stripPadding
}
canvas.End()
return nil
}