"""Toy numbers for five UI terms. Not a screenshot of this blog.

Gray and neutral hex values are Tailwind's default palette as shipped
in tailwindcss 3 (colors.js). The bezier samples solve x(t) = 0.5
so the printed y is progress at half the duration.
"""

def channels(hex_color: str) -> tuple[int, int, int]:
    h = hex_color.removeprefix("#")
    return int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16)


def bezier_y_at_x(x1: float, y1: float, x2: float, y2: float, x_target: float) -> float:
    def bez(p1: float, p2: float, t: float) -> float:
        return 3 * (1 - t) ** 2 * t * p1 + 3 * (1 - t) * t ** 2 * p2 + t ** 3

    lo, hi = 0.0, 1.0
    for _ in range(60):
        mid = (lo + hi) / 2
        if bez(x1, x2, mid) < x_target:
            lo = mid
        else:
            hi = mid
    t = (lo + hi) / 2
    return bez(y1, y2, t)


def main() -> None:
    lines = [
        "L1 The first line of the paragraph.",
        "L2 The second line stays with it.",
        "L3 The third line is still the same paragraph.",
        "L4 The fourth line ends the thought.",
    ]
    # A fragment that holds 3 lines. orphans: 1 allows L1 alone at the bottom
    # of a previous fragment; the toy just labels the split.
    print("fragment_holds=3 total_lines=4")
    print(f"orphan_if_break_after_1={lines[0]}")
    print(f"widow_if_break_before_last={lines[-1]}")
    gray = channels("#6b7280")
    neutral = channels("#737373")
    print(f"tailwind_gray_500={gray} neutral_500={neutral}")
    print(
        "channel_delta_gray_minus_neutral="
        f"{tuple(a - b for a, b in zip(gray, neutral))}"
    )
    curves = {
        "css_ease_out": (0.0, 0.0, 0.58, 1.0),
        "css_ease": (0.25, 0.1, 0.25, 1.0),
        "material_emphasized_decelerate": (0.05, 0.7, 0.1, 1.0),
    }
    print("progress_at_half_duration")
    for name, (x1, y1, x2, y2) in curves.items():
        y = bezier_y_at_x(x1, y1, x2, y2, 0.5)
        print(f"  {name} {y:.6f}")


if __name__ == "__main__":
    main()
