1 /* 2 * Copyright (C) 2022 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.settings.accessibility; 18 19 import android.graphics.Color; 20 import android.view.accessibility.CaptioningManager.CaptionStyle; 21 22 /** Provides utility methods related caption. */ 23 public final class CaptionUtils { 24 25 /** 26 * Unpacks the specified color value to get the color value. 27 * 28 * @param value the specified color value. 29 */ parseColor(int value)30 public static int parseColor(int value) { 31 final int colorValue; 32 if (!CaptionStyle.hasColor(value)) { 33 // "Default" color with variable alpha. 34 colorValue = CaptionStyle.COLOR_UNSPECIFIED; 35 } else if ((value >>> 24) == 0) { 36 // "None" color with variable alpha. 37 colorValue = Color.TRANSPARENT; 38 } else { 39 // Normal color. 40 colorValue = value | 0xFF000000; 41 } 42 return colorValue; 43 } 44 45 /** 46 * Unpacks the specified color value to get the opacity value. 47 * 48 * @param value the specified color value. 49 */ parseOpacity(int value)50 public static int parseOpacity(int value) { 51 final int opacityValue; 52 if (!CaptionStyle.hasColor(value)) { 53 // "Default" color with variable alpha. 54 opacityValue = (value & 0xFF) << 24; 55 } else if ((value >>> 24) == 0) { 56 // "None" color with variable alpha. 57 opacityValue = (value & 0xFF) << 24; 58 } else { 59 // Normal color. 60 opacityValue = value & 0xFF000000; 61 } 62 63 // Opacity value is always white. 64 return opacityValue | 0xFFFFFF; 65 } 66 67 /** 68 * Packs the specified color value and specified opacity value into merged color value. 69 * 70 * @param colorValue the color value. 71 * @param opacityValue the opacity value. 72 */ mergeColorOpacity(int colorValue, int opacityValue)73 public static int mergeColorOpacity(int colorValue, int opacityValue) { 74 final int value; 75 // "Default" is 0x00FFFFFF or, for legacy support, 0x00000100. 76 if (!CaptionStyle.hasColor(colorValue)) { 77 // Encode "default" as 0x00FFFFaa. 78 value = 0x00FFFF00 | Color.alpha(opacityValue); 79 } else if (colorValue == Color.TRANSPARENT) { 80 // Encode "none" as 0x000000aa. 81 value = Color.alpha(opacityValue); 82 } else { 83 // Encode custom color normally. 84 value = (colorValue & 0x00FFFFFF) | (opacityValue & 0xFF000000); 85 } 86 return value; 87 } 88 } 89