11128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta/* 21128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * Copyright (C) 2014 The Android Open Source Project 31128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * 41128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License"); 51128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * you may not use this file except in compliance with the License. 61128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * You may obtain a copy of the License at 71128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * 81128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * http://www.apache.org/licenses/LICENSE-2.0 91128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * 101128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * Unless required by applicable law or agreed to in writing, software 111128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS, 121128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * See the License for the specific language governing permissions and 141128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * limitations under the License. 151128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta */ 161128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 171128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptapackage com.android.layoutlib.bridge.impl; 181128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 191128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptaimport com.android.ide.common.rendering.api.LayoutLog; 201128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptaimport com.android.layoutlib.bridge.Bridge; 211128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 22faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Guptaimport android.graphics.BlendComposite; 23faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Guptaimport android.graphics.BlendComposite.BlendingMode; 24501f66388bcfdde0d657faa113835dcdfa9c86e0Deepanshu Guptaimport android.graphics.PorterDuff; 251128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptaimport android.graphics.PorterDuff.Mode; 261128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptaimport android.graphics.PorterDuffColorFilter_Delegate; 271128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 281128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptaimport java.awt.AlphaComposite; 29faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Guptaimport java.awt.Composite; 301128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 311128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta/** 32989f4cb6da0c1d20f74a278878bc173c690d0848Jerome Gaillard * Provides various utility methods for {@link PorterDuffColorFilter_Delegate}. 331128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta */ 341128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Guptapublic final class PorterDuffUtility { 351128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 36501f66388bcfdde0d657faa113835dcdfa9c86e0Deepanshu Gupta private static final int MODES_COUNT = Mode.values().length; 37501f66388bcfdde0d657faa113835dcdfa9c86e0Deepanshu Gupta 381128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta // Make the class non-instantiable. 391128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta private PorterDuffUtility() { 401128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta } 411128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 421128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta /** 431128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * Convert the porterDuffMode from the framework to its corresponding enum. This defaults to 441128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta * {@link Mode#SRC_OVER} for invalid modes. 451128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta */ 461128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta public static Mode getPorterDuffMode(int porterDuffMode) { 47501f66388bcfdde0d657faa113835dcdfa9c86e0Deepanshu Gupta if (porterDuffMode >= 0 && porterDuffMode < MODES_COUNT) { 48501f66388bcfdde0d657faa113835dcdfa9c86e0Deepanshu Gupta return PorterDuff.intToMode(porterDuffMode); 491128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta } 501128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta Bridge.getLog().error(LayoutLog.TAG_BROKEN, 51501f66388bcfdde0d657faa113835dcdfa9c86e0Deepanshu Gupta String.format("Unknown PorterDuff.Mode: %1$d", porterDuffMode), null); 521128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta assert false; 531128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta return Mode.SRC_OVER; 541128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta } 551128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 561128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta /** 57faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta * A utility method to get the {@link Composite} that represents the filter for the given 58faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta * PorterDuff mode and the alpha. Defaults to {@link Mode#SRC_OVER} for invalid modes. 591128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta */ 60faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta public static Composite getComposite(Mode mode, int alpha255) { 61faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta float alpha1 = alpha255 != 0xFF ? alpha255 / 255.f : 1.f; 62faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta switch (mode) { 631128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case CLEAR: 64faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.CLEAR, alpha1); 65faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case SRC: 66faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.SRC, alpha1); 671128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case DST: 68faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.DST, alpha1); 69faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case SRC_OVER: 70faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha1); 71faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case DST_OVER: 72faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.DST_OVER, alpha1); 73faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case SRC_IN: 74faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha1); 751128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case DST_IN: 76faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.DST_IN, alpha1); 77faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case SRC_OUT: 78faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.SRC_OUT, alpha1); 791128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case DST_OUT: 80faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.DST_OUT, alpha1); 81faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case SRC_ATOP: 82faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha1); 83faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case DST_ATOP: 84faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.DST_ATOP, alpha1); 85faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case XOR: 86faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.XOR, alpha1); 87faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case DARKEN: 88faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return BlendComposite.getInstance(BlendingMode.DARKEN, alpha1); 891128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case LIGHTEN: 90faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return BlendComposite.getInstance(BlendingMode.LIGHTEN, alpha1); 911128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case MULTIPLY: 92faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return BlendComposite.getInstance(BlendingMode.MULTIPLY, alpha1); 931128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta case SCREEN: 94faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return BlendComposite.getInstance(BlendingMode.SCREEN, alpha1); 95faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case ADD: 96faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return BlendComposite.getInstance(BlendingMode.ADD, alpha1); 97faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta case OVERLAY: 98faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return BlendComposite.getInstance(BlendingMode.OVERLAY, alpha1); 99faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta default: 100faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, 101faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta String.format("Unsupported PorterDuff Mode: %1$s", mode.name()), 102faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta null, null /*data*/); 1031128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta 104faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha1); 105faf06eb066e8ba5f3a58fde94bff1bb8b0a2ba02Deepanshu Gupta } 1061128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta } 1071128e789ab65efbc69405b2fd3455f4cd2fc45ecDeepanshu Gupta} 108