Skip to content

Commit c2de656

Browse files
committed
Clamp currentAlpha to 0...255
1 parent 9405f25 commit c2de656

3 files changed

Lines changed: 78 additions & 6 deletions

File tree

TextureLayerView/Sources/TextureLayerView/TextureLayerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public struct TextureLayerView: View {
5353

5454
/// Updates the alpha slider without changing the selected layer.
5555
public func updateAlpha(_ alpha: Int) {
56-
viewModel.currentAlpha = alpha
56+
viewModel.setCurrentAlpha(alpha)
5757
}
5858
}
5959

TextureLayerView/Sources/TextureLayerView/TextureLayerViewModel.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import UIKit
1313
@MainActor
1414
open class TextureLayerViewModel: ObservableObject {
1515

16+
static let alphaRange: ClosedRange<Int> = 0...255
17+
1618
@Published public var currentAlpha: Int = 0
1719

1820
@Published public var isAlphaSliderDragging: Bool = false
@@ -143,10 +145,17 @@ open class TextureLayerViewModel: ObservableObject {
143145

144146
open func onChangeCurrentAlpha(_ alpha: Int) {
145147
guard let selectedLayerId = selectedLayer?.id else { return }
146-
textureLayers.updateAlpha(selectedLayerId, alpha: alpha)
147-
updateCurrentAlpha()
148+
let clamped = Self.clampedAlpha(alpha)
149+
textureLayers.updateAlpha(selectedLayerId, alpha: clamped)
150+
setCurrentAlpha(clamped)
148151
onLayersChanged?(.changeLayerAlpha)
149152
}
153+
154+
func setCurrentAlpha(_ alpha: Int) {
155+
let clamped = Self.clampedAlpha(alpha)
156+
guard currentAlpha != clamped else { return }
157+
currentAlpha = clamped
158+
}
150159
}
151160

152161
public extension TextureLayerViewModel {
@@ -199,13 +208,16 @@ public extension TextureLayerViewModel {
199208

200209
extension TextureLayerViewModel {
201210

211+
static func clampedAlpha(_ alpha: Int) -> Int {
212+
min(max(alphaRange.lowerBound, alpha), alphaRange.upperBound)
213+
}
214+
202215
private func updateCurrentAlpha() {
203216
guard
204217
let selectedLayerId = selectedLayer?.id,
205-
let layer = textureLayers.layer(selectedLayerId),
206-
currentAlpha != layer.alpha
218+
let layer = textureLayers.layer(selectedLayerId)
207219
else { return }
208220

209-
currentAlpha = layer.alpha
221+
setCurrentAlpha(layer.alpha)
210222
}
211223
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// TextureLayerViewModelTests.swift
3+
// TextureLayerViewTests
4+
//
5+
// Created by Eisuke Kusachi on 2026/08/23.
6+
//
7+
8+
import CoreGraphics
9+
import Testing
10+
11+
@testable import TextureLayerView
12+
13+
@MainActor
14+
struct TextureLayerViewModelTests {
15+
16+
private typealias Subject = TextureLayerViewModel
17+
18+
private let textureSize: CGSize = .init(width: 1, height: 1)
19+
20+
@Test
21+
func `setCurrentAlpha clamps values below 0`() {
22+
let subject = makeSubject()
23+
24+
subject.setCurrentAlpha(-1)
25+
26+
#expect(subject.currentAlpha == 0)
27+
}
28+
29+
@Test
30+
func `setCurrentAlpha clamps values above 255`() {
31+
let subject = makeSubject()
32+
33+
subject.setCurrentAlpha(256)
34+
35+
#expect(subject.currentAlpha == 255)
36+
}
37+
38+
@Test
39+
func `onChangeCurrentAlpha clamps the selected layer alpha`() {
40+
let subject = makeSubject()
41+
42+
subject.onChangeCurrentAlpha(-1)
43+
44+
#expect(subject.currentAlpha == 0)
45+
#expect(subject.textureLayers.selectedLayer?.alpha == 0)
46+
47+
subject.onChangeCurrentAlpha(300)
48+
49+
#expect(subject.currentAlpha == 255)
50+
#expect(subject.textureLayers.selectedLayer?.alpha == 255)
51+
}
52+
53+
private func makeSubject() -> Subject {
54+
Subject(
55+
textureLayers: TextureLayersState(
56+
textureLayers: .init(textureSize: textureSize)
57+
)
58+
)
59+
}
60+
}

0 commit comments

Comments
 (0)