1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 package org.tensorflow.lite.task.vision.segmenter; 17 18 import android.graphics.Color; 19 import android.os.Build; 20 import androidx.annotation.RequiresApi; 21 import com.google.auto.value.AutoValue; 22 import org.tensorflow.lite.annotations.UsedByReflection; 23 24 /** Represents a label associated with a color for display purposes. */ 25 @AutoValue 26 @UsedByReflection("image_segmentation_jni.cc") 27 public abstract class ColoredLabel { 28 29 /** 30 * Creates a {@link ColoredLabel} object with an ARGB color int. 31 * 32 * @param label the label string, as provided in the label map packed in the TFLite Model 33 * Metadata. 34 * @param displayName the display name of label, as configured through {@link 35 * ImageSegmenter#ImageSegmenterOptions#Builder#setDisplayNamesLocale} 36 * @param argb the color components for the label in ARGB. See <a 37 * href="https://developer.android.com/reference/android/graphics/Color#color-ints">Android 38 * Color ints.</a> for more details. 39 */ 40 @UsedByReflection("image_segmentation_jni.cc") create(String label, String displayName, int argb)41 public static ColoredLabel create(String label, String displayName, int argb) { 42 return new AutoValue_ColoredLabel(label, displayName, argb); 43 } 44 45 /** 46 * Creates a {@link ColoredLabel} object with a {@link Color} instance. 47 * 48 * @param label the label string, as provided in the label map packed in the TFLite Model 49 * Metadata. 50 * @param displayName the display name of label, as configured through {@link 51 * ImageSegmenter#ImageSegmenterOptions#Builder#setDisplayNamesLocale} 52 * @param color the color components for the label. The Color instatnce is supported on Android 53 * API level 26 and above. For API level lower than 26, use {@link #create(String, String, 54 * int)}. See <a 55 * href="https://developer.android.com/reference/android/graphics/Color#color-instances">Android 56 * Color instances.</a> for more details. 57 */ 58 @RequiresApi(Build.VERSION_CODES.O) create(String label, String displayName, Color color)59 public static ColoredLabel create(String label, String displayName, Color color) { 60 return new AutoValue_ColoredLabel(label, displayName, color.toArgb()); 61 } 62 getlabel()63 public abstract String getlabel(); 64 getDisplayName()65 public abstract String getDisplayName(); 66 67 /** 68 * Gets the ARGB int that represents the color. 69 * 70 * <p>See <a 71 * href="https://developer.android.com/reference/android/graphics/Color#color-ints">Android Color 72 * ints.</a> for more details. 73 */ getArgb()74 public abstract int getArgb(); 75 76 /** 77 * Gets the {@link Color} instance of the underlying color. 78 * 79 * <p>The Color instatnce is supported on Android API level 26 and above. For API level lower than 80 * 26, use {@link #getArgb()}. See <a 81 * href="https://developer.android.com/reference/android/graphics/Color#color-instances">Android 82 * Color instances.</a> for more details. 83 */ 84 @RequiresApi(Build.VERSION_CODES.O) getColor()85 public Color getColor() { 86 return Color.valueOf(getArgb()); 87 } 88 } 89