@@ -129,20 +129,21 @@ case class Col1i(override val rgba: Int) extends Color {
129
129
* @param a The alpha component to add in the `[0.0, 1.0]` range.
130
130
* @return The sum between this color and the given components.
131
131
*/
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 )
133
133
134
134
/**
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.
138
136
*
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.
144
139
*/
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
+ }
146
147
147
148
/**
148
149
* 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 {
155
156
* @param a The alpha component to subtract in the `[0.0, 1.0]` range.
156
157
* @return The subtraction between this color and the given components.
157
158
*/
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 )
159
160
160
161
/**
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.
164
163
*
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.
170
166
*/
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
+ }
172
174
173
175
/**
174
176
* 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 {
183
185
*/
184
186
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)
185
187
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
+
186
216
/**
187
217
* Returns this color with its `r`, `g`, and `b` components inverted.
188
218
*
@@ -222,11 +252,14 @@ case class Col1i(override val rgba: Int) extends Color {
222
252
if (a == 0.0f ) {
223
253
Col1i (0 )
224
254
} 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
+ }
230
263
}
231
264
}
232
265
@@ -286,4 +319,39 @@ object Col1i {
286
319
* @return A new color constructed from the three given components.
287
320
*/
288
321
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 )
289
357
}
0 commit comments