"""Offline verification lab for Android Ch 120: tutorial-colorfilter3."""
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"
NS = "{http://schemas.android.com/apk/res/android}"
VIEW_ITEM_XML = """<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<ImageView android:id="@+id/img_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />
<TextView android:id="@+id/tv_color" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical" android:textSize="12sp" android:text="颜色" android:textColor="#FFFFFFFF" />
<TextView android:id="@+id/tv_mode" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#FFD9ECFF" android:text="模式"/>
</LinearLayout>"""
MAIN_LAYOUT_XML = """<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<GridView android:id="@+id/gd_show" android:background="#FF333333" android:numColumns="6"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>"""
MODES_18 = [
"CLEAR", "SRC", "DST", "SRC_OVER", "DST_OVER", "SRC_IN",
"DST_IN", "SRC_OUT", "DST_OUT", "SRC_ATOP", "DST_ATOP", "XOR",
"DARKEN", "LIGHTEN", "MULTIPLY", "SCREEN", "ADD", "OVERLAY",
]
TEST_COLORS = [0x77E50961, 0xFFE50961, 0x77FFFFFF, 0xFFFFFFFF, 0x77000000, 0xFF000000]
def clamp01(x: float) -> float:
return 0.0 if x < 0.0 else (1.0 if x > 1.0 else x)
def argb_to_premul(c: int):
a = ((c >> 24) & 0xFF) / 255.0
r = ((c >> 16) & 0xFF) / 255.0 * a
g = ((c >> 8) & 0xFF) / 255.0 * a
b = (c & 0xFF) / 255.0 * a
return (a, r, g, b)
def premul_to_argb(p):
a, r, g, b = p
if a <= 1e-9:
return [0, 0, 0, 0]
return [
round(clamp01(a) * 255),
round(clamp01(r / a) * 255),
round(clamp01(g / a) * 255),
round(clamp01(b / a) * 255),
]
def apply_porterduff_color_filter(dst_c: int, filter_c: int, mode: str):
# In PorterDuffColorFilter(color, mode), filter_c is SRC and bitmap pixel is DST
sa, sr, sg, sb = argb_to_premul(filter_c)
da, dr, dg, db = argb_to_premul(dst_c)
sc, dc = (sr, sg, sb), (dr, dg, db)
if mode == "CLEAR":
return [0, 0, 0, 0]
if mode == "SRC":
return premul_to_argb((sa, sr, sg, sb))
if mode == "DST":
return premul_to_argb((da, dr, dg, db))
if mode == "SRC_OVER":
return premul_to_argb((sa + (1 - sa) * da, *(s + (1 - sa) * d for s, d in zip(sc, dc))))
if mode == "DST_OVER":
return premul_to_argb((da + (1 - da) * sa, *(d + (1 - da) * s for s, d in zip(sc, dc))))
if mode == "SRC_IN":
return premul_to_argb((sa * da, *(s * da for s in sc)))
if mode == "DST_IN":
return premul_to_argb((da * sa, *(d * sa for d in dc)))
if mode == "SRC_OUT":
return premul_to_argb((sa * (1 - da), *(s * (1 - da) for s in sc)))
if mode == "DST_OUT":
return premul_to_argb((da * (1 - sa), *(d * (1 - sa) for d in dc)))
if mode == "SRC_ATOP":
return premul_to_argb((da, *(s * da + d * (1 - sa) for s, d in zip(sc, dc))))
if mode == "DST_ATOP":
return premul_to_argb((sa, *(d * sa + s * (1 - da) for s, d in zip(sc, dc))))
if mode == "XOR":
return premul_to_argb((sa * (1 - da) + da * (1 - sa), *(s * (1 - da) + d * (1 - sa) for s, d in zip(sc, dc))))
if mode == "MULTIPLY":
return premul_to_argb((sa * da, *(s * d for s, d in zip(sc, dc))))
if mode == "SCREEN":
return premul_to_argb((sa + da - sa * da, *(s + d - s * d for s, d in zip(sc, dc))))
if mode == "ADD":
return premul_to_argb((clamp01(sa + da), *(clamp01(s + d) for s, d in zip(sc, dc))))
if mode == "DARKEN":
return premul_to_argb((sa + da - sa * da, *(s * (1 - da) + d * (1 - sa) + min(s * da, d * sa) for s, d in zip(sc, dc))))
if mode == "LIGHTEN":
return premul_to_argb((sa + da - sa * da, *(s * (1 - da) + d * (1 - sa) + max(s * da, d * sa) for s, d in zip(sc, dc))))
if mode == "OVERLAY":
a = sa + da - sa * da
out_c = []
for s, d in zip(sc, dc):
if 2 * d <= da:
c = 2 * s * d + s * (1 - da) + d * (1 - sa)
else:
c = sa * da - 2 * (da - d) * (sa - s) + s * (1 - da) + d * (1 - sa)
out_c.append(clamp01(c))
return premul_to_argb((a, *out_c))
raise ValueError(mode)
def case_porterduff_color_filter_18_modes_matrix():
items = [(c, m) for m in MODES_18 for c in TEST_COLORS]
icon_green = 0xFFA4C639 # Android green
sample_opaque_pink = {
m: apply_porterduff_color_filter(icon_green, 0xFFE50961, m)
for m in ("CLEAR", "SRC_IN", "DST_IN", "SRC_OUT", "MULTIPLY", "SCREEN")
}
actual = {
"mode_count": len(MODES_18),
"color_count": len(TEST_COLORS),
"total_grid_items": len(items),
"sample_opaque_pink_on_android_green": sample_opaque_pink,
}
expected = {
"mode_count": 18,
"color_count": 6,
"total_grid_items": 108,
"sample_opaque_pink_on_android_green": {
"CLEAR": [0, 0, 0, 0],
"SRC_IN": [255, 229, 9, 97],
"DST_IN": [255, 164, 198, 57],
"SRC_OUT": [0, 0, 0, 0],
"MULTIPLY": [255, 147, 7, 22],
"SCREEN": [255, 246, 200, 132],
},
}
return {
"name": "porterduff_color_filter_18_modes_matrix",
"summary": "Verify 18x6=108 PorterDuffColorFilter test grid and exact SRC=color vs DST=bitmap pixel blending",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_color_filter_src_in_and_multiply_icon_tinting():
transparent_bg = 0x00000000
opaque_white_icon = 0xFFFFFFFF
tint_color = 0xFFE50961
actual = {
"src_in_on_transparent_bg": apply_porterduff_color_filter(transparent_bg, tint_color, "SRC_IN"),
"src_in_on_opaque_icon": apply_porterduff_color_filter(opaque_white_icon, tint_color, "SRC_IN"),
"multiply_on_transparent_bg": apply_porterduff_color_filter(transparent_bg, tint_color, "MULTIPLY"),
"multiply_on_opaque_white": apply_porterduff_color_filter(opaque_white_icon, tint_color, "MULTIPLY"),
}
expected = {
"src_in_on_transparent_bg": [0, 0, 0, 0],
"src_in_on_opaque_icon": [255, 229, 9, 97],
"multiply_on_transparent_bg": [0, 0, 0, 0],
"multiply_on_opaque_white": [255, 229, 9, 97],
}
return {
"name": "color_filter_src_in_and_multiply_icon_tinting",
"summary": "Verify SRC_IN and MULTIPLY tint opaque icon pixels while leaving transparent background untouched",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_gridview_and_item_layout_xml_verification():
item_root = ET.fromstring(VIEW_ITEM_XML)
main_root = ET.fromstring(MAIN_LAYOUT_XML)
gv = main_root.find("GridView")
actual = {
"item_ids": [c.attrib[NS + "id"] for c in item_root],
"gridview_id": gv.attrib[NS + "id"],
"num_columns": int(gv.attrib[NS + "numColumns"]),
"grid_background": gv.attrib[NS + "background"],
}
expected = {
"item_ids": ["@+id/img_show", "@+id/tv_color", "@+id/tv_mode"],
"gridview_id": "@+id/gd_show",
"num_columns": 6,
"grid_background": "#FF333333",
}
return {
"name": "gridview_and_item_layout_xml_verification",
"summary": "Verify view_item.xml and activity_main.xml 6-column GridView structure",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_color_hex_formatting_and_adapter_binding():
formatted = [f"{c:08X}" for c in TEST_COLORS]
actual = {
"formatted_hex_labels": formatted,
"semi_transparent_alpha_hex": [s[:2] for s in formatted[::2]],
"opaque_alpha_hex": [s[:2] for s in formatted[1::2]],
}
expected = {
"formatted_hex_labels": ["77E50961", "FFE50961", "77FFFFFF", "FFFFFFFF", "77000000", "FF000000"],
"semi_transparent_alpha_hex": ["77", "77", "77"],
"opaque_alpha_hex": ["FF", "FF", "FF"],
}
return {
"name": "color_hex_formatting_and_adapter_binding",
"summary": "Verify %08X color label formatting and semi-transparent vs opaque test pairs",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_porterduff_color_filter_18_modes_matrix(),
case_color_filter_src_in_and_multiply_icon_tinting(),
case_gridview_and_item_layout_xml_verification(),
case_color_hex_formatting_and_adapter_binding(),
]
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-colorfilter3-lab/explore-tutorial-colorfilter3.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-colorfilter3.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1857.zip