1package htmlgen
2
3import (
4	"fmt"
5	"image/color"
6	"io"
7
8	"github.com/muesli/gamut"
9
10	colorful "github.com/lucasb-eyer/go-colorful"
11)
12
13var (
14	header = `
15	<html>
16	<body>
17	`
18	tablecaption = `
19	<h2>%s</h2>
20	`
21	tableheader = `
22	<table width="100%" height="64">
23	<tr>
24	`
25	cell        = `<td bgcolor="%s" align="center"><font face="Courier" color="%s">%s</font></td>`
26	tablefooter = `
27	</tr>
28	</table>
29	`
30	footer = `
31	</body>
32	</html>
33	`
34)
35
36// Header writes the HTML header to buffer
37func Header(buffer io.Writer) {
38	buffer.Write([]byte(header))
39}
40
41// Footer writes the HTML footer to buffer
42func Footer(buffer io.Writer) {
43	buffer.Write([]byte(footer))
44}
45
46// Cell writes a colored HTML table cell to buffer
47func Cell(buffer io.Writer, c color.Color) {
48	col, _ := colorful.MakeColor(c)
49	comp, _ := colorful.MakeColor(gamut.Contrast(c))
50	buffer.Write([]byte(fmt.Sprintf(cell, col.Hex(), comp.Hex(), col.Hex())))
51}
52
53// Table writes a palette of colors as an HTML table to buffer
54func Table(buffer io.Writer, name string, cc []color.Color) {
55	if name != "" {
56		buffer.Write([]byte(fmt.Sprintf(tablecaption, name)))
57	}
58	buffer.Write([]byte(tableheader))
59	for _, c := range cc {
60		Cell(buffer, c)
61	}
62	buffer.Write([]byte(tablefooter))
63}
64