Search in sources :

Example 6 with ColorInt

use of androidx.annotation.ColorInt in project react-native-navigation by wix.

the class TextViewUtils method getTextColor.

@ColorInt
public static int getTextColor(TextView view) {
    SpannedString text = new SpannedString(view.getText());
    ForegroundColorSpan[] spans = text.getSpans(0, text.length(), ForegroundColorSpan.class);
    return spans.length == 0 ? view.getCurrentTextColor() : spans[0].getForegroundColor();
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannedString(android.text.SpannedString) ColorInt(androidx.annotation.ColorInt)

Example 7 with ColorInt

use of androidx.annotation.ColorInt in project AntennaPod by AntennaPod.

the class ThemeUtils method getColorFromAttr.

@ColorInt
public static int getColorFromAttr(Context context, @AttrRes int attr) {
    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(attr, typedValue, true);
    if (typedValue.resourceId != 0) {
        return ContextCompat.getColor(context, typedValue.resourceId);
    }
    return typedValue.data;
}
Also used : TypedValue(android.util.TypedValue) ColorInt(androidx.annotation.ColorInt)

Example 8 with ColorInt

use of androidx.annotation.ColorInt in project Conversations by siacs.

the class StyledAttributes method getColor.

@ColorInt
public static int getColor(Context context, @AttrRes int attr) {
    TypedArray typedArray = context.obtainStyledAttributes(new int[] { attr });
    int color = typedArray.getColor(0, 0);
    typedArray.recycle();
    return color;
}
Also used : TypedArray(android.content.res.TypedArray) ColorInt(androidx.annotation.ColorInt)

Example 9 with ColorInt

use of androidx.annotation.ColorInt in project ExoPlayer by google.

the class ColorParser method parseColorInternal.

@ColorInt
private static int parseColorInternal(String colorExpression, boolean alphaHasFloatFormat) {
    Assertions.checkArgument(!TextUtils.isEmpty(colorExpression));
    colorExpression = colorExpression.replace(" ", "");
    if (colorExpression.charAt(0) == '#') {
        // Parse using Long to avoid failure when colorExpression is greater than #7FFFFFFF.
        int color = (int) Long.parseLong(colorExpression.substring(1), 16);
        if (colorExpression.length() == 7) {
            // Set the alpha value
            color |= 0xFF000000;
        } else if (colorExpression.length() == 9) {
            // We have #RRGGBBAA, but we need #AARRGGBB
            color = ((color & 0xFF) << 24) | (color >>> 8);
        } else {
            throw new IllegalArgumentException();
        }
        return color;
    } else if (colorExpression.startsWith(RGBA)) {
        Matcher matcher = (alphaHasFloatFormat ? RGBA_PATTERN_FLOAT_ALPHA : RGBA_PATTERN_INT_ALPHA).matcher(colorExpression);
        if (matcher.matches()) {
            return Color.argb(alphaHasFloatFormat ? (int) (255 * Float.parseFloat(Assertions.checkNotNull(matcher.group(4)))) : Integer.parseInt(Assertions.checkNotNull(matcher.group(4)), 10), Integer.parseInt(Assertions.checkNotNull(matcher.group(1)), 10), Integer.parseInt(Assertions.checkNotNull(matcher.group(2)), 10), Integer.parseInt(Assertions.checkNotNull(matcher.group(3)), 10));
        }
    } else if (colorExpression.startsWith(RGB)) {
        Matcher matcher = RGB_PATTERN.matcher(colorExpression);
        if (matcher.matches()) {
            return Color.rgb(Integer.parseInt(Assertions.checkNotNull(matcher.group(1)), 10), Integer.parseInt(Assertions.checkNotNull(matcher.group(2)), 10), Integer.parseInt(Assertions.checkNotNull(matcher.group(3)), 10));
        }
    } else {
        // we use our own color map
        Integer color = COLOR_MAP.get(Ascii.toLowerCase(colorExpression));
        if (color != null) {
            return color;
        }
    }
    throw new IllegalArgumentException();
}
Also used : Matcher(java.util.regex.Matcher) ColorInt(androidx.annotation.ColorInt)

Example 10 with ColorInt

use of androidx.annotation.ColorInt in project Signal-Android by WhisperSystems.

the class ThemeUtil method getThemedColor.

@ColorInt
public static int getThemedColor(@NonNull Context context, @AttrRes int attr) {
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    if (theme.resolveAttribute(attr, typedValue, true)) {
        return typedValue.data;
    }
    return Color.RED;
}
Also used : AppCompatResources(androidx.appcompat.content.res.AppCompatResources) Resources(android.content.res.Resources) TypedValue(android.util.TypedValue) ColorInt(androidx.annotation.ColorInt)

Aggregations

ColorInt (androidx.annotation.ColorInt)10 TypedArray (android.content.res.TypedArray)3 TypedValue (android.util.TypedValue)3 ForegroundColorSpan (android.text.style.ForegroundColorSpan)2 SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 Paint (android.graphics.Paint)1 SpannedString (android.text.SpannedString)1 AppCompatResources (androidx.appcompat.content.res.AppCompatResources)1 QuoteSpan (eu.siacs.conversations.ui.text.QuoteSpan)1 Matcher (java.util.regex.Matcher)1