Types and Blends
Due: &types-and-blends-due;
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 &grader-email;. The title of your email
should have the form &types-and-blends-subject; 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.
Assignment
Problem 1: Types
a. Write a predicate, (is-color?
value), that returns #t
when value is either an RGB color or a color
name recognized by MediaScript. In all other cases, it should return
#f. (Note that a similar predicate, color?
is already defined in MediaScript, because it is very useful. You should
write your own definition withouth relying on color?.)
b. Write a procedure, (type-of
value), that returns
the symbol boolean, if
value is a boolean;
the symbol integer, if
value is an integer;
the symbol number, if
value is a number but not an integer;
the symbol procedure, if
value is a procedure;
the symbol string, if
value is a string;
the symbol symbol, if
value is a symbol;
the 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?
Problem 2: Blending colors
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, (rgb-average
color1 color2) 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.
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 lightsalmon
and darksalmon. (You will need to use
color-name->rgb to convert color names into the
corresponding RGB colors.)
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,
(image-11-pixel-blend
startcolor endcolor)
that creates an image 11 pixels wide and 1 pixel high that is a blend
from 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.
Important Evaluation Criteria
We will evaluate your work on the correctness, clarity, and conciseness of
your code. We will also consider the clarity, precision, and accuracy of
the documentation you write for the rgb-average and
rgb-weighted-average procedures.