It’s been a while since I’ve written code in Codea, so I thought it might be fun to test this quick concept I had to find teh average color of an image. Here’s the code:
displayMode(FULLSCREEN) function setup() cameraSource(CAMERA_BACK) end function draw() local cameraImage = image(CAMERA) if cameraImage then background(averageColor(cameraImage)) pushMatrix() translate(WIDTH/2, HEIGHT/2) scale(0.5) sprite(cameraImage, 0, 0) popMatrix() else print("No camera") end --text(math.ceil(DeltaTime*60), 100, 100) end function averageColor(img) --[[ How this works: OpenGL has built in smoothing capabilities. For instance, if you scale an image very large (with smoothing on), you'll see it slightly blurred, not every single pixel. This is very close to the graphics chip, meaning it's very, very fast. So, we can take advantage of that by drawing an image into a 1x1 pixel, making OpenGL do all the work finding the average color, as it tries to smooth the image. ]] local smallImage = image(1,1) setContext(smallImage) pushStyle() smooth() sprite(img,0,0,1,1) popStyle() setContext() return smallImage:get(1,1) end
You can read a brief explenation in the comment in the code on how it works. I may not have all the terminoligy correct, so feel free to correct me, or if you want, feel free to find a way to make the code run even faster.