CSE1020 Lab 02

Part 3: Colour conversion

Part 3 asks you to understand a written description of a simple problem and translate the description into a Java program. The problem solution requires some arithmetic involving int and double types.

You should be able to complete Part 2 in 25 minutes or less.

Colour Spaces

The RGB colour space is a common representation of colour where red, green, and blue light are added together to produce a spectrum of colours. It is commonly used in video cameras, digital cameras, television and computer displays, and mobile phone displays. Colours are described by indicating the amount of red (r), blue (b), and green (g) light. A common way of specifying the r, g, and b values is to use an integer between 0 and 255 for each of the values.

The CMYK colour space is a common representation of colour used in the printing industry where cyan, magenta, yellow, and black ink are combined to produce a spectrum of colours. Colours are described by indicating the amount of cyan (c), magenta (m), yellow (y), and black (k). A common way of specifying the c, m, y, and k values is to use a real number between 0.0 and 1.0 for each of the values.

Here are some examples of colour representations in both colour spaces:

colour rgb cmyk
black 000   0.00.00.01.0
white 255255255   0.00.00.00.0
red 25500   0.01.01.00.0
green 02550   1.00.01.00.0
blue 00255   1.01.00.00.0
crimson (web colour) 2202060   0.00.90909090909090920.72727272727272720.13725490196078427
 

You can convert RGB values to CMYK values using the following formulas:

w = max(r / 255, g / 255, b / 255)
c = (w - (r / 255)) / w
m = (w - (g / 255)) / w
y = (w - (b / 255)) / w
k = 1 - w

Write a program that reads r, g, b values as integers from the command line and prints the equivalent c, m, y, k values. The required output is described in the next section. A possible starting point is shown below that computes w for you (because you don't know how to compute the maximum of three numbers yet), but there is a logical error on the 3 lines highlighted that you will need to fix.

 

 

Sample Output

Your program should print the values of c, m, y, and k on separate lines; for example:

java ColourConvert 255 0 0
0.0
1.0
1.0
0.0

NOTE: If you try java ColourConvert 0 0 0, your program should output something unusual.

 

Test your program

Compile, debug, and run your program. Repeat these steps until you are convinced that your program is correct.

 

Submit

Submit your program using the command:

submit 1020 L02 ColourConvert.java

and wait for the practice labtest.