Image pixels
Move mouse around on the image to display the pixel colour. A histogram is displayed across the bottom.
import coracle.Colour
import coracle.Drawing
import coracle.Image
import coracle.Math.map
class ImagePixels: Drawing() {
var testImage: Image
lateinit
var pixelsLoaded = false
var histogram = IntArray(256){ 0 }
var frame = 0
override fun setup() {
(450, 450)
size= loadImage("input_image")
testImage ()
interactiveMode(0xffffff)
stroke}
override fun draw() {
(testImage, 0, 0, width, height)
image++
frameif(pixelsLoaded){
//Draw histogram
(0xffffff)
stroke(255) { index ->
repeatval x = map(index.toFloat(), 0f, 255f, 0f, width.toFloat()).toInt()
val maxValue = histogram.maxOrNull() ?: 0
val v = map(histogram[index].toFloat(), 0f, maxValue.toFloat(), 0f, height.toFloat()).toInt()
(x, height, x, height - v)
line}
//Live pixel colour
(mouseX, mouseY)?.let{ pixel ->
pixelval pixelColour = Colour(pixel)
(pixelColour)
fill(mouseX, mouseY, 20)
circle}
}else if(frame == 50){
//50 frame delay to ensure image has loaded
()
loadPixels(width){ i ->
repeat(height){ j ->
repeat(i, j)?.let{ pixel ->
pixelval brightness = Colour(pixel).brightness()
[brightness]++
histogram}
}
}
= true
pixelsLoaded }
}
}