pixel: add support for Monochrome types such as the SSD1306 display

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2024-03-01 21:31:06 +01:00
committed by Ron Evans
parent a74770b2e6
commit 60801ba852
3 changed files with 114 additions and 10 deletions
+33 -2
View File
@@ -14,9 +14,9 @@ import (
// 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.
// The color format is sRGB (or close to it) in all cases except for 1-bit.
type Color interface {
RGB888 | RGB565BE | RGB555 | RGB444BE
RGB888 | RGB565BE | RGB555 | RGB444BE | Monochrome
BaseColor
}
@@ -50,6 +50,8 @@ func NewColor[T Color](r, g, b uint8) T {
return any(NewRGB555(r, g, b)).(T)
case RGB444BE:
return any(NewRGB444BE(r, g, b)).(T)
case Monochrome:
return any(NewMonochrome(r, g, b)).(T)
default:
panic("unknown color format")
}
@@ -202,6 +204,35 @@ func (c RGB444BE) RGBA() color.RGBA {
return color
}
type Monochrome bool
func NewMonochrome(r, g, b uint8) Monochrome {
// Very simple black/white split.
// This isn't very accurate (especially for sRGB colors) but is close enough.
if int(r)+int(g)+int(b) > 128*3 { // light, convert to white
return Monochrome(true)
}
// dark, convert to black
return Monochrome(false)
}
func (c Monochrome) BitsPerPixel() int {
return 1
}
func (c Monochrome) RGBA() color.RGBA {
value := uint8(0)
if c {
value = 255
}
return color.RGBA{
R: value,
G: value,
B: value,
A: 255,
}
}
// Gamma brightness lookup table:
// https://victornpb.github.io/gamma-table-generator
// gamma = 0.45 steps = 256 range = 0-255