-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (106 loc) · 2.48 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"log"
"math/rand"
"os"
"time"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
"github.com/lusingander/rgbto256colors-go"
)
const output = "./output.png"
const (
cellW = 80
cellWBuf = 30
cellH = 50
cellHBuf = 20
marginX = 10
marginY = 10
row = 10
col = 3
width = (marginX * (col - 1)) + (cellWBuf * (col - 1)) + ((cellW * 2) * col)
height = marginY + (cellH+cellHBuf)*row
)
var (
white = color.RGBA{255, 255, 255, 255}
)
func fill(i *image.RGBA, c color.Color) {
rect := i.Rect
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
i.Set(x, y, c)
}
}
}
func paintPattern(i *image.RGBA, r, c int, c1, c2 color.Color) {
baseY := marginY + (cellH+cellHBuf)*r
baseX := marginX + (cellW*2+cellWBuf)*c
for y := baseY; y < baseY+cellH; y++ {
for x := baseX; x < baseX+cellW; x++ {
i.Set(x, y, c1)
}
for x := baseX + cellW; x < baseX+(cellW*2); x++ {
i.Set(x, y, c2)
}
}
drawColorCodeString(i, baseX, baseY, c1, c2)
}
func drawColorCodeString(i *image.RGBA, baseX, baseY int, c1, c2 color.Color) {
d := &font.Drawer{
Dst: i,
Src: image.NewUniform(color.Black),
Face: basicfont.Face7x13,
}
d.Dot.X = (fixed.I(baseX))
d.Dot.Y = fixed.I(baseY + cellH + d.Face.Metrics().Height.Floor())
d.DrawString(hexColorString(c1))
d.Dot.X = (fixed.I(baseX + cellW))
d.Dot.Y = fixed.I(baseY + cellH + d.Face.Metrics().Height.Floor())
d.DrawString(hexColorString(c2))
}
func hexColorString(c color.Color) string {
rgba := color.RGBAModel.Convert(c).(color.RGBA)
return fmt.Sprintf("#%.2X%.2X%.2X", rgba.R, rgba.G, rgba.B)
}
func uint8RandGen() func() uint8 {
rand.Seed(time.Now().UnixNano())
return func() uint8 { return uint8(rand.Intn(255)) }
}
func draw(img *image.RGBA) {
fill(img, white)
var randN = uint8RandGen()
for c := 0; c < col; c++ {
for r := 0; r < row; r++ {
c1 := color.RGBA{randN(), randN(), randN(), 255}
c2 := rgbto256colors.FromRGB(c1.R, c1.G, c1.B).ToColor()
paintPattern(img, r, c, c1, c2)
}
}
}
func save(img *image.RGBA) error {
file, err := os.Create(output)
if err != nil {
return err
}
defer file.Close()
err = png.Encode(file, img)
if err != nil {
return err
}
return nil
}
func run(args []string) error {
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw(img)
return save(img)
}
func main() {
if err := run(os.Args); err != nil {
log.Fatal(err)
}
}