-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TintPostProcessor to Fresco PostProcessors
Reviewed By: oprisnik Differential Revision: D54691638 fbshipit-source-id: f1068d7fd5e3a0c7dea78557d36851d3739c24c8
- Loading branch information
1 parent
0d78d83
commit eccdcef
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
imagepipeline/src/main/java/com/facebook/imagepipeline/postprocessors/TintPostProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.imagepipeline.postprocessors | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Canvas | ||
import android.graphics.PorterDuff | ||
import androidx.annotation.ColorInt | ||
import androidx.core.graphics.ColorUtils | ||
import com.facebook.cache.common.CacheKey | ||
import com.facebook.cache.common.SimpleCacheKey | ||
import com.facebook.imagepipeline.request.BasePostprocessor | ||
|
||
/** | ||
* Postprocessor that adds a semi-transparent tint to a bitmap. | ||
* | ||
* By default it will add a tint over every pixel in the bitmap. A PorterDuff.Mode may be provided | ||
* for custom draw behavior. PorterDuff.Mode.SRC_ATOP will avoid drawing over transparent pixels. | ||
* | ||
* For example: Providing color: Color.WHITE and alpha: 0.5f will brighten the image. | ||
* | ||
* @param color The color to tint the image with. Ex: Color.WHITE, Color.BLACK | ||
* @param alphaPercent Sets the alpha bits of the color param for tinting, if provided. 0.0 - 1.0 | ||
* @param porterDuffMode The PorterDuff.Mode to use for drawing, or null to draw over every pixel. | ||
*/ | ||
class TintPostProcessor( | ||
@ColorInt color: Int, | ||
alphaPercent: Float? = null, | ||
private val porterDuffMode: PorterDuff.Mode? = null | ||
) : BasePostprocessor() { | ||
@ColorInt | ||
private val tintColor: Int = | ||
if (alphaPercent != null) { | ||
ColorUtils.setAlphaComponent(color, (alphaPercent * 255).toInt().coerceIn(0, 255)) | ||
} else { | ||
color | ||
} | ||
|
||
private val cacheKey: CacheKey = | ||
SimpleCacheKey("Tint. tintColor=$tintColor, mode=$porterDuffMode") | ||
|
||
override fun process( | ||
sourceBitmap: Bitmap, | ||
) { | ||
if (porterDuffMode == null) { | ||
Canvas(sourceBitmap).drawColor(tintColor) | ||
} else { | ||
Canvas(sourceBitmap).drawColor(tintColor, porterDuffMode) | ||
} | ||
} | ||
|
||
override fun getName(): String = TintPostProcessor::class.toString() | ||
|
||
override fun getPostprocessorCacheKey(): CacheKey = cacheKey | ||
} |