Skip to content

Commit b3e55aa

Browse files
committed
Finished scala tests
1 parent a7b4508 commit b3e55aa

File tree

6 files changed

+855
-170
lines changed

6 files changed

+855
-170
lines changed

src/main/scala/io/github/scalamath/colorlib/Col1i.scala

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,21 @@ case class Col1i(override val rgba: Int) extends Color {
129129
* @param a The alpha component to add in the `[0.0, 1.0]` range.
130130
* @return The sum between this color and the given components.
131131
*/
132-
override def +(r: Float, g: Float, b: Float, a: Float): Color = this + ((r * 255.0f).round, (g * 255.0f).round, (b * 255.0f).round, (a * 255.0f).round)
132+
override def +(r: Float, g: Float, b: Float, a: Float): Color = Col1i(this.r + r, this.g + g, this.b + b, this.a + a)
133133

134134
/**
135-
* Adds the given values to each component of this color and returns the result.
136-
*
137-
* The result is clamped.
135+
* Adds each component of this color with the components of the given color and returns the result.
138136
*
139-
* @param r The red component to add in the `[0, 255]` range.
140-
* @param g The green component to add in the `[0, 255]` range.
141-
* @param b The blue component to add in the `[0, 255]` range.
142-
* @param a The alpha component to add in the `[0, 255]` range.
143-
* @return The sum between this color and the given components.
137+
* @param c The color to add.
138+
* @return The sum between this color and the given one.
144139
*/
145-
override def +(r: Int, g: Int, b: Int, a: Int): Color = Col1i(this.r8 + r, this.g8 + g, this.b8 + b, this.a8 + a)
140+
override def +(c: Color): Color = {
141+
if(c.isInstanceOf[Col1i]) {
142+
Col1i(this.rgba + c.rgba)
143+
} else {
144+
Col4f(this.r + c.r, this.g + c.g, this.b + c.b, this.a + c.a)
145+
}
146+
}
146147

147148
/**
148149
* Subtracts the given values from each component of this color and returns the result.
@@ -155,20 +156,21 @@ case class Col1i(override val rgba: Int) extends Color {
155156
* @param a The alpha component to subtract in the `[0.0, 1.0]` range.
156157
* @return The subtraction between this color and the given components.
157158
*/
158-
override def -(r: Float, g: Float, b: Float, a: Float): Color = this - ((r * 255.0f).round, (g * 255.0f).round, (b * 255.0f).round, (a * 255.0f).round)
159+
override def -(r: Float, g: Float, b: Float, a: Float): Color = Col1i(this.r - r, this.g - g, this.b - b, this.a - a)
159160

160161
/**
161-
* Subtracts the given values from each component of this color and returns the result.
162-
*
163-
* The result is clamped.
162+
* Subtracts each component of the given color from the components of this color and returns the result.
164163
*
165-
* @param r The red component to subtract in the `[0, 255]` range.
166-
* @param g The green component to subtract in the `[0, 255]` range.
167-
* @param b The blue component to subtract in the `[0, 255]` range.
168-
* @param a The alpha component to subtract in the `[0, 255]` range.
169-
* @return The subtraction between this color and the given components.
164+
* @param c The color to subtract.
165+
* @return The subtraction between this color and the given one.
170166
*/
171-
override def -(r: Int, g: Int, b: Int, a: Int): Color = Col1i(this.r8 - r, this.g8 - g, this.b8 - b, this.a8 - a)
167+
override def -(c: Color): Color = {
168+
if(c.isInstanceOf[Col1i]) {
169+
Col1i(this.rgba - c.rgba)
170+
} else {
171+
Col4f(this.r - c.r, this.g - c.g, this.b - c.b, this.a - c.a)
172+
}
173+
}
172174

173175
/**
174176
* Multiplies each component of this color with the given values and returns the result.
@@ -183,6 +185,34 @@ case class Col1i(override val rgba: Int) extends Color {
183185
*/
184186
override def *(r: Float, g: Float, b: Float, a: Float): Color = Col1i((this.r8 * r).round, (this.g8 * g).round, (this.b8 * b).round, (this.a8 * a).round)
185187

188+
/**
189+
* Multiplies each component of this color with each component of the given one and returns the result.
190+
*
191+
* @param c The color to multiply this one by.
192+
* @return The component-wise product between this color and the given one.
193+
*/
194+
override def *(c: Color): Color = {
195+
if(c.isInstanceOf[Col1i]) {
196+
Col1i(this.r * c.r, this.g * c.g, this.b * c.b, this.a * c.a)
197+
} else {
198+
Col4f(this.r * c.r, this.g * c.g, this.b * c.b, this.a * c.a)
199+
}
200+
}
201+
202+
/**
203+
* Divides each component of this color by each component of the given one and returns the result.
204+
*
205+
* @param c The color to divide this one by.
206+
* @return The component-wise division between this color and the given one.
207+
*/
208+
override def /(c: Color): Color = {
209+
if(c.isInstanceOf[Col1i]) {
210+
Col1i(this.r / c.r, this.g / c.g, this.b / c.b, this.a / c.a)
211+
} else {
212+
Col4f(this.r / c.r, this.g / c.g, this.b / c.b, this.a / c.a)
213+
}
214+
}
215+
186216
/**
187217
* Returns this color with its `r`, `g`, and `b` components inverted.
188218
*
@@ -222,11 +252,14 @@ case class Col1i(override val rgba: Int) extends Color {
222252
if(a == 0.0f) {
223253
Col1i(0)
224254
} else {
225-
Col1i(
226-
(this.r * this.a * sa + c.r * c.a) / a,
227-
(this.g * this.a * sa + c.g * c.a) / a,
228-
(this.b * this.a * sa + c.b * c.a) / a, a
229-
)
255+
val r = (this.r * this.a * sa + c.r * c.a) / a
256+
val g = (this.g * this.a * sa + c.g * c.a) / a
257+
val b = (this.b * this.a * sa + c.b * c.a) / a
258+
if(c.isInstanceOf[Col1i]) {
259+
Col1i(r, g, b, a)
260+
} else {
261+
Col4f(r, g, b, a)
262+
}
230263
}
231264
}
232265

@@ -286,4 +319,39 @@ object Col1i {
286319
* @return A new color constructed from the three given components.
287320
*/
288321
def apply(r: Float, g: Float, b: Float) = new Col1i(r, g, b)
322+
323+
/**
324+
* Constructs a color from the given components in the HSV format.
325+
*
326+
* @param h The hue of the color.
327+
* @param s The saturation of the color.
328+
* @param v The lightness (value) of the color.
329+
* @param a The alpha component of the color.
330+
* @return A new color constructed from the given components in the HSV format.
331+
*/
332+
def hsv(h: Float, s: Float, v: Float, a: Float): Col1i = {
333+
val i = (h * 6.0f).floor
334+
val f = h * 6.0f - i
335+
val p = v * (1.0f - s)
336+
val q = v * (1.0f - f * s)
337+
val t = v * (1.0f - (1.0f - f) * s)
338+
i % 6 match {
339+
case 0 => Col1i(v, t, p, a)
340+
case 1 => Col1i(q, v, p, a)
341+
case 2 => Col1i(p, v, t, a)
342+
case 3 => Col1i(p, q, v, a)
343+
case 4 => Col1i(t, p, v, a)
344+
case 5 => Col1i(v, p, q, a)
345+
}
346+
}
347+
348+
/**
349+
* Constructs a color from the given components in the HSV format and sets the alpha component to `1.0`.
350+
*
351+
* @param h The hue of the color.
352+
* @param s The saturation of the color.
353+
* @param v The lightness (value) of the color.
354+
* @return A new color constructed from the given components in the HSV format.
355+
*/
356+
def hsv(h: Float, s: Float, v: Float): Col1i = this.hsv(h, s, v, 1.0f)
289357
}

src/main/scala/io/github/scalamath/colorlib/Col3f.scala

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
144144
*/
145145
override def *(c: Color): Color = if(c.isInstanceOf[Col3f]) this * (c.r, c.g, c.b) else this * (c.r, c.g, c.b, c.a)
146146

147+
/**
148+
* Multiplies each component of this color with the given value and returns the result.
149+
*
150+
* @param f The value to multiply this color by.
151+
* @return The product between this color and the given value.
152+
*/
153+
override def *(f: Float): Color = this * (f, f, f)
154+
147155
/**
148156
* Divides each component of this color by each component of the given one and returns the result.
149157
*
@@ -181,17 +189,35 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
181189
*/
182190
override def lighter(k: Float): Color = Col3f(this.r + (1.0f - this.r) * k, this.g + (1.0f - this.g) * k, this.b + (1.0f - this.b) * k)
183191

192+
/**
193+
* Computes the linear interpolation between this color and the given one by the given weight and returns the result.
194+
*
195+
* The given weight must be in the `[0.0, 1.0]` range, representing the amount of interpolation.
196+
*
197+
* @param to The color to interpolate to.
198+
* @param weight The weight of the interpolation between `0.0` and `1.0`.
199+
* @return The result of linearly interpolating between this color and the given one.
200+
*/
201+
override def lerp(to: Color, weight: Float): Color = {
202+
if(to.isInstanceOf[Col3f]) {
203+
super.lerp(to, weight)
204+
} else {
205+
val f = 1.0f - weight
206+
this * (f, f, f, f) + (to * weight)
207+
}
208+
}
209+
184210
/**
185211
* Blends this color and the given one and returns the result.
186212
*
187-
* This is the equivalent of multiplying the colors together if the given color is a [[Col3f]].
213+
* This returns the given color if it is a [[Col3f]], since its alpha component is always `1.0`.
188214
*
189215
* @param c The color to blend this one with.
190216
* @return The color resulting from overlaying this color over the given one.
191217
*/
192218
override def blend(c: Color): Color = {
193219
if(c.isInstanceOf[Col3f]) {
194-
this * c
220+
c
195221
} else {
196222
val sa = 1.0f - c.a
197223
Col4f(this.r * sa + c.r * c.a, this.g * sa + c.g * c.a, this.b * sa + c.b * c.a)

0 commit comments

Comments
 (0)