# webicon.py
from Processing import *

# Define colors
darkBlue = color(0, 51, 76)
reddish = color(217, 26, 33)
lightBlue = color(112, 150, 158)
yellow = color(252, 227, 166)

# Load image
img = loadImage("obama.jpg")
w = int( img.width() )
h = int( img.height() )

# Open a window and draw the initial image
window( w, h )
image(img, 0, 0)

# Compute luminance with model used for HDTV
def luminance( c ):
    return 0.2126 * red(c) + 0.7152 * green(c) + 0.0722 * blue(c)

# Load pixels so they can be manipulated
loadPixels()

# Loop over all pixels in the images
for i in range(w):
    for j in range(h):
        c = getPixel(i, j)              # Get pixel color
        total = luminance( c )          # Sum up all color components

        if total < 60:                  # Remap to new color depending upon total
            newColor = darkBlue
        elif total < 121:
            newColor = reddish
        elif total < 147:
            newColor = lightBlue
        else:
            newColor = yellow

        setPixel(i, j, newColor)        # Update to new color

updatePixels()                          # Update
