From 35774b54abf66ad1002c51f9e094ee4106b46a9d Mon Sep 17 00:00:00 2001 From: tushar5526 Date: Wed, 24 Aug 2022 14:03:11 +0530 Subject: [PATCH 1/4] Bug fix: image() had wrong args parsing logic Refactored image.py to use constants instead of strings --- p5/core/constants.py | 4 +++ p5/core/image.py | 33 ++++++--------------- p5/sketch/Skia2DRenderer/renderer2d.py | 2 ++ p5/sketch/Vispy2DRenderer/openglrenderer.py | 10 ++++--- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/p5/core/constants.py b/p5/core/constants.py index 8949b84b..fc56528f 100644 --- a/p5/core/constants.py +++ b/p5/core/constants.py @@ -243,3 +243,7 @@ class SType(Enum): # arc modes PIE = "PIE" CHORD = "CHORD" + +# color parse modes +RGB = "RGB" +HSB = "HSB" diff --git a/p5/core/image.py b/p5/core/image.py index 0a4149d3..39104d13 100644 --- a/p5/core/image.py +++ b/p5/core/image.py @@ -36,9 +36,9 @@ from ..pmath.utils import _is_numeric from .structure import push_style -__all__ = ["PImage", "image", "load_image", "image_mode", "load_pixels"] +import constants -_image_mode = "corner" +__all__ = ["PImage", "image", "load_image", "image_mode", "load_pixels"] def _ensure_loaded(func): @@ -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 @@ -510,22 +510,8 @@ def image(*args, size=None): :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:] @@ -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 @@ -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): @@ -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) @@ -641,7 +626,7 @@ 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)) diff --git a/p5/sketch/Skia2DRenderer/renderer2d.py b/p5/sketch/Skia2DRenderer/renderer2d.py index 176f657b..6c06bcc8 100644 --- a/p5/sketch/Skia2DRenderer/renderer2d.py +++ b/p5/sketch/Skia2DRenderer/renderer2d.py @@ -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 diff --git a/p5/sketch/Vispy2DRenderer/openglrenderer.py b/p5/sketch/Vispy2DRenderer/openglrenderer.py index 5cb975fc..9a37ed63 100644 --- a/p5/sketch/Vispy2DRenderer/openglrenderer.py +++ b/p5/sketch/Vispy2DRenderer/openglrenderer.py @@ -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 @@ -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 @@ -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 From 37fb9ea41cd641542a4d44c0c128c095cf24662b Mon Sep 17 00:00:00 2001 From: tushar5526 Date: Wed, 24 Aug 2022 14:09:34 +0530 Subject: [PATCH 2/4] Fix imports --- p5/core/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p5/core/image.py b/p5/core/image.py index 39104d13..57b3c57c 100644 --- a/p5/core/image.py +++ b/p5/core/image.py @@ -36,7 +36,7 @@ from ..pmath.utils import _is_numeric from .structure import push_style -import constants +from . import constants __all__ = ["PImage", "image", "load_image", "image_mode", "load_pixels"] From 09cebebfc81177d53d1d23542356b7a367685c5f Mon Sep 17 00:00:00 2001 From: tushar5526 Date: Wed, 24 Aug 2022 14:12:27 +0530 Subject: [PATCH 3/4] Update docs of image() --- p5/core/image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p5/core/image.py b/p5/core/image.py index 57b3c57c..32677c9c 100644 --- a/p5/core/image.py +++ b/p5/core/image.py @@ -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: @@ -507,9 +510,6 @@ 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 - """ if len(args) == 3: img, location = args[0], args[1:] From a3c35f02dd5e59fdfc71d1cf90a59d342e2e3812 Mon Sep 17 00:00:00 2001 From: tushar5526 Date: Wed, 24 Aug 2022 19:34:39 +0530 Subject: [PATCH 4/4] Update image() calls to use standard variables --- p5/core/attribs.py | 2 +- p5/core/image.py | 2 +- p5/sketch/Vispy2DRenderer/renderer2d.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/p5/core/attribs.py b/p5/core/attribs.py index 338c68a8..c0f503a1 100644 --- a/p5/core/attribs.py +++ b/p5/core/attribs.py @@ -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 diff --git a/p5/core/image.py b/p5/core/image.py index 32677c9c..81105b51 100644 --- a/p5/core/image.py +++ b/p5/core/image.py @@ -628,7 +628,7 @@ def load_pixels(): with push_style(): image_mode(constants.CORNER) p5.renderer.style.tint_enabled = False - image(builtins.pixels, (0, 0)) + image(builtins.pixels, *(0, 0)) builtins.pixels = None diff --git a/p5/sketch/Vispy2DRenderer/renderer2d.py b/p5/sketch/Vispy2DRenderer/renderer2d.py index 6af801b1..fa5c1bbc 100644 --- a/p5/sketch/Vispy2DRenderer/renderer2d.py +++ b/p5/sketch/Vispy2DRenderer/renderer2d.py @@ -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