CSC151.02 2010S Functional Problem Solving : Assignments

Assignment 6: Conditionals and Colors


Due: 10:00 a.m., Wednesday, 10 March 2010

Summary: You will write procedures to help you examine the type of a value. You will also explore some novel color transforms.

Purposes: To gain further experience with types, predicates, and conditionals. To have some fun with representations of colors and their transforms.

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 . The title of your email should have the form CSC151.01 2010S Assignment 6: Conditionals and Colors 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.

You can use the predicates rgb? and color-name?, which are already defined, to help you. You should not use the predicate color?; we want you to write your own version.

b. Write your own version of color-to-rgb, which is a very useful procedure. Your procedure should have the following behavior: If given an RGB color, it returns that color unchanged. If given a color name, it returns the result of calling color-name->rgb on that color name. If given any other type of value, it returns #f.

You should not use the built-in color->rgb procedure; we want you to write your own version.

c. 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.

d. What result do you obtain by calling type-of with an image as its parameter? Why?

e. What result do you obtain by calling color? with an image as its parameter? Why?

Problem 2: RGB Transforms

So far the behavior of the RGB transforms you have seen and/or implemented has not explicitly been dependent on qualities of the input color. We can also explore color transforms that involve conditionals.

a. Write an RGB transform (rgb-stronger rgb) that applies rgb-redder to rgb when red is the largest component of rgb, rgb-greener when green is the largest component, or rgb-bluer when blue is the largest component.

b. Write an RGB transform (rgb-weaker rgb) that applies rgb-greener and rgb-bluer to rgb when red is the largest component of rgb, rgb-redder and rgb-bluer when green is the largest component, or rgb-redder and rgb-greener when blue is the largest component.

Problem 3: Hue Transforms

The previous pair of transforms try to enhance or dampen the most pronounced color in the RGB representation. It may be more useful for us to explore alternate representations of color that are somewhat more intuitive. The HSV (for hue, saturation, and value) representation is one possibility. Hue represents the pure color (e.g., red, blue, yellow, green, or a combination of one of these). Saturation represents the "colorfulness" of the hue in the color. For instance, a completely saturated color would be a pure hue (like red), while a lesser saturated color might appear just as bright but somewhat faded (perhaps rose or pink). Value, then represents the brightness relative to a similarly bright white.

Hue is represented as an angle, or a point on a circle. Thus, the values 0-360 sweep through colors red (0 degrees), yellow (60 degrees), green (120 degrees), cyan (180 degrees), blue (240 degrees), magenta (300 degrees), and back to red (at 360 or 0 degrees).

There are a variety of transformations that can take an RGB color and give an HSV representation. In this problem, we'll focus on just extracting the hue.

Before we describe how to calculate hue, we need some basic values to refer to. Let (R,G,B) refer to the red, green, and blue components of a color, respectively. The chroma of a color, C is the largest of the RGB components minus the smallest of the RGB components. For example, the chroma of (128,64,50) is 128-50, or 78; the chroma of (0,255,0) is 255-0, or 255. The chroma of (255,255,255) is 255-255, or 0.

The raw hue can then be calculated as follows:

  • (G-B)/C if R is a largest component
  • (B-R)/C+2 if G is a largest component
  • (R-G)/C+4 if B is a largest component
  • if C=0 the hue is undefined, since all the components are the same and we would have a gray. In this case, one convention is to set the hue to 0.

The raw hue as given above produces a value between -1 and 6 (corresponding to the 6 cardinal colors described above). If it is negative, we should add 6 to get us back to a positive representation. The final result is converted to the range 0-360 by multiplying by 60 degrees (which is 360/6).

What happens if C is not 0, and two of the components are both the largest? If the formula is well-designed, it shouldn't matter. Hence, you can try the “largest component” tests in order.

Using the algorithm given above, write a procedure (rgb->hue-angle rgb) that takes an RGB color and produces its hue. For example,

> (rgb->hue-angle RGB-RED)
0
> (rgb->hue-angle RGB-YELLOW)
60
> (rgb->hue-angle RGB-GREEN)
120
> (rgb->hue-angle RGB-CYAN)
180
> (rgb->hue-angle RGB-BLUE)
240
> (rgb->hue-angle RGB-MAGENTA)
300
> (rgb->hue-angle RGB-PINK)
7340/21
> (round (rgb->hue-angle RGB-PINK))
350

Note: You may wish to use a let statement or helper procedures to decompose your implementation into managable, meaningful units. Please be sure to give your variables and procedures meaningful names.

Problem 4: Changing Hue

Being able to manipulate the hue in a color can actually be quite useful. MediaScheme can convert an HSV color into an RGB with the procedure (hsv->rgb hsv-list), where hsv-list is a three-element list containing the hue, saturation, and value components of a HSV color. MediaScheme can also extract the saturation and value from an RGB color with the procedures (rgb->saturation rgb) and (rgb->value rgb).

Write a procedure (rgb-change-hue rgb hue) that takes an RGB color and a hue value (in the range 0-360) and creates a new RGB color using the given hue with the saturation and value of rgb.

Warning! hsv->rgb only works with exact numbers.

Problem 5: Hue-Based Transforms

It turns out the making color transforms based on hue can visually interesting.

Original Image Hue rotated 30 deg Hue rotated 90 deg

a. Using the procedures you have written so far, write a procedure (rgb-rotate-hue rgb angle) that takes a color produces a new rgb color where the HSV equivalent has a hue rotated by angle degrees, a number between 0-360.

Hint: If the rotated angle is greater than 360, you must subtract 360 to get the proper "wrapped-around" hue angle.

b. Write a concise expression that will rotate the hues in an entire image by 30 degrees.

Warning: When you apply rgb-rotate-hue thousands of times (as you will in an image of non-trivial size), it is likely to take some time. Do your testing on small images.

Problem 6: Conditional Hue Transforms

Create your own RGB color transform that conditionally transforms the hue of input color using a manner of your own choosing. For instance, you may choose to shift colors close to blue more toward green, leaving the rest unchanged.

Include a description of the effect of your transform in English. We should be able to apply your transform using image-variant or image-transform!.

Important Evaluation Criteria

We will evaluate your work on the correctness, clarity, and conciseness of your code.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-10 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.