ui: introduce OpColor, a specialized OpImage for uniform colors

To avoid allocating an image.Image for OpImage.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-01 13:01:51 +02:00
parent 0061c73a89
commit 00a87f542d
3 changed files with 45 additions and 1 deletions
+29
View File
@@ -5,6 +5,7 @@ package draw
import (
"encoding/binary"
"image"
"image/color"
"math"
"gioui.org/ui"
@@ -18,6 +19,10 @@ type OpImage struct {
Rect image.Rectangle
}
type OpColor struct {
Col color.NRGBA
}
type OpDraw struct {
Rect f32.Rectangle
}
@@ -57,6 +62,30 @@ func (i *OpImage) Decode(data []byte, refs []interface{}) {
}
}
func (c OpColor) Add(o *ui.Ops) {
data := make([]byte, ops.TypeColorLen)
data[0] = byte(ops.TypeColor)
data[1] = c.Col.R
data[2] = c.Col.G
data[3] = c.Col.B
data[4] = c.Col.A
o.Write(data)
}
func (c *OpColor) Decode(data []byte, refs []interface{}) {
if ops.OpType(data[0]) != ops.TypeColor {
panic("invalid op")
}
*c = OpColor{
Col: color.NRGBA{
R: data[1],
G: data[2],
B: data[3],
A: data[4],
},
}
}
func (d OpDraw) Add(o *ui.Ops) {
data := make([]byte, ops.TypeDrawLen)
data[0] = byte(ops.TypeDraw)