CSC151 2007S, Class 09: Writing Your Own Color Transformations Admin: * No Rice Krispy (R) treats today. (Pop Tarts (R) as an alternative) * Notes on HW4 will be available soon * Are there questions on Homework 5? * In case you can't tell, I like to teach using a hybrid prospective-experiential-retrospective technique. I consider it particular important to reflect on what you might have learned after you attempt the learning. * Reading for Friday: Numeric Values in Scheme. * Dr. Davis and I are talking about DrFu this afternoon at 4:15 in 3821 (I think). The talk is meant primarily for CS folks, but if you want to come and listen, I will give you extra credit. Overview: * What is a procedure? * Describing procedures. * Syntax: Writing procedures. * Use in filters To write algorithms, you may need * The ability to name things: define * Basic instructions: rgb.new, rgb.red, rgb.redder, image.new, ... * The ability to sequence computations * Write them one after another in the definitions pane * Write them one after another in the interactions pane * (compose function1 function2) gives you a function that first applies function2 and then function1 * Nesting: Write one instruction within another; the inner one is computed first * Create new functions * Convenience: Encapsulate (and may name) computations * TODAY'S TOPIC * Loop - Do things multiple times, to different values * Do something to each pixel - image.map, image.map! * image.map creates a new image, does not change the existing image * image.map! doesn't create a new image, instead changes the existing image * Conditionals * Haven't learned yet, but coming next week Goals: * Minimize errors --- Encapsulating instructions in Scheme * Idea: You have a computation, and would like to do in more general contexts * Repeatedly * Use *here* and *here* and *here* * Traditionally think about these in terms of "Given *these values*, we compute *this value*" * Example from colors * "Given a color, which we call c, compute a new color ..." * "Given grades on homeworks 1 2 and 3, compute the homework average" * In Scheme (lambda (param1 param2 param3) expression-to-compute-result) * More typically, we name these things (define proc (lambda (param1 param2 param3) expression)) (define draw-blended-pixel (lambda (position) (image.set-pixel! blendy position 0 (rgb.new (+ initial-red (* red-diff (/ position 10))) ...)))) (draw-blended-pixel 0) (draw-blended-pixel 1) (draw-blended-pixel 2) (draw-blended-pixel 3) (draw-blended-pixel 4) (draw-blended-pixel 5) (draw-blended-pixel 6) Today: Image map and color computation (image.map (lambda (color) (rgb.new (+ 32 (rgb.red color)) (rgb.blue color) (rgb.green color))) some-image)