Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

fix(ui-kit): padding follows a smooth face gradient to its border

Soohong Kimcd7e42c5cf0d
diff --git a/docs/ui-kit.md b/docs/ui-kit.md
index 98b7900..209fe7c 100644
--- a/docs/ui-kit.md
+++ b/docs/ui-kit.md
@@ -60,7 +60,7 @@ So every column with an ornament, a cap, a border line or their anti-aliased edg
## Padding
-Padding places the content (text, a card's contents, a bar's fill, a badge's digits). On the element's middle five rows, take the per-column median; the interior is the run of columns around the centre whose largest channel difference from the centre pixel is at most 32. The same on the middle five columns gives the interior rows. When a corner of that rectangle is not interior (a round badge), the rectangle shrinks evenly toward the centre until all four corners are. Each padding is the distance from an element edge to that rectangle, plus 2 px. A content area narrower or shorter than a quarter of its element makes the report `needs-review`: the centre was probably not empty.
+Padding places the content (text, a card's contents, a bar's fill, a badge's digits). On the element's middle five rows, take the per-column median. The interior is the run of columns around the centre that continues while each step to the next column changes no channel by more than 16 and every column stays within 96 of the centre (the median of the central 5 × 5 pixels): a smooth gradient on a button face stays interior, a border's sharp edge ends it. The same on the middle five columns gives the interior rows. A pixel counts as interior when every channel lies within 16 of the range those two runs cover; when a corner of the rectangle is not interior (a round badge, an ornament reaching inward), the rectangle shrinks evenly toward the centre until all four corners are. Each padding is the distance from an element edge to that rectangle, plus 2 px. A content area narrower or shorter than a quarter of its element makes the report `needs-review`: the centre was probably not empty.
## Alpha cleanup
diff --git a/sprite_gen/frames/ui_kit.py b/sprite_gen/frames/ui_kit.py
index c9c5f7d..64416a4 100644
--- a/sprite_gen/frames/ui_kit.py
+++ b/sprite_gen/frames/ui_kit.py
@@ -26,7 +26,10 @@ REFERENCE = 1024
GAP = 48
MIN_PART = 16
MARGIN = 2
-PADDING_TOLERANCE = 32
+# Padding: the interior runs on while each step to the next pixel stays small and the pixel stays near
+# the centre, so a smooth gradient face is interior and a border's sharp edge ends it.
+PADDING_STEP = 16
+PADDING_SPAN = 96
GUIDE_BACKGROUND = (0xF6, 0xF6, 0xF6)
GUIDE_BOX = (0x2F, 0x80, 0xED)
STRETCHES = ("xy", "x", "none")
@@ -150,13 +153,14 @@ def measure_insets(image: Image.Image, stretch: str) -> tuple[dict[str, int], li
def _interior_run(line: Any, centre: Any, middle: int) -> tuple[int, int]:
- close = np.abs(line - centre).max(axis=-1) <= PADDING_TOLERANCE
- if not close[middle]:
+ near = np.abs(line - centre).max(axis=-1) <= PADDING_SPAN
+ smooth = np.abs(np.diff(line, axis=0)).max(axis=-1) <= PADDING_STEP
+ if not near[middle]:
return middle, middle + 1
start, end = middle, middle + 1
- while start > 0 and close[start - 1]:
+ while start > 0 and near[start - 1] and smooth[start - 1]:
start -= 1
- while end < len(close) and close[end]:
+ while end < len(near) and near[end] and smooth[end - 1]:
end += 1
return start, end
@@ -168,11 +172,14 @@ def measure_padding(image: Image.Image) -> tuple[dict[str, int], list[str]]:
cy, cx = height // 2, width // 2
rows, cols = slice(max(0, cy - 2), cy + 3), slice(max(0, cx - 2), cx + 3)
centre = np.median(pixels[rows, cols].reshape(-1, 4), axis=0)
- x0, x1 = _interior_run(np.median(pixels[rows], axis=0), centre, cx)
- y0, y1 = _interior_run(np.median(pixels[:, cols], axis=1), centre, cy)
+ across, down = np.median(pixels[rows], axis=0), np.median(pixels[:, cols], axis=1)
+ x0, x1 = _interior_run(across, centre, cx)
+ y0, y1 = _interior_run(down, centre, cy)
+ interior_values = np.concatenate((across[x0:x1], down[y0:y1]))
+ low, high = interior_values.min(axis=0) - PADDING_STEP, interior_values.max(axis=0) + PADDING_STEP
def interior(x: int, y: int) -> bool:
- return bool(np.abs(pixels[y, x] - centre).max() <= PADDING_TOLERANCE)
+ return bool(((pixels[y, x] >= low) & (pixels[y, x] <= high)).all())
shrink = 0
while True:
diff --git a/tests/frames/test_ui_kit.py b/tests/frames/test_ui_kit.py
index e61608c..73d1405 100644
--- a/tests/frames/test_ui_kit.py
+++ b/tests/frames/test_ui_kit.py
@@ -310,3 +310,19 @@ def test_packed_kit_keeps_ornament_fringes_out_of_the_stretched_middle(tmp_path,
if entry["stretch"] == "xy":
band = stored[insets["top"]:h - insets["bottom"]]
assert np.abs(band - np.median(band, axis=0)[None]).max() <= 8, (name, scale, "y")
+
+
+def test_padding_follows_a_smooth_gradient_face_to_its_border():
+ # a button face that darkens smoothly from top to bottom (70 units over its height), inside a sharp
+ # 4 px silver border: the content area is the whole face, not a band around its centre
+ w, h = 160, 64
+ pixels = np.zeros((h, w, 4), dtype=np.uint8)
+ pixels[:, :] = BORDER
+ face = np.linspace(110, 40, h - 8).round().astype(np.uint8)
+ pixels[4:h - 4, 4:w - 4, 0] = 20
+ pixels[4:h - 4, 4:w - 4, 1] = face[:, None] // 2
+ pixels[4:h - 4, 4:w - 4, 2] = face[:, None]
+ pixels[4:h - 4, 4:w - 4, 3] = 255
+ padding, notes = ui_kit.measure_padding(Image.fromarray(pixels))
+ assert padding == {"left": 6, "right": 6, "top": 6, "bottom": 6}
+ assert notes == []