summaryrefslogtreecommitdiff
path: root/colorbox.go
blob: a9d1583707b5b7eb306dd19b8e0686cde4c4b426 (plain)
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
package main

import (
	"fmt"
	"github.com/mattn/go-gtk/gdk"
	"github.com/mattn/go-gtk/gtk"
	"image/color"
)

func colorBox(c color.Color) *gtk.DrawingArea {
	r, g, b, _ := c.RGBA()
	colstring := fmt.Sprintf("#%02x%02x%02x", (r>>8)&0xff, (g>>8)&0xff, (b>>8)&0xff)

	dArea := gtk.NewDrawingArea()
	var pixmap *gdk.Pixmap
	var gc *gdk.GC

	dArea.Connect("configure-event", func() {
		if pixmap != nil {
			pixmap.Unref()
		}
		alloc := dArea.GetAllocation()
		pixmap = gdk.NewPixmap(dArea.GetWindow().GetDrawable(), alloc.Width, alloc.Height, 24)
		gc = gdk.NewGC(pixmap.GetDrawable())
		gc.SetRgbFgColor(gdk.NewColor(colstring))
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
	})

	dArea.Connect("expose-event", func() {
		if pixmap != nil {
			dArea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	return dArea
}