diff --git a/README.md b/README.md index 0e09ffd..87fab0e 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Fractional positions and sizes are accepted: `drawRectangle`, `drawButton` and ` Color constants: `Graphik.black`, `Graphik.white`, `Graphik.red`, `Graphik.green`, `Graphik.blue`. -An alpha component in a fill color (`(r, g, b, a)`) does not make `drawRectangle` or `drawButton`'s box translucent: on the display the shape is painted opaquely whatever `a` is, and on a surface with per-pixel alpha (`pygame.SRCALPHA`) the color's alpha replaces the pixels' own rather than blending over them. +An alpha component in a fill color (`(r, g, b, a)`) does not make `drawRectangle` or `drawButton`'s box translucent: on the display the shape is painted opaquely whatever `a` is, and on a surface with per-pixel alpha (`pygame.SRCALPHA`) the color's alpha replaces the pixels' own rather than blending over them. `drawText`'s `color` and `drawButton`'s `colorText` ignore an alpha component altogether: the text is painted as if `a` were 255, on the display and on a per-pixel alpha surface alike (only the antialiased glyph edges blend with what is underneath). ## Dependencies - pygame diff --git a/src/main/python/preponderous/graphik/graphik.py b/src/main/python/preponderous/graphik/graphik.py index 29092d2..53f03bf 100644 --- a/src/main/python/preponderous/graphik/graphik.py +++ b/src/main/python/preponderous/graphik/graphik.py @@ -168,7 +168,9 @@ def drawText(self, text, xpos, ypos, size, color): and one font per distinct size is cached and reused while a display mode is set; with none set, the font is rebuilt on every call. - color: Text color as an ``(r, g, b)`` tuple. + color: Text color as an ``(r, g, b)`` tuple. An alpha component + is ignored: the text is painted as if it were 255, on any + surface. """ myFont = self._getFont(size) textSurface = myFont.render(text, True, color) @@ -216,7 +218,8 @@ def drawButton(self, xpos, ypos, width, height, colorBox, colorText, sizeText, t colorBox: Fill color of the box as an ``(r, g, b)`` tuple. As for drawRectangle's ``color``, an alpha component is not blended. - colorText: Color of the label as an ``(r, g, b)`` tuple. + colorText: Color of the label as an ``(r, g, b)`` tuple. As for + drawText's ``color``, an alpha component is ignored. sizeText: Font size of the label, in points, as an ``int``. A float raises ``TypeError`` once the box is painted, before the hit test, so ``function`` is not called. The same diff --git a/src/test/python/preponderous/graphik/test_graphik.py b/src/test/python/preponderous/graphik/test_graphik.py index 759580e..7a2360d 100644 --- a/src/test/python/preponderous/graphik/test_graphik.py +++ b/src/test/python/preponderous/graphik/test_graphik.py @@ -700,6 +700,33 @@ def test_draw_text_renders_in_the_requested_color(): assert all(g == 0 and b == 0 for _, g, b in inked) +@pytest.mark.parametrize("alpha", [0, 128]) +def test_draw_text_ignores_the_alpha_of_its_color(alpha): + # An (r, g, b, a) text color is not drawn translucently: the alpha is + # dropped, so the fully-covered stem pixels come out as the opaque RGB + # color, even at a = 0 -- on the display and on a per-pixel alpha surface + # alike. Antialiasing still blends the glyph edges, as with any color. + graphik = _make_graphik((100, 100)) + display = graphik.getGameDisplay() + display.fill(Graphik.black) + + graphik.drawText("W", 50, 50, 40, Graphik.red + (alpha,)) + + inked = { + _rgb(display, (x, y)) + for x in range(100) + for y in range(100) + } - {Graphik.black} + assert Graphik.red in inked + + surface = pygame.Surface((100, 100), pygame.SRCALPHA) + surface.fill(Graphik.blue + (255,)) + Graphik(surface).drawText("W", 50, 50, 40, Graphik.red + (alpha,)) + + pixels = {tuple(surface.get_at((x, y))) for x in range(100) for y in range(100)} + assert Graphik.red + (255,) in pixels + + def test_draw_text_antialiases_the_glyph_edges(): # Aliased rendering would leave only two colors on the surface, the text # color and the background, and every other text test would still pass.