From 2bdbe8a6842a835551c07053d8181d88e9873ae3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 21 Apr 2025 01:20:31 +0800 Subject: [PATCH] fix `color32.alpha_blend` --- include/typings/vmath.pyi | 2 +- src/modules/vmath.c | 6 ++++++ tests/80_color32.py | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/typings/vmath.pyi b/include/typings/vmath.pyi index 06477cc8..73df8992 100644 --- a/include/typings/vmath.pyi +++ b/include/typings/vmath.pyi @@ -216,7 +216,7 @@ class color32: def ansi_bg(self, text: str) -> str: ... @staticmethod - def alpha_blend(src: color32, dst: color32) -> color32: ... + def alpha_blend(src: color32, dst: color32 | None) -> color32: ... def rgb(r: int, g: int, b: int) -> color32: ... diff --git a/src/modules/vmath.c b/src/modules/vmath.c index e55a1777..92e7cdf9 100644 --- a/src/modules/vmath.c +++ b/src/modules/vmath.c @@ -1071,6 +1071,12 @@ static bool vmath_rgba(int argc, py_Ref argv) { static bool color32_alpha_blend_STATIC(int argc, py_Ref argv) { PY_CHECK_ARGC(2); + if(!py_checktype(&argv[0], tp_color32)) return false; + if(py_isnone(&argv[1])) { + py_assign(py_retval(), &argv[0]); + return true; + } + if(!py_checktype(&argv[1], tp_color32)) return false; c11_color32 src = py_tocolor32(&argv[0]); c11_color32 dst = py_tocolor32(&argv[1]); float alpha = src.a / 255.0f; diff --git a/tests/80_color32.py b/tests/80_color32.py index 8bd748a3..25a1f6ec 100644 --- a/tests/80_color32.py +++ b/tests/80_color32.py @@ -31,4 +31,6 @@ assert a.to_vec3i() == vec3i(int(a.r * alpha), int(a.g * alpha), int(a.b * alpha c = rgb(100, 200, 255) assert c.ansi_fg('xxx') == '\x1b[38;2;100;200;255mxxx\x1b[0m' -assert c.ansi_bg('xxx') == '\x1b[48;2;100;200;255mxxx\x1b[0m' \ No newline at end of file +assert c.ansi_bg('xxx') == '\x1b[48;2;100;200;255mxxx\x1b[0m' + +assert color32.alpha_blend(a, None) == a