interpolating colors

I’m starting a little project: making a python function to apply a color scheme to a gray level image.

For that we need: to read the image (job done by gdal), read a color scheme, get image min an max excluding no data values, interpolate the color scheme, and last but not least, transform the single band gray level into an rgb image.

For today, let’s start with the color interpolation function. Color can be interpolated in rgb, hsv, etc. Let’s write something for rgb, easily portable to other color space.

The idea is to consider separately the 3 color axis and interpolate along them:

def interpolateColor(rgbMin, rgbMax, maxDepth, depth):
    if depth <= 0:
        return rgbMin
    if depth >= maxDepth:
        return rgbMax

    rgb=rgbMin
    for iTriplet in range(3):
        minVal=rgbMin[iTriplet]
        maxVal=rgbMax[iTriplet]
        rgb[iTriplet] = int(minVal + (maxVal-minVal)*(depth/float(maxDepth)))

    return rgb

def test():
    rgbMin=[128, 0 ,34]
    rgbMax=[255, 56, 0]

    niter=10
    for ii in range(niter):
       print ii, interpolateColor(rgbMin, rgbMax, niter, ii)

To test it:

import interpolateColor
interpolateColor.test()

You should get the 9 intermediate colors.

0 Responses to “interpolating colors”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: