Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: image() had wrong args parsing logic #382

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion p5/core/attribs.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def background(*args, **kwargs):
no_tint()
image_mode("corner")
with push_matrix():
image(background_image, (0, 0))
image(background_image, *(0, 0))

return background_image

Expand Down
4 changes: 4 additions & 0 deletions p5/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,7 @@ class SType(Enum):
# arc modes
PIE = "PIE"
CHORD = "CHORD"

# color parse modes
RGB = "RGB"
HSB = "HSB"
41 changes: 13 additions & 28 deletions p5/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
from ..pmath.utils import _is_numeric
from .structure import push_style

__all__ = ["PImage", "image", "load_image", "image_mode", "load_pixels"]
from . import constants

_image_mode = "corner"
__all__ = ["PImage", "image", "load_image", "image_mode", "load_pixels"]


def _ensure_loaded(func):
Expand All @@ -57,7 +57,7 @@ def rfunc(instance, *args, **kwargs):
def _restore_color_mode():
old_mode = color.color_parse_mode
old_range = color.color_range
color.color_mode("RGB", 255, 255, 255, 255)
color.color_mode(constants.RGB, 255, 255, 255, 255)

yield

Expand Down Expand Up @@ -495,6 +495,9 @@ def image(*args, size=None):
should be explicitly mentioned). The color of an image may be
modified with the :meth:`p5.tint` function.

:param img: the image to be displayed.
:type img: p5.Image

:param x: x-coordinate of the image by default
:type float:

Expand All @@ -507,25 +510,8 @@ def image(*args, size=None):
:param h: height to display the image by default
:type float:

:param img: the image to be displayed.
:type img: p5.Image

:param location: location of the image on the screen (depending on the
current image mode, 'corner', 'center', 'corners', this could
represent the coordinate of the top-left corner, center,
top-left corner respectively.)
:type location: tuple | list | np.ndarray | p5.Vector

:param size: target size of the image or the bottom-right image
corner when the image mode is set to 'corners'. By default,
the value is set according to the current image size.

:type size: tuple | list

"""
if len(args) == 2:
img, location = args
elif len(args) == 3:
if len(args) == 3:
img, location = args[0], args[1:]
elif len(args) == 5:
img, location, size = args[0], args[1:3], args[3:]
Expand All @@ -544,11 +530,11 @@ def image(*args, size=None):
lx, ly = location
sx, sy = size

if _image_mode == "center":
if p5.renderer.style.image_mode == constants.CENTER:
lx = int(lx - (sx / 2))
ly = int(ly - (sy / 2))

if _image_mode == "corners":
if p5.renderer.style.image_mode == constants.CORNERS:
sx = sx - lx
sy = sy - ly

Expand Down Expand Up @@ -582,11 +568,10 @@ def image_mode(mode):
Check for typoes.

"""
global _image_mode

if mode.lower() not in ["corner", "center", "corners"]:
raise ValueError("Unknown image mode!")
_image_mode = mode.lower()
p5.renderer.style.image_mode = mode.lower()


def load_image(filename):
Expand Down Expand Up @@ -629,7 +614,7 @@ def load_pixels():
to the image are written to the main display.

"""
pixels = PImage(builtins.width, builtins.height, "RGB")
pixels = PImage(builtins.width, builtins.height, constants.RGB)
# sketch.renderer.flush_geometry()
pixel_data = p5.renderer.fbuffer.read(mode="color", alpha=False)

Expand All @@ -641,9 +626,9 @@ def load_pixels():
yield

with push_style():
image_mode("corner")
image_mode(constants.CORNER)
p5.renderer.style.tint_enabled = False
image(builtins.pixels, (0, 0))
image(builtins.pixels, *(0, 0))

builtins.pixels = None

Expand Down
2 changes: 2 additions & 0 deletions p5/sketch/Skia2DRenderer/renderer2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Style2D:
text_style = constants.NORMAL
text_wrap_style = constants.WORD

image_mode = constants.CORNER

def set_stroke_cap(self, c):
if c == constants.ROUND:
self.stroke_cap = skia.Paint.kRound_Cap
Expand Down
10 changes: 6 additions & 4 deletions p5/sketch/Vispy2DRenderer/openglrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np

from p5.core import p5
from p5.core.constants import SType, ROUND, MITER, LEFT, TOP
from p5.core.constants import SType, ROUND, MITER, LEFT, TOP, CORNER, CENTER, RGB
from p5.pmath import matrix
from .shape import Arc, PShape

Expand Down Expand Up @@ -231,9 +231,9 @@ class Style2D:
stroke_weight = 1
tint_color = COLOR_BLACK
tint_enabled = False
ellipse_mode = "CENTER"
rect_mode = "CORNER"
color_parse_mode = "RGB"
ellipse_mode = CENTER
rect_mode = CORNER
color_parse_mode = RGB
color_range = (255, 255, 255, 255)
stroke_cap = ROUND
stroke_join = MITER
Expand All @@ -244,6 +244,8 @@ class Style2D:
text_align_y = TOP
text_leading = 0

image_mode = CORNER

def set_stroke_cap(self, c):
self.stroke_cap = c

Expand Down
4 changes: 2 additions & 2 deletions p5/sketch/Vispy2DRenderer/renderer2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ def text(self, text_string, position, wrap_at):
if self.style.fill_enabled:
self.style.tint_enabled = True
self.style.tint_color = self.style.fill_color
image(text_image, position)
image(text_image, *position)
if self.style.stroke_enabled and is_stroke_valid:
self.style.tint_enabled = True
self.style.tint_color = self.style.stroke_color
image(text_stroke_image, position)
image(text_stroke_image, *position)

return text_string

Expand Down