summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/eleven/utils/BitmapWithColors.java
blob: aaa46f56c2694d5f90584b03d078b12c3b98600e (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.cyanogenmod.eleven.utils;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Looper;
import android.support.v7.graphics.Palette;
import android.support.v7.graphics.Target;
import android.util.LruCache;

public class BitmapWithColors {
    private static final class BitmapColors {
        public final int mVibrantColor;
        public final int mVibrantDarkColor;
        public final int mVibrantLightColor;
        public final int mDominantColor;

        public BitmapColors(Palette palette) {
            mVibrantColor = determineColor(palette.getVibrantSwatch());
            mVibrantDarkColor = determineColor(palette.getDarkVibrantSwatch());
            mVibrantLightColor = determineColor(palette.getLightVibrantSwatch());
            mDominantColor = determineColor(getDominantSwatch(palette));
        }

        public BitmapColors(int vibrantColor, int vibrantDarkColor) {
            mVibrantColor = vibrantColor;
            mVibrantDarkColor = vibrantDarkColor;
            mVibrantLightColor = Color.TRANSPARENT;
            mDominantColor = vibrantColor;
        }

        private int determineColor(Palette.Swatch swatch) {
            return swatch != null ? swatch.getRgb() : Color.TRANSPARENT;
        }

        private static Palette.Swatch getDominantSwatch(Palette palette) {
            Palette.Swatch dominant = null;
            for (Palette.Swatch swatch : palette.getSwatches()) {
                if (dominant == null || swatch.getPopulation() > dominant.getPopulation()) {
                    dominant = swatch;
                }
            }
            return dominant;
        }
    }

    private static final int CACHE_SIZE_MAX = 20;
    private static final LruCache<Integer, BitmapColors> sCachedColors =
            new LruCache<Integer, BitmapColors>(CACHE_SIZE_MAX);

    private Bitmap mBitmap;
    private int mBitmapKey;
    private BitmapColors mColors;

    public BitmapWithColors(Bitmap bitmap, int bitmapKey) {
        mBitmap = bitmap;
        mBitmapKey = bitmapKey;

        if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
            // we're already running in background, so do the
            // (costly) palette initialization immediately
            loadColorsIfNeeded();
        }
    }

    public BitmapWithColors(Bitmap bitmap, int bitmapKey, int vibrantColor, int vibrantDarkColor) {
        mBitmap = bitmap;
        mBitmapKey = bitmapKey;
        mColors = new BitmapColors(vibrantColor, vibrantDarkColor);
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

    public int getVibrantColor() {
        loadColorsIfNeeded();
        if (mColors.mVibrantColor == Color.TRANSPARENT) {
            return mColors.mVibrantDarkColor;
        }
        return mColors.mVibrantColor;
    }

    public int getVibrantDarkColor() {
        loadColorsIfNeeded();
        if (mColors.mVibrantDarkColor == Color.TRANSPARENT) {
            return mColors.mVibrantColor;
        }
        return mColors.mVibrantDarkColor;
    }

    public int getContrastingColor() {
        loadColorsIfNeeded();

        float contrastToDark = computeContrastBetweenColors(mColors.mDominantColor,
                mColors.mVibrantDarkColor);
        float contrastToLight = computeContrastBetweenColors(mColors.mDominantColor,
                mColors.mVibrantLightColor);
        float contrastToVibrant = computeContrastBetweenColors(mColors.mDominantColor,
                mColors.mVibrantColor);

        int bestColor = mColors.mDominantColor;
        float bestContrast = -1;
        if (contrastToVibrant > bestContrast) {
            bestColor = mColors.mVibrantColor;
            bestContrast = contrastToVibrant;
        }
        if (contrastToDark > bestContrast) {
            bestColor = mColors.mVibrantDarkColor;
            bestContrast = contrastToDark;
        }
        if (contrastToLight > bestContrast) {
            bestColor = mColors.mVibrantLightColor;
            bestContrast = contrastToLight;
        }

        return bestColor;
    }

    /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
    private static float computeContrastBetweenColors(int bg, int fg) {
        if (bg == Color.TRANSPARENT || fg == Color.TRANSPARENT || bg == fg) {
            return -1;
        }

        float bgR = Color.red(bg) / 255f;
        float bgG = Color.green(bg) / 255f;
        float bgB = Color.blue(bg) / 255f;
        bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
        bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
        bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
        float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;

        float fgR = Color.red(fg) / 255f;
        float fgG = Color.green(fg) / 255f;
        float fgB = Color.blue(fg) / 255f;
        fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
        fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
        fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
        float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;

        return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
    }

    private synchronized void loadColorsIfNeeded() {
        if (mColors != null) {
            return;
        }

        synchronized (sCachedColors) {
            mColors = sCachedColors.get(mBitmapKey);
        }
        if (mColors != null) {
            return;
        }

        final Palette p = Palette.from(mBitmap)
                .clearTargets()
                .addTarget(Target.VIBRANT)
                .addTarget(Target.LIGHT_VIBRANT)
                .addTarget(Target.DARK_VIBRANT)
                .generate();
        if (p == null) {
            return;
        }

        mColors = new BitmapColors(p);
        synchronized (sCachedColors) {
            sCachedColors.put(mBitmapKey, mColors);
        }
    }
}