Files
beatglow/draw/pixel/color.go
T
2026-10-29 17:29:41 +01:00

233 lines
5.7 KiB
Go

package pixel
import "image/color"
// Pixel with a particular color, matching the underlying hardware of a
// particular display. Each pixel is at least 1 byte in size.
// The color format is sRGB (or close to it) in all cases except for 1-bit.
type Color interface {
Monochrome | RGB444 | RGB565
BaseColor
}
// BaseColor contains all the methods needed in a color format. This can be used
// in display drivers that want to define their own Color type with just the
// pixel formats the display supports.
type BaseColor interface {
color.Color
// The number of bits when stored.
// This means for example that RGB555 (which is still stored as a 16-bit
// integer) returns 16, while RGB444 returns 12.
BitsPerPixel() int
// Return the given color in color.RGBA format, which is always sRGB. The
// alpha channel is always 255.
RGBAColor() color.RGBA
}
// NewColor returns the given color based on the RGB values passed in the
// parameters. The input value is assumed to be in sRGB color space.
func NewColor[T Color](r, g, b uint8) T {
// Ugly cast from color.RGBA to T. The type switch and interface casts are
// trivially optimized away after instantiation.
var value T
switch any(value).(type) {
case Monochrome:
return any(NewMonochrome(r, g, b)).(T)
case RGB444:
return any(NewRGB444(r, g, b)).(T)
case RGB565:
return any(NewRGB565(r, g, b)).(T)
/*
case RGB555:
return any(NewRGB555(r, g, b)).(T)
case RGB888:
return any(NewRGB888(r, g, b)).(T)
*/
default:
panic("unknown color format")
}
}
// ToGrayBits converts a color to a n-bit grayscale color.
func ToGrayBits(c color.Color, n uint8) uint16 {
r, g, b, _ := c.RGBA()
if n < 1 {
return 0 // no color
} else if n > 16 {
n = 16 // most that will fit
}
// Shift values to fit n bits.
s := 32 - n
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification and used by func RGBToYCbCr in
// image/color/ycbcr.go.
//
// Note that 19595 + 38470 + 7471 equals 65536.
//
// The 28 is 16 + 8 + n. The 16 is the same as used in RGBToYCbCr. The 8 + n is
// because the return value is n-bit color, not 16 bit color.
return uint16((19595*r + 38470*g + 7471*b + 1<<15) >> s)
}
// Monochrome color.
type Monochrome uint8
// Monochrome color names.
const (
Off Monochrome = iota
On
)
func NewMonochrome(r, g, b uint8) Monochrome {
if ToGrayBits(color.RGBA{r, g, b, 0xff}, 1) == 1 {
return On
}
return Off
}
func (Monochrome) BitsPerPixel() int {
return 1
}
func (c Monochrome) RGBA() (r, g, b, a uint32) {
if c == 0 {
return 0, 0, 0, 0
}
return 0xffff, 0xffff, 0xffff, 0xffff
}
func (c Monochrome) RGBAColor() color.RGBA {
if c == 0 {
return color.RGBA{}
}
return color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
}
func IsOn(c color.Color) bool {
if _, ok := c.(Monochrome); ok {
return c == On
}
return ToGrayBits(c, 1) == 1
}
// Color format that is supported by the ST7789 for example.
// It may be a bit faster to use than RGB565BE on very slow SPI buses.
//
// The color format is native endian as a uint16 (0000rrrr_ggggbbbb), not big
// endian which you might expect. I tried swapping the bytes, but it didn't have
// much of a performance impact and made the code harder to read. It is stored
// as a 12-bit big endian value in Image[RGB444] though.
type RGB444 uint16
func NewRGB444(r, g, b uint8) RGB444 {
return RGB444(r>>4)<<8 | RGB444(g>>4)<<4 | RGB444(b>>4)
}
func (c RGB444) BitsPerPixel() int {
return 12
}
func (c RGB444) RGBA() (r, g, b, a uint32) {
r = uint32(c>>8) & 0x0f
r |= (r << 4) | (r << 8) | (r << 12)
g = uint32(c>>4) & 0x0f
g |= (g << 4) | (g << 8) | (g << 12)
b = uint32(c>>0) & 0x0f
b |= (b << 4) | (b << 8) | (b << 12)
a |= 0xffff
return
}
func (c RGB444) RGBAColor() color.RGBA {
color := color.RGBA{
R: uint8(c>>8) << 4,
G: uint8(c>>4) << 4,
B: uint8(c>>0) << 4,
A: 255,
}
// Correct color rounding, so that 0xff roundtrips back to 0xff.
color.R |= color.R >> 4
color.G |= color.G >> 4
color.B |= color.B >> 4
return color
}
// RGB565 color.
type RGB565 uint16
func NewRGB565(r, g, b uint8) RGB565 {
val := uint16(r&0xF8)<<8 +
uint16(g&0xFC)<<3 +
uint16(b&0xF8)>>3
// Swap endianness (make big endian).
// This is done using a single instruction on ARM (rev16).
// TODO: this should only be done on little endian systems, but TinyGo
// doesn't currently (2023) support big endian systems so it's difficult to
// test. Also, big endian systems don't seem fasionable these days.
//val = bits.ReverseBytes16(val)
return RGB565(val)
}
// FromColor retusn the given color.
func FromColor[T Color](c color.Color) T {
var value T
switch any(value).(type) {
case Monochrome:
return monochromeModel(c).(T)
case RGB444:
return rgb444Model(c).(T)
case RGB565:
return rgb565Model(c).(T)
/*
case RGB555:
return rgb555Model(c).(T)
case RGB888:
return rgb888Model(c).(T)
*/
default:
panic("unknown color format")
}
}
func (c RGB565) BitsPerPixel() int {
return 16
}
func (c RGB565) RGBA() (r, g, b, a uint32) {
r = uint32(uint8(c>>12) << 3)
r |= (r >> 5)
r |= (r << 8)
g = uint32(uint8(c>>5) << 2)
g |= (g >> 6)
g |= (g << 8)
b = uint32(uint8(c>>0) << 3)
b |= (b >> 5)
b |= (b << 8)
a = 0xffff
return
}
func (c RGB565) RGBAColor() color.RGBA {
// Note: on ARM, the compiler uses a rev instruction instead of a rev16
// instruction. I wonder whether this can be optimized further to use rev16
// instead?
c = c<<8 | c>>8
color := color.RGBA{
R: uint8(c>>11) << 3,
G: uint8(c>>5) << 2,
B: uint8(c) << 3,
A: 255,
}
// Correct color rounding, so that 0xff roundtrips back to 0xff.
color.R |= color.R >> 5
color.G |= color.G >> 6
color.B |= color.B >> 5
return color
}