forked from mewbak/framebuffer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgb555.go
68 lines (54 loc) · 1.33 KB
/
rgb555.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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package framebuffer
import (
"image"
"image/color"
)
var RGB555Model = color.ModelFunc(
func(c color.Color) color.Color {
if _, ok := c.(RGBColor); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGBColor{
uint8(r >> (8 + (8 - 5))),
uint8(g >> (8 + (8 - 5))),
uint8(b >> (8 + (8 - 5))),
}
})
type RGB555 struct {
Pix []byte
Rect image.Rectangle
Stride int
}
func (i *RGB555) Bounds() image.Rectangle { return i.Rect }
func (i *RGB555) ColorModel() color.Model { return RGB555Model }
func (i *RGB555) At(x, y int) color.Color {
if !(image.Point{x, y}.In(i.Rect)) {
return RGBColor{}
}
pix := i.Pix[i.PixOffset(x, y):]
clr := uint16(pix[0])<<8 | uint16(pix[1])
return RGBColor{
uint8(clr>>10) & mask5,
uint8(clr>>5) & mask5,
uint8(clr) & mask5,
}
}
func (i *RGB555) Set(x, y int, c color.Color) {
i.SetRGB(x, y, RGB555Model.Convert(c).(RGBColor))
}
func (i *RGB555) SetRGB(x, y int, c RGBColor) {
if !(image.Point{x, y}.In(i.Rect)) {
return
}
n := i.PixOffset(x, y)
pix := i.Pix[n:]
clr := (uint16(c.R) << 10) | uint16(c.G<<5) | uint16(c.B)
pix[0] = uint8(clr)
pix[1] = uint8(clr >> 8)
}
func (i *RGB555) PixOffset(x, y int) int {
return (y-i.Rect.Min.Y)*i.Stride + (x-i.Rect.Min.X)*2
}