



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Fall 2008;
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




This assignment: (1) introduces three color models that are used in computing and graphics and (2) gives you practice in writing functions. Start early on this assignment and keep track of your time. Just before you submit, tell us how many hours you took in a comment at the top of class A4Methods. This assignment has been given before in CS100J. We give it again because it is such a good one, although we have made some changes. We ask you not to cheat by obtaining a copy of the earlier solution or a version handed in by another student. Such cheating helps no one, especially you, and it makes more unnecessary work for us. We are working hard to make this a good course, and in return we expect you to be honest. We will not take kindly to cheating. You may work with one partner. Form your group on the CMS several days before the assignment is due; do not wait until the last minute. Remember: it is dishonest for partners to split the work in half and work independently. Program and test and debug together, alternating writing the methods (with the other person watching and helping). We give you a class A4Tester, with some test cases in it already. You have to add test cases, as discussed below. When you start on a method, think of the test cases you will need to test the method and write the assertEquals statements before writing the method body. Add more test cases as you write the method body to ensure that there is complete test coverage โso that each statement or expression in the method body is exercised during at least one test case. In writing this assignment, we made heavy use of articles on the various color models on Wikipedia. Color model RGB The RGB model is an additive model in which red , green , and blue are mixed to produce other colors, as shown in the image to the left. Black is the absence of color; white is the maximum presence of all three. RGB has its roots in the 1953 RCA color-TV standards and in the Polaroid camera. RGB is used in your TV screen and on web pages. The amount of each color is represented by 8 bits, which gives a number in the range 0..255. Black is[0, 0, 0] (the first integer is the red, R; the second, G, green; the third, B, blue), and white is [255, 255, 255]. There are 16,777,216 different colors. Class java.awt.Color has some constants that you can use for some of the possible colors. For example, Color.magenta is [255, 0 , 255] and Color.orange is [255, 200, 0]. Web page http://en.wikipedia.org/wiki/List_of_colors, gives names to many colors in the RGB model. In the upper right is a colored image. Below it is its separation into red, green, and blue. In the three separation panels, the closer to black a point is the less of that color it has. The white snow is made up of a large amount of all three colors and the brown barn is made up of red and green with very little blue. Here is a high resolution version of these four images. In some graphics systems on the web, RGB is used with Java double numbers in the range 0..1.0, so that 255 is actually given by 1.0. Later, when we do calculations, we convert each number in the integer range
0..255 to a double number in the range 0..1.0, calculate, and then convert back to 0..255. The complementary color of RGB color [r, g, b] is the color [255-r, 255-g, 255-b]. It is like a color negative. Color model CMYK On your ink-jet printer, you buy expensive ink cartridges in the colors cyan (RGB color [0, 255, 255]), magenta [255, 0, 255], yellow [255, 255, 0], and black [0, 0, 0]. Black is referred to using K, for "Key plate". Theoretically, you need only CMY, but the K (the black) is there because without it, black doesn't look black enough. Also, you have to use a lot of CMY colors to get real black, and the paper gets too wet. Finally, text is usually black, and you save a lot of ink by having the additional black cartridge. In the upper right, you see an image; below it is its separation into its cyan, magenta, and yellow. To the right of that, you see the same image separated into four components; C, M, Y, K. Much less of the CMY colors are needed to make the image when black is also used. Printers use CMYK. We usually print on white paper, and the absence of C, M, Y, and K leaves it white. As more of each color is added, color is added to the page. Add more of each of the three basic colors and the image gets blacker. This works well in printing on white paper. The RGB system starts with a black background and adds more and more of each color to get white. This is called an "additive system"; CMYK is a "subtractive" system. Here is an enlarged version of the CMY image and an enlarged CMYK image. In our CMYK system, each of the four components is represented by a double value in the range 0..1.0. Color Model HSV (or HSB) The HSV model, used heavily in graphics applications, was created in 1978 by Alvy Ray Smith. Artists prefer the HSV model over others because of its similarities with the way humans perceive color. HSV can be explained in terms of the cone that appears to the left. H, the Hue , defines the basic color. It is an angle in the range 0! H< 360, if one views the top of the cone as a disk. Red is at angle 0. As the angle increases, the hue changes to orange, yellow, green, light blue, dark blue, violet, and back to red. The image in this paragraph shows the angles for some colors. S, in the range 0! S! 1, is the Saturation. It indicates the distance from the center of the desk. The lower the S value, the more faded and grayer the color. The higher the S value, the stronger and more vibrant the color. V, the Value , also called the Brightness , is in the range 0! V! 1. It indicates the distance from the point of
2. Function roundTo5(double). This is the function we really want to use, and it is most easily written by calling truncateTo5 appropriately. There is a return statement in it, so the program compiles. Write this function, using the guidance given as comments in the body. We provided test cases for it in class A4Tester. This function will be used later to get values to appear in the GUI in a consistent format --always 5 characters long. 2. Function toString(Color). Write this function. When this is written, RGB values will appear in the title of the GUI and in the two color panes. We provided one test case for this function in class A4tester; that is all that is needed. 3. Function toString(CMYK). Write this function. When written, CMYK values will appear in the two color panes. This function should call function roundTo5 to round each CMYK value to 5 characters. We do NOT provide test cases for this function. You write at least two of them. 4. Function toString(HSV). Write this function. When written, HSV values will appear in the two color panes. This function should call function roundTo5 to truncate each HSV value to 5 characters. We do NOT provide test cases for this function. You write at least two of them. 5. Function RGBtoCMYK. This function converts an RGB value to a CMYK value. When you get it working, moving the RGB sliders will cause the CMYK sliders to move as well. There are several different ways to convert, depending on how much black is used in the CMYK model. Our conversion uses as much black as possible. Let R, G, and B be the color components of the RGB in the range 0..1.0 (not 0..255!!). Then the conversion is as follows:
Let MAX be the maximum and MIN be the minimum of the (R, G, B) values. H will satisfy 0! H! 360 and S, V, R, G, and B will be in 0..1. H is given by 5 different cases: (a) MAX = MIN: H = 0. (b) MAX = R and G " B: H = 60.0 * (G - B) / (MAX - MIN). (c) MAX = R and G < B: H = 60.0 * (G - B) / (MAX - MIN) + 360. (d) MAX = G: H = 60.0 * (B - R) / (MAX - MIN) + 120. (e) MAX = B: H = 60.0 * (R - G) / (MAX - MIN) + 240. S is given by: if MAX = 0 then 0, else 1 โ Min/Max. V = MAX. You have to provide at least 5 test cases in class A4Tester, so that each expression in the cases for H are evaluated in at least one test case.
8. Function HSVtoRGB. This function converts an HSV value to an RGB value. When you get it working, everything in the GUI should work. Let Hi = floor(H/60) % 6 , f = H/60 โ Hi, p = V(1-S), q = V(1-fS), t = V(1-(1-f)S). Then R, G, and B depend on the value Hi as follows: If Hi = 0 , then R = V, G = t, B = p If Hi = 1 , then R = q, G = V, B = p If Hi = 2 , then R = p, G = V, B = t If Hi = 3 , then R = p, G = q, B = V If Hi = 4 , then R = t, G = p, B = V If Hi = 5 , then R = V, G = p, B = q This produces RGB values in the range 0..1.0, and they must be converted to the range 0..255 (use rounding). Be sure you do this. Provide at least 6 test cases for this function because of the 6 possible values of Hi. Submission of the assignment Place a comment at the top of class A4Methods that indicates how much time you spent on this assignment. Make sure that class A4Methods is indented properly. Make sure that class A4Tester has the appropriate test cases. Submit files A4Methods.java and A4Tester.java on the CMS by the due date. We hope you have enjoyed this assignment and found it instructive.