NotificationColorUtil.java revision 622c64a9ce8b5024c33fc9b0c722c5203950a13c
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.internal.util;
18
19import android.annotation.ColorInt;
20import android.annotation.FloatRange;
21import android.annotation.IntRange;
22import android.annotation.NonNull;
23import android.app.Notification;
24import android.content.Context;
25import android.content.res.ColorStateList;
26import android.content.res.Resources;
27import android.graphics.Bitmap;
28import android.graphics.Color;
29import android.graphics.drawable.AnimationDrawable;
30import android.graphics.drawable.BitmapDrawable;
31import android.graphics.drawable.Drawable;
32import android.graphics.drawable.Icon;
33import android.graphics.drawable.VectorDrawable;
34import android.text.SpannableStringBuilder;
35import android.text.Spanned;
36import android.text.style.CharacterStyle;
37import android.text.style.ForegroundColorSpan;
38import android.text.style.TextAppearanceSpan;
39import android.util.Log;
40import android.util.Pair;
41
42import java.util.Arrays;
43import java.util.WeakHashMap;
44
45/**
46 * Helper class to process legacy (Holo) notifications to make them look like material notifications.
47 *
48 * @hide
49 */
50public class NotificationColorUtil {
51
52    private static final String TAG = "NotificationColorUtil";
53    private static final boolean DEBUG = false;
54
55    private static final Object sLock = new Object();
56    private static NotificationColorUtil sInstance;
57
58    private final ImageUtils mImageUtils = new ImageUtils();
59    private final WeakHashMap<Bitmap, Pair<Boolean, Integer>> mGrayscaleBitmapCache =
60            new WeakHashMap<Bitmap, Pair<Boolean, Integer>>();
61
62    private final int mGrayscaleIconMaxSize; // @dimen/notification_large_icon_width (64dp)
63
64    public static NotificationColorUtil getInstance(Context context) {
65        synchronized (sLock) {
66            if (sInstance == null) {
67                sInstance = new NotificationColorUtil(context);
68            }
69            return sInstance;
70        }
71    }
72
73    private NotificationColorUtil(Context context) {
74        mGrayscaleIconMaxSize = context.getResources().getDimensionPixelSize(
75                com.android.internal.R.dimen.notification_large_icon_width);
76    }
77
78    /**
79     * Checks whether a Bitmap is a small grayscale icon.
80     * Grayscale here means "very close to a perfect gray"; icon means "no larger than 64dp".
81     *
82     * @param bitmap The bitmap to test.
83     * @return True if the bitmap is grayscale; false if it is color or too large to examine.
84     */
85    public boolean isGrayscaleIcon(Bitmap bitmap) {
86        // quick test: reject large bitmaps
87        if (bitmap.getWidth() > mGrayscaleIconMaxSize
88                || bitmap.getHeight() > mGrayscaleIconMaxSize) {
89            return false;
90        }
91
92        synchronized (sLock) {
93            Pair<Boolean, Integer> cached = mGrayscaleBitmapCache.get(bitmap);
94            if (cached != null) {
95                if (cached.second == bitmap.getGenerationId()) {
96                    return cached.first;
97                }
98            }
99        }
100        boolean result;
101        int generationId;
102        synchronized (mImageUtils) {
103            result = mImageUtils.isGrayscale(bitmap);
104
105            // generationId and the check whether the Bitmap is grayscale can't be read atomically
106            // here. However, since the thread is in the process of posting the notification, we can
107            // assume that it doesn't modify the bitmap while we are checking the pixels.
108            generationId = bitmap.getGenerationId();
109        }
110        synchronized (sLock) {
111            mGrayscaleBitmapCache.put(bitmap, Pair.create(result, generationId));
112        }
113        return result;
114    }
115
116    /**
117     * Checks whether a Drawable is a small grayscale icon.
118     * Grayscale here means "very close to a perfect gray"; icon means "no larger than 64dp".
119     *
120     * @param d The drawable to test.
121     * @return True if the bitmap is grayscale; false if it is color or too large to examine.
122     */
123    public boolean isGrayscaleIcon(Drawable d) {
124        if (d == null) {
125            return false;
126        } else if (d instanceof BitmapDrawable) {
127            BitmapDrawable bd = (BitmapDrawable) d;
128            return bd.getBitmap() != null && isGrayscaleIcon(bd.getBitmap());
129        } else if (d instanceof AnimationDrawable) {
130            AnimationDrawable ad = (AnimationDrawable) d;
131            int count = ad.getNumberOfFrames();
132            return count > 0 && isGrayscaleIcon(ad.getFrame(0));
133        } else if (d instanceof VectorDrawable) {
134            // We just assume you're doing the right thing if using vectors
135            return true;
136        } else {
137            return false;
138        }
139    }
140
141    public boolean isGrayscaleIcon(Context context, Icon icon) {
142        if (icon == null) {
143            return false;
144        }
145        switch (icon.getType()) {
146            case Icon.TYPE_BITMAP:
147                return isGrayscaleIcon(icon.getBitmap());
148            case Icon.TYPE_RESOURCE:
149                return isGrayscaleIcon(context, icon.getResId());
150            default:
151                return false;
152        }
153    }
154
155    /**
156     * Checks whether a drawable with a resoure id is a small grayscale icon.
157     * Grayscale here means "very close to a perfect gray"; icon means "no larger than 64dp".
158     *
159     * @param context The context to load the drawable from.
160     * @return True if the bitmap is grayscale; false if it is color or too large to examine.
161     */
162    public boolean isGrayscaleIcon(Context context, int drawableResId) {
163        if (drawableResId != 0) {
164            try {
165                return isGrayscaleIcon(context.getDrawable(drawableResId));
166            } catch (Resources.NotFoundException ex) {
167                Log.e(TAG, "Drawable not found: " + drawableResId);
168                return false;
169            }
170        } else {
171            return false;
172        }
173    }
174
175    /**
176     * Inverts all the grayscale colors set by {@link android.text.style.TextAppearanceSpan}s on
177     * the text.
178     *
179     * @param charSequence The text to process.
180     * @return The color inverted text.
181     */
182    public CharSequence invertCharSequenceColors(CharSequence charSequence) {
183        if (charSequence instanceof Spanned) {
184            Spanned ss = (Spanned) charSequence;
185            Object[] spans = ss.getSpans(0, ss.length(), Object.class);
186            SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
187            for (Object span : spans) {
188                Object resultSpan = span;
189                if (resultSpan instanceof CharacterStyle) {
190                    resultSpan = ((CharacterStyle) span).getUnderlying();
191                }
192                if (resultSpan instanceof TextAppearanceSpan) {
193                    TextAppearanceSpan processedSpan = processTextAppearanceSpan(
194                            (TextAppearanceSpan) span);
195                    if (processedSpan != resultSpan) {
196                        resultSpan = processedSpan;
197                    } else {
198                        // we need to still take the orgininal for wrapped spans
199                        resultSpan = span;
200                    }
201                } else if (resultSpan instanceof ForegroundColorSpan) {
202                    ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
203                    int foregroundColor = originalSpan.getForegroundColor();
204                    resultSpan = new ForegroundColorSpan(processColor(foregroundColor));
205                } else {
206                    resultSpan = span;
207                }
208                builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
209                        ss.getSpanFlags(span));
210            }
211            return builder;
212        }
213        return charSequence;
214    }
215
216    private TextAppearanceSpan processTextAppearanceSpan(TextAppearanceSpan span) {
217        ColorStateList colorStateList = span.getTextColor();
218        if (colorStateList != null) {
219            int[] colors = colorStateList.getColors();
220            boolean changed = false;
221            for (int i = 0; i < colors.length; i++) {
222                if (ImageUtils.isGrayscale(colors[i])) {
223
224                    // Allocate a new array so we don't change the colors in the old color state
225                    // list.
226                    if (!changed) {
227                        colors = Arrays.copyOf(colors, colors.length);
228                    }
229                    colors[i] = processColor(colors[i]);
230                    changed = true;
231                }
232            }
233            if (changed) {
234                return new TextAppearanceSpan(
235                        span.getFamily(), span.getTextStyle(), span.getTextSize(),
236                        new ColorStateList(colorStateList.getStates(), colors),
237                        span.getLinkTextColor());
238            }
239        }
240        return span;
241    }
242
243    private int processColor(int color) {
244        return Color.argb(Color.alpha(color),
245                255 - Color.red(color),
246                255 - Color.green(color),
247                255 - Color.blue(color));
248    }
249
250    /**
251     * Finds a suitable color such that there's enough contrast.
252     *
253     * @param color the color to start searching from.
254     * @param other the color to ensure contrast against. Assumed to be lighter than {@param color}
255     * @param findFg if true, we assume {@param color} is a foreground, otherwise a background.
256     * @param minRatio the minimum contrast ratio required.
257     * @return a color with the same hue as {@param color}, potentially darkened to meet the
258     *          contrast ratio.
259     */
260    private static int findContrastColor(int color, int other, boolean findFg, double minRatio) {
261        int fg = findFg ? color : other;
262        int bg = findFg ? other : color;
263        if (ColorUtilsFromCompat.calculateContrast(fg, bg) >= minRatio) {
264            return color;
265        }
266
267        double[] lab = new double[3];
268        ColorUtilsFromCompat.colorToLAB(findFg ? fg : bg, lab);
269
270        double low = 0, high = lab[0];
271        final double a = lab[1], b = lab[2];
272        for (int i = 0; i < 15 && high - low > 0.00001; i++) {
273            final double l = (low + high) / 2;
274            if (findFg) {
275                fg = ColorUtilsFromCompat.LABToColor(l, a, b);
276            } else {
277                bg = ColorUtilsFromCompat.LABToColor(l, a, b);
278            }
279            if (ColorUtilsFromCompat.calculateContrast(fg, bg) > minRatio) {
280                low = l;
281            } else {
282                high = l;
283            }
284        }
285        return ColorUtilsFromCompat.LABToColor(low, a, b);
286    }
287
288    /**
289     * Finds a suitable color such that there's enough contrast.
290     *
291     * @param color the color to start searching from.
292     * @param other the color to ensure contrast against. Assumed to be darker than {@param color}
293     * @param findFg if true, we assume {@param color} is a foreground, otherwise a background.
294     * @param minRatio the minimum contrast ratio required.
295     * @return a color with the same hue as {@param color}, potentially darkened to meet the
296     *          contrast ratio.
297     */
298    public static int findContrastColorAgainstDark(int color, int other, boolean findFg,
299            double minRatio) {
300        int fg = findFg ? color : other;
301        int bg = findFg ? other : color;
302        if (ColorUtilsFromCompat.calculateContrast(fg, bg) >= minRatio) {
303            return color;
304        }
305
306        float[] hsl = new float[3];
307        ColorUtilsFromCompat.colorToHSL(findFg ? fg : bg, hsl);
308
309        float low = hsl[2], high = 1;
310        for (int i = 0; i < 15 && high - low > 0.00001; i++) {
311            final float l = (low + high) / 2;
312            hsl[2] = l;
313            if (findFg) {
314                fg = ColorUtilsFromCompat.HSLToColor(hsl);
315            } else {
316                bg = ColorUtilsFromCompat.HSLToColor(hsl);
317            }
318            if (ColorUtilsFromCompat.calculateContrast(fg, bg) > minRatio) {
319                high = l;
320            } else {
321                low = l;
322            }
323        }
324        return findFg ? fg : bg;
325    }
326
327    public static int ensureTextContrastOnBlack(int color) {
328        return findContrastColorAgainstDark(color, Color.BLACK, true /* fg */, 12);
329    }
330
331    /**
332     * Finds a text color with sufficient contrast over bg that has the same hue as the original
333     * color, assuming it is for large text.
334     */
335    public static int ensureLargeTextContrast(int color, int bg) {
336        return findContrastColor(color, bg, true, 3);
337    }
338
339    /**
340     * Finds a text color with sufficient contrast over bg that has the same hue as the original
341     * color.
342     */
343    private static int ensureTextContrast(int color, int bg) {
344        return findContrastColor(color, bg, true, 4.5);
345    }
346
347    /** Finds a background color for a text view with given text color and hint text color, that
348     * has the same hue as the original color.
349     */
350    public static int ensureTextBackgroundColor(int color, int textColor, int hintColor) {
351        color = findContrastColor(color, hintColor, false, 3.0);
352        return findContrastColor(color, textColor, false, 4.5);
353    }
354
355    private static String contrastChange(int colorOld, int colorNew, int bg) {
356        return String.format("from %.2f:1 to %.2f:1",
357                ColorUtilsFromCompat.calculateContrast(colorOld, bg),
358                ColorUtilsFromCompat.calculateContrast(colorNew, bg));
359    }
360
361    /**
362     * Resolves {@param color} to an actual color if it is {@link Notification#COLOR_DEFAULT}
363     */
364    public static int resolveColor(Context context, int color) {
365        if (color == Notification.COLOR_DEFAULT) {
366            return context.getColor(com.android.internal.R.color.notification_icon_default_color);
367        }
368        return color;
369    }
370
371    /**
372     * Resolves a Notification's color such that it has enough contrast to be used as the
373     * color for the Notification's action and header text.
374     *
375     * @param notificationColor the color of the notification or {@link Notification#COLOR_DEFAULT}
376     * @return a color of the same hue with enough contrast against the backgrounds.
377     */
378    public static int resolveContrastColor(Context context, int notificationColor) {
379        final int resolvedColor = resolveColor(context, notificationColor);
380
381        final int actionBg = context.getColor(
382                com.android.internal.R.color.notification_action_list);
383        final int notiBg = context.getColor(
384                com.android.internal.R.color.notification_material_background_color);
385
386        int color = resolvedColor;
387        color = NotificationColorUtil.ensureLargeTextContrast(color, actionBg);
388        color = NotificationColorUtil.ensureTextContrast(color, notiBg);
389
390        if (color != resolvedColor) {
391            if (DEBUG){
392                Log.w(TAG, String.format(
393                        "Enhanced contrast of notification for %s %s (over action)"
394                                + " and %s (over background) by changing #%s to %s",
395                        context.getPackageName(),
396                        NotificationColorUtil.contrastChange(resolvedColor, color, actionBg),
397                        NotificationColorUtil.contrastChange(resolvedColor, color, notiBg),
398                        Integer.toHexString(resolvedColor), Integer.toHexString(color)));
399            }
400        }
401        return color;
402    }
403
404    /**
405     * Lighten a color by a specified value
406     * @param baseColor the base color to lighten
407     * @param amount the amount to lighten the color from 0 to 100. This corresponds to the L
408     *               increase in the LAB color space.
409     * @return the lightened color
410     */
411    public static int lightenColor(int baseColor, int amount) {
412        final double[] result = ColorUtilsFromCompat.getTempDouble3Array();
413        ColorUtilsFromCompat.colorToLAB(baseColor, result);
414        result[0] = Math.min(100, result[0] + amount);
415        return ColorUtilsFromCompat.LABToColor(result[0], result[1], result[2]);
416    }
417
418    public static int resolveAmbientColor(Context context, int notificationColor) {
419        final int resolvedColor = resolveColor(context, notificationColor);
420
421        int color = resolvedColor;
422        color = NotificationColorUtil.ensureTextContrastOnBlack(color);
423
424        if (color != resolvedColor) {
425            if (DEBUG){
426                Log.w(TAG, String.format(
427                        "Ambient contrast of notification for %s is %s (over black)"
428                                + " by changing #%s to #%s",
429                        context.getPackageName(),
430                        NotificationColorUtil.contrastChange(resolvedColor, color, Color.BLACK),
431                        Integer.toHexString(resolvedColor), Integer.toHexString(color)));
432            }
433        }
434        return color;
435    }
436
437    public static int resolvePrimaryColor(Context context, int backgroundColor) {
438        boolean useDark = shouldUseDark(backgroundColor);
439        if (useDark) {
440            return context.getColor(
441                    com.android.internal.R.color.notification_primary_text_color_light);
442        } else {
443            return context.getColor(
444                    com.android.internal.R.color.notification_primary_text_color_dark);
445        }
446    }
447
448    public static int resolveSecondaryColor(Context context, int backgroundColor) {
449        boolean useDark = shouldUseDark(backgroundColor);
450        if (useDark) {
451            return context.getColor(
452                    com.android.internal.R.color.notification_secondary_text_color_light);
453        } else {
454            return context.getColor(
455                    com.android.internal.R.color.notification_secondary_text_color_dark);
456        }
457    }
458
459    public static int resolveActionBarColor(Context context, int backgroundColor) {
460        if (backgroundColor == Notification.COLOR_DEFAULT) {
461            return context.getColor(com.android.internal.R.color.notification_action_list);
462        }
463        return getShiftedColor(backgroundColor, 7);
464    }
465
466    /**
467     * Get a color that stays in the same tint, but darkens or lightens it by a certain
468     * amount.
469     * This also looks at the lightness of the provided color and shifts it appropriately.
470     *
471     * @param color the base color to use
472     * @param amount the amount from 1 to 100 how much to modify the color
473     * @return the now color that was modified
474     */
475    public static int getShiftedColor(int color, int amount) {
476        final double[] result = ColorUtilsFromCompat.getTempDouble3Array();
477        ColorUtilsFromCompat.colorToLAB(color, result);
478        if (result[0] >= 4) {
479            result[0] = Math.max(0, result[0] - amount);
480        } else {
481            result[0] = Math.min(100, result[0] + amount);
482        }
483        return ColorUtilsFromCompat.LABToColor(result[0], result[1], result[2]);
484    }
485
486    private static boolean shouldUseDark(int backgroundColor) {
487        boolean useDark = backgroundColor == Notification.COLOR_DEFAULT;
488        if (!useDark) {
489            useDark = ColorUtilsFromCompat.calculateLuminance(backgroundColor) > 0.5;
490        }
491        return useDark;
492    }
493
494    /**
495     * Framework copy of functions needed from android.support.v4.graphics.ColorUtils.
496     */
497    private static class ColorUtilsFromCompat {
498        private static final double XYZ_WHITE_REFERENCE_X = 95.047;
499        private static final double XYZ_WHITE_REFERENCE_Y = 100;
500        private static final double XYZ_WHITE_REFERENCE_Z = 108.883;
501        private static final double XYZ_EPSILON = 0.008856;
502        private static final double XYZ_KAPPA = 903.3;
503
504        private static final int MIN_ALPHA_SEARCH_MAX_ITERATIONS = 10;
505        private static final int MIN_ALPHA_SEARCH_PRECISION = 1;
506
507        private static final ThreadLocal<double[]> TEMP_ARRAY = new ThreadLocal<>();
508
509        private ColorUtilsFromCompat() {}
510
511        /**
512         * Composite two potentially translucent colors over each other and returns the result.
513         */
514        public static int compositeColors(@ColorInt int foreground, @ColorInt int background) {
515            int bgAlpha = Color.alpha(background);
516            int fgAlpha = Color.alpha(foreground);
517            int a = compositeAlpha(fgAlpha, bgAlpha);
518
519            int r = compositeComponent(Color.red(foreground), fgAlpha,
520                    Color.red(background), bgAlpha, a);
521            int g = compositeComponent(Color.green(foreground), fgAlpha,
522                    Color.green(background), bgAlpha, a);
523            int b = compositeComponent(Color.blue(foreground), fgAlpha,
524                    Color.blue(background), bgAlpha, a);
525
526            return Color.argb(a, r, g, b);
527        }
528
529        private static int compositeAlpha(int foregroundAlpha, int backgroundAlpha) {
530            return 0xFF - (((0xFF - backgroundAlpha) * (0xFF - foregroundAlpha)) / 0xFF);
531        }
532
533        private static int compositeComponent(int fgC, int fgA, int bgC, int bgA, int a) {
534            if (a == 0) return 0;
535            return ((0xFF * fgC * fgA) + (bgC * bgA * (0xFF - fgA))) / (a * 0xFF);
536        }
537
538        /**
539         * Returns the luminance of a color as a float between {@code 0.0} and {@code 1.0}.
540         * <p>Defined as the Y component in the XYZ representation of {@code color}.</p>
541         */
542        @FloatRange(from = 0.0, to = 1.0)
543        public static double calculateLuminance(@ColorInt int color) {
544            final double[] result = getTempDouble3Array();
545            colorToXYZ(color, result);
546            // Luminance is the Y component
547            return result[1] / 100;
548        }
549
550        /**
551         * Returns the contrast ratio between {@code foreground} and {@code background}.
552         * {@code background} must be opaque.
553         * <p>
554         * Formula defined
555         * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
556         */
557        public static double calculateContrast(@ColorInt int foreground, @ColorInt int background) {
558            if (Color.alpha(background) != 255) {
559                throw new IllegalArgumentException("background can not be translucent: #"
560                        + Integer.toHexString(background));
561            }
562            if (Color.alpha(foreground) < 255) {
563                // If the foreground is translucent, composite the foreground over the background
564                foreground = compositeColors(foreground, background);
565            }
566
567            final double luminance1 = calculateLuminance(foreground) + 0.05;
568            final double luminance2 = calculateLuminance(background) + 0.05;
569
570            // Now return the lighter luminance divided by the darker luminance
571            return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
572        }
573
574        /**
575         * Convert the ARGB color to its CIE Lab representative components.
576         *
577         * @param color  the ARGB color to convert. The alpha component is ignored
578         * @param outLab 3-element array which holds the resulting LAB components
579         */
580        public static void colorToLAB(@ColorInt int color, @NonNull double[] outLab) {
581            RGBToLAB(Color.red(color), Color.green(color), Color.blue(color), outLab);
582        }
583
584        /**
585         * Convert RGB components to its CIE Lab representative components.
586         *
587         * <ul>
588         * <li>outLab[0] is L [0 ...100)</li>
589         * <li>outLab[1] is a [-128...127)</li>
590         * <li>outLab[2] is b [-128...127)</li>
591         * </ul>
592         *
593         * @param r      red component value [0..255]
594         * @param g      green component value [0..255]
595         * @param b      blue component value [0..255]
596         * @param outLab 3-element array which holds the resulting LAB components
597         */
598        public static void RGBToLAB(@IntRange(from = 0x0, to = 0xFF) int r,
599                @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
600                @NonNull double[] outLab) {
601            // First we convert RGB to XYZ
602            RGBToXYZ(r, g, b, outLab);
603            // outLab now contains XYZ
604            XYZToLAB(outLab[0], outLab[1], outLab[2], outLab);
605            // outLab now contains LAB representation
606        }
607
608        /**
609         * Convert the ARGB color to it's CIE XYZ representative components.
610         *
611         * <p>The resulting XYZ representation will use the D65 illuminant and the CIE
612         * 2° Standard Observer (1931).</p>
613         *
614         * <ul>
615         * <li>outXyz[0] is X [0 ...95.047)</li>
616         * <li>outXyz[1] is Y [0...100)</li>
617         * <li>outXyz[2] is Z [0...108.883)</li>
618         * </ul>
619         *
620         * @param color  the ARGB color to convert. The alpha component is ignored
621         * @param outXyz 3-element array which holds the resulting LAB components
622         */
623        public static void colorToXYZ(@ColorInt int color, @NonNull double[] outXyz) {
624            RGBToXYZ(Color.red(color), Color.green(color), Color.blue(color), outXyz);
625        }
626
627        /**
628         * Convert RGB components to it's CIE XYZ representative components.
629         *
630         * <p>The resulting XYZ representation will use the D65 illuminant and the CIE
631         * 2° Standard Observer (1931).</p>
632         *
633         * <ul>
634         * <li>outXyz[0] is X [0 ...95.047)</li>
635         * <li>outXyz[1] is Y [0...100)</li>
636         * <li>outXyz[2] is Z [0...108.883)</li>
637         * </ul>
638         *
639         * @param r      red component value [0..255]
640         * @param g      green component value [0..255]
641         * @param b      blue component value [0..255]
642         * @param outXyz 3-element array which holds the resulting XYZ components
643         */
644        public static void RGBToXYZ(@IntRange(from = 0x0, to = 0xFF) int r,
645                @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
646                @NonNull double[] outXyz) {
647            if (outXyz.length != 3) {
648                throw new IllegalArgumentException("outXyz must have a length of 3.");
649            }
650
651            double sr = r / 255.0;
652            sr = sr < 0.04045 ? sr / 12.92 : Math.pow((sr + 0.055) / 1.055, 2.4);
653            double sg = g / 255.0;
654            sg = sg < 0.04045 ? sg / 12.92 : Math.pow((sg + 0.055) / 1.055, 2.4);
655            double sb = b / 255.0;
656            sb = sb < 0.04045 ? sb / 12.92 : Math.pow((sb + 0.055) / 1.055, 2.4);
657
658            outXyz[0] = 100 * (sr * 0.4124 + sg * 0.3576 + sb * 0.1805);
659            outXyz[1] = 100 * (sr * 0.2126 + sg * 0.7152 + sb * 0.0722);
660            outXyz[2] = 100 * (sr * 0.0193 + sg * 0.1192 + sb * 0.9505);
661        }
662
663        /**
664         * Converts a color from CIE XYZ to CIE Lab representation.
665         *
666         * <p>This method expects the XYZ representation to use the D65 illuminant and the CIE
667         * 2° Standard Observer (1931).</p>
668         *
669         * <ul>
670         * <li>outLab[0] is L [0 ...100)</li>
671         * <li>outLab[1] is a [-128...127)</li>
672         * <li>outLab[2] is b [-128...127)</li>
673         * </ul>
674         *
675         * @param x      X component value [0...95.047)
676         * @param y      Y component value [0...100)
677         * @param z      Z component value [0...108.883)
678         * @param outLab 3-element array which holds the resulting Lab components
679         */
680        public static void XYZToLAB(@FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_X) double x,
681                @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Y) double y,
682                @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Z) double z,
683                @NonNull double[] outLab) {
684            if (outLab.length != 3) {
685                throw new IllegalArgumentException("outLab must have a length of 3.");
686            }
687            x = pivotXyzComponent(x / XYZ_WHITE_REFERENCE_X);
688            y = pivotXyzComponent(y / XYZ_WHITE_REFERENCE_Y);
689            z = pivotXyzComponent(z / XYZ_WHITE_REFERENCE_Z);
690            outLab[0] = Math.max(0, 116 * y - 16);
691            outLab[1] = 500 * (x - y);
692            outLab[2] = 200 * (y - z);
693        }
694
695        /**
696         * Converts a color from CIE Lab to CIE XYZ representation.
697         *
698         * <p>The resulting XYZ representation will use the D65 illuminant and the CIE
699         * 2° Standard Observer (1931).</p>
700         *
701         * <ul>
702         * <li>outXyz[0] is X [0 ...95.047)</li>
703         * <li>outXyz[1] is Y [0...100)</li>
704         * <li>outXyz[2] is Z [0...108.883)</li>
705         * </ul>
706         *
707         * @param l      L component value [0...100)
708         * @param a      A component value [-128...127)
709         * @param b      B component value [-128...127)
710         * @param outXyz 3-element array which holds the resulting XYZ components
711         */
712        public static void LABToXYZ(@FloatRange(from = 0f, to = 100) final double l,
713                @FloatRange(from = -128, to = 127) final double a,
714                @FloatRange(from = -128, to = 127) final double b,
715                @NonNull double[] outXyz) {
716            final double fy = (l + 16) / 116;
717            final double fx = a / 500 + fy;
718            final double fz = fy - b / 200;
719
720            double tmp = Math.pow(fx, 3);
721            final double xr = tmp > XYZ_EPSILON ? tmp : (116 * fx - 16) / XYZ_KAPPA;
722            final double yr = l > XYZ_KAPPA * XYZ_EPSILON ? Math.pow(fy, 3) : l / XYZ_KAPPA;
723
724            tmp = Math.pow(fz, 3);
725            final double zr = tmp > XYZ_EPSILON ? tmp : (116 * fz - 16) / XYZ_KAPPA;
726
727            outXyz[0] = xr * XYZ_WHITE_REFERENCE_X;
728            outXyz[1] = yr * XYZ_WHITE_REFERENCE_Y;
729            outXyz[2] = zr * XYZ_WHITE_REFERENCE_Z;
730        }
731
732        /**
733         * Converts a color from CIE XYZ to its RGB representation.
734         *
735         * <p>This method expects the XYZ representation to use the D65 illuminant and the CIE
736         * 2° Standard Observer (1931).</p>
737         *
738         * @param x X component value [0...95.047)
739         * @param y Y component value [0...100)
740         * @param z Z component value [0...108.883)
741         * @return int containing the RGB representation
742         */
743        @ColorInt
744        public static int XYZToColor(@FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_X) double x,
745                @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Y) double y,
746                @FloatRange(from = 0f, to = XYZ_WHITE_REFERENCE_Z) double z) {
747            double r = (x * 3.2406 + y * -1.5372 + z * -0.4986) / 100;
748            double g = (x * -0.9689 + y * 1.8758 + z * 0.0415) / 100;
749            double b = (x * 0.0557 + y * -0.2040 + z * 1.0570) / 100;
750
751            r = r > 0.0031308 ? 1.055 * Math.pow(r, 1 / 2.4) - 0.055 : 12.92 * r;
752            g = g > 0.0031308 ? 1.055 * Math.pow(g, 1 / 2.4) - 0.055 : 12.92 * g;
753            b = b > 0.0031308 ? 1.055 * Math.pow(b, 1 / 2.4) - 0.055 : 12.92 * b;
754
755            return Color.rgb(
756                    constrain((int) Math.round(r * 255), 0, 255),
757                    constrain((int) Math.round(g * 255), 0, 255),
758                    constrain((int) Math.round(b * 255), 0, 255));
759        }
760
761        /**
762         * Converts a color from CIE Lab to its RGB representation.
763         *
764         * @param l L component value [0...100]
765         * @param a A component value [-128...127]
766         * @param b B component value [-128...127]
767         * @return int containing the RGB representation
768         */
769        @ColorInt
770        public static int LABToColor(@FloatRange(from = 0f, to = 100) final double l,
771                @FloatRange(from = -128, to = 127) final double a,
772                @FloatRange(from = -128, to = 127) final double b) {
773            final double[] result = getTempDouble3Array();
774            LABToXYZ(l, a, b, result);
775            return XYZToColor(result[0], result[1], result[2]);
776        }
777
778        private static int constrain(int amount, int low, int high) {
779            return amount < low ? low : (amount > high ? high : amount);
780        }
781
782        private static float constrain(float amount, float low, float high) {
783            return amount < low ? low : (amount > high ? high : amount);
784        }
785
786        private static double pivotXyzComponent(double component) {
787            return component > XYZ_EPSILON
788                    ? Math.pow(component, 1 / 3.0)
789                    : (XYZ_KAPPA * component + 16) / 116;
790        }
791
792        public static double[] getTempDouble3Array() {
793            double[] result = TEMP_ARRAY.get();
794            if (result == null) {
795                result = new double[3];
796                TEMP_ARRAY.set(result);
797            }
798            return result;
799        }
800
801        /**
802         * Convert HSL (hue-saturation-lightness) components to a RGB color.
803         * <ul>
804         * <li>hsl[0] is Hue [0 .. 360)</li>
805         * <li>hsl[1] is Saturation [0...1]</li>
806         * <li>hsl[2] is Lightness [0...1]</li>
807         * </ul>
808         * If hsv values are out of range, they are pinned.
809         *
810         * @param hsl 3-element array which holds the input HSL components
811         * @return the resulting RGB color
812         */
813        @ColorInt
814        public static int HSLToColor(@NonNull float[] hsl) {
815            final float h = hsl[0];
816            final float s = hsl[1];
817            final float l = hsl[2];
818
819            final float c = (1f - Math.abs(2 * l - 1f)) * s;
820            final float m = l - 0.5f * c;
821            final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));
822
823            final int hueSegment = (int) h / 60;
824
825            int r = 0, g = 0, b = 0;
826
827            switch (hueSegment) {
828                case 0:
829                    r = Math.round(255 * (c + m));
830                    g = Math.round(255 * (x + m));
831                    b = Math.round(255 * m);
832                    break;
833                case 1:
834                    r = Math.round(255 * (x + m));
835                    g = Math.round(255 * (c + m));
836                    b = Math.round(255 * m);
837                    break;
838                case 2:
839                    r = Math.round(255 * m);
840                    g = Math.round(255 * (c + m));
841                    b = Math.round(255 * (x + m));
842                    break;
843                case 3:
844                    r = Math.round(255 * m);
845                    g = Math.round(255 * (x + m));
846                    b = Math.round(255 * (c + m));
847                    break;
848                case 4:
849                    r = Math.round(255 * (x + m));
850                    g = Math.round(255 * m);
851                    b = Math.round(255 * (c + m));
852                    break;
853                case 5:
854                case 6:
855                    r = Math.round(255 * (c + m));
856                    g = Math.round(255 * m);
857                    b = Math.round(255 * (x + m));
858                    break;
859            }
860
861            r = constrain(r, 0, 255);
862            g = constrain(g, 0, 255);
863            b = constrain(b, 0, 255);
864
865            return Color.rgb(r, g, b);
866        }
867
868        /**
869         * Convert the ARGB color to its HSL (hue-saturation-lightness) components.
870         * <ul>
871         * <li>outHsl[0] is Hue [0 .. 360)</li>
872         * <li>outHsl[1] is Saturation [0...1]</li>
873         * <li>outHsl[2] is Lightness [0...1]</li>
874         * </ul>
875         *
876         * @param color  the ARGB color to convert. The alpha component is ignored
877         * @param outHsl 3-element array which holds the resulting HSL components
878         */
879        public static void colorToHSL(@ColorInt int color, @NonNull float[] outHsl) {
880            RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), outHsl);
881        }
882
883        /**
884         * Convert RGB components to HSL (hue-saturation-lightness).
885         * <ul>
886         * <li>outHsl[0] is Hue [0 .. 360)</li>
887         * <li>outHsl[1] is Saturation [0...1]</li>
888         * <li>outHsl[2] is Lightness [0...1]</li>
889         * </ul>
890         *
891         * @param r      red component value [0..255]
892         * @param g      green component value [0..255]
893         * @param b      blue component value [0..255]
894         * @param outHsl 3-element array which holds the resulting HSL components
895         */
896        public static void RGBToHSL(@IntRange(from = 0x0, to = 0xFF) int r,
897                @IntRange(from = 0x0, to = 0xFF) int g, @IntRange(from = 0x0, to = 0xFF) int b,
898                @NonNull float[] outHsl) {
899            final float rf = r / 255f;
900            final float gf = g / 255f;
901            final float bf = b / 255f;
902
903            final float max = Math.max(rf, Math.max(gf, bf));
904            final float min = Math.min(rf, Math.min(gf, bf));
905            final float deltaMaxMin = max - min;
906
907            float h, s;
908            float l = (max + min) / 2f;
909
910            if (max == min) {
911                // Monochromatic
912                h = s = 0f;
913            } else {
914                if (max == rf) {
915                    h = ((gf - bf) / deltaMaxMin) % 6f;
916                } else if (max == gf) {
917                    h = ((bf - rf) / deltaMaxMin) + 2f;
918                } else {
919                    h = ((rf - gf) / deltaMaxMin) + 4f;
920                }
921
922                s = deltaMaxMin / (1f - Math.abs(2f * l - 1f));
923            }
924
925            h = (h * 60f) % 360f;
926            if (h < 0) {
927                h += 360f;
928            }
929
930            outHsl[0] = constrain(h, 0f, 360f);
931            outHsl[1] = constrain(s, 0f, 1f);
932            outHsl[2] = constrain(l, 0f, 1f);
933        }
934
935    }
936}
937