"""Offline verification lab for Android Ch 113: tutorial-xfermode-porterduff."""
import json
VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"
def split_argb(c: int):
return ((c >> 24) & 0xFF, (c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF)
def rgb_max_diff(c1: int, c2: int) -> int:
_, r1, g1, b1 = split_argb(c1)
_, r2, g2, b2 = split_argb(c2)
return max(abs(r1 - r2), abs(g1 - g2), abs(b1 - b2))
def apply_avoid_xfermode(dst: int, src: int, op_color: int, tolerance: int, mode: str) -> int:
diff = rgb_max_diff(dst, op_color)
if mode == "TARGET":
return src if diff <= tolerance else dst
elif mode == "AVOID":
return src if diff > (255 - tolerance) else dst
raise ValueError(mode)
def apply_pixel_xor(dst: int, src: int, op_color: int) -> int:
return ((op_color ^ src ^ dst) & 0x00FFFFFF) | 0xFF000000
def case_avoid_xfermode_target_mode_tolerance():
paint_red = 0xFFDE5347 # ARGB(255, 222, 83, 71)
# Exact match with tolerance=0
exact_hit = apply_avoid_xfermode(0xFFCCD1D4, paint_red, 0xFFCCD1D4, 0, "TARGET")
exact_miss = apply_avoid_xfermode(0xFFCCD1D5, paint_red, 0xFFCCD1D4, 0, "TARGET")
# Tolerance=25 around 0xFFD9E5F3
near_hit = apply_avoid_xfermode(0xFFC8DCF0, paint_red, 0xFFD9E5F3, 25, "TARGET")
far_miss = apply_avoid_xfermode(0xFF90A0B0, paint_red, 0xFFD9E5F3, 25, "TARGET")
actual = {
"tol0_exact_match_tinted": f"0x{exact_hit:08X}",
"tol0_off_by_1_preserved": f"0x{exact_miss:08X}",
"tol25_within_range_tinted": f"0x{near_hit:08X}",
"tol25_far_color_preserved": f"0x{far_miss:08X}",
}
expected = {
"tol0_exact_match_tinted": "0xFFDE5347",
"tol0_off_by_1_preserved": "0xFFCCD1D5",
"tol25_within_range_tinted": "0xFFDE5347",
"tol25_far_color_preserved": "0xFF90A0B0",
}
return {
"name": "avoid_xfermode_target_mode_tolerance",
"summary": "Verify AvoidXfermode.Mode.TARGET exact (tol=0) and tolerance=25 color matching",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_avoid_xfermode_avoid_mode_tolerance():
paint_red = 0xFFDE5347
op_color = 0xFFD9E5F3
# With AVOID and tolerance=230, threshold = 255 - 230 = 25 -> diff > 25 gets tinted, diff <= 25 protected
protected = apply_avoid_xfermode(0xFFD9E5F3, paint_red, op_color, 230, "AVOID")
tinted = apply_avoid_xfermode(0xFF404040, paint_red, op_color, 230, "AVOID")
actual = {
"matching_pixel_protected": f"0x{protected:08X}",
"different_pixel_tinted": f"0x{tinted:08X}",
}
expected = {
"matching_pixel_protected": "0xFFD9E5F3",
"different_pixel_tinted": "0xFFDE5347",
}
return {
"name": "avoid_xfermode_avoid_mode_tolerance",
"summary": "Verify AvoidXfermode.Mode.AVOID protects opColor while tinting dissimilar pixels",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pixel_xor_xfermode_bitwise_formula():
op_color = 0xFFD9E5F3
src = 0xFFDE5347
dst = 0xFF336699
res = apply_pixel_xor(dst, src, op_color)
a, r, g, b = split_argb(res)
actual = {
"result_hex": f"0x{res:08X}",
"alpha_forced_255": a,
"rgb": [r, g, b],
}
expected = {
"result_hex": "0xFF34D02D",
"alpha_forced_255": 255,
"rgb": [0x34, 0xD0, 0x2D],
}
return {
"name": "pixel_xor_xfermode_bitwise_formula",
"summary": "Verify PixelXorXfermode bitwise (opColor ^ src ^ dst) with opaque alpha=255",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_api16_deprecation_and_hardware_accel_notice():
deprecated_in_api = 16
actual = {
"avoid_and_pixelxor_deprecated_api": deprecated_in_api,
"hardware_accelerated_supported": False,
"recommended_replacement": "PorterDuffXfermode",
}
expected = {
"avoid_and_pixelxor_deprecated_api": 16,
"hardware_accelerated_supported": False,
"recommended_replacement": "PorterDuffXfermode",
}
return {
"name": "api16_deprecation_and_hardware_accel_notice",
"summary": "Verify API 16 deprecation status of AvoidXfermode & PixelXorXfermode and PorterDuffXfermode replacement",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_avoid_xfermode_target_mode_tolerance(),
case_avoid_xfermode_avoid_mode_tolerance(),
case_pixel_xor_xfermode_bitwise_formula(),
case_api16_deprecation_and_hardware_accel_notice(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-xfermode-porterduff-lab/explore-tutorial-xfermode-porterduff.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-xfermode-porterduff.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1980.zip