• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.github.mikephil.charting.utils;
3 
4 import android.content.res.Resources;
5 import android.graphics.Color;
6 
7 import java.util.ArrayList;
8 import java.util.List;
9 
10 /**
11  * Class that holds predefined color integer arrays (e.g.
12  * ColorTemplate.VORDIPLOM_COLORS) and convenience methods for loading colors
13  * from resources.
14  *
15  * @author Philipp Jahoda
16  */
17 public class ColorTemplate {
18 
19     /**
20      * an "invalid" color that indicates that no color is set
21      */
22     public static final int COLOR_NONE = 0x00112233;
23 
24     /**
25      * this "color" is used for the Legend creation and indicates that the next
26      * form should be skipped
27      */
28     public static final int COLOR_SKIP = 0x00112234;
29 
30     /**
31      * THE COLOR THEMES ARE PREDEFINED (predefined color integer arrays), FEEL
32      * FREE TO CREATE YOUR OWN WITH AS MANY DIFFERENT COLORS AS YOU WANT
33      */
34     public static final int[] LIBERTY_COLORS = {
35             Color.rgb(207, 248, 246), Color.rgb(148, 212, 212), Color.rgb(136, 180, 187),
36             Color.rgb(118, 174, 175), Color.rgb(42, 109, 130)
37     };
38     public static final int[] JOYFUL_COLORS = {
39             Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120),
40             Color.rgb(106, 167, 134), Color.rgb(53, 194, 209)
41     };
42     public static final int[] PASTEL_COLORS = {
43             Color.rgb(64, 89, 128), Color.rgb(149, 165, 124), Color.rgb(217, 184, 162),
44             Color.rgb(191, 134, 134), Color.rgb(179, 48, 80)
45     };
46     public static final int[] COLORFUL_COLORS = {
47             Color.rgb(193, 37, 82), Color.rgb(255, 102, 0), Color.rgb(245, 199, 0),
48             Color.rgb(106, 150, 31), Color.rgb(179, 100, 53)
49     };
50     public static final int[] VORDIPLOM_COLORS = {
51             Color.rgb(192, 255, 140), Color.rgb(255, 247, 140), Color.rgb(255, 208, 140),
52             Color.rgb(140, 234, 255), Color.rgb(255, 140, 157)
53     };
54     public static final int[] MATERIAL_COLORS = {
55             rgb("#2ecc71"), rgb("#f1c40f"), rgb("#e74c3c"), rgb("#3498db")
56     };
57 
58     /**
59      * Converts the given hex-color-string to rgb.
60      *
61      * @param hex
62      * @return
63      */
rgb(String hex)64     public static int rgb(String hex) {
65         int color = (int) Long.parseLong(hex.replace("#", ""), 16);
66         int r = (color >> 16) & 0xFF;
67         int g = (color >> 8) & 0xFF;
68         int b = (color >> 0) & 0xFF;
69         return Color.rgb(r, g, b);
70     }
71 
72     /**
73      * Returns the Android ICS holo blue light color.
74      *
75      * @return
76      */
getHoloBlue()77     public static int getHoloBlue() {
78         return Color.rgb(51, 181, 229);
79     }
80 
81     /**
82      * Sets the alpha component of the given color.
83      *
84      * @param color
85      * @param alpha 0 - 255
86      * @return
87      */
colorWithAlpha(int color, int alpha)88     public static int colorWithAlpha(int color, int alpha) {
89         return (color & 0xffffff) | ((alpha & 0xff) << 24);
90     }
91 
92     /**
93      * turn an array of resource-colors (contains resource-id integers) into an
94      * array list of actual color integers
95      *
96      * @param r
97      * @param colors an integer array of resource id's of colors
98      * @return
99      */
createColors(Resources r, int[] colors)100     public static List<Integer> createColors(Resources r, int[] colors) {
101 
102         List<Integer> result = new ArrayList<Integer>();
103 
104         for (int i : colors) {
105             result.add(r.getColor(i));
106         }
107 
108         return result;
109     }
110 
111     /**
112      * Turns an array of colors (integer color values) into an ArrayList of
113      * colors.
114      *
115      * @param colors
116      * @return
117      */
createColors(int[] colors)118     public static List<Integer> createColors(int[] colors) {
119 
120         List<Integer> result = new ArrayList<Integer>();
121 
122         for (int i : colors) {
123             result.add(i);
124         }
125 
126         return result;
127     }
128 }
129