Created
May 9, 2022 06:14
-
-
Save ephemient/fb4f3189397b13b2a826990b4dcd8a09 to your computer and use it in GitHub Desktop.
Invert black and white while maintaining colors
This file contains hidden or 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
<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> |
This file contains hidden or 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
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