fun getHslColor(context: Context?, bitmap: Bitmap?): Int {
if (bitmap == null || context == null || context.resources == null) {
Log.d(
TAG,
"getHslColor: Bitmap is null or context is null or resource is null"
)
return 0
}
val rect =
Rect(0, 1, bitmap.width, bitmap.height - 1)
val hsl = FloatArray(HSL_LENGTH)
val palette = Palette.from(bitmap)
.setRegion(rect.left, rect.top, rect.right, rect.bottom)
.clearFilters()
.maximumColorCount(SWATCH_LENGTH)
.generate()
palette?.let {
val swatches = compare(palette.swatches)
sMainHsl = getMutiColor(swatches)
hsl[HUE_INDEX] = sMainHsl[HUE_INDEX]
hsl[SATURATION_INDEX] = sMainHsl[SATURATION_INDEX]
hsl[LIGHTNESS_INDEX] = sMainHsl[LIGHTNESS_INDEX]
// If it is colored hsl you can display the corresponding color according to the current color range.
// if (Math.abs(hsl[SATURATION_INDEX]) >= FLOAT_COMPARE_VALUE ||
// Math.abs(hsl[LIGHTNESS_INDEX] - CHROMATIC_HSL) > FLOAT_COMPARE_VALUE) {
// return handleMutiColor(hsl[HUE_INDEX], context)
// }
// Use demo test
return Color.HSVToColor(hsl)
}
return Color.HSVToColor(hsl)
}
取色器使用
Add the JitPack repository to your build file in your root build.gradle