diff --git a/lib.py b/lib.py index b456db9..fc74b16 100644 --- a/lib.py +++ b/lib.py @@ -6,6 +6,12 @@ class Pixel(namedtuple('Pixel', ['r', 'g', 'b'])): raise ValueError('Some component too bright.') if min(self.r, self.g, self.b) < 0: 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): return Pixel( self.r + other.r, @@ -58,7 +64,7 @@ class Picture: seq = [] for line in self.pixels: for pix in line: - seq.extend(pix) + seq.extend(pix._to_valid()) # bytes… b = bytes(seq)