Skip to content

Commit 6f66077

Browse files
authored
Add set methods and constructors to ColorRGBA that take in a Vector param (#1757)
* Add additional set methods to ColorRGBA I've noticed that ColorRGBA has a very useful .toVector4f() method (and also toVector3() method), which are great for instances where you need to do math with a Color value returned from an ImageRaster, but there is no equally convenient method to convert that Vector back into a color, so I thought these 2 methods could be useful (specifically for Vector4fs, but I added one for Vector3f too since there was already a toVector3f() method) I initially thought of adding a new toColorRGBA() method to the Vector classes, but it seemed more appropriate to put this functionality into the ColorRGBA class here with set methods the way I did. Any thoughts on this addition? * Update ColorRGBA.java * add vector constructor I added constructors that take in a Vector3f and Vector4f In the case of a vector3 constructor I also just set the alpha value to 1.0 since it's more likely the user wants a visible opaque color, but can change this if there's a more optimal solution. * Update ColorRGBA.java * Update ColorRGBA.java grammar correction in javadoc * Update ColorRGBA.java
1 parent 0da5a7f commit 6f66077

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

jme3-core/src/main/java/com/jme3/math/ColorRGBA.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,34 @@ public ColorRGBA(ColorRGBA rgba) {
161161
this.g = rgba.g;
162162
this.b = rgba.b;
163163
}
164+
165+
/**
166+
* Constructor creates a new <code>ColorRGBA</code> object, based on
167+
* a provided Vector4f.
168+
*
169+
* @param vec4 The <code>Vector4f</code> object that will have its x, y, z, and w
170+
* values copied to this color's r, g, b, and a values respectively.
171+
*/
172+
public ColorRGBA(Vector4f vec4) {
173+
this.a = vec4.w;
174+
this.r = vec4.x;
175+
this.g = vec4.y;
176+
this.b = vec4.z;
177+
}
178+
179+
/**
180+
* Constructor creates a new <code>ColorRGBA</code> object, based on
181+
* a provided Vector3f, at full opacity with a 1.0 alpha value by default
182+
*
183+
* @param vec3 The <code>Vector3f</code> object that will have its x, y, and z
184+
* values copied to this color's r, g, and b values respectively.
185+
*/
186+
public ColorRGBA(Vector3f vec3) {
187+
this.a = 1.0f;
188+
this.r = vec3.x;
189+
this.g = vec3.y;
190+
this.b = vec3.z;
191+
}
164192

165193
/**
166194
* <code>set</code> sets the RGBA values of this <code>ColorRGBA</code>.
@@ -203,6 +231,52 @@ public ColorRGBA set(ColorRGBA rgba) {
203231
}
204232
return this;
205233
}
234+
235+
/**
236+
* <code>set</code> sets the values of this <code>ColorRGBA</code> to those
237+
* set by a parameter Vector4f.
238+
*
239+
* @param vec4 The 4 component vector that will have its x, y, z, and w values copied to
240+
* this <code>ColorRGBA</code>'s r, g, b, and a values respectively.
241+
*
242+
* @return this
243+
*/
244+
public ColorRGBA set(Vector4f vec4) {
245+
if (vec4 == null) {
246+
r = 0;
247+
g = 0;
248+
b = 0;
249+
a = 0;
250+
} else {
251+
r = vec4.x;
252+
g = vec4.y;
253+
b = vec4.z;
254+
a = vec4.w;
255+
}
256+
return this;
257+
}
258+
259+
/**
260+
* <code>set</code> sets the values of this <code>ColorRGBA</code> to those
261+
* set by a parameter Vector3f.
262+
*
263+
* @param vec3 The 3 component vector that will have its x, y, and z values copied to
264+
* this <code>ColorRGBA</code>'s r, g, and b values respectively.
265+
*
266+
* @return this
267+
*/
268+
public ColorRGBA set(Vector3f vec3) {
269+
if (vec3 == null) {
270+
r = 0;
271+
g = 0;
272+
b = 0;
273+
} else {
274+
r = vec3.x;
275+
g = vec3.y;
276+
b = vec3.z;
277+
}
278+
return this;
279+
}
206280

207281
/**
208282
* Sets the red color to the specified value.

0 commit comments

Comments
 (0)