Convert BrightnessUtils to use brightness as float
convertGammaToLinear needed to be constrained to 0-12, as sometimes ret variable was 12.000001. The first parameter for gammaToLinear_minValue_shouldReturnMin has been changed - initally it used 1 (MIN_INT) but it should have been using 0 (GAMMA_SPACE_MIN). Since the function rounded the value at the end, the result ended up the same. The original functions cannot be removed yet since Car uses these methods. Bug: 150671605 Test: m -j99 RunSettingsLibRoboTests ROBOTEST_FILTER="com.android.settingslib.display.BrightnessUtilsTest" Change-Id: I4bba2ca0785f94597590c8f4bb3a7a8a834d6c83
This commit is contained in:
@@ -67,7 +67,7 @@ public class BrightnessUtils {
|
||||
|
||||
/**
|
||||
* Version of {@link #convertGammaToLinear} that takes and returns float values.
|
||||
* TODO: brightnessfloat Merge with above method later.
|
||||
* TODO(flc): refactor Android Auto to use float version
|
||||
*
|
||||
* @param val The slider value.
|
||||
* @param min The minimum acceptable value for the setting.
|
||||
@@ -83,9 +83,13 @@ public class BrightnessUtils {
|
||||
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]
|
||||
// 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, ret / 12);
|
||||
return MathUtils.lerp(min, max, normalizedRet / 12);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,16 +115,7 @@ public class BrightnessUtils {
|
||||
* @return The corresponding slider value
|
||||
*/
|
||||
public static final int convertLinearToGamma(int val, int min, int 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));
|
||||
return convertLinearToGammaFloat((float) val, (float) min, (float) max);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.android.settingslib.display;
|
||||
|
||||
import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX;
|
||||
import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MIN;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
@@ -27,26 +28,40 @@ import org.robolectric.RobolectricTestRunner;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BrightnessUtilsTest {
|
||||
|
||||
private static final int MIN = 1;
|
||||
private static final int MAX = 255;
|
||||
private static final int MIN_INT = 1;
|
||||
private static final int MAX_INT = 255;
|
||||
private static final float MIN_FLOAT = 0.0f;
|
||||
private static final float MAX_FLOAT = 1.0f;
|
||||
|
||||
@Test
|
||||
public void linearToGamma_minValue_shouldReturn0() {
|
||||
assertThat(BrightnessUtils.convertLinearToGamma(MIN, MIN, MAX)).isEqualTo(0);
|
||||
public void linearToGamma_minValue_shouldReturnMin() {
|
||||
assertThat(BrightnessUtils.convertLinearToGamma(MIN_INT, MIN_INT, MAX_INT))
|
||||
.isEqualTo(GAMMA_SPACE_MIN);
|
||||
assertThat(BrightnessUtils.convertLinearToGammaFloat(MIN_FLOAT, MIN_FLOAT, MAX_FLOAT))
|
||||
.isEqualTo(GAMMA_SPACE_MIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void linearToGamma_maxValue_shouldReturnGammaSpaceMax() {
|
||||
assertThat(BrightnessUtils.convertLinearToGamma(MAX, MIN, MAX)).isEqualTo(GAMMA_SPACE_MAX);
|
||||
assertThat(BrightnessUtils.convertLinearToGamma(MAX_INT, MIN_INT, MAX_INT))
|
||||
.isEqualTo(GAMMA_SPACE_MAX);
|
||||
assertThat(BrightnessUtils.convertLinearToGammaFloat(MAX_FLOAT, MIN_FLOAT, MAX_FLOAT))
|
||||
.isEqualTo(GAMMA_SPACE_MAX);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gammaToLinear_minValue_shouldReturnMin() {
|
||||
assertThat(BrightnessUtils.convertGammaToLinear(MIN, MIN, MAX)).isEqualTo(MIN);
|
||||
assertThat(BrightnessUtils.convertGammaToLinear(GAMMA_SPACE_MIN, MIN_INT, MAX_INT))
|
||||
.isEqualTo(MIN_INT);
|
||||
assertThat(BrightnessUtils.convertGammaToLinearFloat(GAMMA_SPACE_MIN, MIN_FLOAT, MAX_FLOAT))
|
||||
.isEqualTo(MIN_FLOAT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gammaToLinear_gammaSpaceValue_shouldReturnMax() {
|
||||
assertThat(BrightnessUtils.convertGammaToLinear(GAMMA_SPACE_MAX, MIN, MAX)).isEqualTo(MAX);
|
||||
assertThat(BrightnessUtils.convertGammaToLinear(GAMMA_SPACE_MAX, MIN_INT, MAX_INT))
|
||||
.isEqualTo(MAX_INT);
|
||||
assertThat(BrightnessUtils.convertGammaToLinearFloat(GAMMA_SPACE_MAX, MIN_FLOAT, MAX_FLOAT))
|
||||
.isEqualTo(MAX_FLOAT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user