Skip to content

背景色コマンドが無いときに Tortoise 既定の白を描くようにする - #45

Merged
temoki merged 1 commit into
mainfrom
fix-44-default-background
Aug 5, 2026
Merged

背景色コマンドが無いときに Tortoise 既定の白を描くようにする#45
temoki merged 1 commit into
mainfrom
fix-44-default-background

Conversation

@temoki

@temoki temoki commented Aug 5, 2026

Copy link
Copy Markdown
Owner

概要

Fixes #44

.backgroundColor コマンドを一度も出していないプログラムが、TortoiseCanvas でも TortoiseSVG でも背景を塗らない問題を修正します。Tortoise.backgroundColor は生成直後から白を返すので、「空の Tortoise は白と答えるのに、描画すると何も塗られない」という食い違いになっていました。

原因

初期背景色の定義が4箇所に散らばり、値が食い違っていました。

場所 修正前
Tortoise._backgroundColor .white
CommandPlayer.play(initialBackgroundColor:) の既定 .clear
CanvasModel.backgroundColor の初期値/空ストリーム時のフォールバック .clear
SVGBuilder.bgColor の初期値 .clear

新設した Color.defaultBackground(白)に4箇所すべてを寄せ、単一の情報源にしました。

issue の提案との差異

issue では「元の TortoisebackgroundColorinitialBackgroundColor として渡す」案が挙がっていましたが、そのままだと不正確なので採用していません。Tortoise.backgroundColor は全コマンド適用後の現在値なので、途中で背景色を変えるプログラムでは、その変更が0フレーム目に遡って適用されてしまいます(例: 10番目のコマンドで青にすると、0〜9フレーム目まで青になる)。

Tortoise は必ず白から始まり、変更は必ずコマンドとして記録されるため、初期値は定数です。backgroundChangeIsNotBackDated テストでこの挙動を固定しました。

挙動の変更(破壊的)

背景色を明示していない描画が、TortoiseCanvas / TortoiseSVG ともに白地になります(従来は透明)。

透明を利用していた場合 — SwiftUI の .background() を透過させる、アルファ付き PNG を書き出すなど — は tortoise.backgroundColor = .clear を明示してください。両レンダラとも alpha == 0 のときは塗り/<rect> を省略する挙動のままなので、その経路は従来どおり動きます。CHANGELOG に移行方法を記載しました。

テスト

issue の再現手順そのものを含め、11件追加(合計 131件):

  • TortoiseCore: 既定値の一致、後述の遡り適用がないこと、明示的な .clear の維持
  • TortoiseUI: 赤い下地の上にキャンバスを置き角のピクセルを実測(修正前は赤 #FF2600、修正後は白・アルファ 1.0)。.clear 指定時は赤が透けることも検証
  • TortoiseSVG: 既定で <rect fill="#ffffff"/> が入ること、.clear では入らないこと(旧挙動を固定していた既存テストを更新)

ピクセル比較のしきい値は、レンダーターゲットの色空間に依存しないよう緩めに取っています(#43 の再発防止)。

ゴールデン画像

  • SVG: 背景を設定していない14件に <rect fill="#ffffff"/> が1行増えました。明示している2件(backgroundAndColors / showcase)は無変更です。再録して目視確認済み
  • Canvas PNG: 無変更。テストが白い下地の上でレンダリングしているため、出力が同一です

チェックリスト

  • swift test がローカルで通る(131件)
  • xcrun swift-format lint --recursive --strict Sources Tests が通る
  • 新しい挙動に対するテストを追加
  • CHANGELOG.md を更新(Fixed / Changed 両方に記載)
  • ゴールデン画像: SVG を再録し目視確認済み。PNG は出力が変わらないため再録不要

A program that never set a background color rendered with no background at
all, even though `Tortoise.backgroundColor` reports white from the moment it
is created — an empty tortoise claimed white but drew nothing. On a dark host
the default black pen was nearly invisible, `ImageRenderer` exports came out
as fully transparent PNGs, and the SVG carried no background `<rect>`.

The initial background was defined in four places that disagreed:
`Tortoise` started at `.white`, `CommandPlayer.play`'s
`initialBackgroundColor` defaulted to `.clear`, and `CanvasModel` and
`SVGBuilder` each kept a `.clear` fallback for an empty stream. Collapse
them onto one constant, `Color.defaultBackground`.

Note that the fix is *not* to thread `Tortoise.backgroundColor` in as
`initialBackgroundColor`, as the issue suggested: that property is the
current value, not the initial one, so a program that changes its background
partway through would have the change back-dated to frame 0. A tortoise
always starts white and records every change as a command, so the initial
value is a constant. `backgroundChangeIsNotBackDated` pins this.

Transparency is still available but must now be asked for
(`tortoise.backgroundColor = .clear`); both renderers still skip the fill
when alpha is 0, so SwiftUI's `.background()` modifier keeps working.

The 14 SVG goldens whose scenarios set no background gain one
`<rect fill="#ffffff"/>` line; the two that set one are untouched. Canvas
PNG goldens are unchanged — they already render on a white backdrop.
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.78%. Comparing base (876476c) to head (18803f8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #45      +/-   ##
==========================================
+ Coverage   92.57%   92.78%   +0.21%     
==========================================
  Files          16       16              
  Lines         943      943              
==========================================
+ Hits          873      875       +2     
+ Misses         70       68       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@temoki
temoki merged commit ab29998 into main Aug 5, 2026
6 checks passed
@temoki
temoki deleted the fix-44-default-background branch August 5, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

背景色コマンドが無いと TortoiseCanvas / TortoiseSVG が背景を塗らない(Tortoise の既定は白)

1 participant