Skip to content

Instantly share code, notes, and snippets.

@ephemient
Created May 9, 2022 06:14
Show Gist options
  • Save ephemient/fb4f3189397b13b2a826990b4dcd8a09 to your computer and use it in GitHub Desktop.
Save ephemient/fb4f3189397b13b2a826990b4dcd8a09 to your computer and use it in GitHub Desktop.
Invert black and white while maintaining colors
<style>
img {
filter: url(#invert);
}
</style>
<svg style="display: none;">
<defs>
<filter id="invert">
<feColorMatrix in="SourceGraphic" type="matrix" values="0.402 -1.174 -0.228 0 1 -0.598 -0.174 -0.228 0 1 -0.598 -1.174 0.772 0 1 0 0 0 1 0"></feColorMatrix>
</filter>
</defs>
</svg>
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
// YUV2RGB * (255 - Y) * RGB2YUV
val invertMatrix = ColorMatrix(
floatArrayOf(
0.402f, -1.174f, -0.228f, 0.0f, 255.0f,
-0.598f, -0.174f, -0.228f, 0.0f, 255.0f,
-0.598f, -1.174f, 0.772f, 0.0f, 255.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
)
)
fun ImageView.invert() {
colorFilter = ColorMatrixColorFilter(invertMatrix)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment