1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.graphics; 18 19/** 20 * A color filter that transforms colors through a 4x5 color matrix. This filter 21 * can be used to change the saturation of pixels, convert from YUV to RGB, etc. 22 * 23 * @see ColorMatrix 24 */ 25public class ColorMatrixColorFilter extends ColorFilter { 26 private final ColorMatrix mMatrix = new ColorMatrix(); 27 28 /** 29 * Create a color filter that transforms colors through a 4x5 color matrix. 30 * 31 * @param matrix 4x5 matrix used to transform colors. It is copied into 32 * the filter, so changes made to the matrix after the filter 33 * is constructed will not be reflected in the filter. 34 */ 35 public ColorMatrixColorFilter(ColorMatrix matrix) { 36 mMatrix.set(matrix); 37 update(); 38 } 39 40 /** 41 * Create a color filter that transforms colors through a 4x5 color matrix. 42 * 43 * @param array Array of floats used to transform colors, treated as a 4x5 44 * matrix. The first 20 entries of the array are copied into 45 * the filter. See ColorMatrix. 46 */ 47 public ColorMatrixColorFilter(float[] array) { 48 if (array.length < 20) { 49 throw new ArrayIndexOutOfBoundsException(); 50 } 51 mMatrix.set(array); 52 update(); 53 } 54 55 /** 56 * Returns the {@link ColorMatrix} used by this filter. The returned 57 * value is never null. Modifying the returned matrix does not have 58 * any effect until you call {@link #setColorMatrix(ColorMatrix)}. 59 * 60 * @see #setColorMatrix(ColorMatrix) 61 * 62 * @hide 63 */ 64 public ColorMatrix getColorMatrix() { 65 return mMatrix; 66 } 67 68 /** 69 * Specifies the color matrix used by this filter. If the specified 70 * color matrix is null, this filter's color matrix will be reset to 71 * the identity matrix. 72 * 73 * @param matrix A {@link ColorMatrix} or null 74 * 75 * @see #getColorMatrix() 76 * @see android.graphics.ColorMatrix#reset() 77 * @see #setColorMatrix(float[]) 78 * 79 * @hide 80 */ 81 public void setColorMatrix(ColorMatrix matrix) { 82 if (matrix == null) { 83 mMatrix.reset(); 84 } else if (matrix != mMatrix) { 85 mMatrix.set(matrix); 86 } 87 update(); 88 } 89 90 /** 91 * Specifies the color matrix used by this filter. If the specified 92 * color matrix is null, this filter's color matrix will be reset to 93 * the identity matrix. 94 * 95 * @param array Array of floats used to transform colors, treated as a 4x5 96 * matrix. The first 20 entries of the array are copied into 97 * the filter. See {@link ColorMatrix}. 98 * 99 * @see #getColorMatrix() 100 * @see android.graphics.ColorMatrix#reset() 101 * @see #setColorMatrix(ColorMatrix) 102 * 103 * @throws ArrayIndexOutOfBoundsException if the specified array's 104 * length is < 20 105 * 106 * @hide 107 */ 108 public void setColorMatrix(float[] array) { 109 if (array == null) { 110 mMatrix.reset(); 111 } else { 112 if (array.length < 20) { 113 throw new ArrayIndexOutOfBoundsException(); 114 } 115 116 mMatrix.set(array); 117 } 118 update(); 119 } 120 121 private void update() { 122 final float[] colorMatrix = mMatrix.getArray(); 123 destroyFilter(native_instance); 124 native_instance = nativeColorMatrixFilter(colorMatrix); 125 } 126 127 private static native long nativeColorMatrixFilter(float[] array); 128} 129