Created
February 20, 2023 23:46
-
-
Save uqmessias/6991cfe7d9e9f87faad5a3e6d99e1564 to your computer and use it in GitHub Desktop.
It converts an RGB Int into a vector with R, G, and G color parts. It also does the same by inverting the R with G (this is needed for an internal project). And all of this in Kotlin
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 java.awt.Color | |
/** | |
* Got from https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS44LjEwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiaW1wb3J0IGphdmEuYXd0LkNvbG9yXG4vKipcbiAqIFlvdSBjYW4gZWRpdCwgcnVuLCBhbmQgc2hhcmUgdGhpcyBjb2RlLlxuICogcGxheS5rb3RsaW5sYW5nLm9yZ1xuICovXG5mdW4gbWFpbigpIHtcbiAgICB2YWwgcmdiOiBJbnQgPSAweDFmOGI1NFxuICAgIFxuICAgIHZhbCByZ2JSZWRDb2xvcjogSW50ID0gKHJnYiBzaHIgMTYpIGFuZCAweEZGXG4gICAgdmFsIHJnYkdyZWVuQ29sb3I6IEludCA9IChyZ2Igc2hyIDgpIGFuZCAweEZGXG4gICAgdmFsIHJnYkJsdWVDb2xvcjogSW50ID0gcmdiIGFuZCAweEZGXG4gICAgXG4gICAgdmFsIGdiclJlZENvbG9yOiBJbnQgPSByZ2IgYW5kIDB4RkZcbiAgICB2YWwgZ2JyR3JlZW5Db2xvcjogSW50ID0gKHJnYiBzaHIgOCkgYW5kIDB4RkZcbiAgICB2YWwgZ2JyQmx1ZUNvbG9yOiBJbnQgPSAocmdiIHNociAxNikgYW5kIDB4RkZcbiAgICBcbiAgICB2YWwgZ2JyOiBJbnQgPSAocmdiUmVkQ29sb3IgYW5kIDB4RkYpIG9yICgocmdiR3JlZW5Db2xvciBhbmQgMHhGRikgc2hsIDgpIG9yICgocmdiQmx1ZUNvbG9yIGFuZCAweEZGKSBzaGwgMTYpXG4gICAgXG5cdHByaW50bG4oXCJSR0IgIyR7cmdiLnRvU3RyaW5nKDE2KX0sICRyZ2IgKFtyOiAkcmdiUmVkQ29sb3IsIGc6ICRyZ2JHcmVlbkNvbG9yLCBiOiAkcmdiQmx1ZUNvbG9yXSlcXG5HQlIgIyR7Z2JyLnRvU3RyaW5nKDE2KX0sICRnYnIgKFtyOiAkZ2JyUmVkQ29sb3IsIGc6ICRnYnJHcmVlbkNvbG9yLCBiOiAkZ2JyQmx1ZUNvbG9yXSlcIilcbn1cbiJ9 | |
*/ | |
fun main() { | |
val rgb: Int = 0x1f8b54 | |
val rgbRedColor: Int = (rgb shr 16) and 0xFF | |
val rgbGreenColor: Int = (rgb shr 8) and 0xFF | |
val rgbBlueColor: Int = rgb and 0xFF | |
val gbrRedColor: Int = rgb and 0xFF | |
val gbrGreenColor: Int = (rgb shr 8) and 0xFF | |
val gbrBlueColor: Int = (rgb shr 16) and 0xFF | |
val gbr: Int = (rgbRedColor and 0xFF) or ((rgbGreenColor and 0xFF) shl 8) or ((rgbBlueColor and 0xFF) shl 16) | |
println("RGB #${rgb.toString(16)}, $rgb ([r: $rgbRedColor, g: $rgbGreenColor, b: $rgbBlueColor])\nGBR #${gbr.toString(16)}, $gbr ([r: $gbrRedColor, g: $gbrGreenColor, b: $gbrBlueColor])") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment