本文原作者: BennuC,原文发布于: BennuCTech

Palette 即调色板这个功能其实很早就发布了,Jetpack 同样将这个功能也纳入其中,想要使用这个功能,需要先依赖库。

implementation 'androidx.palette:palette:1.0.0'

本篇文章就来讲解一下如何使用 Palette 在图片中提取颜色。

创建 Palette

创建 Palette 其实很简单,如下:

var builder = Palette.from(bitmap)
var palette = builder.generate()

这样,我们就通过一个 Bitmap 创建一个 Pallete 对象。

注意: 直接使用 Palette.generate(bitmap) 也可以,但是这个方法已经不推荐使用了,网上很多老文章中依然使用这种方式。建议还是使用 Palette.Builder 这种方式。

generate() 这个函数是同步的,当然考虑图片处理可能比较耗时,Android 同时提供了异步函数。

public AsyncTask<Bitmap, Void, Palette> generate(@NonNull final PaletteAsyncListener listener) {

通过一个 PaletteAsyncListener 来获取 Palette 实例,这个接口如下:

public interface PaletteAsyncListener {/*** Called when the {@link Palette} has been generated. {@code null} will be passed when an* error occurred during generation.*/void onGenerated(@Nullable Palette palette);
}

提取颜色

有了 Palette 实例,通过 Palette 对象的相应函数就可以获取图片中的颜色,而且不只一种颜色,下面一一列举:

  • getDominantColor: 获取图片中的主色调;

  • getMutedColor: 获取图片中柔和的颜色;

  • getDarkMutedColor: 获取图片中柔和的暗色;

  • getLightMutedColor: 获取图片中柔和的亮色;

  • getVibrantColor: 获取图片中有活力的颜色;

  • getDarkVibrantColor: 获取图片中有活力的暗色;

  • getLightVibrantColor: 获取图片中有活力的亮色。

这些函数都需要提供一个默认颜色,如果这个颜色 Swatch 无效则使用这个默认颜色。光这么说不直观,我们来测试一下,代码如下:

var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.a)
var builder = Palette.from(bitmap)
var palette = builder.generate()
color0.setBackgroundColor(palette.getDominantColor(Color.WHITE))
color1.setBackgroundColor(palette.getMutedColor(Color.WHITE))
color2.setBackgroundColor(palette.getDarkMutedColor(Color.WHITE))
color3.setBackgroundColor(palette.getLightMutedColor(Color.WHITE))
color4.setBackgroundColor(palette.getVibrantColor(Color.WHITE))
color5.setBackgroundColor(palette.getDarkVibrantColor(Color.WHITE))
color6.setBackgroundColor(palette.getLightVibrantColor(Color.WHITE))

运行后结果如下:

这样各个颜色的差别就一目了然。除了上面的函数,还可以使用 getColorForTarget 这个函数,如下:

@ColorInt
public int getColorForTarget(@NonNull final Target target, @ColorInt final int defaultColor) {

这个函数需要一个 Target,提供了 6 个静态字段,如下:

/*** A target which has the characteristics of a vibrant color which is light in luminance.
*/
public static final Target LIGHT_VIBRANT;/*** A target which has the characteristics of a vibrant color which is neither light or dark.*/
public static final Target VIBRANT;/*** A target which has the characteristics of a vibrant color which is dark in luminance.*/
public static final Target DARK_VIBRANT;/*** A target which has the characteristics of a muted color which is light in luminance.*/
public static final Target LIGHT_MUTED;/*** A target which has the characteristics of a muted color which is neither light or dark.*/
public static final Target MUTED;/*** A target which has the characteristics of a muted color which is dark in luminance.*/
public static final Target DARK_MUTED;

其实就是对应着上面除了主色调之外的六种颜色。

文字颜色自动适配

在上面的运行结果中可以看到,每个颜色上面的文字都很清楚的显示,而且它们并不是同一种颜色。其实这也是 Palette 提供的功能。

通过下面的函数,我们可以得到各种色调所对应的 Swatch 对象:

  • getDominantSwatch

  • getMutedSwatch

  • getDarkMutedSwatch

  • getLightMutedSwatch

  • getVibrantSwatch

  • getDarkVibrantSwatch

  • getLightVibrantSwatch

注意: 同上面一样,也可以通过 getSwatchForTarget(@NonNull final Target target) 来获取。

Swatch 类提供了以下函数:

  • getPopulation(): 样本中的像素数量;

  • getRgb(): 颜色的 RBG 值;

  • getHsl(): 颜色的 HSL 值;

  • getBodyTextColor(): 能都适配这个 Swatch 的主体文字的颜色值;

  • getTitleTextColor(): 能都适配这个 Swatch 的标题文字的颜色值。

所以我们通过 getBodyTextColor() 和 getTitleTextColor() 可以很容易得到在这个颜色上可以很好实现的标题和主体文本颜色。所以上面的测试代码完整如下:

var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.a)
var builder = Palette.from(bitmap)
var palette = builder.generate()color0.setBackgroundColor(palette.getDominantColor(Color.WHITE))
color0.setTextColor(palette.dominantSwatch?.bodyTextColor ?: Color.WHITE)color1.setBackgroundColor(palette.getMutedColor(Color.WHITE))
color1.setTextColor(palette.mutedSwatch?.bodyTextColor ?: Color.WHITE)color2.setBackgroundColor(palette.getDarkMutedColor(Color.WHITE))
color2.setTextColor(palette.darkMutedSwatch?.bodyTextColor ?: Color.WHITE)color3.setBackgroundColor(palette.getLightMutedColor(Color.WHITE))
color3.setTextColor(palette.lightMutedSwatch?.bodyTextColor ?: Color.WHITE)color4.setBackgroundColor(palette.getVibrantColor(Color.WHITE))
color4.setTextColor(palette.vibrantSwatch?.bodyTextColor ?: Color.WHITE)color5.setBackgroundColor(palette.getDarkVibrantColor(Color.WHITE))
color5.setTextColor(palette.darkVibrantSwatch?.bodyTextColor ?: Color.WHITE)color6.setBackgroundColor(palette.getLightVibrantColor(Color.WHITE))
color6.setTextColor(palette.lightVibrantSwatch?.bodyTextColor ?: Color.WHITE)

这样每个颜色上的文字都可以清晰的显示。

那么这个标题和主体文本颜色有什么差别,他们又是如何得到的?我们来看看源码:

/*** Returns an appropriate color to use for any 'title' text which is displayed over this* {@link Swatch}'s color. This color is guaranteed to have sufficient contrast.*/
@ColorInt
public int getTitleTextColor() {ensureTextColorsGenerated();return mTitleTextColor;
}/*** Returns an appropriate color to use for any 'body' text which is displayed over this* {@link Swatch}'s color. This color is guaranteed to have sufficient contrast.*/
@ColorInt
public int getBodyTextColor() {ensureTextColorsGenerated();return mBodyTextColor;
}

可以看到都会先执行 ensureTextColorsGenerated(),它的源码如下:

private void ensureTextColorsGenerated() {if (!mGeneratedTextColors) {// First check white, as most colors will be darkfinal int lightBodyAlpha = ColorUtils.calculateMinimumAlpha(Color.WHITE, mRgb, MIN_CONTRAST_BODY_TEXT);final int lightTitleAlpha = ColorUtils.calculateMinimumAlpha(Color.WHITE, mRgb, MIN_CONTRAST_TITLE_TEXT);if (lightBodyAlpha != -1 && lightTitleAlpha != -1) {// If we found valid light values, use them and returnmBodyTextColor = ColorUtils.setAlphaComponent(Color.WHITE, lightBodyAlpha);mTitleTextColor = ColorUtils.setAlphaComponent(Color.WHITE, lightTitleAlpha);mGeneratedTextColors = true;return;}final int darkBodyAlpha = ColorUtils.calculateMinimumAlpha(Color.BLACK, mRgb, MIN_CONTRAST_BODY_TEXT);final int darkTitleAlpha = ColorUtils.calculateMinimumAlpha(Color.BLACK, mRgb, MIN_CONTRAST_TITLE_TEXT);if (darkBodyAlpha != -1 && darkTitleAlpha != -1) {// If we found valid dark values, use them and returnmBodyTextColor = ColorUtils.setAlphaComponent(Color.BLACK, darkBodyAlpha);mTitleTextColor = ColorUtils.setAlphaComponent(Color.BLACK, darkTitleAlpha);mGeneratedTextColors = true;return;}// If we reach here then we can not find title and body values which use the same// lightness, we need to use mismatched valuesmBodyTextColor = lightBodyAlpha != -1? ColorUtils.setAlphaComponent(Color.WHITE, lightBodyAlpha): ColorUtils.setAlphaComponent(Color.BLACK, darkBodyAlpha);mTitleTextColor = lightTitleAlpha != -1? ColorUtils.setAlphaComponent(Color.WHITE, lightTitleAlpha): ColorUtils.setAlphaComponent(Color.BLACK, darkTitleAlpha);mGeneratedTextColors = true;}
}

通过代码可以看到,这两种文本颜色实际上要么是白色要么是黑色,只是透明度 Alpha 不同。

这里面有一个关键函数,即 ColorUtils.calculateMinimumAlpha():

public static int calculateMinimumAlpha(@ColorInt int foreground, @ColorInt int background,float minContrastRatio) {if (Color.alpha(background) != 255) {throw new IllegalArgumentException("background can not be translucent: #"+ Integer.toHexString(background));}// First lets check that a fully opaque foreground has sufficient contrastint testForeground = setAlphaComponent(foreground, 255);double testRatio = calculateContrast(testForeground, background);if (testRatio < minContrastRatio) {// Fully opaque foreground does not have sufficient contrast, return errorreturn -1;}// Binary search to find a value with the minimum value which provides sufficient contrastint numIterations = 0;int minAlpha = 0;int maxAlpha = 255;while (numIterations <= MIN_ALPHA_SEARCH_MAX_ITERATIONS &&(maxAlpha - minAlpha) > MIN_ALPHA_SEARCH_PRECISION) {final int testAlpha = (minAlpha + maxAlpha) / 2;testForeground = setAlphaComponent(foreground, testAlpha);testRatio = calculateContrast(testForeground, background);if (testRatio < minContrastRatio) {minAlpha = testAlpha;} else {maxAlpha = testAlpha;}numIterations++;}// Conservatively return the max of the range of possible alphas, which is known to pass.return maxAlpha;
}

它根据背景色和前景色计算前景色最合适的 Alpha。这期间如果小于 minContrastRatio 则返回 -1,说明这个前景色不合适。而标题和主体文本的差别就是这个 minContrastRatio 不同而已。

回到 ensureTextColorsGenerated 代码可以看到,先根据当前色调,计算出白色前景色的 Alpha,如果两个 Alpha 都不是 -1,就返回对应颜色;否则计算黑色前景色的 Alpha,如果都不是 -1,返回对应颜色;否则标题和主体文本一个用白色一个用黑色,返回对应颜色即可。

更多功能

上面我们创建 Palette 时先通过 Palette.from(bitmap) 得到了一个 Palette.Builder 对象,通过这个 builder 可以实现更多功能,比如:

  • addFilter: 增加一个过滤器;

  • setRegion: 设置图片上的提取区域;

  • maximumColorCount: 调色板的最大颜色数。

等等。

总结

通过上面我们看到,Palette 的功能很强大,但是它使用起来非常简单,可以让我们很方便的提取图片中的颜色,并且适配合适的文字颜色。同时注意因为 ColorUtils 是 public 的,所以当我们需要文字自动适配颜色的情况时,也可以通过 ColorUtils 的几个函数自己实现计算动态颜色的方案。


长按右侧二维码

查看更多开发者精彩分享

"开发者说·DTalk" 面向中国开发者们征集 Google 移动应用 (apps & games) 相关的产品/技术内容。欢迎大家前来分享您对移动应用的行业洞察或见解、移动开发过程中的心得或新发现、以及应用出海的实战经验总结和相关产品的使用反馈等。我们由衷地希望可以给这些出众的中国开发者们提供更好展现自己、充分发挥自己特长的平台。我们将通过大家的技术内容着重选出优秀案例进行谷歌开发技术专家 (GDE) 的推荐。

 点击屏末 | 阅读原文 | 即刻报名参与 "开发者说·DTalk"


Android Jetpack: 利用 Palette 进行图片取色 | 开发者说·DTalk相关推荐

  1. Android Jetpack Compose 最全上手指南 | 开发者说·DTalk

    本文原作者: 码农西哥,原文发布于微信公众号: Android 技术杂货铺  https://mp.weixin.qq.com/s/7tKv_RamfW0rG8tZHXH_rg 在 2019 年的 G ...

  2. Android下利用Bitmap切割图片

    在自己自定义的一个组件中由于需要用图片显示数字编号,而当前图片就只有一张,上面有0-9是个数字,于是不得不考虑将其中一个个的数字切割下来,需要显示什么数字,只需要组合一下就好了. 下面是程序的关键代码 ...

  3. Kotlin Jetpack 实战: 图解协程原理 | 开发者说·DTalk

    本文原作者: 朱涛,原文发布于: 朱涛的自习室 https://mp.weixin.qq.com/s/fN4cSg6jcFZo3Wb2_xcJVw 协程 (Coroutines),是 Kotlin「最 ...

  4. android matrix 实现点击旋转,Android中利用matrix 控制图片的旋转、缩放、移动

    本文主要讲解利用android中Matrix控制图形的旋转缩放移动,具体参见一下代码: /** * 使用矩阵控制图片移动.缩放.旋转 */ public class CommonImgEffectVi ...

  5. Android之利用ColorMatrix进行图片的各种特效处理

    原图:效果1:效果2: 效果3:效果4: 查看官方的API,其中ColorMatrix的说明如下: 5x4 matrix for transforming the color+alpha compon ...

  6. 拓展交流空间,分享开发精彩 | 开发者说·DTalk 鉴赏

    日月其迈,岁律更新,时间的洗礼让开发者们更加坚韧,持续探索,不断追求,同样也激励着我们为开发者提供更多的帮助与支持.不断迭代的技术产品是开发者们的趁手工具,定期更新的政策助力打造安全可靠的生态,透彻易 ...

  7. Android 利用图片取色法巧妙制作彩虹调色圆环

    Android 利用图片取色法巧妙制作彩虹调色圆环 前言 说干就干 编写布局 编写逻辑 完事 前言 先看UI设计图 理论上应该自定义一个圆环View,但看到UI设计师已经将彩虹

  8. 【译文】利用Palette为android应用着色

    利用Palette为android应用着色 原文地址:Coloring Android Apps with Palette 材料设计中定义特征的方式之一就是利用颜色在屏幕上强调内容.通过使用Palet ...

  9. Android Palette 提取图片的主色调

    博主声明: 转载请在开头附加本文链接及作者信息,并标记为转载.本文由博主 威威喵 原创,请多支持与指教. 本文首发于此 博主:威威喵 | 博客主页:https://blog.csdn.net/smil ...

最新文章

  1. 使用rsync实现数据实时同步备份--实战
  2. 编译安装openresty+mysql+php7
  3. python signal模块作用_如何理解python中信号Signal?
  4. 【java基础】zip压缩文件
  5. 开源项目之kisso
  6. 下载部署和管理Windows Azure应用程序评估
  7. kcf算法中cos_window是什么意思_知网/维普查重系统算法介绍(史上最详细)
  8. shll脚本带参数输入给导出的数据库文件命名以及创造路径
  9. 计算机英语词汇_通信人必备英语词汇大全
  10. Matlab 最速下降法 实列及代码实现
  11. matlab中如何设置曲线图,(excel表格制图表)如何将excel表中数据做出曲线图
  12. Daily English Dictation Number Three
  13. AD域服务器作用及好处
  14. erp系统不能连接服务器配置,erp系统云服务器怎么配置
  15. 【号外】联手腾讯故宫试水“互联网+”
  16. JAVA8 Map新方法:compute,computeIfAbsent,putIfAbsent与put的区别
  17. 回溯法-子集树排序树满m叉树
  18. Java程序设计基础【1】
  19. mysql 8.0.28安装教程(超简单)
  20. js实现点击下载文件

热门文章

  1. BIOS常见中英文对照表
  2. hive split 函数
  3. ProxyPool 爬虫代理IP池(分享)
  4. 在人际交往中你悟出过什么道理?
  5. java设计模式-克隆模式(复制模式)
  6. ImageMagick--介绍
  7. ArrayList超细详解
  8. 2022-2028全球钴镍合金行业调研及趋势分析报告
  9. 直播回放|跨境追踪(Re-ID)技术与Milvus的完美邂逅
  10. HDU-4539-郑厂长系列故事——排兵布阵