Functional Problem Solving (CSC 151 2015S) : EBoards

CSC151.01 2015S, Review Session 4: Drawings, Colors, Testing, and More


Overview

Admin

Your Questions

Can you explain what you mean by "flatten" the colors in an image?

Currently there are 256x256x256 - about 8 million different colors, but that's because each component can take on 256 different values from 0 to 255.

What if we can only represent three values per pixel? 0 or 128 or 255. We'll have more than monochrome, but much less than full color. If we start with a full-color picture and convert each component to the nearest of 0 or 128 or 255, we get a "flatter" picture.

(define closest-multiple-of-128
  (lambda (val)
    (* 128 (round (/ val 128)))))

(define irgb-flatten-128
  (lambda (color)
    (irgb (closest-multiple-of-128 (irgb-red color))
          (closest-multiple-of-128 (irgb-green color))
          (closest-multiple-of-128 (irgb-blue color)))))

    > (irgb->rgb-list (irgb-flatten-128 (irgb 90 50 200)))
    '(128 0 255)

Now we can use this to see what happens to our favorite kitty.

    > (define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))
    > (image-show (image-variant kitten irgb-flatten-128))
    2

(Sam also did many interesting examples on the computer that those who missed this session will never see.)

Why use image-transform-pixel! rather than image-get-pixel and image-set-pixel?

It's shorter (about half as long).

If you intend to transform the same pixel, it avoids errors

Can you explain gamma correction?

Probably not, but I'll try.

When we gamma correct, we have a "gamma" that represents the correction. Above 1 is darker, below 1 is lighter.

Better than irgb-darker and irgb-lighter because it treats bright and dark colors differently.

Start with a number between 0 and 255. Say 83.

Convert to a number between 0 and 1 by dividing by 255.

Take that number and put it to the gamma power. We get another number between 0 and 1.

Multiply by 255, and we have a number between 0 and 255 again.

    > (/ 83 255)
    83/255
    > (exact->inexact (/ 83 255))
    0.3254901960784314
    > (define gamma 1.2)
    > (expt 0.3254 1.2)
    0.2599567258034535
    > (* 255 .2599)
    66.2745

    > (round (* 255 (expt (/ 83 255) 1.2)))
    66.0
    > (irgb 66.2 89.005 72.66)
    4348232
    > (irgb->rgb-list (irgb 66.2 89.005 72.66))
    '(66 89 72)

Why did you use 16 for irgb-darker and irgb-lighter?

  1. Much less than 16 is hard for people (aka "Sam") to see. 2. Computer scientists like powers of 2. 3. We wanted it to be less of a shift on a component than irgb-redder.

It was late. I needed a number.

How do you figure out the right gamma value for a desired effect?

I have no idea.

What might have been on the quiz?

Given these postconditions, write two tests for this procedure.

What do you expect to happen when I run these tests on this procedure?

Write irgb-greener, assuming that it's not already defined.

Write an irgb-greener that works even for colors with a green component of 255.

Explain what color you get from the following expression (give the RGB values).

My Questions