• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
17 package com.android.internal.graphics.cam;
18 
19 
20 import android.annotation.NonNull;
21 import android.graphics.Color;
22 
23 import com.android.internal.graphics.ColorUtils;
24 
25 /**
26  * Collection of methods for transforming between color spaces.
27  *
28  * <p>Methods are named $xFrom$Y. For example, lstarFromInt() returns L* from an ARGB integer.
29  *
30  * <p>These methods, generally, convert colors between the L*a*b*, XYZ, and sRGB spaces.
31  *
32  * <p>L*a*b* is a perceptually accurate color space. This is particularly important in the L*
33  * dimension: it measures luminance and unlike lightness measures traditionally used in UI work via
34  * RGB or HSL, this luminance transitions smoothly, permitting creation of pleasing shades of a
35  * color, and more pleasing transitions between colors.
36  *
37  * <p>XYZ is commonly used as an intermediate color space for converting between one color space to
38  * another. For example, to convert RGB to L*a*b*, first RGB is converted to XYZ, then XYZ is
39  * convered to L*a*b*.
40  *
41  * <p>sRGB is a "specification originated from work in 1990s through cooperation by Hewlett-Packard
42  * and Microsoft, and it was designed to be a standard definition of RGB for the internet, which it
43  * indeed became...The standard is based on a sampling of computer monitors at the time...The whole
44  * idea of sRGB is that if everyone assumed that RGB meant the same thing, then the results would be
45  * consistent, and reasonably good. It worked." - Fairchild, Color Models and Systems: Handbook of
46  * Color Psychology, 2015
47  */
48 public final class CamUtils {
CamUtils()49     private CamUtils() {
50     }
51 
52     // Transforms XYZ color space coordinates to 'cone'/'RGB' responses in CAM16.
53     static final float[][] XYZ_TO_CAM16RGB = {
54             {0.401288f, 0.650173f, -0.051461f},
55             {-0.250268f, 1.204414f, 0.045854f},
56             {-0.002079f, 0.048952f, 0.953127f}
57     };
58 
59     // Transforms 'cone'/'RGB' responses in CAM16 to XYZ color space coordinates.
60     static final float[][] CAM16RGB_TO_XYZ = {
61             {1.86206786f, -1.01125463f, 0.14918677f},
62             {0.38752654f, 0.62144744f, -0.00897398f},
63             {-0.01584150f, -0.03412294f, 1.04996444f}
64     };
65 
66     // Need this, XYZ coordinates in internal ColorUtils are private
67 
68     // sRGB specification has D65 whitepoint - Stokes, Anderson, Chandrasekar, Motta - A Standard
69     // Default Color Space for the Internet: sRGB, 1996
70     static final float[] WHITE_POINT_D65 = {95.047f, 100.0f, 108.883f};
71 
72     // This is a more precise sRGB to XYZ transformation matrix than traditionally
73     // used. It was derived using Schlomer's technique of transforming the xyY
74     // primaries to XYZ, then applying a correction to ensure mapping from sRGB
75     // 1, 1, 1 to the reference white point, D65.
76     static final float[][] SRGB_TO_XYZ = {
77             {0.41233895f, 0.35762064f, 0.18051042f},
78             {0.2126f, 0.7152f, 0.0722f},
79             {0.01932141f, 0.11916382f, 0.95034478f}
80     };
81 
intFromLstar(float lstar)82     static int intFromLstar(float lstar) {
83         if (lstar < 1) {
84             return 0xff000000;
85         } else if (lstar > 99) {
86             return 0xffffffff;
87         }
88 
89         // XYZ to LAB conversion routine, assume a and b are 0.
90         float fy = (lstar + 16.0f) / 116.0f;
91 
92         // fz = fx = fy because a and b are 0
93         float fz = fy;
94         float fx = fy;
95 
96         float kappa = 24389f / 27f;
97         float epsilon = 216f / 24389f;
98         boolean lExceedsEpsilonKappa = (lstar > 8.0f);
99         float yT = lExceedsEpsilonKappa ? fy * fy * fy : lstar / kappa;
100         boolean cubeExceedEpsilon = (fy * fy * fy) > epsilon;
101         float xT = cubeExceedEpsilon ? fx * fx * fx : (116f * fx - 16f) / kappa;
102         float zT = cubeExceedEpsilon ? fz * fz * fz : (116f * fx - 16f) / kappa;
103 
104         return ColorUtils.XYZToColor(xT * CamUtils.WHITE_POINT_D65[0],
105                 yT * CamUtils.WHITE_POINT_D65[1], zT * CamUtils.WHITE_POINT_D65[2]);
106     }
107 
108     /** Returns L* from L*a*b*, perceptual luminance, from an ARGB integer (ColorInt). */
lstarFromInt(int argb)109     public static float lstarFromInt(int argb) {
110         return lstarFromY(yFromInt(argb));
111     }
112 
lstarFromY(float y)113     static float lstarFromY(float y) {
114         y = y / 100.0f;
115         final float e = 216.f / 24389.f;
116         float yIntermediate;
117         if (y <= e) {
118             return ((24389.f / 27.f) * y);
119         } else {
120             yIntermediate = (float) Math.cbrt(y);
121         }
122         return 116.f * yIntermediate - 16.f;
123     }
124 
yFromInt(int argb)125     static float yFromInt(int argb) {
126         final float r = linearized(Color.red(argb));
127         final float g = linearized(Color.green(argb));
128         final float b = linearized(Color.blue(argb));
129         float[][] matrix = SRGB_TO_XYZ;
130         float y = (r * matrix[1][0]) + (g * matrix[1][1]) + (b * matrix[1][2]);
131         return y;
132     }
133 
134     @NonNull
xyzFromInt(int argb)135     static float[] xyzFromInt(int argb) {
136         final float r = linearized(Color.red(argb));
137         final float g = linearized(Color.green(argb));
138         final float b = linearized(Color.blue(argb));
139 
140         float[][] matrix = SRGB_TO_XYZ;
141         float x = (r * matrix[0][0]) + (g * matrix[0][1]) + (b * matrix[0][2]);
142         float y = (r * matrix[1][0]) + (g * matrix[1][1]) + (b * matrix[1][2]);
143         float z = (r * matrix[2][0]) + (g * matrix[2][1]) + (b * matrix[2][2]);
144         return new float[]{x, y, z};
145     }
146 
yFromLstar(float lstar)147     static float yFromLstar(float lstar) {
148         float ke = 8.0f;
149         if (lstar > ke) {
150             return (float) Math.pow(((lstar + 16.0) / 116.0), 3) * 100f;
151         } else {
152             return lstar / (24389f / 27f) * 100f;
153         }
154     }
155 
linearized(int rgbComponent)156     static float linearized(int rgbComponent) {
157         float normalized = (float) rgbComponent / 255.0f;
158 
159         if (normalized <= 0.04045f) {
160             return (normalized / 12.92f) * 100.0f;
161         } else {
162             return (float) Math.pow(((normalized + 0.055f) / 1.055f), 2.4f) * 100.0f;
163         }
164     }
165 }
166