android palette
所在包目录
android-sdk-windows\extras\android\compatibility\v7\palette\libs
private void extract(Bitmap bitmap) { // 提取颜色 // Palette palette = Palette.generate(bitmap); Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { // 提取 // 有活力的颜色 Palette.Swatch vibrant = palette.getVibrantSwatch(); // 有活力的暗色 Palette.Swatch darkVibrant = palette.getDarkVibrantSwatch(); // 有活力的亮色 Palette.Swatch lightVibrant = palette.getLightVibrantSwatch(); // 柔和的颜色 Palette.Swatch muted = palette.getMutedSwatch(); // 柔和的暗色 Palette.Swatch darkMuted = palette.getDarkMutedSwatch(); // 柔和的亮色 Palette.Swatch lightMuted = palette.getLightMutedSwatch(); mTextView.setText("有活力的颜色"); if (vibrant != null) { ll.setBackgroundColor(vibrant.getRgb()); mTextView.setBackgroundColor(vibrant.getRgb()); mTextView.setTextColor(vibrant.getTitleTextColor()); } }
以下的分析 转载了, 再慢慢看有时间
http://blog.csdn.net/yebo0505/article/details/43234113
第一步,将图片缩小,再整个过程中,可以降低计算量和减少内存的使用,跟不缩小也能达到一样的效果
[java] view plaincopy在CODE上查看代码片派生到我的代码片
/**
* Scale the bitmap down so that it’s smallest dimension is
* {@value #CALCULATE_BITMAP_MIN_DIMENSION}px. If {@code bitmap} is smaller than this, than it
* is returned.
*/
private static Bitmap scaleBitmapDown(Bitmap bitmap) {
final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());
if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) { // If the bitmap is small enough already, just return it return bitmap; } final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension; return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio), Math.round(bitmap.getHeight() * scaleRatio), false); }
第二步,将缩小后的图片数据,放在一个int 数组里
[java] view plaincopy在CODE上查看代码片派生到我的代码片
/**
* Factory-method to generate a {@link ColorCutQuantizer} from a {@link Bitmap} object.
*
* @param bitmap Bitmap to extract the pixel data from
* @param maxColors The maximum number of colors that should be in the result palette.
*/
static ColorCutQuantizer fromBitmap(Bitmap bitmap, int maxColors) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); return new ColorCutQuantizer(new ColorHistogram(pixels), maxColors);
}
第三步,将这个int 数组由小到大排序,就相当于,将一张图片一样的颜色堆在一起,然后计算共有多少种颜色,每种颜色它是多大,这些是在一个叫ColorHistogram(颜色直方图)类里面计算的,用颜色直方图来说,就是共有多少柱颜色,每柱颜色有多高
[java] view plaincopy在CODE上查看代码片派生到我的代码片
/**
* Class which provides a histogram for RGB values.
*/
final class ColorHistogram {
private final int[] mColors; private final int[] mColorCounts; private final int mNumberColors; /** * A new {@link ColorHistogram} instance. * * @param pixels array of image contents */ ColorHistogram(final int[] pixels) { // Sort the pixels to enable counting below Arrays.sort(pixels); // Count number of distinct colors mNumberColors = countDistinctColors(pixels); // Create arrays mColors = new int[mNumberColors]; mColorCounts = new int[mNumberColors]; // Finally count the frequency of each color countFrequencies(pixels); } /** * @return 获取共用多少柱不同颜色 number of distinct colors in the image. */ int getNumberOfColors() { return mNumberColors; } /** * @return 获取排好序后的不同颜色的数组 an array containing all of the distinct colors in the image. */ int[] getColors() { return mColors; } /** * @return 获取保存每一柱有多高的数组 an array containing the frequency of a distinct colors within the image. */ int[] getColorCounts() { return mColorCounts; } //计算共用多少柱不同颜色 private static int countDistinctColors(final int[] pixels) { if (pixels.length < 2) { // If we have less than 2 pixels we can stop here return pixels.length; } // If we have at least 2 pixels, we have a minimum of 1 color... int colorCount = 1; int currentColor = pixels[0]; // Now iterate from the second pixel to the end, counting distinct colors for (int i = 1; i < pixels.length; i++) { // If we encounter a new color, increase the population if (pixels[i] != currentColor) { currentColor = pixels[i]; colorCount++; } } return colorCount; } //计算每一柱有多高 private void countFrequencies(final int[] pixels) { if (pixels.length == 0) { return; } int currentColorIndex = 0; int currentColor = pixels[0]; mColors[currentColorIndex] = currentColor; mColorCounts[currentColorIndex] = 1; Log.i("pixels.length",""+ pixels.length); if (pixels.length == 1) { // If we only have one pixel, we can stop here return; } // Now iterate from the second pixel to the end, population distinct colors for (int i = 1; i < pixels.length; i++) { if (pixels[i] == currentColor) { // We've hit the same color as before, increase population mColorCounts[currentColorIndex]++; } else { // We've hit a new color, increase index currentColor = pixels[i]; currentColorIndex++; mColors[currentColorIndex] = currentColor; mColorCounts[currentColorIndex] = 1; } } }
}
第四步,将各种颜色,根据RGB转HSL算法,得出对应的HSL(H: Hue 色相,S:Saturation 饱和度L Lightness 明度),根据特定的条件,比如是明度L是否接近白色,黑色,还有一个判断叫isNearRedILine,解释是@return true if the color lies close to the red side of the I line(接近红色私密区域附近?).,然后根据这三个条件,过滤掉这些颜色,什么是HSL和RGB转HSL算法可以查看下百科,比较有详细说明
[java] view plaincopy在CODE上查看代码片派生到我的代码片
/**
* Private constructor.
*
* @param colorHistogram histogram representing an image’s pixel data
* @param maxColors The maximum number of colors that should be in the result palette.
*/
private ColorCutQuantizer(ColorHistogram colorHistogram, int maxColors) {
final int rawColorCount = colorHistogram.getNumberOfColors();
final int[] rawColors = colorHistogram.getColors();//颜色数组
final int[] rawColorCounts = colorHistogram.getColorCounts();//对应rawColors每一个颜色数组的大小
// First, lets pack the populations into a SparseIntArray so that they can be easily // retrieved without knowing a color's index mColorPopulations = new SparseIntArray(rawColorCount); for (int i = 0; i < rawColors.length; i++) { mColorPopulations.append(rawColors[i], rawColorCounts[i]); } // Now go through all of the colors and keep those which we do not want to ignore mColors = new int[rawColorCount]; int validColorCount = 0; for (int color : rawColors) { if (!shouldIgnoreColor(color)) { mColors[validColorCount++] = color; } } Log.d("mColors length", ""+mColors.length); if (validColorCount <= maxColors) { // The image has fewer colors than the maximum requested, so just return the colors mQuantizedColors = new ArrayList<Swatch>(); for (final int color : mColors) { mQuantizedColors.add(new Swatch(color, mColorPopulations.get(color))); } } else { // We need use quantization to reduce the number of colors mQuantizedColors = quantizePixels(validColorCount - 1, maxColors); }
}
[java] view plaincopy在CODE上查看代码片派生到我的代码片
[java] view plaincopy在CODE上查看代码片派生到我的代码片
这里截了张图看看
[java] view plaincopy在CODE上查看代码片派生到我的代码片
第五步,根据是各种亮度,饱和度的取值范围,比如有活力的暗色,有活力的亮色,柔和的颜色,柔和的暗色,柔和的亮色,找到对应的颜色
[java] view plaincopy在CODE上查看代码片派生到我的代码片
private Swatch findColor(float targetLuma, float minLuma, float maxLuma,
float targetSaturation, float minSaturation, float maxSaturation) {
Swatch max = null;
float maxValue = 0f;
for (Swatch swatch : mSwatches) { final float sat = swatch.getHsl()[1]; final float luma = swatch.getHsl()[2]; if (sat >= minSaturation && sat <= maxSaturation && luma >= minLuma && luma <= maxLuma && !isAlreadySelected(swatch)) { float thisValue = createComparisonValue(sat, targetSaturation, luma, targetLuma, swatch.getPopulation(), mHighestPopulation); if (max == null || thisValue > maxValue) { max = swatch; maxValue = thisValue; } } } return max;
}
更多相关文章
- Android使用selector改变文字的颜色
- Android常用颜色表
- Android 监听WiFi的开关状态实现代码
- Android 代码片段---从相册或相机获取图片保存并处理
- android如何用代码实现界面ui
- android中如何在代码中直接设置View的layout_weight属性
- Android震动代码解读
- android 学习笔记有用代码片段(2)
- 2.Retrofit上传list数组给后端