Skip to content

Instantly share code, notes, and snippets.

@smurching
Last active January 25, 2018 19:56
Show Gist options
  • Save smurching/d6e918a84c79155b616f9339b30fdfca to your computer and use it in GitHub Desktop.
Save smurching/d6e918a84c79155b616f9339b30fdfca to your computer and use it in GitHub Desktop.
// You can run this gist in a scala console
import java.awt.image.{BufferedImage}
import java.awt.Color
// Set image pixel at (0, 0)
val image = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY)
val b: Byte = 26
val color = new Color(b & 0xff, b & 0xff, b & 0xff)
image.setRGB(0, 0, color.getRGB)
// Get int whose bytes represent RGBA channels from image and color instance, respectively
// Note that the int values differ
val imageInt = image.getRGB(0, 0)
val colorInt = color.getRGB
println(s"Int color from image: $imageInt (${Integer.toBinaryString(imageInt)}), " +
s"int color from Color instance: $colorInt (${Integer.toBinaryString(colorInt)})")
// Try extracting individual color channel values from the RGBA ints above. Note that they differ
val imageColorObject = new Color(imageInt)
val imageB = imageColorObject.getBlue.toByte
val imageG = imageColorObject.getGreen.toByte
val imageR = imageColorObject.getRed.toByte
val colorB = color.getBlue.toByte
val colorG = color.getGreen.toByte
val colorR = color.getRed.toByte
println(s"Image object BGR: ($imageB, $imageG, $imageR)")
println(s"Color object BGR: ($colorB, $colorG, $colorR)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment