1 /* 2 * Copyright (C) 2018 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.settingslib.display; 18 19 import android.util.MathUtils; 20 21 public class BrightnessUtils { 22 23 public static final int GAMMA_SPACE_MIN = 0; 24 public static final int GAMMA_SPACE_MAX = 65535; 25 26 // Hybrid Log Gamma constant values 27 private static final float R = 0.5f; 28 private static final float A = 0.17883277f; 29 private static final float B = 0.28466892f; 30 private static final float C = 0.55991073f; 31 32 /** 33 * A function for converting from the gamma space that the slider works in to the 34 * linear space that the setting works in. 35 * 36 * The gamma space effectively provides us a way to make linear changes to the slider that 37 * result in linear changes in perception. If we made changes to the slider in the linear space 38 * then we'd see an approximately logarithmic change in perception (c.f. Fechner's Law). 39 * 40 * Internally, this implements the Hybrid Log Gamma electro-optical transfer function, which is 41 * a slight improvement to the typical gamma transfer function for displays whose max 42 * brightness exceeds the 120 nit reference point, but doesn't set a specific reference 43 * brightness like the PQ function does. 44 * 45 * Note that this transfer function is only valid if the display's backlight value is a linear 46 * control. If it's calibrated to be something non-linear, then a different transfer function 47 * should be used. 48 * 49 * @param val The slider value. 50 * @param min The minimum acceptable value for the setting. 51 * @param max The maximum acceptable value for the setting. 52 * @return The corresponding setting value. 53 */ convertGammaToLinear(int val, int min, int max)54 public static final int convertGammaToLinear(int val, int min, int max) { 55 final float normalizedVal = MathUtils.norm(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, val); 56 final float ret; 57 if (normalizedVal <= R) { 58 ret = MathUtils.sq(normalizedVal / R); 59 } else { 60 ret = MathUtils.exp((normalizedVal - C) / A) + B; 61 } 62 63 // HLG is normalized to the range [0, 12], so we need to re-normalize to the range [0, 1] 64 // in order to derive the correct setting value. 65 return Math.round(MathUtils.lerp(min, max, ret / 12)); 66 } 67 68 /** 69 * Version of {@link #convertGammaToLinear} that takes and returns float values. 70 * TODO(flc): refactor Android Auto to use float version 71 * 72 * @param val The slider value. 73 * @param min The minimum acceptable value for the setting. 74 * @param max The maximum acceptable value for the setting. 75 * @return The corresponding setting value. 76 */ convertGammaToLinearFloat(int val, float min, float max)77 public static final float convertGammaToLinearFloat(int val, float min, float max) { 78 final float normalizedVal = MathUtils.norm(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, val); 79 final float ret; 80 if (normalizedVal <= R) { 81 ret = MathUtils.sq(normalizedVal / R); 82 } else { 83 ret = MathUtils.exp((normalizedVal - C) / A) + B; 84 } 85 86 // HLG is normalized to the range [0, 12], ensure that value is within that range, 87 // it shouldn't be out of bounds. 88 final float normalizedRet = MathUtils.constrain(ret, 0, 12); 89 90 // Re-normalize to the range [0, 1] 91 // in order to derive the correct setting value. 92 return MathUtils.lerp(min, max, normalizedRet / 12); 93 } 94 95 /** 96 * A function for converting from the linear space that the setting works in to the 97 * gamma space that the slider works in. 98 * 99 * The gamma space effectively provides us a way to make linear changes to the slider that 100 * result in linear changes in perception. If we made changes to the slider in the linear space 101 * then we'd see an approximately logarithmic change in perception (c.f. Fechner's Law). 102 * 103 * Internally, this implements the Hybrid Log Gamma opto-electronic transfer function, which is 104 * a slight improvement to the typical gamma transfer function for displays whose max 105 * brightness exceeds the 120 nit reference point, but doesn't set a specific reference 106 * brightness like the PQ function does. 107 * 108 * Note that this transfer function is only valid if the display's backlight value is a linear 109 * control. If it's calibrated to be something non-linear, then a different transfer function 110 * should be used. 111 * 112 * @param val The brightness setting value. 113 * @param min The minimum acceptable value for the setting. 114 * @param max The maximum acceptable value for the setting. 115 * @return The corresponding slider value 116 */ convertLinearToGamma(int val, int min, int max)117 public static final int convertLinearToGamma(int val, int min, int max) { 118 return convertLinearToGammaFloat((float) val, (float) min, (float) max); 119 } 120 121 /** 122 * Version of {@link #convertLinearToGamma} that takes float values. 123 * TODO: brightnessfloat merge with above method(?) 124 * @param val The brightness setting value. 125 * @param min The minimum acceptable value for the setting. 126 * @param max The maximum acceptable value for the setting. 127 * @return The corresponding slider value 128 */ convertLinearToGammaFloat(float val, float min, float max)129 public static final int convertLinearToGammaFloat(float val, float min, float max) { 130 // For some reason, HLG normalizes to the range [0, 12] rather than [0, 1] 131 final float normalizedVal = MathUtils.norm(min, max, val) * 12; 132 final float ret; 133 if (normalizedVal <= 1f) { 134 ret = MathUtils.sqrt(normalizedVal) * R; 135 } else { 136 ret = A * MathUtils.log(normalizedVal - B) + C; 137 } 138 139 return Math.round(MathUtils.lerp(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, ret)); 140 } 141 } 142