1b6086751979cb14740815502597e9fcfddb7054aztenghui/* 2b6086751979cb14740815502597e9fcfddb7054aztenghui * Copyright (C) 2017 The Android Open Source Project 3b6086751979cb14740815502597e9fcfddb7054aztenghui * 4b6086751979cb14740815502597e9fcfddb7054aztenghui * Licensed under the Apache License, Version 2.0 (the "License"); 5b6086751979cb14740815502597e9fcfddb7054aztenghui * you may not use this file except in compliance with the License. 6b6086751979cb14740815502597e9fcfddb7054aztenghui * You may obtain a copy of the License at 7b6086751979cb14740815502597e9fcfddb7054aztenghui * 8b6086751979cb14740815502597e9fcfddb7054aztenghui * http://www.apache.org/licenses/LICENSE-2.0 9b6086751979cb14740815502597e9fcfddb7054aztenghui * 10b6086751979cb14740815502597e9fcfddb7054aztenghui * Unless required by applicable law or agreed to in writing, software 11b6086751979cb14740815502597e9fcfddb7054aztenghui * distributed under the License is distributed on an "AS IS" BASIS, 12b6086751979cb14740815502597e9fcfddb7054aztenghui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b6086751979cb14740815502597e9fcfddb7054aztenghui * See the License for the specific language governing permissions and 14b6086751979cb14740815502597e9fcfddb7054aztenghui * limitations under the License. 15b6086751979cb14740815502597e9fcfddb7054aztenghui */ 16b6086751979cb14740815502597e9fcfddb7054aztenghui 17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.vectordrawable.graphics.drawable; 18b6086751979cb14740815502597e9fcfddb7054aztenghui 19ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20b6086751979cb14740815502597e9fcfddb7054aztenghui 21b6086751979cb14740815502597e9fcfddb7054aztenghuiimport android.animation.TypeEvaluator; 22b6086751979cb14740815502597e9fcfddb7054aztenghuiimport android.animation.ValueAnimator; 231f1b5540916924b36bc81cc3edb4f02245116ddeAurimas Liutikas 24ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.annotation.RestrictTo; 25b6086751979cb14740815502597e9fcfddb7054aztenghui 26b6086751979cb14740815502597e9fcfddb7054aztenghui/** 27b6086751979cb14740815502597e9fcfddb7054aztenghui * This evaluator can be used to perform type interpolation between integer 28b6086751979cb14740815502597e9fcfddb7054aztenghui * values that represent ARGB colors. 29b6086751979cb14740815502597e9fcfddb7054aztenghui * @hide 30b6086751979cb14740815502597e9fcfddb7054aztenghui */ 31b6086751979cb14740815502597e9fcfddb7054aztenghui@RestrictTo(LIBRARY_GROUP) 32b6086751979cb14740815502597e9fcfddb7054aztenghuipublic class ArgbEvaluator implements TypeEvaluator { 33b6086751979cb14740815502597e9fcfddb7054aztenghui private static final ArgbEvaluator sInstance = new ArgbEvaluator(); 34b6086751979cb14740815502597e9fcfddb7054aztenghui 35b6086751979cb14740815502597e9fcfddb7054aztenghui /** 36b6086751979cb14740815502597e9fcfddb7054aztenghui * Returns an instance of <code>ArgbEvaluator</code> that may be used in 37b6086751979cb14740815502597e9fcfddb7054aztenghui * {@link ValueAnimator#setEvaluator(TypeEvaluator)}. The same instance may 38b6086751979cb14740815502597e9fcfddb7054aztenghui * be used in multiple <code>Animator</code>s because it holds no state. 39b6086751979cb14740815502597e9fcfddb7054aztenghui * 40b6086751979cb14740815502597e9fcfddb7054aztenghui * @return An instance of <code>ArgbEvalutor</code>. 41b6086751979cb14740815502597e9fcfddb7054aztenghui */ 42b6086751979cb14740815502597e9fcfddb7054aztenghui public static ArgbEvaluator getInstance() { 43b6086751979cb14740815502597e9fcfddb7054aztenghui return sInstance; 44b6086751979cb14740815502597e9fcfddb7054aztenghui } 45b6086751979cb14740815502597e9fcfddb7054aztenghui 46b6086751979cb14740815502597e9fcfddb7054aztenghui /** 47b6086751979cb14740815502597e9fcfddb7054aztenghui * This function returns the calculated in-between value for a color 48b6086751979cb14740815502597e9fcfddb7054aztenghui * given integers that represent the start and end values in the four 49b6086751979cb14740815502597e9fcfddb7054aztenghui * bytes of the 32-bit int. Each channel is separately linearly interpolated 50b6086751979cb14740815502597e9fcfddb7054aztenghui * and the resulting calculated values are recombined into the return value. 51b6086751979cb14740815502597e9fcfddb7054aztenghui * 52b6086751979cb14740815502597e9fcfddb7054aztenghui * @param fraction The fraction from the starting to the ending values 53b6086751979cb14740815502597e9fcfddb7054aztenghui * @param startValue A 32-bit int value representing colors in the 54b6086751979cb14740815502597e9fcfddb7054aztenghui * separate bytes of the parameter 55b6086751979cb14740815502597e9fcfddb7054aztenghui * @param endValue A 32-bit int value representing colors in the 56b6086751979cb14740815502597e9fcfddb7054aztenghui * separate bytes of the parameter 57b6086751979cb14740815502597e9fcfddb7054aztenghui * @return A value that is calculated to be the linearly interpolated 58b6086751979cb14740815502597e9fcfddb7054aztenghui * result, derived by separating the start and end values into separate 59b6086751979cb14740815502597e9fcfddb7054aztenghui * color channels and interpolating each one separately, recombining the 60b6086751979cb14740815502597e9fcfddb7054aztenghui * resulting values in the same way. 61b6086751979cb14740815502597e9fcfddb7054aztenghui */ 62b6086751979cb14740815502597e9fcfddb7054aztenghui @Override 63b6086751979cb14740815502597e9fcfddb7054aztenghui public Object evaluate(float fraction, Object startValue, Object endValue) { 64b6086751979cb14740815502597e9fcfddb7054aztenghui int startInt = (Integer) startValue; 65b6086751979cb14740815502597e9fcfddb7054aztenghui float startA = ((startInt >> 24) & 0xff) / 255.0f; 66b6086751979cb14740815502597e9fcfddb7054aztenghui float startR = ((startInt >> 16) & 0xff) / 255.0f; 67b6086751979cb14740815502597e9fcfddb7054aztenghui float startG = ((startInt >> 8) & 0xff) / 255.0f; 68b6086751979cb14740815502597e9fcfddb7054aztenghui float startB = ((startInt) & 0xff) / 255.0f; 69b6086751979cb14740815502597e9fcfddb7054aztenghui 70b6086751979cb14740815502597e9fcfddb7054aztenghui int endInt = (Integer) endValue; 71b6086751979cb14740815502597e9fcfddb7054aztenghui float endA = ((endInt >> 24) & 0xff) / 255.0f; 72b6086751979cb14740815502597e9fcfddb7054aztenghui float endR = ((endInt >> 16) & 0xff) / 255.0f; 73b6086751979cb14740815502597e9fcfddb7054aztenghui float endG = ((endInt >> 8) & 0xff) / 255.0f; 74b6086751979cb14740815502597e9fcfddb7054aztenghui float endB = ((endInt) & 0xff) / 255.0f; 75b6086751979cb14740815502597e9fcfddb7054aztenghui 76b6086751979cb14740815502597e9fcfddb7054aztenghui // convert from sRGB to linear 77b6086751979cb14740815502597e9fcfddb7054aztenghui startR = (float) Math.pow(startR, 2.2); 78b6086751979cb14740815502597e9fcfddb7054aztenghui startG = (float) Math.pow(startG, 2.2); 79b6086751979cb14740815502597e9fcfddb7054aztenghui startB = (float) Math.pow(startB, 2.2); 80b6086751979cb14740815502597e9fcfddb7054aztenghui 81b6086751979cb14740815502597e9fcfddb7054aztenghui endR = (float) Math.pow(endR, 2.2); 82b6086751979cb14740815502597e9fcfddb7054aztenghui endG = (float) Math.pow(endG, 2.2); 83b6086751979cb14740815502597e9fcfddb7054aztenghui endB = (float) Math.pow(endB, 2.2); 84b6086751979cb14740815502597e9fcfddb7054aztenghui 85b6086751979cb14740815502597e9fcfddb7054aztenghui // compute the interpolated color in linear space 86b6086751979cb14740815502597e9fcfddb7054aztenghui float a = startA + fraction * (endA - startA); 87b6086751979cb14740815502597e9fcfddb7054aztenghui float r = startR + fraction * (endR - startR); 88b6086751979cb14740815502597e9fcfddb7054aztenghui float g = startG + fraction * (endG - startG); 89b6086751979cb14740815502597e9fcfddb7054aztenghui float b = startB + fraction * (endB - startB); 90b6086751979cb14740815502597e9fcfddb7054aztenghui 91b6086751979cb14740815502597e9fcfddb7054aztenghui // convert back to sRGB in the [0..255] range 92b6086751979cb14740815502597e9fcfddb7054aztenghui a = a * 255.0f; 93b6086751979cb14740815502597e9fcfddb7054aztenghui r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f; 94b6086751979cb14740815502597e9fcfddb7054aztenghui g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f; 95b6086751979cb14740815502597e9fcfddb7054aztenghui b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f; 96b6086751979cb14740815502597e9fcfddb7054aztenghui 97b6086751979cb14740815502597e9fcfddb7054aztenghui return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b); 98b6086751979cb14740815502597e9fcfddb7054aztenghui } 99b6086751979cb14740815502597e9fcfddb7054aztenghui} 100