[racket] 2htdp/image: colors and alphas

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Mon Dec 27 08:13:18 EST 2010

I'm trying to port the picturing-programs teachpack to still work with 5.1.  (BTW, approximately how long do I have to do that before 5.1 is released?)  Right now it crashes because the API for make-bitmap has changed -- for the better, I think, but I still need to fix things.

I've got a "map-image" function that creates a [bitmap] image the same width and height as an existing image by mapping a color->color function on its pixels, and a "build-image" function that creates a [bitmap] image of a specified width and height by mapping a specified function on the coordinates of its pixels.  I also wrote (but didn't really document) a "map-masked-image" and "build-masked-image" which do the same, but the mapped function is also allowed to return #f, in which case the specified pixel is made transparent (i.e. alpha=0).  I can tweak those to work with the new make-bitmap, but the process got me wondering...

It would be nice if I could allow students to generate alphas other than 1 and 0.  It seems to me the cleanest way to do this is with an alpha-color struct (which existed in htdp/image, but seems to be gone from 2htdp/image).  I can do that myself, putting in glue code between it and 2htdp/image to convert back and forth among various color-like-things (color, alpha-color, string, symbol, color%, and #f for transparency), but 2htdp/image is already doing a lot of that, and it feels really inefficient to re-do it.

What would you think of ALL "color" structs actually having an alpha component, with the constructor allowing alpha to default to 1?

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.