網站首頁 編程語言 正文
前言
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 AsyncTaskgenerate( @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 dark final 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 return mBodyTextColor = 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 return mBodyTextColor = 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 values mBodyTextColor = 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 contrast int testForeground = setAlphaComponent(foreground, 255); double testRatio = calculateContrast(testForeground, background); if (testRatio < minContrastRatio) { // Fully opaque foreground does not have sufficient contrast, return error return -1; } // Binary search to find a value with the minimum value which provides sufficient contrast int 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的幾個函數自己實現計算動態顏色的方案。
原文鏈接:https://juejin.cn/post/7077380907333582879
相關推薦
- 2023-01-08 利用Qt實現獲取計算機的硬件信息_C 語言
- 2023-05-14 Redis為什么默認有16個數據庫問題_Redis
- 2024-01-14 npm install時 cannot read properties of null的問題
- 2022-02-18 spring常見錯誤:Error creating bean with name ‘xxx‘
- 2022-09-29 Python?pkg_resources模塊動態加載插件實例分析_python
- 2022-09-13 Python使用os模塊實現更高效地讀寫文件_python
- 2022-10-06 Python?PaddlePaddle機器學習之求解線性模型_python
- 2022-07-22 Android 舊項目打包 api-versions.xml Stream closed
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支