1 /*
2  * Copyright (C) 2017 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 com.android.systemui.statusbar.notification;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Color;
22 import android.graphics.ColorMatrix;
23 import android.graphics.ColorMatrixColorFilter;
24 import android.graphics.LinearGradient;
25 import android.graphics.Paint;
26 import android.graphics.PorterDuff;
27 import android.graphics.PorterDuffXfermode;
28 import android.graphics.Shader;
29 import android.graphics.drawable.Drawable;
30 
31 /**
32  * A utility class to colorize bitmaps with a color gradient and a special blending mode
33  */
34 public class ImageGradientColorizer {
colorize(Drawable drawable, int backgroundColor, boolean isRtl)35     public Bitmap colorize(Drawable drawable, int backgroundColor, boolean isRtl) {
36         int width = drawable.getIntrinsicWidth();
37         int height = drawable.getIntrinsicHeight();
38         int size = Math.min(width, height);
39         int widthInset = (width - size) / 2;
40         int heightInset = (height - size) / 2;
41         drawable = drawable.mutate();
42         drawable.setBounds(- widthInset, - heightInset, width - widthInset, height - heightInset);
43         Bitmap newBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
44         Canvas canvas = new Canvas(newBitmap);
45 
46         // Values to calculate the luminance of a color
47         float lr = 0.2126f;
48         float lg = 0.7152f;
49         float lb = 0.0722f;
50 
51         // Extract the red, green, blue components of the color extraction color in
52         // float and int form
53         int tri = Color.red(backgroundColor);
54         int tgi = Color.green(backgroundColor);
55         int tbi = Color.blue(backgroundColor);
56 
57         float tr = tri / 255f;
58         float tg = tgi / 255f;
59         float tb = tbi / 255f;
60 
61         // Calculate the luminance of the color extraction color
62         float cLum = (tr * lr + tg * lg + tb * lb) * 255;
63 
64         ColorMatrix m = new ColorMatrix(new float[] {
65                 lr, lg, lb, 0, tri - cLum,
66                 lr, lg, lb, 0, tgi - cLum,
67                 lr, lg, lb, 0, tbi - cLum,
68                 0, 0, 0, 1, 0,
69         });
70 
71         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
72         LinearGradient linearGradient =  new LinearGradient(0, 0, size, 0,
73                 new int[] {0, Color.argb(0.5f, 1, 1, 1), Color.BLACK},
74                 new float[] {0.0f, 0.4f, 1.0f}, Shader.TileMode.CLAMP);
75         paint.setShader(linearGradient);
76         Bitmap fadeIn = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
77         Canvas fadeInCanvas = new Canvas(fadeIn);
78         drawable.clearColorFilter();
79         drawable.draw(fadeInCanvas);
80 
81         if (isRtl) {
82             // Let's flip the gradient
83             fadeInCanvas.translate(size, 0);
84             fadeInCanvas.scale(-1, 1);
85         }
86         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
87         fadeInCanvas.drawPaint(paint);
88 
89         Paint coloredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
90         coloredPaint.setColorFilter(new ColorMatrixColorFilter(m));
91         coloredPaint.setAlpha((int) (0.5f * 255));
92         canvas.drawBitmap(fadeIn, 0, 0, coloredPaint);
93 
94         linearGradient =  new LinearGradient(0, 0, size, 0,
95                 new int[] {0, Color.argb(0.5f, 1, 1, 1), Color.BLACK},
96                 new float[] {0.0f, 0.6f, 1.0f}, Shader.TileMode.CLAMP);
97         paint.setShader(linearGradient);
98         fadeInCanvas.drawPaint(paint);
99         canvas.drawBitmap(fadeIn, 0, 0, null);
100 
101         return newBitmap;
102     }
103 }
104