NaraMotion / tests /test_context.py
ArifiIslam's picture
Release NaraMotion v0.26.0: Structure of Arrays (SoA), Path Arena Allocator, Uniform Grid Indexing, Dual-Buffer Rendering & Centroid Spring Pop-in
b516e00 verified
Raw History Blame Contribute Delete
2.04 kB
import pytest
from naramotion import (
Image, Context, Format, Rect, Circle, Ellipse, RoundRect,
Triangle, Line, Box, CompOp, FillRule, StrokeCap, StrokeJoin,
LinearGradient
)
def test_context_lifecycle():
img = Image(300, 300, Format.PRGB32)
ctx = Context(img)
ctx.fill_all("white")
ctx.end()
def test_context_shapes_rendering():
img = Image(500, 500, Format.PRGB32)
with Context(img) as ctx:
ctx.fill_all("#1A1A1A")
# Rect
ctx.fill_rect(20, 20, 80, 80, "red")
ctx.stroke_width = 3.0
ctx.stroke_rect(20, 20, 80, 80, "yellow")
# Circle
ctx.fill_circle(200, 60, 40, "blue")
ctx.stroke_circle(200, 60, 40, "cyan")
# Ellipse
ctx.fill_ellipse(350, 60, 50, 30, "green")
ctx.stroke_ellipse(350, 60, 50, 30, "lime")
# RoundRect
ctx.fill_round_rect(20, 150, 100, 60, 15, "magenta")
ctx.stroke_round_rect(20, 150, 100, 60, 15, "white")
# Triangle
ctx.fill_triangle(180, 210, 240, 150, 300, 210, "orange")
# Line
ctx.stroke_width = 5.0
ctx.stroke_line(20, 250, 480, 250, "purple")
# Arcs and Pies
ctx.fill_pie(100, 350, 60, 60, 0, 1.57, "gold")
ctx.stroke_arc(250, 350, 60, 60, 0, 3.14, "white")
def test_context_state_stack():
img = Image(200, 200, Format.PRGB32)
with Context(img) as ctx:
ctx.global_alpha = 0.5
ctx.stroke_width = 10.0
with ctx.push_state():
ctx.global_alpha = 1.0
ctx.stroke_width = 2.0
assert ctx.global_alpha == 1.0
assert ctx.stroke_width == 2.0
assert abs(ctx.global_alpha - 0.5) < 1e-4
assert abs(ctx.stroke_width - 10.0) < 1e-4
def test_context_clipping():
img = Image(200, 200, Format.PRGB32)
with Context(img) as ctx:
ctx.clip_to_rect(50, 50, 100, 100)
ctx.fill_all("red")
ctx.restore_clipping()