Compare commits

..

2 Commits

Author SHA1 Message Date
blueloveTH
2bdbe8a684 fix color32.alpha_blend 2025-04-21 01:20:31 +08:00
blueloveTH
6c25f6b1de fix a bug 2025-04-20 23:41:18 +08:00
3 changed files with 12 additions and 3 deletions

View File

@ -216,7 +216,7 @@ class color32:
def ansi_bg(self, text: str) -> str: ... def ansi_bg(self, text: str) -> str: ...
@staticmethod @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: ... def rgb(r: int, g: int, b: int) -> color32: ...

View File

@ -896,7 +896,7 @@ static bool color32_from_hex_STATIC(int argc, py_Ref argv) {
c11_color32 color; c11_color32 color;
int res; int res;
if(hex.size == 7) { if(hex.size == 7) {
res = scanf(hex.data, "#%2hhx%2hhx%2hhx", &color.r, &color.g, &color.b); res = sscanf(hex.data, "#%2hhx%2hhx%2hhx", &color.r, &color.g, &color.b);
if(res != 3) return ValueError("invalid hex color format"); if(res != 3) return ValueError("invalid hex color format");
color.a = 255; color.a = 255;
} else { } else {
@ -1071,6 +1071,12 @@ static bool vmath_rgba(int argc, py_Ref argv) {
static bool color32_alpha_blend_STATIC(int argc, py_Ref argv) { static bool color32_alpha_blend_STATIC(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); 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 src = py_tocolor32(&argv[0]);
c11_color32 dst = py_tocolor32(&argv[1]); c11_color32 dst = py_tocolor32(&argv[1]);
float alpha = src.a / 255.0f; float alpha = src.a / 255.0f;

View File

@ -12,6 +12,7 @@ assert a.with_a(255).a == 255 and a.with_a(255).g == a.g
assert a.to_hex() == '#64c8ff78' assert a.to_hex() == '#64c8ff78'
assert color32.from_hex('#64c8ff78') == a assert color32.from_hex('#64c8ff78') == a
assert color32.from_hex('#64c8ff') == a.with_a(255)
assert rgb(100, 200, 255) != a assert rgb(100, 200, 255) != a
assert rgba(100, 200, 255, 120 / 255) == a assert rgba(100, 200, 255, 120 / 255) == a
@ -30,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) c = rgb(100, 200, 255)
assert c.ansi_fg('xxx') == '\x1b[38;2;100;200;255mxxx\x1b[0m' 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' assert c.ansi_bg('xxx') == '\x1b[48;2;100;200;255mxxx\x1b[0m'
assert color32.alpha_blend(a, None) == a