Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion BusTimeApp/DesignSystem/SkyBackground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ struct SkyCanvas: View {
private static let blueStarColor = Color(red: 0.78, green: 0.87, blue: 1.0)
/// 最も明るい、十字に描く星の色です。
private static let warmStarColor = Color(red: 1.0, green: 0.847, blue: 0.494)
/// 明るい星とみなす明るさの下限です。これより暗い星は、空がかすむと隠れます。
private static let brightStarThreshold: Double = 0.94

/// 水平線の位置です。これより下は海面で、太陽・月もこの高さに沈みます。
private static let horizonRatio: Double = 0.55
Expand Down Expand Up @@ -1694,6 +1696,11 @@ struct SkyCanvas: View {
let isBlinking = pseudoRandom(index &* 977 &+ tick) > 0.9
guard !isBlinking else { continue }

// 空気の澄み具合に応じて、暗い星から隠れます。明るい星は季節を問わず見えます。
// 夜空の色は変えずに、冬の澄んだ夜と春のかすんだ夜を見分けられるようにします。
let isFaint = brightness <= Self.brightStarThreshold
guard !isFaint || pseudoRandom(index &* 31 &+ 17) < sky.starClarity else { continue }

let originX = snapped(CGFloat(horizontal) * size.width)
let originY = snapped(CGFloat(vertical) * size.height)

Expand All @@ -1702,7 +1709,7 @@ struct SkyCanvas: View {
// 大きさは変えず、色と濃さだけで明るさの差を表します。
if brightness > 0.992 {
appendCell(to: &warmPath, x: originX, y: originY)
} else if brightness > 0.94 {
} else if brightness > Self.brightStarThreshold {
appendCell(to: &whitePath, x: originX, y: originY)
} else if brightness > 0.80 {
appendCell(to: &bluePath, x: originX, y: originY)
Expand Down
103 changes: 47 additions & 56 deletions BusTimeApp/DesignSystem/SkyPalette.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,21 @@ enum Season: CaseIterable {
}
}

/// 空に混ぜる季節の色です。
fileprivate var tint: RGBComponents {
/// 夜空の澄み具合です。1で最も澄み、暗い星まで見えます。
///
/// 夜は空に季節の色を混ぜると、黒に近い紺が灰色や茶色に濁ります。
/// そこで空の色は変えず、星の見え方で季節の空気の違いを表します。
/// 乾いた冬と秋は澄み、湿った夏と、かすみの出る春は暗い星が隠れます。
var starClarity: Double {
switch self {
case .spring:
// 霞んだ淡い桃色です。
return RGBComponents(red: 0.98, green: 0.90, blue: 0.92)
case .summer:
// 濃く澄んだ青です。
return RGBComponents(red: 0.30, green: 0.55, blue: 0.95)
case .autumn:
// 夕暮れに寄せた橙です。
return RGBComponents(red: 0.95, green: 0.72, blue: 0.48)
case .winter:
// 冷たく白い青です。
return RGBComponents(red: 0.82, green: 0.88, blue: 0.97)
}
}

/// 季節の色をどれだけ混ぜるかです。強すぎると時刻ごとの変化が消えます。
var tintStrength: Double {
switch self {
case .spring, .autumn:
return 0.10
case .summer, .winter:
return 0.13
return 1.0
case .autumn:
return 0.9
case .summer:
return 0.7
case .spring:
return 0.55
}
}
}
Expand Down Expand Up @@ -475,12 +465,6 @@ private struct SkyKeyframe {
seasonal[season] ?? SkyColors(top: skyTop, bottom: skyBottom)
}

/// 季節の色(`Season.tint`)を混ぜる強さの倍率です。
///
/// 季節ごとの色を持つキーフレームは、それ自体が季節の空なので混ぜません。
var seasonTintWeight: Double {
seasonal.isEmpty ? 1 : 0
}
}

/// 空の上端と下端の色の組です。
Expand Down Expand Up @@ -561,6 +545,13 @@ struct SkyPalette: Equatable {
day.season
}

/// 夜空の澄み具合です。隣り合う季節のあいだでは、日付に応じて移ります。
var starClarity: Double {
let blend = day.seasonBlend
return blend.earlier.starClarity
+ (blend.later.starClarity - blend.earlier.starClarity) * blend.ratio
}

/// 太陽が地平線より上にあるかどうかです。太陽と月のどちらを描くかを決めます。
var isSunUp: Bool {
daylight.isSunUp(at: hour)
Expand Down Expand Up @@ -593,20 +584,25 @@ struct SkyPalette: Equatable {
autumn: (top: (Double, Double, Double), bottom: (Double, Double, Double)),
winter: (top: (Double, Double, Double), bottom: (Double, Double, Double))
) -> [Season: SkyColors] {
func colors(_ pair: (top: (Double, Double, Double), bottom: (Double, Double, Double))) -> SkyColors {
SkyColors(
top: RGBComponents(red: pair.top.0, green: pair.top.1, blue: pair.top.2),
bottom: RGBComponents(red: pair.bottom.0, green: pair.bottom.1, blue: pair.bottom.2)
)
}
return [
.spring: colors(spring),
.summer: colors(summer),
.autumn: colors(autumn),
.winter: colors(winter)
[
.spring: colors(top: spring.top, bottom: spring.bottom),
.summer: colors(top: summer.top, bottom: summer.bottom),
.autumn: colors(top: autumn.top, bottom: autumn.bottom),
.winter: colors(top: winter.top, bottom: winter.bottom)
]
}

/// 0〜1の赤・緑・青の組から、空の上端と下端の色を作ります。
private static func colors(
top: (Double, Double, Double),
bottom: (Double, Double, Double)
) -> SkyColors {
SkyColors(
top: RGBComponents(red: top.0, green: top.1, blue: top.2),
bottom: RGBComponents(red: bottom.0, green: bottom.1, blue: bottom.2)
)
}

/// 1日の空の色を定義するキーフレームです。時刻順に並べる必要があります。
///
/// 日の出の前後は、空が白み始める薄明(約1時間15分前)から、
Expand Down Expand Up @@ -679,14 +675,18 @@ struct SkyPalette: Equatable {
offset: -0.75,
skyTop: RGBComponents(red: 0.882, green: 0.482, blue: 0.310),
skyBottom: RGBComponents(red: 0.996, green: 0.855, blue: 0.612),
nightness: 0.10
nightness: 0.10,
// 夏の夕焼けは、ほかの季節より濃い橙から赤へ焼けます。
seasonal: [.summer: colors(top: (0.93, 0.40, 0.20), bottom: (1.00, 0.78, 0.45))]
),
SkyKeyframe(
event: .sunset,
offset: 0.5,
skyTop: RGBComponents(red: 0.278, green: 0.157, blue: 0.353),
skyBottom: RGBComponents(red: 0.867, green: 0.416, blue: 0.365),
nightness: 0.74
nightness: 0.74,
// 夏の残照は、地平線の赤が濃く残ります。
seasonal: [.summer: colors(top: (0.31, 0.13, 0.30), bottom: (0.92, 0.36, 0.24))]
),
SkyKeyframe(
event: .sunset,
Expand Down Expand Up @@ -779,9 +779,6 @@ struct SkyPalette: Equatable {
/// 夜の強調色の淡い背景の不透明度です。
private static let nightAccentSoftOpacity: Double = 0.30

/// 空の下端に混ぜる季節の色の強さです。上端に対する割合で持ちます。
/// 地平線付近は時刻による色の変化が大きいので、季節の色は控えめにします。
private static let horizonTintScale: Double = 0.6
/// 街灯を点ける空の暗さの下限です。星が見え始める暗さに揃えます。
private static let streetLightNightness: Double = 0.22
/// 街灯を消す時刻を、日の出の何時間前にするかです。
Expand Down Expand Up @@ -1006,9 +1003,9 @@ struct SkyPalette: Equatable {

/// ある季節の、2つのキーフレームのあいだの空の色です。
///
/// 昼は季節ごとの空の色をそのまま使います。季節ごとの色を持たない時間帯は、
/// 共通の空に季節の色をうっすら混ぜます。夏は青く、秋は暖かく、冬は白っぽくなります。
/// 混ぜる強さもキーフレームのあいだで補間し、朝夕に急に切り替わらないようにします。
/// 季節の色を時間帯に関係なく一律に混ぜると、暗い夜空や彩度の高い夕焼けほど濁ります。
/// そのため季節の違いは、キーフレームごとに持たせた季節の空の色だけで表します。
/// 季節ごとの色を持たない時間帯は、どの季節も同じ空になります。
private static func skyColors(
between previous: SkyKeyframe,
and next: SkyKeyframe,
Expand All @@ -1017,15 +1014,9 @@ struct SkyPalette: Equatable {
) -> SkyColors {
let from = previous.colors(for: season)
let to = next.colors(for: season)
let tintStrength = season.tintStrength
* interpolate(from: previous.seasonTintWeight, to: next.seasonTintWeight, ratio: ratio)
return SkyColors(
top: from.top
.mixed(with: to.top, ratio: ratio)
.mixed(with: season.tint, ratio: tintStrength),
bottom: from.bottom
.mixed(with: to.bottom, ratio: ratio)
.mixed(with: season.tint, ratio: tintStrength * horizonTintScale)
top: from.top.mixed(with: to.top, ratio: ratio),
bottom: from.bottom.mixed(with: to.bottom, ratio: ratio)
)
}

Expand Down
60 changes: 55 additions & 5 deletions BusTimeAppTests/BusTimeAppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,55 @@ struct BusTimeAppTests {
#expect(Season(launchArgument: "fall") == .autumn)
}

@Test
func nightSkyStaysDeepNavyInEverySeason() {
// 夜空に季節の色を混ぜると、黒に近い紺が灰色や茶色っぽく濁ります。
// 以前の秋の23時の空の上端は(30, 27, 39)でした。
for season in Season.allCases {
for hour in [23.0, 0, 2] {
let top = components(of: SkyPalette.at(hour: hour, season: season).skyColor(at: 0))
#expect(top.r < 0.06, "\(season)の\(hour)時の空が明るく濁っています")
#expect(top.g < 0.08, "\(season)の\(hour)時の空が明るく濁っています")
#expect(top.b > max(top.r, top.g) + 0.05, "\(season)の\(hour)時の空が紺ではありません")
}
}
}

@Test
func summerSunsetGlowsDeepOrange() {
// 夏の夕焼けに夏の青を混ぜると、空の上端が(150, 141, 164)の灰紫になっていました。
// 夏は青を混ぜず、ほかの季節より濃い橙に焼けます。
func sunsetGlow(_ season: Season) -> (r: Double, g: Double, b: Double, a: Double) {
let day = SeasonalDay(season: season)
let palette = SkyPalette.at(hour: day.daylight.sunset - 0.75, day: day)
return components(of: palette.skyColor(at: 0))
}

let summer = sunsetGlow(.summer)
#expect(summer.r > 0.85)
#expect(summer.b < 0.3)
for season in [Season.spring, .autumn, .winter] {
#expect(saturation(summer) > saturation(sunsetGlow(season)))
}
}

@Test
func starsShowHowClearTheNightAirIs() {
// 夜空の色は変えず、星の見え方で季節を表します。
// 乾いた冬が最も澄み、秋、湿った夏、かすみの出る春の順に暗い星が隠れます。
let order: [Season] = [.winter, .autumn, .summer, .spring]
for (clearer, hazier) in zip(order, order.dropFirst()) {
#expect(
SkyPalette.at(hour: 23, season: clearer).starClarity
> SkyPalette.at(hour: 23, season: hazier).starClarity
)
}
// 季節のあいだの日は、2つの季節の澄み具合のあいだになります。
let lateMay = SkyPalette.at(hour: 23, day: SeasonalDay(dayOfYear: 151)).starClarity
#expect(lateMay > Season.spring.starClarity)
#expect(lateMay < Season.summer.starClarity)
}

@Test
func skyChangesGraduallyFromDayToDay() {
// 隣り合う日の同じ時刻で、空の色がほとんど変わらないことを確かめます。
Expand Down Expand Up @@ -1820,11 +1869,6 @@ struct BusTimeAppTests {
let palette = SkyPalette.at(hour: 12, season: season)
return (components(of: palette.skyColor(at: 0)), components(of: palette.skyColor(at: 1)))
}
func saturation(_ color: RGBA) -> Double {
let highest = max(color.r, color.g, color.b)
return highest > 0 ? (highest - min(color.r, color.g, color.b)) / highest : 0
}

// 夏は濃く高い青、秋は澄んだ青、春はかすんだ淡い空、冬は低く白っぽい空です。
// 空の上端の鮮やかさが、この順に下がります。
let order: [Season] = [.summer, .autumn, .spring, .winter]
Expand Down Expand Up @@ -2213,6 +2257,12 @@ private func cieLab(_ color: (r: Double, g: Double, b: Double, a: Double)) -> (l
return (116 * f(y) - 16, 500 * (f(x) - f(y)), 200 * (f(y) - f(z)))
}

/// 色の鮮やかさ(HSVの彩度)です。0が灰色、1が最も鮮やかです。
private func saturation(_ color: (r: Double, g: Double, b: Double, a: Double)) -> Double {
let highest = max(color.r, color.g, color.b)
return highest > 0 ? (highest - min(color.r, color.g, color.b)) / highest : 0
}

/// WCAGの相対輝度です。
private func relativeLuminance(_ color: (r: Double, g: Double, b: Double, a: Double)) -> Double {
func channel(_ value: Double) -> Double {
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
- 0時、明け方、朝、昼、夕方、夜のキーフレーム間を連続補間し、空・水面・地面・カード・文字・アクセントを同じ時刻から生成します。
- 日の出・日の入りは、海浜幕張駅付近の緯度経度とその日の日付から計算します(NOAAの太陽位置の近似式)。夏至はおよそ4:25〜19:00、冬至は6:45〜16:30で、南中は経度と均時差により11:23〜11:54です。
- 昼の空は季節ごとに彩度・明度・地平線のかすみを描き分けます。春はかすんだ淡い空、夏は濃く高い青、秋は澄んだ深い青、冬は低く白っぽい空です。同じ正午の空は、どの季節の組み合わせでもΔE 10以上離れます。
- 夜と朝夕の空には季節の色を一律に混ぜません。夜空はどの季節も深い紺のままにし、季節の違いは空気の澄み具合として星の見え方で表します(冬が最も澄み、秋、夏、かすみの出る春の順に暗い星が隠れます)。夏の夕焼けは青を混ぜず、濃い橙から赤へ焼けます。
- 季節の色は月で切り替えず、各季節の代表日(4/15、7/16、10/16、1/15)で最も強く、隣の季節の代表日へ向けて日ごとになだらかに移します。5月末・8月末・11月末・2月末が2つの季節のちょうど中間です。
- 空のキーフレームは時計の時刻ではなく、日の出・南中・日の入りからの前後で持ちます。薄明の長さは季節であまり変わらないため、昼の長さだけが日付に応じて伸び縮みします。空の色、太陽・月の軌道、太陽と月の入れ替わり、街灯は同じ日の出・日の入り(`DaylightSchedule`)を共有し、空と天体の位置が食い違わないようにしています。季節と背景の時刻は、運行判定と同じ日本時間の暦とアプリ内の現在時刻(`AppDate`)で決めます。
- 空と海は色数を量子化し、ベイヤーディザと決定的な疑似乱数で、毎フレームちらつかないピクセル表現にしています。
Expand Down Expand Up @@ -1155,6 +1156,9 @@ UIテストでは、ホーム/時刻表の遷移、最大Dynamic Typeでの主
- `forcedSeasonChangesOnlyTheDateUsedForSeasons`: `-forceSeason`が時刻を残したまま月日だけを季節の代表日へ移す
- `forcedDayOfYearPicksThatDayAndOutranksTheSeason`: `-forceDayOfYear`の日付指定と優先順位
- `seasonOverrideIgnoresUnknownValues`: 不明な季節名や範囲外の日数を無視
- `nightSkyStaysDeepNavyInEverySeason`: 夜空が季節の色で灰色や茶色に濁らない
- `summerSunsetGlowsDeepOrange`: 夏の夕焼けが青を混ぜず、ほかの季節より濃い橙になる
- `starsShowHowClearTheNightAirIs`: 夜の季節の違いを星の見え方(冬が最も澄み、春が最もかすむ)で表す
- `skyChangesGraduallyFromDayToDay`: 隣り合う日の同じ時刻の空の色差(ΔE)が一年を通して2.0以下
- `seasonsBlendBetweenTheirRepresentativeDays`: 季節の色が代表日で最も強く、隣の季節へ連続して移る
- `seasonsLookDifferentAtNoon`: 同じ正午の空が季節どうしで見分けられる色差(ΔE 10以上)を持つ
Expand Down
Loading