1 /* 2 * Copyright 2017, 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 package com.android.managedprovisioning.preprovisioning.anim; 17 18 import android.graphics.Color; 19 20 public class ColorMatcher { 21 private static final int MAX_VALUE = 0xff; 22 private static final int BUCKET_SIZE = 32; 23 findClosestColor(int targetColor)24 public int findClosestColor(int targetColor) { 25 int r = bucketize(Color.red(targetColor)); 26 int g = bucketize(Color.green(targetColor)); 27 int b = bucketize(Color.blue(targetColor)); 28 29 return Color.argb(MAX_VALUE, r, g, b); 30 } 31 bucketize(int value)32 private int bucketize(int value) { 33 int result = (int) Math.round(((double) value / BUCKET_SIZE)) * BUCKET_SIZE; 34 return Math.min(MAX_VALUE, result); 35 } 36 }