OSDN Git Service

Added two new methods to convert ColorRGBA to Vector3f and Vector4f. These should...
authordanyrioux@gmail.com <danyrioux@gmail.com@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Sat, 3 Sep 2011 10:24:38 +0000 (10:24 +0000)
committerdanyrioux@gmail.com <danyrioux@gmail.com@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Sat, 3 Sep 2011 10:24:38 +0000 (10:24 +0000)
Formatting.

git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@8155 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

engine/src/core/com/jme3/math/ColorRGBA.java

index 3d523b8..cf023d5 100644 (file)
@@ -1,35 +1,32 @@
 /*\r
- * Copyright (c) 2009-2010 jMonkeyEngine\r
- * All rights reserved.\r
+ * Copyright (c) 2009-2010 jMonkeyEngine All rights reserved.\r
  *\r
  * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions are\r
- * met:\r
- *\r
- * * Redistributions of source code must retain the above copyright\r
- *   notice, this list of conditions and the following disclaimer.\r
+ * modification, are permitted provided that the following conditions are met:\r
+ *  *\r
+ * * Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the following disclaimer.\r
  *\r
  * * Redistributions in binary form must reproduce the above copyright\r
- *   notice, this list of conditions and the following disclaimer in the\r
- *   documentation and/or other materials provided with the distribution.\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
  *\r
  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors\r
- *   may be used to endorse or promote products derived from this software\r
- *   without specific prior written permission.\r
+ * may be used to endorse or promote products derived from this software\r
+ * without specific prior written permission.\r
  *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+ * POSSIBILITY OF SUCH DAMAGE.\r
  */\r
-\r
 package com.jme3.math;\r
 \r
 import com.jme3.export.InputCapsule;\r
@@ -39,25 +36,24 @@ import com.jme3.export.OutputCapsule;
 import com.jme3.export.Savable;\r
 import java.io.IOException;\r
 \r
-\r
 /**\r
- * <code>ColorRGBA</code> defines a color made from a collection of\r
- * red, green and blue values. An alpha value determines is transparency.\r
- * All values must be between 0 and 1. If any value is set higher or lower\r
- * than these constraints they are clamped to the min or max. That is, if\r
- * a value smaller than zero is set the value clamps to zero. If a value\r
- * higher than 1 is passed, that value is clamped to 1. However, because the\r
- * attributes r, g, b, a are public for efficiency reasons, they can be\r
- * directly modified with invalid values. The client should take care when\r
- * directly addressing the values. A call to clamp will assure that the values\r
- * are within the constraints.\r
+ * <code>ColorRGBA</code> defines a color made from a collection of red, green\r
+ * and blue values. An alpha value determines is transparency. All values must\r
+ * be between 0 and 1. If any value is set higher or lower than these\r
+ * constraints they are clamped to the min or max. That is, if a value smaller\r
+ * than zero is set the value clamps to zero. If a value higher than 1 is\r
+ * passed, that value is clamped to 1. However, because the attributes r, g, b,\r
+ * a are public for efficiency reasons, they can be directly modified with\r
+ * invalid values. The client should take care when directly addressing the\r
+ * values. A call to clamp will assure that the values are within the\r
+ * constraints.\r
+ *\r
  * @author Mark Powell\r
  * @version $Id: ColorRGBA.java,v 1.29 2007/09/09 18:25:14 irrisor Exp $\r
  */\r
 public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable {\r
 \r
     static final long serialVersionUID = 1;\r
-    \r
     /**\r
      * the color black (0,0,0).\r
      */\r
@@ -105,21 +101,19 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
     /**\r
      * the color orange (251/255, 130/255,0).\r
      */\r
-    public static final ColorRGBA Orange = new ColorRGBA(251f/255f, 130f/255f, 0f, 1f);\r
+    public static final ColorRGBA Orange = new ColorRGBA(251f / 255f, 130f / 255f, 0f, 1f);\r
     /**\r
      * the color brown (65/255, 40/255, 25/255).\r
      */\r
-    public static final ColorRGBA Brown = new ColorRGBA(65f/255f, 40f/255f, 25f/255f, 1f);\r
+    public static final ColorRGBA Brown = new ColorRGBA(65f / 255f, 40f / 255f, 25f / 255f, 1f);\r
     /**\r
      * the color pink (1, 0.68, 0.68).\r
      */\r
     public static final ColorRGBA Pink = new ColorRGBA(1f, 0.68f, 0.68f, 1f);\r
-\r
     /**\r
      * the black color with no alpha (0, 0, 0, 0);\r
      */\r
     public static final ColorRGBA BlackNoAlpha = new ColorRGBA(0f, 0f, 0f, 0f);\r
-\r
     /**\r
      * The red component of the color.\r
      */\r
@@ -133,7 +127,7 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      */\r
     public float b;\r
     /**\r
-     * the alpha component of the color.  0 is transparent and 1 is opaque\r
+     * the alpha component of the color. 0 is transparent and 1 is opaque\r
      */\r
     public float a;\r
 \r
@@ -156,10 +150,10 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @param a the alpha component of this color.\r
      */\r
     public ColorRGBA(float r, float g, float b, float a) {\r
-      this.r = r;\r
-      this.g = g;\r
-      this.b = b;\r
-      this.a = a;\r
+        this.r = r;\r
+        this.g = g;\r
+        this.b = b;\r
+        this.a = a;\r
     }\r
 \r
     /**\r
@@ -186,11 +180,11 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return this\r
      */\r
     public ColorRGBA set(float r, float g, float b, float a) {\r
-      this.r = r;\r
-      this.g = g;\r
-      this.b = b;\r
-      this.a = a;\r
-      return this;\r
+        this.r = r;\r
+        this.g = g;\r
+        this.b = b;\r
+        this.a = a;\r
+        return this;\r
     }\r
 \r
     /**\r
@@ -201,17 +195,17 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return this\r
      */\r
     public ColorRGBA set(ColorRGBA rgba) {\r
-      if(rgba == null) {\r
-          r = 0;\r
-          g = 0;\r
-          b = 0;\r
-          a = 0;\r
-      } else {\r
-          r = rgba.r;\r
-          g = rgba.g;\r
-          b = rgba.b;\r
-          a = rgba.a;\r
-      }\r
+        if (rgba == null) {\r
+            r = 0;\r
+            g = 0;\r
+            b = 0;\r
+            a = 0;\r
+        } else {\r
+            r = rgba.r;\r
+            g = rgba.g;\r
+            b = rgba.b;\r
+            a = rgba.a;\r
+        }\r
         return this;\r
     }\r
 \r
@@ -254,7 +248,7 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return the float array that contains the color elements.\r
      */\r
     public float[] getColorArray() {\r
-        return new float[] {r,g,b,a};\r
+        return new float[]{r, g, b, a};\r
     }\r
 \r
     /**\r
@@ -264,26 +258,26 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return The float[] after storage.\r
      */\r
     public float[] getColorArray(float[] store) {\r
-        store[0]=r;\r
-        store[1]=g;\r
-        store[2]=b;\r
-        store[3]=a;\r
+        store[0] = r;\r
+        store[1] = g;\r
+        store[2] = b;\r
+        store[3] = a;\r
         return store;\r
     }\r
 \r
-    public float getAlpha(){\r
+    public float getAlpha() {\r
         return a;\r
     }\r
 \r
-    public float getRed(){\r
+    public float getRed() {\r
         return r;\r
     }\r
 \r
-    public float getBlue(){\r
+    public float getBlue() {\r
         return b;\r
     }\r
 \r
-    public float getGreen(){\r
+    public float getGreen() {\r
         return g;\r
     }\r
 \r
@@ -294,11 +288,11 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @param changeAmnt An amount between 0.0 - 1.0 representing a precentage\r
      *  change from this towards finalColor\r
      */\r
-    public void interpolate(ColorRGBA finalColor,float changeAmnt){\r
-        this.r=(1-changeAmnt)*this.r + changeAmnt*finalColor.r;\r
-        this.g=(1-changeAmnt)*this.g + changeAmnt*finalColor.g;\r
-        this.b=(1-changeAmnt)*this.b + changeAmnt*finalColor.b;\r
-        this.a=(1-changeAmnt)*this.a + changeAmnt*finalColor.a;\r
+    public void interpolate(ColorRGBA finalColor, float changeAmnt) {\r
+        this.r = (1 - changeAmnt) * this.r + changeAmnt * finalColor.r;\r
+        this.g = (1 - changeAmnt) * this.g + changeAmnt * finalColor.g;\r
+        this.b = (1 - changeAmnt) * this.b + changeAmnt * finalColor.b;\r
+        this.a = (1 - changeAmnt) * this.a + changeAmnt * finalColor.a;\r
     }\r
 \r
     /**\r
@@ -309,11 +303,11 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @param changeAmnt An amount between 0.0 - 1.0 representing a precentage\r
      *  change from beginColor towards finalColor\r
      */\r
-    public void interpolate(ColorRGBA beginColor,ColorRGBA finalColor,float changeAmnt){\r
-        this.r=(1-changeAmnt)*beginColor.r + changeAmnt*finalColor.r;\r
-        this.g=(1-changeAmnt)*beginColor.g + changeAmnt*finalColor.g;\r
-        this.b=(1-changeAmnt)*beginColor.b + changeAmnt*finalColor.b;\r
-        this.a=(1-changeAmnt)*beginColor.a + changeAmnt*finalColor.a;\r
+    public void interpolate(ColorRGBA beginColor, ColorRGBA finalColor, float changeAmnt) {\r
+        this.r = (1 - changeAmnt) * beginColor.r + changeAmnt * finalColor.r;\r
+        this.g = (1 - changeAmnt) * beginColor.g + changeAmnt * finalColor.g;\r
+        this.b = (1 - changeAmnt) * beginColor.b + changeAmnt * finalColor.b;\r
+        this.a = (1 - changeAmnt) * beginColor.a + changeAmnt * finalColor.a;\r
     }\r
 \r
     /**\r
@@ -324,11 +318,11 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return a random color.\r
      */\r
     public static ColorRGBA randomColor() {\r
-      ColorRGBA rVal = new ColorRGBA(0, 0, 0, 1);\r
-      rVal.r = FastMath.nextRandomFloat();\r
-      rVal.g = FastMath.nextRandomFloat();\r
-      rVal.b = FastMath.nextRandomFloat();\r
-      return rVal;\r
+        ColorRGBA rVal = new ColorRGBA(0, 0, 0, 1);\r
+        rVal.r = FastMath.nextRandomFloat();\r
+        rVal.g = FastMath.nextRandomFloat();\r
+        rVal.b = FastMath.nextRandomFloat();\r
+        return rVal;\r
     }\r
 \r
     /**\r
@@ -394,7 +388,7 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return the string representation of this color.\r
      */\r
     public String toString() {\r
-        return "Color["+r+", "+g+", "+b+", "+a+"]";\r
+        return "Color[" + r + ", " + g + ", " + b + ", " + a + "]";\r
     }\r
 \r
     @Override\r
@@ -408,7 +402,7 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
 \r
     /**\r
      * Saves this ColorRGBA into the given float[] object.\r
-     * \r
+     *\r
      * @param floats\r
      *            The float[] to take this ColorRGBA. If null, a new float[4] is\r
      *            created.\r
@@ -433,19 +427,27 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return true if the colors are equal, false otherwise.\r
      */\r
     public boolean equals(Object o) {\r
-        if( !(o instanceof ColorRGBA) ) {\r
+        if (!(o instanceof ColorRGBA)) {\r
             return false;\r
         }\r
 \r
-        if(this == o) {\r
+        if (this == o) {\r
             return true;\r
         }\r
 \r
-        ColorRGBA comp = (ColorRGBA)o;\r
-        if (Float.compare(r, comp.r) != 0) return false;\r
-        if (Float.compare(g, comp.g) != 0) return false;\r
-        if (Float.compare(b, comp.b) != 0) return false;\r
-        if (Float.compare(a, comp.a) != 0) return false;\r
+        ColorRGBA comp = (ColorRGBA) o;\r
+        if (Float.compare(r, comp.r) != 0) {\r
+            return false;\r
+        }\r
+        if (Float.compare(g, comp.g) != 0) {\r
+            return false;\r
+        }\r
+        if (Float.compare(b, comp.b) != 0) {\r
+            return false;\r
+        }\r
+        if (Float.compare(a, comp.a) != 0) {\r
+            return false;\r
+        }\r
         return true;\r
     }\r
 \r
@@ -456,15 +458,14 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
      * @return the hash code value of this color.\r
      */\r
     public int hashCode() {\r
-      int hash = 37;\r
-      hash += 37 * hash + Float.floatToIntBits(r);\r
-      hash += 37 * hash + Float.floatToIntBits(g);\r
-      hash += 37 * hash + Float.floatToIntBits(b);\r
-      hash += 37 * hash + Float.floatToIntBits(a);\r
-      return hash;\r
+        int hash = 37;\r
+        hash += 37 * hash + Float.floatToIntBits(r);\r
+        hash += 37 * hash + Float.floatToIntBits(g);\r
+        hash += 37 * hash + Float.floatToIntBits(b);\r
+        hash += 37 * hash + Float.floatToIntBits(a);\r
+        return hash;\r
     }\r
 \r
-\r
     public void write(JmeExporter e) throws IOException {\r
         OutputCapsule capsule = e.getCapsule(this);\r
         capsule.write(r, "r", 0);\r
@@ -481,50 +482,72 @@ public final class ColorRGBA implements Savable, Cloneable, java.io.Serializable
         a = capsule.readFloat("a", 0);\r
     }\r
 \r
-    public byte[] asBytesRGBA(){\r
+    public byte[] asBytesRGBA() {\r
         byte[] store = new byte[4];\r
-        store[0] = (byte)((int)(r * 255) & 0xFF);\r
-        store[1] = (byte)((int)(g * 255) & 0xFF);\r
-        store[2] = (byte)((int)(b * 255) & 0xFF);\r
-        store[3] = (byte)((int)(a * 255) & 0xFF);\r
+        store[0] = (byte) ((int) (r * 255) & 0xFF);\r
+        store[1] = (byte) ((int) (g * 255) & 0xFF);\r
+        store[2] = (byte) ((int) (b * 255) & 0xFF);\r
+        store[3] = (byte) ((int) (a * 255) & 0xFF);\r
         return store;\r
     }\r
 \r
     public int asIntARGB() {\r
         int argb = (((int) (a * 255) & 0xFF) << 24)\r
-                 | (((int) (r * 255) & 0xFF) << 16)\r
-                 | (((int) (g * 255) & 0xFF) << 8)\r
-                 | (((int) (b * 255) & 0xFF));\r
+                | (((int) (r * 255) & 0xFF) << 16)\r
+                | (((int) (g * 255) & 0xFF) << 8)\r
+                | (((int) (b * 255) & 0xFF));\r
         return argb;\r
     }\r
 \r
     public int asIntRGBA() {\r
         int rgba = (((int) (r * 255) & 0xFF) << 24)\r
-                 | (((int) (g * 255) & 0xFF) << 16)\r
-                 | (((int) (b * 255) & 0xFF) << 8)\r
-                 | (((int) (a * 255) & 0xFF));\r
+                | (((int) (g * 255) & 0xFF) << 16)\r
+                | (((int) (b * 255) & 0xFF) << 8)\r
+                | (((int) (a * 255) & 0xFF));\r
         return rgba;\r
     }\r
 \r
     public int asIntABGR() {\r
         int abgr = (((int) (a * 255) & 0xFF) << 24)\r
-                 | (((int) (b * 255) & 0xFF) << 16)\r
-                 | (((int) (g * 255) & 0xFF) << 8)\r
-                 | (((int) (r * 255) & 0xFF));\r
+                | (((int) (b * 255) & 0xFF) << 16)\r
+                | (((int) (g * 255) & 0xFF) << 8)\r
+                | (((int) (r * 255) & 0xFF));\r
         return abgr;\r
     }\r
 \r
     public void fromIntARGB(int color) {\r
         a = ((byte) (color >> 24) & 0xFF) / 255f;\r
         r = ((byte) (color >> 16) & 0xFF) / 255f;\r
-        g = ((byte) (color >> 8)  & 0xFF) / 255f;\r
-        b = ((byte) (color)       & 0xFF) / 255f;\r
+        g = ((byte) (color >> 8) & 0xFF) / 255f;\r
+        b = ((byte) (color) & 0xFF) / 255f;\r
     }\r
 \r
     public void fromIntRGBA(int color) {\r
         r = ((byte) (color >> 24) & 0xFF) / 255f;\r
         g = ((byte) (color >> 16) & 0xFF) / 255f;\r
-        b = ((byte) (color >> 8)  & 0xFF) / 255f;\r
-        a = ((byte) (color)       & 0xFF) / 255f;\r
+        b = ((byte) (color >> 8) & 0xFF) / 255f;\r
+        a = ((byte) (color) & 0xFF) / 255f;\r
+    }\r
+\r
+    /**\r
+     * Transform the current ColorRGBA to a Vector3f using\r
+     * x = r, y = g, z = b. The Alpha value is not used.\r
+     *\r
+     * This method is useful to use for shaders assignment.\r
+     * @return A Vector3f containing the RGB value of current color definition.\r
+     */\r
+    public Vector3f toVector3f() {\r
+        return new Vector3f(r, g, b);\r
+    }\r
+\r
+    /**\r
+     * Transform the current ColorRGBA to a Vector4f using\r
+     * x = r, y = g, z = b, w = a.\r
+     *\r
+     * This method is useful to use for shaders assignment.\r
+     * @return A Vector4f containing the RGBA value of current color definition.\r
+     */\r
+    public Vector4f toVector4f() {\r
+        return new Vector4f(r, g, b, a);\r
     }\r
 }\r