Due: 11 a.m., Wednesday, 1 October 2008
Summary: You will write procedures to help you examine the type of a value. You will also use the color manipulation operations you learned in the lab on RGB colors to blend colors together.
Purposes: To gain further experience with types, predicates, conditionals, and documentation. To help you think more about the RGB representation of colors.
Expected Time: Two to three hours.
Collaboration: We encourage you to work in groups of size three. You may, however, work alone or work in a group of size two or size four. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.
Submitting:
Email your answer to <weinman>. The title of your email
should have the form HW4 and
should contain your answers to all parts of the assignment. Scheme code
should be in the body of the message.
Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.
a. Write a predicate, (, that returns color?
value)#t when
value is either an RGB color or a color name
recognized by DrFu. In all other cases, it should return #f.
(Note that this predicate is already defined in
DrFu, because it is very useful. You should write your own definition.)
b. Write a procedure, (, that returns
type-of
value)
boolean, if
value is a boolean;integer, if
value is an integer;number, if
value is a number but not an integer;procedure, if
value is a procedure;string, if
value is a string;symbol, if
value is a symbol;other, if
value is anything else.
c. What result do you obtain by calling type-of
with an image as its parameter? Why?
d. What result do you obtain by calling color?
with an image as its parameter? Why?
A common effect in digital graphics is a color blend, in which colors range more or less smoothly from one color to another. For example, the following image shows a blend from blue to red. Isn't it beautiful?

What tools does one need to construct blends like the above? You must
know how to manipulate the red, green, and blue components of colors
used in digital images. And you know how to do that. In particular,
you can extract the red, green, and blue components of a color using
rgb-red,
rgb-green, and
rgb-blue, and you can construct an RGB color
using rgb-new.
Along with basic mathematical operations and the
image-set-pixel! operation, this is enough to let you
construct small color blends.
a. Write and document a procedure, ( that
takes two RGB colors as parameters
and computes the average of those two colors.
That is, the procedure should produce a new RGB color where
the red component is
the average of the red components of the two colors,
the green component is the average of the
green components of the two colors, and the blue component is
the average of the blue components of the two colors.
rgb-average
color1 color2)
You should document the rgb-average procedure using
the 6 P's.
b. Consider the following procedures that build an image for a
five-pixel blend. You should test the procedure with a few pairs of
colors, such as blue and red, green and yellow, and Seattle salmon
and Oregon salmon. (You will need to use
cname->rgb to convert color names into the
corresponding RGB colors.)
;;; Procedure:
;;; image-five-pixel-blend
;;; Parameters:
;;; startcolor, an RGB color
;;; endcolor, an RGB color
;;; Purpose:
;;; Creates a 5x1 pixel image that contains a blend from startcolor to
;;; endcolor.
;;; Produces:
;;; fiveblend, an image identifier
(define image-five-pixel-blend
(lambda (startcolor endcolor)
(image-five-pixel-blend-helper (image-new 5 1) startcolor endcolor)))
(define image-five-pixel-blend-helper
(lambda (fivebyone startcolor endcolor)
(image-set-pixel! fivebyone 0 0 startcolor)
(image-set-pixel! fivebyone 4 0 endcolor)
(image-set-pixel! fivebyone 2 0 (rgb-average startcolor endcolor))
(image-set-pixel! fivebyone 1 0
(rgb-average startcolor
(image-get-pixel fivebyone 2 0)))
(image-set-pixel! fivebyone 3 0
(rgb-average endcolor
(image-get-pixel fivebyone 2 0)))
fivebyone))
i. Explain why you think image-five-pixel-blend
uses a helper procedure.
ii. Explain why you think image-five-pixel-blend-helper ends with fivebyone.
c. Write and document a procedure,
(rgb-weighted-average
weight
color1
color2),
that takes a weight between 0 and 1 and two RGB colors, and computes
the RGB color made up of weight parts
of color1 and
(- 1 weight) parts of
color2.
It is easiest to think about this procedure in terms of particular
components. Suppose the red component of color1
is 0 and the red component of color2 is 120.
If weight is 0.2, then the red component of
the weighted average will be
96 (that is, 0.2*0 + 0.8*120). If
weight is 0.75, then the red component
of the weighted average will be 30 (that is, 0.75*0 + 0.25*120).
For another example,
suppose the green component of color1
is 200 and the green component of color2 is
0. If weight is 0.2, then the green component
of the weighted average will be 40 (that is 0.2*200 + 0.8*0).
Similarly, if weight is 0.75, then the
green component of the weighted average will be
150 (that is 0.75*200 + 0.25*0).
What if the components are both non-zero? Suppose the blue
component of color1 is 120 and the
blue component of color2 is 180.
If weight is 0.2, then the green component
of the weighted average will be 168 (0.2*120+0.8*180 =
24+144). If weight is
0.75, then the green component of the weighted average will
be 135 (you can do the math).
d. Write a
procedure,
(
that creates an image 11 pixels wide and 1 pixel high that is a blend
from image-11-pixel-blend
startcolor endcolor)startcolor to
endcolor.
startcolor and endcolor
should be RGB colors.
To compute the color for each pixel, you should call the
rgb-weighted-average procedure that you wrote in
part (c).
Use image-five-pixel-blend and its helper as a
model for your procedure(s).
Note that we picked 11 pixels because the math is easy; 1.0 divided into ten equal segments means that each weight differs by 0.1 from its neighbors.
rgb-average and
rgb-weighted-average procedures.