CSCI 200 - Spring 2023
Foundational Programming Concepts & Design

Lab 2B - Color Space Converter

This lab is due by Friday, February 17, 2023, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.


Colors


There are many ways to represent color to a computer.

RGB

One common format is the RGB Color Space. This follows an additive color model where every color is represented as a combination of red, green, and blue.

RGB Color Cube

When all values are zero, the resultant color is black. When all values are one, the resultant color is white. Any other combination then adds the components as visualized in the corresponding color cube. The axes are defined as the amount of red, green, and blue to add together.

HSV

Another common format is the HSV Color Space. This space is defined by Hue, Saturation, and Value. The HSV color space can be visualized as a cone (or a cylinder).

HSV color solid cone

The Hue is an angle (in degrees) along the color wheel denoting the dominant color. The Saturation is a distance (between zero and one) denoting the amount of the color that is present. The Value (also between zero and one) is then the brightness of the color.

In this format, whenever value is zero the color is always black regardless of hue and saturation. When saturation is zero and value is one, then the color is white regardless of the hue.


Instructions


It is possible to represent the same color in multiple formats. We will write a program that converts from one format to another. Begin by reviewing the RGB to HSV Conversion equations.

We will create two functions called rgb_to_hsv and hsv_to_rgb that match the following format.


Pass-by-Value & Pass-by-Reference


One of the limitations of functions is that they can only return a single value. A workaround to this limitation is to pass parameters by reference. When the function completes, the arguments corresponding to these parameters will contain the modified values. A generic function prototype would match the following template:

void function_name( const dataType IN_PARAM_1, const dataType IN_PARAM_2, // input to the function
                    dataType& outParam1, dataType& outParam2 );           // output from the function

This usage of constant pass-by-value input and pass-by-reference output reinforces the procedural programming style.

int value1, value2, value3, value4;           // no variables are initialized
cin >> value1 >> value2;                      // user enters value1 and value2
function_name(value1, value2, value3, value4);// function call populates value3 and value4
cout << value1 << " " << value2 << endl;      // all variables now have a value
cout << value3 << " " << value4 << endl;      // all variables now have a value

Your program should first prompt the user which direction they wish to convert, either
RGB -> HSV
or
HSV -> RGB
Prompt the user to input the values on the left hand side and then call the corresponding function to compute the right hand side. Display these resultant values to the user.

Your functions must match the following specifications:

    • Function Name: rgb_to_hsv
    • Input:
      • int passed by constant value corresponding to the red channel
      • int passed by constant value corresponding to the green channel
      • int passed by constant value corresponding to the blue channel
      • double passed by reference corresponding to the hue
      • double passed by reference corresponding to the saturation
      • double passed by reference corresponding to the value
    • Output: None
    • Description: Converts RGB color to HSV color.

    • Function Name: hsv_to_rgb
    • Input:
      • double passed by constant value corresponding to the hue
      • double passed by constant value corresponding to the saturation
      • double passed by constant value corresponding to the value
      • int passed by reference corresponding to the red channel
      • int passed by reference corresponding to the green channel
      • int passed by reference corresponding to the blue channel
    • Output: None
    • Description: Converts cartesian (x, y) to polar (r, θ).


Test Cases


Below are some known conversions to test that your calculations are correct:

RGB -> HSV
RGB HSV
(255, 0, 0) (0, 1, 1)
(0, 255, 0) (120, 1, 1)
(0, 0, 255) (240, 1, 1)
(0, 0, 0) (0, 0, 0)
(255, 255, 255) (0, 0, 1)
(128, 128, 128) (0, 0, 0.501961)
(0, 43, 91) (211.817, 1, 0.356863)
(201, 151, 78) (36.1618, 0.61194, 0.788235)
HSV -> RGB
HSV RGB
(0, 1, 1) (255, 0, 0)
(120, 1, 1) (0, 255, 0)
(240, 1, 1) (0, 0, 255)
(0, 0, 0) (0, 0, 0)
(0, 0, 1) (255, 255, 255)
(0, 0, 0.501961) (128, 128, 128)
(211.817, 1, 0.356863) (0, 42, 91)
(36.1618, 0.61194, 0.788235) (200, 152, 78)

Refactor To Multiple Files


Once your solution is working, refactor your code to use multiple files. Your project should consist of the following files with respective contents:

Be sure to update your Makefile to work with the additional files.


Lab Submission


Submit your color_conversion.h, color_conversion.cpp, main.cpp, Makefile file(s).

You will submit your solution to this lab with the rest of Set2. Detailed instructions for doing this are posted in Assignment 2.


This lab is due by Friday, February 17, 2023, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.