Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ColorHelper.java « helper « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25df2a90d33dfb1f75e0bad5b55ca0119ede2b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package de.luhmer.owncloudnewsreader.helper;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.database.model.Feed;

public class ColorHelper {
    public static String getCssColor(int color) {
        // using %f for the double value would result in a localized string, e.g. 0,12 which
        // would be an invalid css color string
        return String.format("rgba(%d,%d,%d,%s)",
                Color.red(color),
                Color.green(color),
                Color.blue(color),
                Double.toString(Color.alpha(color)/255.0));
    }

    public static int[] getColorsFromAttributes(Context context, int... attr) {
        final TypedArray a = context
                .obtainStyledAttributes(attr);
        int[] colors = new int[a.getIndexCount()];
        for(int i=0; i<a.getIndexCount(); i++) {
            colors[i] = a.getColor(i,0);
        }
        a.recycle();
        return colors;
    }

    public static int getColorFromAttribute(Context context, int attr) {
        int[] colors = getColorsFromAttributes(context, attr);
        if(colors.length >= 1)
            return colors[0];
        else
            return 0;
    }

    public static int getFeedColor(Context context, Feed item) {
        int color;
        if(item != null && item.getAvgColour() != null)
            color = Integer.parseInt(item.getAvgColour());
        else
            color = getColorFromAttribute(context, R.attr.dividerLineColor);
        return color;
    }
}