Spaces:
Sleeping
Sleeping
fix(rank): readable text on light pills (auto dark/white by background)
Browse filesGray "other" pills used white text (low contrast). Pick text colour from
background luminance; applied to the image and the native PPTX.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1840 unmodified lines18411842184318441845184618473 unmodified lines185118521853185418551856185766 unmodified lines192419251926192719281929193019311932193380 unmodified lines20142015201620172018201920201840 unmodified linesby_lat = lambda cs: sorted(cs, key=lambda c: -anchors[c][1])return by_lat(mainland[:half]), by_lat(mainland[half:])def _draw_pill(ov, x, y, w, h, name, rank_text, value_text, color, ranked, fs, rank_color=None):"""膠囊標籤:左側色塊(名次圓圈+縣市名)+右側白底數值,外框同色"""wl = w * 0.64ov.add_patch(FancyBboxPatch((x, y - h / 2), w, h, boxstyle=f"round,pad=0,rounding_size={h / 2}",3 unmodified linesov.add_patch(Circle((x + h / 2, y), h * 0.36, fc="white", ec="white", zorder=8))ov.text(x + h / 2, y, rank_text, ha="center", va="center", fontsize=fs * 0.72, fontweight="bold",color=rank_color or color, zorder=9)ov.text(x + h + 0.08, y, name, ha="left", va="center", fontsize=fs, fontweight="bold", color="white", zorder=9)ov.text(x + wl + (w - wl) / 2, y, value_text, ha="center", va="center", fontsize=fs, fontweight="bold",color="#111827" if ranked else "#9ca3af", zorder=9)66 unmodified linesrank, raw = label_of(n)# 灰色(其他)膠囊的名次數字用較深的灰,白圈裡才看得清楚pill_color = color_of[k] if k != "none" else "#d1d5db"rank_color = color_of[k] if k in ("top", "bottom") else "#6b7280"_draw_pill(ov, x, y, pill_w, pill_h, n, rank, raw, pill_color, k != "none", 15, rank_color=rank_color)scene["pills"].append(dict(x=x, y=y, w=pill_w, h=pill_h, name=n, rank=rank, value=raw, color=pill_color,rank_color=rank_color, ranked=k != "none", fs=15))left, right = _rank_columns(anchors)line_kw = dict(color="#8a8f98", lw=0.9, zorder=3, solid_capstyle="round")80 unmodified lines_pp_shape(g.shapes, "round", x, y - h / 2, w, h, fill="#ffffff", line=pl["color"], line_pt=1.3, name="外框")name_box = _pp_shape(g.shapes, "round", x, y - h / 2, wl, h, fill=pl["color"], line=pl["color"], line_pt=1.3,name="縣市")_pp_fill_text(name_box.text_frame, pl["name"], fs, True, "#ffffff", "l", "m", margins=(h * 1.05, 0, 0.02, 0))circle = _pp_shape(g.shapes, "oval", x + h / 2 - h * 0.36, y - h * 0.36, h * 0.72, h * 0.72, fill="#ffffff",name="名次")_pp_fill_text(circle.text_frame, pl["rank"], fs * 0.72, True, pl["rank_color"], "c", "m")1840 unmodified lines1841184218431844184518461847184818491850185118523 unmodified lines185618571858185918601861186266 unmodified lines19291930193119321933193419351936193719381939194080 unmodified lines20212022202320242025202620271840 unmodified linesby_lat = lambda cs: sorted(cs, key=lambda c: -anchors[c][1])return by_lat(mainland[:half]), by_lat(mainland[half:])def _contrast_text(fill_hex: str) -> str:"""依底色明暗挑文字色:淺底(例如灰色)用深色字,深底用白字,避免灰底白字看不清楚"""r, g, b = mpl.colors.to_rgb(fill_hex)return "#1f2937" if 0.299 * r + 0.587 * g + 0.114 * b > 0.62 else "#ffffff"def _draw_pill(ov, x, y, w, h, name, rank_text, value_text, color, ranked, fs, rank_color=None, text_color="#ffffff"):"""膠囊標籤:左側色塊(名次圓圈+縣市名)+右側白底數值,外框同色"""wl = w * 0.64ov.add_patch(FancyBboxPatch((x, y - h / 2), w, h, boxstyle=f"round,pad=0,rounding_size={h / 2}",3 unmodified linesov.add_patch(Circle((x + h / 2, y), h * 0.36, fc="white", ec="white", zorder=8))ov.text(x + h / 2, y, rank_text, ha="center", va="center", fontsize=fs * 0.72, fontweight="bold",color=rank_color or color, zorder=9)ov.text(x + h + 0.08, y, name, ha="left", va="center", fontsize=fs, fontweight="bold", color=text_color, zorder=9)ov.text(x + wl + (w - wl) / 2, y, value_text, ha="center", va="center", fontsize=fs, fontweight="bold",color="#111827" if ranked else "#9ca3af", zorder=9)66 unmodified linesrank, raw = label_of(n)# 灰色(其他)膠囊的名次數字用較深的灰,白圈裡才看得清楚pill_color = color_of[k] if k != "none" else "#d1d5db"text_color = _contrast_text(pill_color)rank_color = color_of[k] if k in ("top", "bottom") and text_color == "#ffffff" else "#374151"_draw_pill(ov, x, y, pill_w, pill_h, n, rank, raw, pill_color, k != "none", 15, rank_color=rank_color,text_color=text_color)scene["pills"].append(dict(x=x, y=y, w=pill_w, h=pill_h, name=n, rank=rank, value=raw, color=pill_color,rank_color=rank_color, text_color=text_color, ranked=k != "none", fs=15))left, right = _rank_columns(anchors)line_kw = dict(color="#8a8f98", lw=0.9, zorder=3, solid_capstyle="round")80 unmodified lines_pp_shape(g.shapes, "round", x, y - h / 2, w, h, fill="#ffffff", line=pl["color"], line_pt=1.3, name="外框")name_box = _pp_shape(g.shapes, "round", x, y - h / 2, wl, h, fill=pl["color"], line=pl["color"], line_pt=1.3,name="縣市")_pp_fill_text(name_box.text_frame, pl["name"], fs, True, pl["text_color"], "l", "m", margins=(h * 1.05, 0, 0.02, 0))circle = _pp_shape(g.shapes, "oval", x + h / 2 - h * 0.36, y - h * 0.36, h * 0.72, h * 0.72, fill="#ffffff",name="名次")_pp_fill_text(circle.text_frame, pl["rank"], fs * 0.72, True, pl["rank_color"], "c", "m")
app.py+13-6