summaryrefslogtreecommitdiff
path: root/hw2/include/color.h
blob: b2ca490e878b67e24ce33fbc86946b8270546af9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************/
/* 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_ */