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 android.graphics.cts.utils; 18 19 /** 20 * Copied from frameworks/base/core to support SystemPalette CTS test 21 * 22 * The frame, or viewing conditions, where a color was seen. Used, along with a color, to create a 23 * color appearance model representing the color. 24 * 25 * <p>To convert a traditional color to a color appearance model, it requires knowing what 26 * conditions the color was observed in. Our perception of color depends on, for example, the tone 27 * of the light illuminating the color, how bright that light was, etc. 28 * 29 * <p>This class is modelled separately from the color appearance model itself because there are a 30 * number of calculations during the color => CAM conversion process that depend only on the viewing 31 * conditions. Caching those calculations in a Frame instance saves a significant amount of time. 32 */ 33 public final class Frame { 34 // Standard viewing conditions assumed in RGB specification - Stokes, Anderson, Chandrasekar, 35 // Motta - A Standard Default Color Space for the Internet: sRGB, 1996. 36 // 37 // White point = D65 38 // Luminance of adapting field: 200 / Pi / 5, units are cd/m^2. 39 // sRGB ambient illuminance = 64 lux (per sRGB spec). However, the spec notes this is 40 // artificially low and based on monitors in 1990s. Use 200, the sRGB spec says this is the 41 // real average, and a survey of lux values on Wikipedia confirms this is a comfortable 42 // default: somewhere between a very dark overcast day and office lighting. 43 // Per CAM16 introduction paper (Li et al, 2017) Ew = pi * lw, and La = lw * Yb/Yw 44 // Ew = ambient environment luminance, in lux. 45 // Yb/Yw is taken to be midgray, ~20% relative luminance (XYZ Y 18.4, CIELAB L* 50). 46 // Therefore La = (Ew / pi) * .184 47 // La = 200 / pi * .184 48 // Image surround to 10 degrees = ~20% relative luminance = CIELAB L* 50 49 // 50 // Not from sRGB standard: 51 // Surround = average, 2.0. 52 // Discounting illuminant = false, doesn't occur for self-luminous displays 53 public static final Frame DEFAULT = 54 Frame.make( 55 CamUtils.WHITE_POINT_D65, 56 (float) (200.0f / Math.PI * CamUtils.yFromLstar(50.0f) / 100.f), 50.0f, 2.0f, 57 false); 58 59 private final float mAw; 60 private final float mNbb; 61 private final float mNcb; 62 private final float mC; 63 private final float mNc; 64 private final float mN; 65 private final float[] mRgbD; 66 private final float mFl; 67 private final float mFlRoot; 68 private final float mZ; 69 getAw()70 float getAw() { 71 return mAw; 72 } 73 getN()74 float getN() { 75 return mN; 76 } 77 getNbb()78 float getNbb() { 79 return mNbb; 80 } 81 getNcb()82 float getNcb() { 83 return mNcb; 84 } 85 getC()86 float getC() { 87 return mC; 88 } 89 getNc()90 float getNc() { 91 return mNc; 92 } 93 getRgbD()94 float[] getRgbD() { 95 return mRgbD; 96 } 97 getFl()98 float getFl() { 99 return mFl; 100 } 101 getFlRoot()102 float getFlRoot() { 103 return mFlRoot; 104 } 105 getZ()106 float getZ() { 107 return mZ; 108 } 109 Frame(float n, float aw, float nbb, float ncb, float c, float nc, float[] rgbD, float fl, float fLRoot, float z)110 private Frame(float n, float aw, float nbb, float ncb, float c, float nc, float[] rgbD, 111 float fl, float fLRoot, float z) { 112 mN = n; 113 mAw = aw; 114 mNbb = nbb; 115 mNcb = ncb; 116 mC = c; 117 mNc = nc; 118 mRgbD = rgbD; 119 mFl = fl; 120 mFlRoot = fLRoot; 121 mZ = z; 122 } 123 124 /** Create a custom frame. */ make(float[] whitepoint, float adaptingLuminance, float backgroundLstar, float surround, boolean discountingIlluminant)125 public static Frame make(float[] whitepoint, float adaptingLuminance, 126 float backgroundLstar, float surround, boolean discountingIlluminant) { 127 // Transform white point XYZ to 'cone'/'rgb' responses 128 float[][] matrix = CamUtils.XYZ_TO_CAM16RGB; 129 float[] xyz = whitepoint; 130 float rW = (xyz[0] * matrix[0][0]) + (xyz[1] * matrix[0][1]) + (xyz[2] * matrix[0][2]); 131 float gW = (xyz[0] * matrix[1][0]) + (xyz[1] * matrix[1][1]) + (xyz[2] * matrix[1][2]); 132 float bW = (xyz[0] * matrix[2][0]) + (xyz[1] * matrix[2][1]) + (xyz[2] * matrix[2][2]); 133 134 // Scale input surround, domain (0, 2), to CAM16 surround, domain (0.8, 1.0) 135 float f = 0.8f + (surround / 10.0f); 136 // "Exponential non-linearity" 137 float c = (f >= 0.9) ? MathUtils.lerp(0.59f, 0.69f, ((f - 0.9f) * 10.0f)) : MathUtils.lerp( 138 0.525f, 0.59f, ((f - 0.8f) * 10.0f)); 139 // Calculate degree of adaptation to illuminant 140 float d = discountingIlluminant ? 1.0f : f * (1.0f - ((1.0f / 3.6f) * (float) Math.exp( 141 (-adaptingLuminance - 42.0f) / 92.0f))); 142 // Per Li et al, if D is greater than 1 or less than 0, set it to 1 or 0. 143 d = (d > 1.0) ? 1.0f : (d < 0.0) ? 0.0f : d; 144 // Chromatic induction factor 145 float nc = f; 146 147 // Cone responses to the whitepoint, adjusted for illuminant discounting. 148 // 149 // Why use 100.0 instead of the white point's relative luminance? 150 // 151 // Some papers and implementations, for both CAM02 and CAM16, use the Y 152 // value of the reference white instead of 100. Fairchild's Color Appearance 153 // Models (3rd edition) notes that this is in error: it was included in the 154 // CIE 2004a report on CIECAM02, but, later parts of the conversion process 155 // account for scaling of appearance relative to the white point relative 156 // luminance. This part should simply use 100 as luminance. 157 float[] rgbD = new float[]{d * (100.0f / rW) + 1.0f - d, d * (100.0f / gW) + 1.0f - d, 158 d * (100.0f / bW) + 1.0f - d, }; 159 // Luminance-level adaptation factor 160 float k = 1.0f / (5.0f * adaptingLuminance + 1.0f); 161 float k4 = k * k * k * k; 162 float k4F = 1.0f - k4; 163 float fl = (k4 * adaptingLuminance) + (0.1f * k4F * k4F * (float) Math.cbrt( 164 5.0 * adaptingLuminance)); 165 166 // Intermediate factor, ratio of background relative luminance to white relative luminance 167 float n = CamUtils.yFromLstar(backgroundLstar) / whitepoint[1]; 168 169 // Base exponential nonlinearity 170 // note Schlomer 2018 has a typo and uses 1.58, the correct factor is 1.48 171 float z = 1.48f + (float) Math.sqrt(n); 172 173 // Luminance-level induction factors 174 float nbb = 0.725f / (float) Math.pow(n, 0.2); 175 float ncb = nbb; 176 177 // Discounted cone responses to the white point, adjusted for post-chromatic 178 // adaptation perceptual nonlinearities. 179 float[] rgbAFactors = new float[]{(float) Math.pow(fl * rgbD[0] * rW / 100.0, 0.42), 180 (float) Math.pow(fl * rgbD[1] * gW / 100.0, 0.42), (float) Math.pow( 181 fl * rgbD[2] * bW / 100.0, 0.42)}; 182 183 float[] rgbA = new float[]{(400.0f * rgbAFactors[0]) / (rgbAFactors[0] + 27.13f), 184 (400.0f * rgbAFactors[1]) / (rgbAFactors[1] + 27.13f), 185 (400.0f * rgbAFactors[2]) / (rgbAFactors[2] + 27.13f), }; 186 187 float aw = ((2.0f * rgbA[0]) + rgbA[1] + (0.05f * rgbA[2])) * nbb; 188 189 return new Frame(n, aw, nbb, ncb, c, nc, rgbD, fl, (float) Math.pow(fl, 0.25), z); 190 } 191 } 192