Skip to content

Commit 3a508cc

Browse files
committed
Add GradientPattern
(Implemented using Anthropic's Claude)
1 parent f72bfe4 commit 3a508cc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/main/scala/Pattern.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,27 @@ def stripeAtObject(
9494
): color.Color = {
9595
patternAtShape(pattern, obj, worldPoint)
9696
}
97+
98+
// Gradient pattern implementation using linear interpolation
99+
case class GradientPattern(
100+
a: color.Color,
101+
b: color.Color,
102+
override val transform: matrix.Matrix = matrix.Matrix.identity()
103+
) extends Pattern(transform) {
104+
105+
def patternAt(point: tuple.Tuple): color.Color = {
106+
// Get the fractional part of x coordinate for interpolation
107+
val distance = point.x - math.floor(point.x)
108+
109+
// Linear interpolation between colors a and b
110+
color.Color(
111+
a.red + distance * (b.red - a.red),
112+
a.green + distance * (b.green - a.green),
113+
a.blue + distance * (b.blue - a.blue)
114+
)
115+
}
116+
117+
def withTransform(newTransform: matrix.Matrix): GradientPattern = {
118+
GradientPattern(a, b, newTransform)
119+
}
120+
}

src/test/scala/PatternSuite.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,41 @@ class PatternSuite extends munit.FunSuite {
135135

136136
assertEquals(color.isEqual(c, expected), true)
137137
}
138+
139+
test("A gradient linearly interpolates between colors") {
140+
val pattern = GradientPattern(white, black)
141+
142+
// At x=0, should be white
143+
assertEquals(
144+
color.isEqual(pattern.patternAt(tuple.makePoint(0, 0, 0)), white),
145+
true
146+
)
147+
148+
// At x=0.25, should be color(0.75, 0.75, 0.75)
149+
assertEquals(
150+
color.isEqual(
151+
pattern.patternAt(tuple.makePoint(0.25, 0, 0)),
152+
color.Color(0.75, 0.75, 0.75)
153+
),
154+
true
155+
)
156+
157+
// At x=0.5, should be color(0.5, 0.5, 0.5)
158+
assertEquals(
159+
color.isEqual(
160+
pattern.patternAt(tuple.makePoint(0.5, 0, 0)),
161+
color.Color(0.5, 0.5, 0.5)
162+
),
163+
true
164+
)
165+
166+
// At x=0.75, should be color(0.25, 0.25, 0.25)
167+
assertEquals(
168+
color.isEqual(
169+
pattern.patternAt(tuple.makePoint(0.75, 0, 0)),
170+
color.Color(0.25, 0.25, 0.25)
171+
),
172+
true
173+
)
174+
}
138175
}

0 commit comments

Comments
 (0)