"""Offline verification lab for Android Ch 119: tutorial-colorfilterl2."""
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}"
LAYOUT_XML = """<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="5dp" tools:context=".MainActivity">
<ImageView android:id="@+id/img_meizi" android:layout_width="300dp" android:layout_height="300dp" android:src="@mipmap/img_demo" />
<EditText android:id="@+id/edit_mul" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" />
<EditText android:id="@+id/edit_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" />
<Button android:id="@+id/btn_change" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="变化" />
</RelativeLayout>"""
def lighting_filter(argb, mul: int, add: int):
a, r, g, b = argb
mr, mg, mb = (mul >> 16) & 0xFF, (mul >> 8) & 0xFF, mul & 0xFF
ar, ag, ab = (add >> 16) & 0xFF, (add >> 8) & 0xFF, add & 0xFF
nr = min(255, max(0, (r * mr) // 255 + ar))
ng = min(255, max(0, (g * mg) // 255 + ag))
nb = min(255, max(0, (b * mb) // 255 + ab))
return [a, nr, ng, nb]
def case_lighting_color_filter_exact_skia_formula():
px = [255, 200, 120, 80]
# Multiply red by 1.0 (0xFF), green by ~0.5 (0x80), blue by 0 (0x00), add +30 green
out = lighting_filter(px, 0xFF8000, 0x001E00)
# Contrast with clamping (not modulo % 255!) when result exceeds 255
clamped = lighting_filter(px, 0xFFFFFF, 0x640000) # 200 + 100 = 300 -> clamped to 255
actual = {
"filtered_argb": out,
"clamped_overflow_argb": clamped,
"uses_clamp_0_255_not_modulo": clamped[1] == 255,
}
expected = {
"filtered_argb": [255, 200, 90, 0],
"clamped_overflow_argb": [255, 255, 120, 80],
"uses_clamp_0_255_not_modulo": True,
}
return {
"name": "lighting_color_filter_exact_skia_formula",
"summary": "Verify Skia LightingColorFilter per-channel clamp((C*mul_c)//255 + add_c, 0, 255) math",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_lighting_filter_identity_tint_and_channel_removal():
px = [255, 180, 140, 100]
actual = {
"identity_0xFFFFFF_0x000000": lighting_filter(px, 0xFFFFFF, 0x000000),
"remove_blue_yellow_tint": lighting_filter(px, 0xFFFF00, 0x000000),
"add_green_highlight": lighting_filter(px, 0xFFFFFF, 0x003000),
"solid_red_flood": lighting_filter(px, 0x000000, 0xFF0000),
}
expected = {
"identity_0xFFFFFF_0x000000": [255, 180, 140, 100],
"remove_blue_yellow_tint": [255, 180, 140, 0],
"add_green_highlight": [255, 180, 188, 100],
"solid_red_flood": [255, 255, 0, 0],
}
return {
"name": "lighting_filter_identity_tint_and_channel_removal",
"summary": "Verify LightingColorFilter identity, blue channel removal, green boost, and solid color flood",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_lighting_filter_alpha_invariance_and_clamp():
semi_px = [128, 200, 150, 100]
out = lighting_filter(semi_px, 0xFFFFFF, 0x646464)
actual = {
"input_alpha": semi_px[0],
"output_alpha_unchanged": out[0],
"output_argb": out,
}
expected = {
"input_alpha": 128,
"output_alpha_unchanged": 128,
"output_argb": [128, 255, 250, 200],
}
return {
"name": "lighting_filter_alpha_invariance_and_clamp",
"summary": "Verify LightingColorFilter leaves Alpha untouched and clamps RGB channels at 255",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_layout_and_hex_input_parsing_caveat():
root = ET.fromstring(LAYOUT_XML)
ids = [c.attrib[NS + "id"] for c in root if (NS + "id") in c.attrib]
hex_str = "FFFF00"
parsed_hex = int(hex_str, 16)
dec_fails = False
try:
int(hex_str, 10)
except ValueError:
dec_fails = True
actual = {
"layout_ids": ids,
"hex_FFFF00_value": parsed_hex,
"base10_parseInt_rejects_hex_letters": dec_fails,
}
expected = {
"layout_ids": ["@+id/img_meizi", "@+id/edit_mul", "@+id/edit_add", "@+id/btn_change"],
"hex_FFFF00_value": 16776960,
"base10_parseInt_rejects_hex_letters": True,
}
return {
"name": "layout_and_hex_input_parsing_caveat",
"summary": "Verify layout widget IDs and radix-16 parsing requirement for hex color inputs",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_lighting_color_filter_exact_skia_formula(),
case_lighting_filter_identity_tint_and_channel_removal(),
case_lighting_filter_alpha_invariance_and_clamp(),
case_layout_and_hex_input_parsing_caveat(),
]
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-colorfilterl2-lab/explore-tutorial-colorfilterl2.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-colorfilterl2.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1858.zip
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip