Skip to content

Commit 3e64e75

Browse files
aykevldeadprogram
authored andcommitted
epd2in13: use better black/white approximation
The previoius behavior was that entirely black pixels were treated as white, and anything else as black. That's at least counter-intuitive. This patch changes the behavior to actually look at the color values and use a cutoff around medium gray: darker colors are treated as black, and lighter colors are treated as white. This is a backwards incompatible change, but I think this behavior makes a lot more sense.
1 parent 2c2da5c commit 3e64e75

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

waveshare-epd/epd2in13/epd2in13.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,22 @@ func (d *Device) SetLUT(fullUpdate bool) {
179179
}
180180

181181
// SetPixel modifies the internal buffer in a single pixel.
182-
// The display have 2 colors: black and white
183-
// We use RGBA(0,0,0, 255) as white (transparent)
184-
// Anything else as black
182+
// The display have 2 colors: black and white. We use a very simple cutoff to
183+
// determine whether a pixel is black or white (darker colors are black, lighter
184+
// colors are white).
185185
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
186186
x, y = d.xy(x, y)
187187
if x < 0 || x >= d.logicalWidth || y < 0 || y >= d.height {
188188
return
189189
}
190190
byteIndex := (x + y*d.logicalWidth) / 8
191-
if c.R == 0 && c.G == 0 && c.B == 0 { // TRANSPARENT / WHITE
191+
// Very simle black/white split.
192+
// This isn't very accurate (especially for sRGB colors) but is close enough
193+
// to the truth that it probably doesn't matter much - especially on an
194+
// e-paper display.
195+
if int(c.R)+int(c.G)+int(c.B) > 128*3 { // light, convert to white
192196
d.buffer[byteIndex] |= 0x80 >> uint8(x%8)
193-
} else { // WHITE / EMPTY
197+
} else { // dark, convert to black
194198
d.buffer[byteIndex] &^= 0x80 >> uint8(x%8)
195199
}
196200
}

0 commit comments

Comments
 (0)