ColorUtils.java revision e1dfa4c5019de5823d05ea09417f28e55e7430fa
1/* 2 * Copyright 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 android.support.v7.graphics; 18 19import android.graphics.Color; 20 21final class ColorUtils { 22 23 private ColorUtils() {} 24 25 /** 26 * @return luma value according to to XYZ color space in the range 0.0 - 1.0 27 */ 28 static float calculateXyzLuma(int color) { 29 return (0.2126f * Color.red(color) + 30 0.7152f * Color.green(color) + 31 0.0722f * Color.blue(color)) / 255f; 32 } 33 34 static float calculateContrast(int color1, int color2) { 35 return Math.abs(ColorUtils.calculateXyzLuma(color1) - ColorUtils.calculateXyzLuma(color2)); 36 } 37 38 static void RGBtoHSL(int r, int g, int b, float[] hsl) { 39 final float rf = r / 255f; 40 final float gf = g / 255f; 41 final float bf = b / 255f; 42 43 final float max = Math.max(rf, Math.max(gf, bf)); 44 final float min = Math.min(rf, Math.min(gf, bf)); 45 final float deltaMaxMin = max - min; 46 47 float h, s; 48 float l = (max + min) / 2f; 49 50 if (max == min) { 51 // Monochromatic 52 h = s = 0f; 53 } else { 54 if (max == rf) { 55 h = ((gf - bf) / deltaMaxMin) % 6f; 56 } else if (max == gf) { 57 h = ((bf - rf) / deltaMaxMin) + 2f; 58 } else { 59 h = ((rf - gf) / deltaMaxMin) + 4f; 60 } 61 62 s = deltaMaxMin / (1f - Math.abs(2f * l - 1f)); 63 } 64 65 hsl[0] = (h * 60f) % 360f; 66 hsl[1] = s; 67 hsl[2] = l; 68 } 69 70 static int HSLtoRGB (float[] hsl) { 71 final float h = hsl[0]; 72 final float s = hsl[1]; 73 final float l = hsl[2]; 74 75 final float c = (1f - Math.abs(2 * l - 1f)) * s; 76 final float m = l - 0.5f * c; 77 final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f)); 78 79 final int hueSegment = (int) h / 60; 80 81 int r = 0, g = 0, b = 0; 82 83 switch (hueSegment) { 84 case 0: 85 r = Math.round(255 * (c + m)); 86 g = Math.round(255 * (x + m)); 87 b = Math.round(255 * m); 88 break; 89 case 1: 90 r = Math.round(255 * (x + m)); 91 g = Math.round(255 * (c + m)); 92 b = Math.round(255 * m); 93 break; 94 case 2: 95 r = Math.round(255 * m); 96 g = Math.round(255 * (c + m)); 97 b = Math.round(255 * (x + m)); 98 break; 99 case 3: 100 r = Math.round(255 * m); 101 g = Math.round(255 * (x + m)); 102 b = Math.round(255 * (c + m)); 103 break; 104 case 4: 105 r = Math.round(255 * (x + m)); 106 g = Math.round(255 * m); 107 b = Math.round(255 * (c + m)); 108 break; 109 case 5: 110 case 6: 111 r = Math.round(255 * (c + m)); 112 g = Math.round(255 * m); 113 b = Math.round(255 * (x + m)); 114 break; 115 } 116 117 r = Math.max(0, Math.min(255, r)); 118 g = Math.max(0, Math.min(255, g)); 119 b = Math.max(0, Math.min(255, b)); 120 121 return Color.rgb(r, g, b); 122 } 123 124} 125