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