-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (62 loc) · 2.52 KB
/
main.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
75
76
77
78
79
80
81
package footballkit
import (
"bytes"
"image"
"image/color"
"image/png"
"log"
)
// RenderImage Given a string description of a football kit, render a rendered image of that kit
func RenderImage(stripDescription string) *image.Image {
var bodyAsset, leftArmAsset, rightArmAsset, shortsAsset, socksAsset = decodeFootballKit(stripDescription)
/*
This is not at all clever - we chose a fixed size (based on the wikipedia football kit images) and
have hardcoded all our shapes to that.
100x135 is our image sizes
left arm 0,0 ... 31x59
body 31,0 ... 38x59
right arm 69,0 ... 31x59
shorts 0,59 ... 100x36
socks 0,95 ... 100x40
*/
m := image.NewNRGBA(image.Rect(0, 0, 100, 135))
imagePaste(bodyAsset.filename, m, 31, 0, bodyAsset.colourOne, bodyAsset.colourTwo)
imagePaste(leftArmAsset.filename, m, 0, 0, leftArmAsset.colourOne, leftArmAsset.colourTwo)
imagePaste(rightArmAsset.filename, m, 69, 0, rightArmAsset.colourOne, rightArmAsset.colourTwo)
imagePaste(shortsAsset.filename, m, 0, 59, shortsAsset.colourOne, shortsAsset.colourTwo)
imagePaste(socksAsset.filename, m, 0, 95, socksAsset.colourOne, socksAsset.colourTwo)
var img image.Image = m
return &img
}
// Takes a name of a filename image file and replaces all the strong red/green pixels with the alternate
// specified colours, and pastes into a destination image object at 'destX' and 'destY'
func imagePaste(assetName string, destImage *image.NRGBA, destX int, destY int, colorA color.NRGBA, colorB color.NRGBA) {
// find the raw filename bytes
pngData, err := Asset("data/" + assetName)
if err != nil {
log.Println("filename error")
return
}
// decode the raw filename png image
pngImg, err := png.Decode(bytes.NewBuffer(pngData))
if err != nil {
log.Println("decode png error")
return
}
// we are going to pixel by pixel place it into the destination
// BUT are going to do some colour corrections on the way
// this is not exactly high performance but these are very small images so meh..
bounds := pngImg.Bounds()
for x := 0; x < bounds.Dx(); x++ {
for y := 0; y < bounds.Dy(); y++ {
pngColour := destImage.ColorModel().Convert(pngImg.At(x, y)).(color.NRGBA)
if pngColour.A > 200 && pngColour.R == 255 && pngColour.G == 0 && pngColour.B == 0 {
destImage.SetNRGBA(x+destX, y+destY, colorA)
} else if pngColour.A > 200 && pngColour.R == 0 && pngColour.G == 255 && pngColour.B == 0 {
destImage.SetNRGBA(x+destX, y+destY, colorB)
} else {
destImage.SetNRGBA(x+destX, y+destY, pngColour)
}
}
}
}