背景色コマンドが無いときに Tortoise 既定の白を描くようにする - #45
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
Fixes #44
.backgroundColorコマンドを一度も出していないプログラムが、TortoiseCanvasでもTortoiseSVGでも背景を塗らない問題を修正します。Tortoise.backgroundColorは生成直後から白を返すので、「空の Tortoise は白と答えるのに、描画すると何も塗られない」という食い違いになっていました。原因
初期背景色の定義が4箇所に散らばり、値が食い違っていました。
Tortoise._backgroundColor.whiteCommandPlayer.play(initialBackgroundColor:)の既定.clearCanvasModel.backgroundColorの初期値/空ストリーム時のフォールバック.clearSVGBuilder.bgColorの初期値.clear新設した
Color.defaultBackground(白)に4箇所すべてを寄せ、単一の情報源にしました。issue の提案との差異
issue では「元の
TortoiseのbackgroundColorをinitialBackgroundColorとして渡す」案が挙がっていましたが、そのままだと不正確なので採用していません。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 の再発防止)。
ゴールデン画像
<rect fill="#ffffff"/>が1行増えました。明示している2件(backgroundAndColors/showcase)は無変更です。再録して目視確認済みチェックリスト
swift testがローカルで通る(131件)xcrun swift-format lint --recursive --strict Sources Testsが通るCHANGELOG.mdを更新(Fixed / Changed 両方に記載)