diff --git a/book2/image.py b/book2/image.py index 7389c3e..1c472b2 100644 --- a/book2/image.py +++ b/book2/image.py @@ -4,7 +4,13 @@ class TextureImage: + ''' + A tool class for loading image file. + Attributes: + filename: The image file. + + ''' def __init__(self, filename: str) -> None: self._load(filename) @@ -12,18 +18,32 @@ def _load(self, filename: str) -> None: image = Image.open(filename) self.image_height = image.height self.image_width = image.width - self.data = [[0 for i in range(self.image_width)] for j in range(self.image_height)] + self.data = [[0 for i in range(self.image_width)] + for j in range(self.image_height)] _data = image.load() for w in range(self.image_width): for h in range(self.image_height): r, g, b = _data[w, h] self.data[h][w] = (r, g, b) - + def pixel_data(self, x: int, y: int) -> "Color": + ''' + Find pixel :class:`Color` with given coordinate. + + Args: + x: The x coord. + y: The y coord. + + Returns: + Color: The color info. + + ''' x = self.clamp(x, 0, self.image_width) y = self.clamp(y, 0, self.image_height) - return Color(x=self.data[y][x][0], y=self.data[y][x][1], z=self.data[y][x][2]) - + return Color(x=self.data[y][x][0], + y=self.data[y][x][1], + z=self.data[y][x][2]) + @staticmethod def clamp(x: int, low: int, high: int) -> int: if x < low: @@ -36,7 +56,3 @@ def clamp(x: int, low: int, high: int) -> int: if __name__ == '__main__': ti = TextureImage('./earthmap.jpg') print(ti.data) - - - - diff --git a/book2/interval.py b/book2/interval.py index e5bc8b5..c855ee3 100644 --- a/book2/interval.py +++ b/book2/interval.py @@ -7,27 +7,32 @@ class Interval: ''' - Util class for detecting the relation between the target and two :type:`int`. + Util class for detecting the relation between the target and two + :type:`int`. Attributes: min: The minimum float. max: The maximum float. - + a: One of :class:`Interval`s when it is made up by two + :class:`Intervals`. + b: Another :class:`Interval`. ''' - def __init__(self, min: float=-INFINITY, max: float=INFINITY, - a: Optional["Interval"]=None, b: Optional["Interval"]=None) -> None: + def __init__(self, min: float = -INFINITY, max: float = INFINITY, + a: Optional["Interval"] = None, + b: Optional["Interval"] = None) -> None: self.min = min self.max = max - # NOTE: `a` and `b` will cover the `min` and `max` if both + # NOTE: `a` and `b` will cover the `min` and `max` if both # assigned. Use dict parameters if you want to pass to `a` and `b`. if a is not None and b is not None: self.min = a.min if a.min < b.min else b.min self.max = a.max if a.max > b.max else b.max - + def size(self) -> float: + '''The difference between the :arg:`max` and the :arg:`min`.''' return self.max - self.min - + def contains(self, x: float) -> bool: ''' Detect if the target is in the :class:`Interval` with edges included. @@ -37,17 +42,18 @@ def contains(self, x: float) -> bool: ''' return x >= self.min and x <= self.max - + def surrounds(self, x: float) -> bool: ''' - Detect if the target is out of the :class:`Interval` with edges excluded. + Detect if the target is out of the :class:`Interval` with edges + excluded. Args: x: The target. ''' return x > self.min and x < self.max - + def clamp(self, x: float) -> float: ''' Return the clamped value of the target within the :class:`Interval`. @@ -62,7 +68,7 @@ def clamp(self, x: float) -> float: return self.max else: return x - + def expand(self, delta: float) -> "Interval": padding = delta / 2 return Interval(self.min - padding, self.max + padding) @@ -71,4 +77,4 @@ def expand(self, delta: float) -> "Interval": EMPTY = Interval(+INFINITY, -INFINITY) -UNIVERSE = Interval(-INFINITY, +INFINITY) \ No newline at end of file +UNIVERSE = Interval(-INFINITY, +INFINITY)