diff --git a/graphics/java/android/graphics/ColorMatrix.java b/graphics/java/android/graphics/ColorMatrix.java index f73abcb5cd6cf..1b1849ebb835d 100644 --- a/graphics/java/android/graphics/ColorMatrix.java +++ b/graphics/java/android/graphics/ColorMatrix.java @@ -19,23 +19,43 @@ package android.graphics; 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: + * *
* [ a, b, c, d, e, * f, g, h, i, j, * k, l, m, n, o, - * p, q, r, s, t ] - *+ * p, q, r, s, t ] + * + *
+ * When applied to a color [R, G, B, A], the resulting color
+ * is computed as:
+ *
[r, g, b, a], the resulting color
- * is computed as (after clamping):
* - * 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; - *+ * 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; + * + *
+ * That resulting color [R’, G’, B’, A’]
+ * then has each channel clamped to the 0 to 255
+ * range.
+ *
+ * The sample ColorMatrix below inverts incoming colors by scaling each
+ * channel by -1, and then shifting the result up by
+ * 255 to remain in the standard color space.
+ *
+ * [ -1, 0, 0, 0, 255, + * 0, -1, 0, 0, 255, + * 0, 0, -1, 0, 255, + * 0, 0, 0, 1, 0 ]*/ @SuppressWarnings({ "MismatchedReadAndWriteOfArray", "PointlessArithmeticExpression" }) public class ColorMatrix { @@ -50,24 +70,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: *
@@ -82,7 +102,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.
*/
@@ -96,7 +116,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.
*/
@@ -112,12 +132,14 @@ public class ColorMatrix {
a[12] = bScale;
a[18] = aScale;
}
-
+
/**
* Set the rotation on a color axis by the specified values.
+ *
* axis=0 correspond to a rotation around the RED color
* axis=1 correspond to a rotation around the GREEN color
* axis=2 correspond to a rotation around the BLUE color
+ *
*/
public void setRotate(int axis, float degrees) {
reset();
@@ -151,8 +173,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.
+ *
+ * It is legal for either matA or matB to be the same colormatrix as this.
+ *
*/
public void setConcat(ColorMatrix matA, ColorMatrix matB) {
float[] tmp;
@@ -161,7 +185,7 @@ public class ColorMatrix {
} else {
tmp = mArray;
}
-
+
final float[] a = matA.mArray;
final float[] b = matB.mArray;
int index = 0;
@@ -174,38 +198,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.
+ *
+ * This is logically the same as calling setConcat(this, prematrix);
+ *
*/
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.
+ *
+ * This is logically the same as calling setConcat(postmatrix, this);
+ *
*/
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;
@@ -215,7 +244,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
*/
@@ -227,7 +256,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
*/
@@ -240,4 +269,3 @@ public class ColorMatrix {
m[10] = 1; m[11] = 1.772f; m[12] = 0;
}
}
-