From ef766709093476c3e3415bf7f73d987bba10885f Mon Sep 17 00:00:00 2001 From: Material Design Team Date: Thu, 1 Dec 2022 07:32:49 -0500 Subject: [PATCH] [M3][Color] Interal changes PiperOrigin-RevId: 492161895 --- .../material/color/utilities/QuantizerMap.java | 4 ++-- .../material/color/utilities/QuantizerWsmeans.java | 12 ++++++++---- .../material/color/utilities/QuantizerWu.java | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/java/com/google/android/material/color/utilities/QuantizerMap.java b/lib/java/com/google/android/material/color/utilities/QuantizerMap.java index 665814a3826..f1b13b71184 100644 --- a/lib/java/com/google/android/material/color/utilities/QuantizerMap.java +++ b/lib/java/com/google/android/material/color/utilities/QuantizerMap.java @@ -19,7 +19,7 @@ import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; import androidx.annotation.RestrictTo; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; // TODO(b/254603377): Use copybara to release material color utilities library directly to github. @@ -34,7 +34,7 @@ public final class QuantizerMap implements Quantizer { @Override public QuantizerResult quantize(int[] pixels, int colorCount) { - final HashMap pixelByCount = new HashMap<>(); + final Map pixelByCount = new LinkedHashMap<>(); for (int pixel : pixels) { final Integer currentPixelCount = pixelByCount.get(pixel); final int newPixelCount = currentPixelCount == null ? 1 : currentPixelCount + 1; diff --git a/lib/java/com/google/android/material/color/utilities/QuantizerWsmeans.java b/lib/java/com/google/android/material/color/utilities/QuantizerWsmeans.java index 4c535b76828..152b62c7dd3 100644 --- a/lib/java/com/google/android/material/color/utilities/QuantizerWsmeans.java +++ b/lib/java/com/google/android/material/color/utilities/QuantizerWsmeans.java @@ -21,8 +21,9 @@ import androidx.annotation.RestrictTo; import java.util.Arrays; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Random; // TODO(b/254603377): Use copybara to release material color utilities library directly to github. /** @@ -75,7 +76,10 @@ public int compareTo(Distance other) { */ public static Map quantize( int[] inputPixels, int[] startingClusters, int maxColors) { - Map pixelToCount = new HashMap<>(); + // Uses a seeded random number generator to ensure consistent results. + Random random = new Random(0x42688); + + Map pixelToCount = new LinkedHashMap<>(); double[][] points = new double[inputPixels.length][]; int[] pixels = new int[inputPixels.length]; PointProvider pointProvider = new PointProviderLab(); @@ -121,7 +125,7 @@ public static Map quantize( int[] clusterIndices = new int[pointCount]; for (int i = 0; i < pointCount; i++) { - clusterIndices[i] = (int) Math.floor(Math.random() * clusterCount); + clusterIndices[i] = random.nextInt(clusterCount); } int[][] indexMatrix = new int[clusterCount][]; @@ -215,7 +219,7 @@ public static Map quantize( } } - Map argbToPopulation = new HashMap<>(); + Map argbToPopulation = new LinkedHashMap<>(); for (int i = 0; i < clusterCount; i++) { int count = pixelCountSums[i]; if (count == 0) { diff --git a/lib/java/com/google/android/material/color/utilities/QuantizerWu.java b/lib/java/com/google/android/material/color/utilities/QuantizerWu.java index 312c60e56b9..6f13688f7e5 100644 --- a/lib/java/com/google/android/material/color/utilities/QuantizerWu.java +++ b/lib/java/com/google/android/material/color/utilities/QuantizerWu.java @@ -20,7 +20,7 @@ import androidx.annotation.RestrictTo; import java.util.ArrayList; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -57,7 +57,7 @@ public QuantizerResult quantize(int[] pixels, int colorCount) { createMoments(); CreateBoxesResult createBoxesResult = createBoxes(colorCount); List colors = createResult(createBoxesResult.resultCount); - HashMap resultMap = new HashMap<>(); + Map resultMap = new LinkedHashMap<>(); for (int color : colors) { resultMap.put(color, 0); }