Convert pixels to valid on export

master
WIPocket 3 years ago
parent 6a2b181bdd
commit 0e56d3c7c3
No known key found for this signature in database
GPG Key ID: 3C8207ED1C3146F8

@ -6,6 +6,12 @@ class Pixel(namedtuple('Pixel', ['r', 'g', 'b'])):
raise ValueError('Some component too bright.') raise ValueError('Some component too bright.')
if min(self.r, self.g, self.b) < 0: if min(self.r, self.g, self.b) < 0:
raise ValueError('Some component negative.') raise ValueError('Some component negative.')
def _to_valid(self):
return Pixel(
int(max(0, min(255, self.r))),
int(max(0, min(255, self.g))),
int(max(0, min(255, self.b))),
)
def __add__(self, other): def __add__(self, other):
return Pixel( return Pixel(
self.r + other.r, self.r + other.r,
@ -58,7 +64,7 @@ class Picture:
seq = [] seq = []
for line in self.pixels: for line in self.pixels:
for pix in line: for pix in line:
seq.extend(pix) seq.extend(pix._to_valid())
# bytes… # bytes…
b = bytes(seq) b = bytes(seq)

Loading…
Cancel
Save