1 /*
2  * Copyright 2018 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 androidx.core.animation;
18 
19 import android.annotation.SuppressLint;
20 
21 import org.jspecify.annotations.NonNull;
22 
23 /**
24  * This evaluator can be used to perform type interpolation between integer
25  * values that represent ARGB colors.
26  */
27 public final class ArgbEvaluator implements TypeEvaluator<Integer> {
28     private static final ArgbEvaluator sInstance = new ArgbEvaluator();
29 
30     /**
31      * Returns an instance of <code>ArgbEvaluator</code> that may be used in
32      * {@link ValueAnimator#setEvaluator(TypeEvaluator)}. The same instance may
33      * be used in multiple <code>Animator</code>s because it holds no state.
34      * @return An instance of <code>ArgbEvaluator</code>.
35      */
getInstance()36     public static @NonNull ArgbEvaluator getInstance() {
37         return sInstance;
38     }
39 
ArgbEvaluator()40     private ArgbEvaluator() {
41     }
42 
43     /**
44      * This function returns the calculated in-between value for a color
45      * given integers that represent the start and end values in the four
46      * bytes of the 32-bit int. Each channel is separately linearly interpolated
47      * and the resulting calculated values are recombined into the return value.
48      *
49      * @param fraction The fraction from the starting to the ending values
50      * @param startValue A 32-bit int value representing colors in the
51      * separate bytes of the parameter
52      * @param endValue A 32-bit int value representing colors in the
53      * separate bytes of the parameter
54      * @return A value that is calculated to be the linearly interpolated
55      * result, derived by separating the start and end values into separate
56      * color channels and interpolating each one separately, recombining the
57      * resulting values in the same way.
58      */
59     @SuppressLint("AutoBoxing") /* Generics */
60     @Override
evaluate( float fraction, @SuppressLint("AutoBoxing") @NonNull Integer startValue, @SuppressLint("AutoBoxing") @NonNull Integer endValue )61     public @NonNull Integer evaluate(
62             float fraction,
63             @SuppressLint("AutoBoxing") @NonNull Integer startValue,
64             @SuppressLint("AutoBoxing") @NonNull Integer endValue
65     ) {
66         int startInt = startValue;
67         float startA = ((startInt >> 24) & 0xff) / 255.0f;
68         float startR = ((startInt >> 16) & 0xff) / 255.0f;
69         float startG = ((startInt >>  8) & 0xff) / 255.0f;
70         float startB =  (startInt        & 0xff) / 255.0f;
71 
72         int endInt = endValue;
73         float endA = ((endInt >> 24) & 0xff) / 255.0f;
74         float endR = ((endInt >> 16) & 0xff) / 255.0f;
75         float endG = ((endInt >>  8) & 0xff) / 255.0f;
76         float endB =  (endInt        & 0xff) / 255.0f;
77 
78         // convert from sRGB to linear
79         startR = (float) Math.pow(startR, 2.2);
80         startG = (float) Math.pow(startG, 2.2);
81         startB = (float) Math.pow(startB, 2.2);
82 
83         endR = (float) Math.pow(endR, 2.2);
84         endG = (float) Math.pow(endG, 2.2);
85         endB = (float) Math.pow(endB, 2.2);
86 
87         // compute the interpolated color in linear space
88         float a = startA + fraction * (endA - startA);
89         float r = startR + fraction * (endR - startR);
90         float g = startG + fraction * (endG - startG);
91         float b = startB + fraction * (endB - startB);
92 
93         // convert back to sRGB in the [0..255] range
94         a = a * 255.0f;
95         r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f;
96         g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f;
97         b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f;
98 
99         return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b);
100     }
101 }
102