• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.graphics;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.os.Build;
23 
24 /**
25  * A color filter that transforms colors through a 4x5 color matrix. This filter
26  * can be used to change the saturation of pixels, convert from YUV to RGB, etc.
27  *
28  * @see ColorMatrix
29  */
30 @android.ravenwood.annotation.RavenwoodKeepWholeClass
31 public class ColorMatrixColorFilter extends ColorFilter {
32     @UnsupportedAppUsage
33     private final ColorMatrix mMatrix = new ColorMatrix();
34 
35     /**
36      * Create a color filter that transforms colors through a 4x5 color matrix.
37      *
38      * @param matrix 4x5 matrix used to transform colors. It is copied into
39      *               the filter, so changes made to the matrix after the filter
40      *               is constructed will not be reflected in the filter.
41      */
ColorMatrixColorFilter(@onNull ColorMatrix matrix)42     public ColorMatrixColorFilter(@NonNull ColorMatrix matrix) {
43         mMatrix.set(matrix);
44     }
45 
46     /**
47      * Create a color filter that transforms colors through a 4x5 color matrix.
48      *
49      * @param array Array of floats used to transform colors, treated as a 4x5
50      *              matrix. The first 20 entries of the array are copied into
51      *              the filter. See ColorMatrix.
52      */
ColorMatrixColorFilter(@onNull float[] array)53     public ColorMatrixColorFilter(@NonNull float[] array) {
54         if (array.length < 20) {
55             throw new ArrayIndexOutOfBoundsException();
56         }
57         mMatrix.set(array);
58     }
59 
60     /**
61      * Copies the ColorMatrix from the filter into the passed ColorMatrix.
62      *
63      * @param colorMatrix Set to the current value of the filter's ColorMatrix.
64      */
getColorMatrix(ColorMatrix colorMatrix)65     public void getColorMatrix(ColorMatrix colorMatrix) {
66         colorMatrix.set(mMatrix);
67     }
68 
69     /**
70      * Copies the provided color matrix to be used by this filter.
71      *
72      * If the specified color matrix is null, this filter's color matrix will be reset to the
73      * identity matrix.
74      *
75      * @param matrix A {@link ColorMatrix} or null
76      *
77      * @see #getColorMatrix(ColorMatrix)
78      * @see #setColorMatrixArray(float[])
79      * @see ColorMatrix#reset()
80      *
81      * @hide
82      */
83     @UnsupportedAppUsage
setColorMatrix(@ullable ColorMatrix matrix)84     public void setColorMatrix(@Nullable ColorMatrix matrix) {
85         if (matrix == null) {
86             mMatrix.reset();
87         } else {
88             mMatrix.set(matrix);
89         }
90         nativeSetColorMatrix(getNativeInstance(), mMatrix.getArray());
91     }
92 
93     /**
94      * Copies the provided color matrix to be used by this filter.
95      *
96      * If the specified color matrix is null, this filter's color matrix will be reset to the
97      * identity matrix.
98      *
99      * @param array Array of floats used to transform colors, treated as a 4x5
100      *              matrix. The first 20 entries of the array are copied into
101      *              the filter. See {@link ColorMatrix}.
102      *
103      * @see #getColorMatrix(ColorMatrix)
104      * @see #setColorMatrix(ColorMatrix)
105      * @see ColorMatrix#reset()
106      *
107      * @throws ArrayIndexOutOfBoundsException if the specified array's
108      *         length is < 20
109      *
110      * @hide
111      */
112     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
setColorMatrixArray(@ullable float[] array)113     public void setColorMatrixArray(@Nullable float[] array) {
114         // called '...Array' so that passing null isn't ambiguous
115         if (array == null) {
116             mMatrix.reset();
117         } else {
118             if (array.length < 20) {
119                 throw new ArrayIndexOutOfBoundsException();
120             }
121             mMatrix.set(array);
122         }
123         nativeSetColorMatrix(getNativeInstance(), mMatrix.getArray());
124     }
125 
126     @Override
createNativeInstance()127     long createNativeInstance() {
128         return nativeColorMatrixFilter(mMatrix.getArray());
129     }
130 
nativeColorMatrixFilter(float[] array)131     private static native long nativeColorMatrixFilter(float[] array);
132 
nativeSetColorMatrix(long colorMatrixColorFilter, float[] array)133     private static native void nativeSetColorMatrix(long colorMatrixColorFilter, float[] array);
134 }
135