summaryrefslogtreecommitdiffstats
path: root/graphics/java
diff options
context:
space:
mode:
authorChris Craik <ccraik@google.com>2014-11-19 13:17:34 -0800
committerChris Craik <ccraik@google.com>2014-11-20 13:28:01 -0800
commitdb7cc60eb29c215e65081211dfe65ba4d017691c (patch)
tree57373a0ea004db5de31c086cb7c6400468070441 /graphics/java
parent6c912b7d056c67b41fd46f31de168795e97c2336 (diff)
downloadframeworks_base-db7cc60eb29c215e65081211dfe65ba4d017691c.zip
frameworks_base-db7cc60eb29c215e65081211dfe65ba4d017691c.tar.gz
frameworks_base-db7cc60eb29c215e65081211dfe65ba4d017691c.tar.bz2
Improve ColorMatrix docs
bug:18415632 Clean up some text, and add a sample. Change-Id: I39fdda2004e1932831058701f95f67355786ad9d
Diffstat (limited to 'graphics/java')
-rw-r--r--graphics/java/android/graphics/ColorMatrix.java94
1 files changed, 61 insertions, 33 deletions
diff --git a/graphics/java/android/graphics/ColorMatrix.java b/graphics/java/android/graphics/ColorMatrix.java
index 1242eb5..64f0c05 100644
--- a/graphics/java/android/graphics/ColorMatrix.java
+++ b/graphics/java/android/graphics/ColorMatrix.java
@@ -21,23 +21,43 @@ import android.util.FloatMath;
import java.util.Arrays;
/**
- * 4x5 matrix for transforming the color+alpha components of a Bitmap.
- * The matrix is stored in a single array, and its treated as follows:
+ * 4x5 matrix for transforming the color and alpha components of a Bitmap.
+ * The matrix can be passed as single array, and is treated as follows:
+ *
* <pre>
* [ a, b, c, d, e,
* f, g, h, i, j,
* k, l, m, n, o,
- * p, q, r, s, t ]
- * </pre>
+ * p, q, r, s, t ]</pre>
+ *
+ * <p>
+ * When applied to a color <code>[R, G, B, A]</code>, the resulting color
+ * is computed as:
+ * </p>
+ *
+ * <pre>
+ * R&rsquo; = a*R + b*G + c*B + d*A + e;
+ * G&rsquo; = f*R + g*G + h*B + i*A + j;
+ * B&rsquo; = k*R + l*G + m*B + n*A + o;
+ * A&rsquo; = p*R + q*G + r*B + s*A + t;</pre>
+ *
+ * <p>
+ * That resulting color <code>[R&rsquo;, G&rsquo;, B&rsquo;, A&rsquo;]</code>
+ * then has each channel clamped to the <code>0</code> to <code>255</code>
+ * range.
+ * </p>
+ *
+ * <p>
+ * The sample ColorMatrix below inverts incoming colors by scaling each
+ * channel by <code>-1</code>, and then shifting the result up by
+ * <code>255</code> to remain in the standard color space.
+ * </p>
*
- * When applied to a color <code>[r, g, b, a]</code>, the resulting color
- * is computed as (after clamping):
* <pre>
- * R' = a*R + b*G + c*B + d*A + e;
- * G' = f*R + g*G + h*B + i*A + j;
- * B' = k*R + l*G + m*B + n*A + o;
- * A' = p*R + q*G + r*B + s*A + t;
- * </pre>
+ * [ -1, 0, 0, 0, 255,
+ * 0, -1, 0, 0, 255,
+ * 0, 0, -1, 0, 255,
+ * 0, 0, 0, 1, 0 ]</pre>
*/
@SuppressWarnings({ "MismatchedReadAndWriteOfArray", "PointlessArithmeticExpression" })
public class ColorMatrix {
@@ -52,24 +72,24 @@ public class ColorMatrix {
}
/**
- * Create a new colormatrix initialized with the specified array of values.
+ * Create a new colormatrix initialized with the specified array of values.
*/
public ColorMatrix(float[] src) {
System.arraycopy(src, 0, mArray, 0, 20);
}
-
+
/**
* Create a new colormatrix initialized with the specified colormatrix.
*/
public ColorMatrix(ColorMatrix src) {
System.arraycopy(src.mArray, 0, mArray, 0, 20);
}
-
+
/**
* Return the array of floats representing this colormatrix.
*/
public final float[] getArray() { return mArray; }
-
+
/**
* Set this colormatrix to identity:
* <pre>
@@ -84,7 +104,7 @@ public class ColorMatrix {
Arrays.fill(a, 0);
a[0] = a[6] = a[12] = a[18] = 1;
}
-
+
/**
* Assign the src colormatrix into this matrix, copying all of its values.
*/
@@ -98,7 +118,7 @@ public class ColorMatrix {
public void set(float[] src) {
System.arraycopy(src, 0, mArray, 0, 20);
}
-
+
/**
* Set this colormatrix to scale by the specified values.
*/
@@ -114,12 +134,14 @@ public class ColorMatrix {
a[12] = bScale;
a[18] = aScale;
}
-
+
/**
* Set the rotation on a color axis by the specified values.
+ * <p>
* <code>axis=0</code> correspond to a rotation around the RED color
* <code>axis=1</code> correspond to a rotation around the GREEN color
* <code>axis=2</code> correspond to a rotation around the BLUE color
+ * </p>
*/
public void setRotate(int axis, float degrees) {
reset();
@@ -153,8 +175,10 @@ public class ColorMatrix {
/**
* Set this colormatrix to the concatenation of the two specified
* colormatrices, such that the resulting colormatrix has the same effect
- * as applying matB and then applying matA. It is legal for either matA or
- * matB to be the same colormatrix as this.
+ * as applying matB and then applying matA.
+ * <p>
+ * It is legal for either matA or matB to be the same colormatrix as this.
+ * </p>
*/
public void setConcat(ColorMatrix matA, ColorMatrix matB) {
float[] tmp;
@@ -163,7 +187,7 @@ public class ColorMatrix {
} else {
tmp = mArray;
}
-
+
final float[] a = matA.mArray;
final float[] b = matB.mArray;
int index = 0;
@@ -176,38 +200,43 @@ public class ColorMatrix {
a[j + 2] * b[14] + a[j + 3] * b[19] +
a[j + 4];
}
-
+
if (tmp != mArray) {
System.arraycopy(tmp, 0, mArray, 0, 20);
}
}
/**
- * Concat this colormatrix with the specified prematrix. This is logically
- * the same as calling setConcat(this, prematrix);
+ * Concat this colormatrix with the specified prematrix.
+ * <p>
+ * This is logically the same as calling setConcat(this, prematrix);
+ * </p>
*/
public void preConcat(ColorMatrix prematrix) {
setConcat(this, prematrix);
}
/**
- * Concat this colormatrix with the specified postmatrix. This is logically
- * the same as calling setConcat(postmatrix, this);
+ * Concat this colormatrix with the specified postmatrix.
+ * <p>
+ * This is logically the same as calling setConcat(postmatrix, this);
+ * </p>
*/
public void postConcat(ColorMatrix postmatrix) {
setConcat(postmatrix, this);
}
///////////////////////////////////////////////////////////////////////////
-
+
/**
- * Set the matrix to affect the saturation of colors. A value of 0 maps the
- * color to gray-scale. 1 is identity.
+ * Set the matrix to affect the saturation of colors.
+ *
+ * @param sat A value of 0 maps the color to gray-scale. 1 is identity.
*/
public void setSaturation(float sat) {
reset();
float[] m = mArray;
-
+
final float invSat = 1 - sat;
final float R = 0.213f * invSat;
final float G = 0.715f * invSat;
@@ -217,7 +246,7 @@ public class ColorMatrix {
m[5] = R; m[6] = G + sat; m[7] = B;
m[10] = R; m[11] = G; m[12] = B + sat;
}
-
+
/**
* Set the matrix to convert RGB to YUV
*/
@@ -229,7 +258,7 @@ public class ColorMatrix {
m[5] = -0.16874f; m[6] = -0.33126f; m[7] = 0.5f;
m[10] = 0.5f; m[11] = -0.41869f; m[12] = -0.08131f;
}
-
+
/**
* Set the matrix to convert from YUV to RGB
*/
@@ -242,4 +271,3 @@ public class ColorMatrix {
m[10] = 1; m[11] = 1.772f; m[12] = 0;
}
}
-