Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Feb 10, 2024
1 parent 3ed24a4 commit d3b486e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 53 deletions.
21 changes: 12 additions & 9 deletions rich_pixels/_pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class Pixels:

def __init__(self) -> None:
self._segments: Segments | None = None

Expand Down Expand Up @@ -96,23 +95,27 @@ def __rich_console__(
if __name__ == "__main__":
console = Console()
images_path = Path(__file__).parent / "../tests/.sample_data/images"
pixels = Pixels.from_image_path(images_path / "bulbasaur.png",
renderer=FullcellRenderer())
pixels = Pixels.from_image_path(
images_path / "bulbasaur.png", renderer=FullcellRenderer()
)
console.print("\\[case.1] print with fullpixels renderer")
console.print(pixels)

pixels = Pixels.from_image_path(images_path / "bulbasaur.png",
renderer=FullcellRenderer(default_color="black"))
pixels = Pixels.from_image_path(
images_path / "bulbasaur.png", renderer=FullcellRenderer(default_color="black")
)
console.print("\\[case.2] print with fullpixels renderer and default_color")
console.print(pixels)

pixels = Pixels.from_image_path(images_path / "bulbasaur.png",
renderer=HalfcellRenderer())
pixels = Pixels.from_image_path(
images_path / "bulbasaur.png", renderer=HalfcellRenderer()
)
console.print("\\[case.3] print with halfpixels renderer")
console.print(pixels)

pixels = Pixels.from_image_path(images_path / "bulbasaur.png",
renderer=HalfcellRenderer(default_color="black"))
pixels = Pixels.from_image_path(
images_path / "bulbasaur.png", renderer=HalfcellRenderer(default_color="black")
)
console.print("\\[case.4] print with halfpixels renderer and default_color")
console.print(pixels)

Expand Down
65 changes: 24 additions & 41 deletions rich_pixels/_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def __init__(
default_color: str | None = None,
) -> None:
self.default_color = default_color
self.null_style = None if default_color is None else Style.parse(
f"on {default_color}")
self.null_style = (
None if default_color is None else Style.parse(f"on {default_color}")
)

def render(self, image: Image, resize: Tuple[int, int] | None) -> list[Segment]:
"""
Expand All @@ -48,8 +49,9 @@ def render(self, image: Image, resize: Tuple[int, int] | None) -> list[Segment]:
for y in self._get_range(height):
this_row: list[Segment] = []

this_row += self._render_line(line_index=y, width=width,
get_pixel=get_pixel)
this_row += self._render_line(
line_index=y, width=width, get_pixel=get_pixel
)
this_row.append(Segment("\n", self.null_style))

# TODO: Double-check if this is required - I've forgotten...
Expand All @@ -65,11 +67,7 @@ def _get_range(self, height: int) -> range:
raise NotImplementedError

def _render_line(
self,
*,
line_index: int,
width: int,
get_pixel: GetPixel
self, *, line_index: int, width: int, get_pixel: GetPixel
) -> list[Segment]:
"""
Render a line of pixels.
Expand All @@ -89,42 +87,35 @@ def render(self, image: Image, resize: Tuple[int, int] | None) -> list[Segment]:
target_height += 1

if image.size[1] != target_height:
resize = (resize[0], target_height) if resize else (
image.size[0], target_height)
resize = (
(resize[0], target_height) if resize else (image.size[0], target_height)
)

return super().render(image, resize)

def _get_range(self, height: int) -> range:
return range(0, height, 2)

def _render_line(
self,
*,
line_index: int,
width: int,
get_pixel: GetPixel
self, *, line_index: int, width: int, get_pixel: GetPixel
) -> list[Segment]:
line = []
for x in range(width):
line.append(self._render_halfcell(x=x, y=line_index, get_pixel=get_pixel))
return line

def _render_halfcell(
self,
*,
x: int,
y: int,
get_pixel: GetPixel
) -> Segment:
def _render_halfcell(self, *, x: int, y: int, get_pixel: GetPixel) -> Segment:
colors = []

# get lower pixel, render lower pixel use foreground color, so it must be first
lower_color = _get_color(get_pixel((x, y + 1)),
default_color=self.default_color)
lower_color = _get_color(
get_pixel((x, y + 1)), default_color=self.default_color
)
colors.append(lower_color or "")
# get upper pixel, render upper pixel use background color, it is optional
upper_color = _get_color(get_pixel((x, y)), default_color=self.default_color)
if upper_color: colors.append(upper_color or "")
if upper_color:
colors.append(upper_color or "")

style = Style.parse(" on ".join(colors)) if colors else self.null_style
# use lower halfheight block to render if lower pixel is not transparent
Expand All @@ -140,26 +131,18 @@ def _get_range(self, height: int) -> range:
return range(height)

def _render_line(
self,
*,
line_index: int,
width: int,
get_pixel: GetPixel
self, *, line_index: int, width: int, get_pixel: GetPixel
) -> list[Segment]:
line = []
for x in range(width):
line.append(self._render_fullcell(x=x, y=line_index, get_pixel=get_pixel))
return line

def _render_fullcell(
self,
*,
x: int,
y: int,
get_pixel: GetPixel
) -> Segment:
def _render_fullcell(self, *, x: int, y: int, get_pixel: GetPixel) -> Segment:
pixel = get_pixel((x, y))
style = Style.parse(
f"on {_get_color(pixel, default_color=self.default_color)}") if pixel[
3] > 0 else self.null_style
style = (
Style.parse(f"on {_get_color(pixel, default_color=self.default_color)}")
if pixel[3] > 0
else self.null_style
)
return Segment(" ", style)
10 changes: 7 additions & 3 deletions tests/test_pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def get_console():

def test_png_image_path(svg_snapshot):
console = get_console()
pixels = Pixels.from_image_path(SAMPLE_DATA_DIR / "images/bulbasaur.png", renderer=FullcellRenderer())
pixels = Pixels.from_image_path(
SAMPLE_DATA_DIR / "images/bulbasaur.png", renderer=FullcellRenderer()
)
console.print(pixels)
svg = console.export_svg()
assert svg == svg_snapshot
Expand All @@ -33,8 +35,10 @@ def test_png_image_path(svg_snapshot):
def test_ascii_text(svg_snapshot):
console = get_console()
ascii = (SAMPLE_DATA_DIR / "ascii/rich_pixels.txt").read_text(encoding="utf-8")
mapping = {"#": Segment(" ", Style.parse("on #50b332")),
"=": Segment(" ", Style.parse("on #10ada3"))}
mapping = {
"#": Segment(" ", Style.parse("on #50b332")),
"=": Segment(" ", Style.parse("on #10ada3")),
}
pixels = Pixels.from_ascii(
ascii,
mapping=mapping,
Expand Down

0 comments on commit d3b486e

Please sign in to comment.