From a4f5c798e780479566db136bd856244f95badaba Mon Sep 17 00:00:00 2001 From: Lichtkasten Dev Date: Wed, 4 Nov 2026 01:28:09 +0000 Subject: [PATCH] Trikot-Erkennung: Teamfarben per Klick ins Foto aufnehmen (gleiche Messung wie die Erkennung) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D --- .../Lichtkasten/Views/JerseySetupView.swift | 159 ++++++++++++++++-- 1 file changed, 149 insertions(+), 10 deletions(-) diff --git a/Sources/Lichtkasten/Views/JerseySetupView.swift b/Sources/Lichtkasten/Views/JerseySetupView.swift index 7f9ba00..f39a0be 100644 --- a/Sources/Lichtkasten/Views/JerseySetupView.swift +++ b/Sources/Lichtkasten/Views/JerseySetupView.swift @@ -9,7 +9,66 @@ struct JerseySetupView: View { private var jersey: Binding { $state.settings.jersey } + /// Which color the next click in the photo sets: team + shirt/shorts + struct PickTarget: Equatable { var team: UUID; var shorts: Bool } + @State private var target: PickTarget? + @State private var photo: CGImage? + var body: some View { + HStack(alignment: .top, spacing: 16) { + settingsColumn.frame(width: 560) + photoColumn.frame(minWidth: 520, maxWidth: .infinity, maxHeight: .infinity) + } + .padding(18) + .frame(minWidth: 1140, minHeight: 640) + .task(id: state.cursorID) { await loadPhoto() } + .onAppear { + if target == nil, let first = state.settings.jersey.teams.first { target = PickTarget(team: first.id, shorts: false) } + } + } + + // MARK: Photo → click on the shirt + + private var photoColumn: some View { + VStack(alignment: .leading, spacing: 8) { + HStack { + Text(targetText).font(.headline) + Spacer() + Button { state.moveCursor(by: -1) } label: { Image(systemName: "chevron.left") } + Text(state.cursorItem?.filename ?? "–").font(.caption).foregroundStyle(.secondary).lineLimit(1) + Button { state.moveCursor(by: 1) } label: { Image(systemName: "chevron.right") } + } + ImageColorPicker(image: photo) { color in + guard let t = target, let i = state.settings.jersey.teams.firstIndex(where: { $0.id == t.team }) else { return } + if t.shorts { state.settings.jersey.teams[i].shortsColor = color } else { state.settings.jersey.teams[i].shirtColor = color } + // next useful target: shirt → shorts? no – shirt of the next team + if !t.shorts, i + 1 < state.settings.jersey.teams.count { + target = PickTarget(team: state.settings.jersey.teams[i + 1].id, shorts: false) + } + } + .background(Color.black) + .clipShape(RoundedRectangle(cornerRadius: 6)) + Text("Team und „Trikot“/„Hose“ links wählen, dann im Foto auf eine einfarbige Trikotfläche klicken (nicht auf die Nummer). Mit ‹ › ein passendes Bild suchen.") + .font(.caption).foregroundStyle(.secondary) + } + } + + private var targetText: String { + guard let t = target, let team = state.settings.jersey.teams.first(where: { $0.id == t.team }) else { + return "Farbe aus dem Foto aufnehmen" + } + return "Klick setzt: \(team.name) – \(t.shorts ? "Hose" : "Trikot")" + } + + private func loadPhoto() async { + guard let item = state.cursorItem else { photo = nil; return } + let url = state.previewURL(for: item) + photo = await Task.detached(priority: .userInitiated) { JerseyRecognizer.loadImage(url) }.value + } + + // MARK: Settings + + private var settingsColumn: some View { VStack(alignment: .leading, spacing: 12) { Text("Trikot-Erkennung").font(.title3.bold()) Text("Pro Spiel die Teams wählen und die Trikotfarbe festlegen. Lichtkasten liest Nummern auf Trikot und Hose, bestimmt das Team über die Farbe und schlägt den Spieler aus deinen Code-Ersetzungen vor (Präfix + Nummer, z. B. w10).") @@ -65,8 +124,6 @@ struct JerseySetupView: View { .disabled(state.settings.jersey.teams.isEmpty || state.folder == nil) } } - .padding(18) - .frame(width: 680) } @ViewBuilder @@ -84,17 +141,19 @@ struct JerseySetupView: View { } } Spacer() - ColorPicker("Trikot", selection: cgBinding(team.shirtColor), supportsOpacity: false) + swatchButton(i, shorts: false) Toggle("Hose", isOn: Binding( get: { state.settings.jersey.teams[i].shortsColor != nil }, - set: { state.settings.jersey.teams[i].shortsColor = $0 ? state.settings.jersey.teams[i].shirtColor : nil })) + set: { on in + state.settings.jersey.teams[i].shortsColor = on ? state.settings.jersey.teams[i].shirtColor : nil + if on { target = PickTarget(team: state.settings.jersey.teams[i].id, shorts: true) } + })) .toggleStyle(.checkbox) - if state.settings.jersey.teams[i].shortsColor != nil { - ColorPicker("", selection: Binding( - get: { cg(state.settings.jersey.teams[i].shortsColor ?? state.settings.jersey.teams[i].shirtColor) }, - set: { state.settings.jersey.teams[i].shortsColor = rgb($0) }), supportsOpacity: false) - .labelsHidden() - } + .help("Eigene Hosenfarbe (z. B. weißes Trikot, grüne Hose)") + if state.settings.jersey.teams[i].shortsColor != nil { swatchButton(i, shorts: true) } + ColorPicker("", selection: cgBinding(team.shirtColor), supportsOpacity: false) + .labelsHidden() + .help("Alternativ: Farbe manuell wählen") Button(role: .destructive) { state.settings.jersey.teams.remove(at: i) } label: { Image(systemName: "trash") } .buttonStyle(.borderless) } @@ -102,6 +161,24 @@ struct JerseySetupView: View { } } + /// Color swatch; click = "next click in the photo sets this color". + private func swatchButton(_ i: Int, shorts: Bool) -> some View { + let team = state.settings.jersey.teams[i] + let c = shorts ? (team.shortsColor ?? team.shirtColor) : team.shirtColor + let active = target == PickTarget(team: team.id, shorts: shorts) + return Button { target = PickTarget(team: team.id, shorts: shorts) } label: { + VStack(spacing: 2) { + RoundedRectangle(cornerRadius: 4) + .fill(Color(red: c.r, green: c.g, blue: c.b)) + .frame(width: 34, height: 24) + .overlay(RoundedRectangle(cornerRadius: 4).stroke(active ? Color.accentColor : Color.secondary, lineWidth: active ? 3 : 1)) + Text(shorts ? "Hose" : "Trikot").font(.caption2) + } + } + .buttonStyle(.plain) + .help("Auswählen und dann im Foto auf \(shorts ? "die Hose" : "das Trikot") klicken") + } + private func example(_ t: JerseyTeam) -> String { guard let set = state.codeLibrary.sets.first(where: { $0.id == t.codeSetID }), let e = set.entries.first(where: { $0.code.lowercased().hasPrefix(t.prefix.lowercased()) }) else { return "" } @@ -129,3 +206,65 @@ struct JerseySetupView: View { Binding(get: { cg(b.wrappedValue) }, set: { b.wrappedValue = rgb($0) }) } } + +/// Photo in which a click samples the kit color (k-means over a small patch, the same measure +/// the recognizer uses – numbers, folds and highlights drop out). +struct ImageColorPicker: View { + let image: CGImage? + let onPick: (KitColor) -> Void + @State private var marker: CGPoint? + @State private var lastColor: KitColor? + + var body: some View { + GeometryReader { geo in + if let image { + let fit = Self.fitRect(CGSize(width: image.width, height: image.height), in: geo.size) + ZStack(alignment: .topLeading) { + Image(decorative: image, scale: 1) + .resizable() + .frame(width: fit.width, height: fit.height) + .offset(x: fit.minX, y: fit.minY) + if let m = marker { + Circle().stroke(Color.white, lineWidth: 2) + .background(Circle().fill(lastColor.map { Color(red: $0.r, green: $0.g, blue: $0.b) } ?? .clear)) + .frame(width: 22, height: 22) + .shadow(radius: 2) + .offset(x: fit.minX + m.x * fit.width - 11, y: fit.minY + m.y * fit.height - 11) + } + } + .frame(width: geo.size.width, height: geo.size.height, alignment: .topLeading) + .contentShape(Rectangle()) + .onTapGesture(coordinateSpace: .local) { loc in + guard fit.contains(loc) else { return } + let nx = (loc.x - fit.minX) / fit.width, ny = (loc.y - fit.minY) / fit.height + guard let color = Self.sample(image, nx: nx, ny: ny) else { return } + marker = CGPoint(x: nx, y: ny) + lastColor = color + onPick(color) + } + .onHover { inside in if inside { NSCursor.crosshair.push() } else { NSCursor.pop() } } + } else { + Text("Kein Bild ausgewählt").foregroundStyle(.secondary) + .frame(width: geo.size.width, height: geo.size.height) + } + } + .onChange(of: image.map { ObjectIdentifier($0) }) { _, _ in marker = nil } + } + + static func fitRect(_ img: CGSize, in box: CGSize) -> CGRect { + guard img.width > 0, img.height > 0 else { return .zero } + let s = min(box.width / img.width, box.height / img.height) + let w = img.width * s, h = img.height * s + return CGRect(x: (box.width - w) / 2, y: (box.height - h) / 2, width: w, height: h) + } + + /// Patch of ~1.5 % of the image width around the click. + static func sample(_ img: CGImage, nx: CGFloat, ny: CGFloat) -> KitColor? { + let W = CGFloat(img.width), H = CGFloat(img.height) + let r = max(8, W * 0.0075) + let rect = CGRect(x: nx * W - r, y: ny * H - r, width: 2 * r, height: 2 * r) + .intersection(CGRect(x: 0, y: 0, width: W, height: H)).integral + guard rect.width > 2, let crop = img.cropping(to: rect) else { return nil } + return JerseyRecognizer.dominantColor(crop, centerOnly: false) + } +}