Fix error computing tertiary hue

After adding 60 degrees to the hue, the result needs to be normalized to
0 to 360. Code downstream of Shades.of does not perform the
normalization, so the resulting color will be the result of undefined
behavior.

Test: `atest ColorSchemeTest`, newly added testTertiaryHueWrapsProperly
passes.
Bug: 204463343

Change-Id: I93168c98ba64fb865888469220571dc6d437105d
This commit is contained in:
James O'Leary
2021-11-09 11:08:48 -05:00
parent 62924f1213
commit 62918fe10b
2 changed files with 12 additions and 1 deletions

View File

@@ -85,9 +85,10 @@ public class ColorScheme(@ColorInt seed: Int, val darkTheme: Boolean) {
val camSeed = Cam.fromInt(seedArgb)
val hue = camSeed.hue
val chroma = camSeed.chroma.coerceAtLeast(ACCENT1_CHROMA)
val tertiaryHue = wrapDegrees((hue + ACCENT3_HUE_SHIFT).toInt())
accent1 = Shades.of(hue, chroma).toList()
accent2 = Shades.of(hue, ACCENT2_CHROMA).toList()
accent3 = Shades.of(hue + ACCENT3_HUE_SHIFT, ACCENT3_CHROMA).toList()
accent3 = Shades.of(tertiaryHue.toFloat(), ACCENT3_CHROMA).toList()
neutral1 = Shades.of(hue, NEUTRAL1_CHROMA).toList()
neutral2 = Shades.of(hue, NEUTRAL2_CHROMA).toList()
}

View File

@@ -22,6 +22,7 @@ import android.testing.AndroidTestingRunner;
import androidx.test.filters.SmallTest;
import com.android.internal.graphics.cam.Cam;
import com.android.systemui.SysuiTestCase;
import org.junit.Assert;
@@ -90,4 +91,13 @@ public class ColorSchemeTest extends SysuiTestCase {
List<Integer> rankedSeedColors = ColorScheme.getSeedColors(wallpaperColors);
Assert.assertEquals(rankedSeedColors, List.of(0xffaec00a, 0xffbe0000, 0xffcc040f));
}
@Test
public void testTertiaryHueWrapsProperly() {
int colorInt = 0xffB3588A; // H350 C50 T50
ColorScheme colorScheme = new ColorScheme(colorInt, false /* darkTheme */);
int tertiaryMid = colorScheme.getAccent3().get(colorScheme.getAccent3().size() / 2);
Cam cam = Cam.fromInt(tertiaryMid);
Assert.assertEquals(cam.getHue(), 50.0, 10.0);
}
}