#include using namespace std; enum color_sys { rgb=0, hsv, html }; struct color { float r, g, b; float h, s, v; int hr, hg, hb; }; //////////////////////////// Prototypes //////////////////////// void print_menu(int &in, int &out); void input_color(color &c, int in); void output_color(color &c, int out); void perform_conversion(int in, int out, color &c); void html2rgb(color &c); void hsv2rgb(color &c); void rgb2hsv(color &c); void rgb2html(color &c); int main() { color c; int in, out; print_menu(in, out); input_color(c, in); perform_conversion(in, out, c); output_color(c, out); return 0; } //////////////// Input - Output Operations ////////////////////// // Function that prints out the options and inputs the parameters void print_menu(int &in, int &out) { cout << "Select the input format:" << endl << "0 - RGB " << endl << "1 - HSV " << endl << "2 - HTML" << endl; cin >> in; cout << "Select the output format:" << endl << "0 - RGB " << endl << "1 - HSV " << endl << "2 - HTML" << endl; cin >> out; } void input_color(color &c, int in) { // To be implemented by the student } void output_color(color &c, int out) { // To be implemented by the student } // Function that performs the appropriate conversions. void perform_conversion(int in, int out, color &c) { if (in == html) html2rgb(c); else if (in == hsv) hsv2rgb(c); if (out == hsv) rgb2hsv(c); else if (out == html) rgb2html(c); } ///////////// Conversion Functions /////////////////// void html2rgb(color &c) { // To be implemented by the student } float max(float a, float b, float c) { if (a >= b && a >= c) return a; else if (b >= a && b >= c) return b; else return c; } float min(float a, float b, float c) { if (a <= b && a <= c) return a; else if (b <= a && b <= c) return b; else return c; } void hsv2rgb(color &c) { // To be implemented by the student } void rgb2hsv(color &c) { if (c.r == c.g && c.g == c.b) { // shade of gray c.h = 0; // default - could be anything; c.s = 0; // no saturation c.v = c.r; } c.v = max(c.r, c.g, c.b); float mn = min(c.r, c.g, c.b), md = c.r+c.g+c.b-c.v-mn; //median value c.s = 1-mn/c.v; float offset = (md - mn) * 60 /(c.v - mn); if (c.r >= c.g && c.r >= c.b) { // dominant red if (c.g >= c.b) // subdominant green c.h = 0 + offset; else // subdominant blue c.h = 360 - offset; } // To be finished by the student } void rgb2html(color &c) { c.hr = int(c.r * 255); c.hg = int(c.g * 255); c.hb = int(c.b * 255); }