blob: b2ca490e878b67e24ce33fbc86946b8270546af9 (
plain) (
tree)
|
|
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527 */
/* without consent of either the College of William & Mary or */
/* Pieter Peers. */
/******************************************************************/
#ifndef _COLOR_H_
#define _COLOR_H_
#include <ostream>
class color {
public:
/////////////
// Typedef //
/////////////
typedef float value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* iterator;
typedef const value_type* const_iterator;
//////////////////
// Data Members //
//////////////////
union {
struct { value_type r, g, b; };
struct { value_type red, green, blue; };
value_type data[3];
};
public:
/////////////////
// Constructor //
/////////////////
explicit color(const_reference value=0.0f);
color(const_reference r, const_reference g, const_reference b);
color(const color& col);
////////////////
// Inspectors //
////////////////
const_reference operator[](size_t index) const;
reference operator[](size_t index);
size_t size(void) const { return 3; }
iterator begin(void);
const_iterator begin(void) const;
iterator end(void);
const_iterator end(void) const;
///////////////
// Operators //
///////////////
color& operator=(const color& col);
bool operator==(const color& col) const;
bool operator!=(const color& col) const;
color operator+(const color& col) const;
color operator-(const color& col) const;
color operator*(const color& col) const;
color operator*(const_reference scale) const;
color operator/(const color& col) const;
color operator/(const_reference scale) const;
color& operator+=(const color& col);
color& operator-=(const color& col);
color& operator*=(const color& col);
color& operator*=(const_reference scale);
color& operator/=(const color& col);
color& operator/=(const_reference scale);
///////////////
// Modifiers //
///////////////
color& abs(void);
color& clamp(const_reference lowerBound=0.0f, const_reference upperBounds=1.0f);
/////////////
// Friends //
/////////////
friend void swap(color& a, color& b) { return a._swap(b); }
friend color abs(const color& c) { return color(c).abs(); }
friend color clamp(const color& c, const_reference lowerBound=0.0f, const_reference upperBound=1.0f) { return color(c).clamp(lowerBound, upperBound); }
friend color operator*(const_reference scale, const color& col) { return (col*scale); }
friend std::ostream& operator<<(std::ostream& s, const color& col)
{
s << "(" << col.r << "," << col.g << "," << col.b << ")";
return s;
}
private:
/////////////////////
// Private Methods //
/////////////////////
void _assign(const color& col);
void _swap(color& col);
};
#endif /* _COLOR_H_ */
|