CIS 1.5 (Science Section)

Brooklyn College

Professor Langsam

 

Assignment #4

 

Colors may be produced by one of two methods[1].

 

For systems that produce color by the transmission of light (e.g., LCD panels, digital cameras, monitors, etc) the additive color approach is used. The additive primary colors are red (R), green (G), and blue (B). Adding R and G light makes yellow (Y). Similarly, G + B = cyan (C) and R + B = magenta (M). Combining all three additive primaries makes white, while the absence of any light results in black. This system is known as the RGB format, in which we specify the amount of red, green and blue light. If we use a scale of 0-255 for each color, then the color of an individual pixel, may be stored in 3 bytes (known as 24-bit color) and allows for 256ª256ª256 ≈ 16 million distinct colors.

 

For systems that produce color by the reflection of light (e.g., prints, film, etc.) the subtractive color approach is used. The picture is illuminated by white light (which contains all possible colors) and the dye or pigment removes the unwanted color, reflecting only the colors that are observed. The subtractive primaries are cyan (C), magenta (M) and yellow (Y). Theoretically, equal amounts of cyan, magenta and yellow, C+M+Y, should absorb all light producing black. In practice this is difficult to achieve so that a fourth pigment, black (K – in order not to confuse this with blue), is added. This system is known as the CMYK format. The CYMK format represents colors on a scale from 0.0 to 1.0.

 

Ordinarily, editing of an image on a computer is done using RGB, which the computer converts to CMYK for printing. The conversion may be done as follows:

 

 

  • If the RGB values are all 0 then the CMY values are all 0 and the K value is 1
  • Otherwise use the following formulas:

 

                                             

 

Write a program that converts RGB to CMYK.

 


Strategy

 

  1. Write a function that reads an RGB value and rejects invalid values.
  2. Write a function max(a, b, c) that returns the maximum of its three parameters.
  3. Write a function RGBtoCMYK(r, g, b, c, m, y, k) that receives RGB values as input parameters, and returns CMYK values as reference parameters.

 

 

Data – Part I

 

Your program should read a data file, colors.dat, consisting of an unknown number of RGB pixel values and convert them to the equivalent colors in CMYK. Use the following data:

 

                    50                    50                    50

                      0                      0                      0

                  255                  255                  255

                      0                  100                  100

                  200                  200                      0

                  150                      0                  150

                  300                      0                      0

                      1                      1                      1

                      0                      0                  245

 

 

All output is to be to a file Be sure to comment your program, use meaningful variables and use the structured programming techniques you have learned in class.


Data – Part II

 

An image may be represented as a rectangular array whose values represent the color of each pixel. The number of rows and columns is known as the image resolution. Thus an image containing 640ª480 pixels would be represented by a two dimensional array containing 640 columns and 480 rows. Since the RGB representation specifies three values for each pixel, we may consider each value to be stored in a separate two-dimensional array or layer. Thus an RGB image can be considered to be a supersition of three layers–one for red, one for blue and one for green. This representation is known as a bit-mapped image.

 

Your program should assume a maximum resolution of 10ª10. The data file, image.dat, is in the following format:

 

                                                            header: rows columns

 

5

4

 

 

                                                            Data: red (R) row-major order

 

0

0

0

0

20

20

0

0

100

150

150

150

100

0

0

0

0

255

255

255

 

                                                            Data: green (G) row-major order

 

0

0

0

255

20

100

100

100

0

0

0

0

100

100

250

250

255

255

0

0

 

                                                            Data: blue (B) row-major order

 

0

100

255

0

20

100

100

100

0

0

0

0

100

20

20

100

0

255

255

255

 

Your output should be in the form of a table in which each position contains the CMYK values for that pixel in the form C,M,Y,K.

 

All output is to be to a file Be sure to comment your program, use meaningful variables and use the structured programming techniques you have learned in class.



[1] http://en.wikipedia.org/wiki/RGB