/************************************************ * This class represents a color using three * floating-point components: red, green and blue. * Each component must be on the interval [0, 1]. * * Encapsulation * ------------- * Colors are commonly represented using either * integers from 0 to 255 or floating-point * values from 0.0 to 1.0. * * By declaring the member variables private, * the class controls how these values are stored * and modified. The constructors and mutators * convert integer arguments to floating-point * values and restrict all components to [0, 1]. * * Consequently, the user cannot accidentally * assign an invalid value directly to a member * variable. This restriction on the state of * an object is called a class invariant. * * Overloading * ----------- * The constructors and mutators are overloaded * to accept either integer or floating-point * arguments. Regardless of which representation * the user chooses, the internal representation * remains the same. * * Alternative color spaces * ----------------------- * While the class stores colors using the RGB * (red, green and blue) color space, other color * spaces may be more appropriate for particular * applications. These include: * * YIQ: Luma and two chrominance components * HSV: Hue, saturation and value * HSL: Hue, saturation and lightness * * The transformation from RGB to YIQ is linear * and can be described using a matrix. * The transformations to HSV and HSL are * non-linear. * * The corresponding member functions calculate * these properties as required. It is therefore * unnecessary to store these additional values * or to update them whenever a color changes. * * These member functions are declared const * because calculating a property of a color * does not change the color itself. * * Color adjustments * ----------------- * Two additional mutators allow the user to * modify a color: * * adjust_brightness(s) * Scales the components relative to black. * Black remains unchanged. * * adjust_shadows(s) * Scales the components relative to white. * White remains unchanged. * * Both member functions preserve the class * invariant. ************************************************/ #include #include #include #include // Class declarations class Color; // Function declarations int main(); // Class definitions class Color { public: Color( double red, double green, double blue ); Color( int red, int green, int blue ); // Accessors for the stored components double red() const; double green() const; double blue() const; // Mutators accepting either representation void red( double new_red ); void green( double new_green ); void blue( double new_blue ); void red( int new_red ); void green( int new_green ); void blue( int new_blue ); // Calculated properties in the YIQ color space double YIQ_Y() const; double YIQ_I() const; double YIQ_Q() const; // Calculated properties in the HSV and HSL // color spaces double value() const; double chroma() const; double lightness() const; double hue() const; double HSV_saturation() const; double HSL_saturation() const; // Mutators for adjusting the color void adjust_brightness( double s ); void adjust_shadows( double s ); private: // Each component must be on [0, 1]. double red_; double green_; double blue_; // Private helper functions static double clamp( double x ); static double positive_angle( double x ); }; // Function definitions int main() { Color purple{ 128, 0, 128 }; std::cout << "Purple:" << std::endl; std::cout << "Red: " << purple.red() << std::endl; std::cout << "Green: " << purple.green() << std::endl; std::cout << "Blue: " << purple.blue() << std::endl; std::cout << std::endl; std::cout << "Hue: " << purple.hue() << " degrees" << std::endl; std::cout << "Saturation: " << 100.0*purple.HSV_saturation() << " %" << std::endl; std::cout << "Value: " << 100.0*purple.value() << " %" << std::endl; std::cout << std::endl; Color antique_bronze{ 0.4, 0.36, 0.12 }; std::cout << "Antique bronze:" << std::endl; std::cout << "Hue: " << antique_bronze.hue() << " degrees" << std::endl; std::cout << "Saturation: " << 100.0*antique_bronze.HSV_saturation() << " %" << std::endl; std::cout << "Value: " << 100.0*antique_bronze.value() << " %" << std::endl; std::cout << std::endl; antique_bronze.adjust_brightness( 1.1 ); std::cout << "Brighter antique bronze:" << std::endl; std::cout << "Hue: " << antique_bronze.hue() << " degrees" << std::endl; std::cout << "Saturation: " << 100.0*antique_bronze.HSV_saturation() << " %" << std::endl; std::cout << "Value: " << 100.0*antique_bronze.value() << " %" << std::endl; return 0; } // Initialize the three components, ensuring // that each is on the interval [0, 1]. Color::Color( double red, double green, double blue ): red_{ clamp( red ) }, green_{ clamp( green ) }, blue_{ clamp( blue ) } { // Empty constructor body } // Convert the integer components to floating-point // values and delegate to the other constructor. Color::Color( int red, int green, int blue ): Color{ red/255.0, green/255.0, blue/255.0 } { // Empty constructor body } // Accessors double Color::red() const { return red_; } double Color::green() const { return green_; } double Color::blue() const { return blue_; } // Mutators accepting floating-point arguments void Color::red( double new_red ) { red_ = clamp( new_red ); } void Color::green( double new_green ) { green_ = clamp( new_green ); } void Color::blue( double new_blue ) { blue_ = clamp( new_blue ); } // Mutators accepting integer arguments // // Each converts its argument to a floating-point // value and calls the corresponding overloaded // member function. void Color::red( int new_red ) { red( new_red/255.0 ); } void Color::green( int new_green ) { green( new_green/255.0 ); } void Color::blue( int new_blue ) { blue( new_blue/255.0 ); } // The YIQ color space // // There is a linear transformation from RGB // to YIQ: // // / Y \ / 0.299 0.587 0.114 \/ R \ // | I | = | 0.596 -0.275 -0.321 || G | // \ Q / \ 0.212 -0.523 0.311 /\ B / // // These components are calculated as required // rather than stored as member variables. double Color::YIQ_Y() const { return 0.299*red() + 0.587*green() + 0.114*blue(); } double Color::YIQ_I() const { return 0.596*red() - 0.275*green() - 0.321*blue(); } double Color::YIQ_Q() const { return 0.212*red() - 0.523*green() + 0.311*blue(); } // The HSV and HSL color spaces // // Unlike YIQ, the transformations from RGB // to HSV and HSL are non-linear. // // Value: // The largest of the three components. // // Chroma: // The difference between the largest and // smallest components. // // Lightness: // The arithmetic mean of the largest and // smallest components. // // Hue: // An angle on [0, 360) identifying the color. // // Saturation: // The chroma relative to the maximum possible // chroma for the given value or lightness. double Color::value() const { return std::max( red(), std::max( green(), blue() ) ); } double Color::chroma() const { return value() - std::min( red(), std::min( green(), blue() ) ); } double Color::lightness() const { return value() - chroma()/2.0; } // The hue is undefined for an achromatic color // (black, white or gray). In this case, we // return zero by convention. double Color::hue() const { double c{ chroma() }; double v{ value() }; if ( c == 0.0 ) { return 0.0; } else if ( v == red() ) { return positive_angle( 60.0*((green() - blue())/c) ); } else if ( v == green() ) { return 60.0*(2.0 + (blue() - red())/c); } else { return 60.0*(4.0 + (red() - green())/c); } } // Calculate saturation relative to value. double Color::HSV_saturation() const { double v{ value() }; if ( v == 0.0 ) { return 0.0; } else { return chroma()/v; } } // Calculate saturation relative to lightness. double Color::HSL_saturation() const { double l{ lightness() }; if ( (l == 0.0) || (l == 1.0) ) { return 0.0; } else { return (chroma()/2.0)/std::min( l, 1.0 - l ); } } // Scale each component relative to black. // // s <= 0: Change the color to black. // 0 < s < 1: Darken the color. // s == 1: Leave the color unchanged. // s > 1: Brighten the color. // // Black remains unchanged for any finite s. void Color::adjust_brightness( double s ) { if ( !std::isfinite( s ) ) { throw std::invalid_argument{ "The scaling factor must be finite." }; } red( s*red() ); green( s*green() ); blue( s*blue() ); } // Scale each component relative to white. // // s <= 0: Change the color to white. // 0 < s < 1: Lighten the color. // s == 1: Leave the color unchanged. // s > 1: Deepen the shadows. // // White remains unchanged for any finite s. void Color::adjust_shadows( double s ) { if ( !std::isfinite( s ) ) { throw std::invalid_argument{ "The scaling factor must be finite." }; } red( 1.0 - s*(1.0 - red()) ); green( 1.0 - s*(1.0 - green()) ); blue( 1.0 - s*(1.0 - blue()) ); } // Private static helper functions // Restrict a component to the interval [0, 1]. // A value of not-a-number is not a valid component. double Color::clamp( double x ) { if ( std::isnan( x ) ) { throw std::invalid_argument{ "A color component cannot be NaN." }; } return std::max( 0.0, std::min( x, 1.0 ) ); } // Given an angle on [-60, 60], return the // equivalent angle on [0, 360). double Color::positive_angle( double x ) { if ( x < 0.0 ) { return x + 360.0; } else { return x; } }