Thea
ColorL.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2013
12 //
13 //============================================================================
14 
15 /*
16  ORIGINAL HEADER
17 
18  @file ColorL.h
19 
20  Monochrome Color class
21 
22  @maintainer Morgan McGuire, http://graphics.cs.williams.edu
23  @created 2007-01-31
24  @edited 2009-03-20
25 
26  Copyright 2000-2009, Morgan McGuire.
27  All rights reserved.
28  */
29 
30 #ifndef __Thea_ColorL_hpp__
31 #define __Thea_ColorL_hpp__
32 
33 #include "Common.hpp"
34 #include "Math.hpp"
35 
36 namespace Thea {
37 
38 // Forward declarations
39 class ColorL8;
40 class ColorRgba;
41 
46 class THEA_API ColorL
47 {
48  private:
49  Real val;
50 
51  public:
52  typedef Real value_type;
53 
55  ColorL() : val(0) {}
56 
58  explicit ColorL(Real v) : val(v) {}
59 
61  ColorL(ColorL8 const & other);
62 
64  ColorL(ColorRgba const & other);
65 
67  Real value() const { return val; }
68 
70  Real & value() { return val; }
71 
73  Real const * data() const { return &val; }
74 
76  Real * data() { return &val; }
77 
79  template <typename IntegerT> Real const & operator[](IntegerT channel) const
80  {
81  debugAssertM(channel == 0, "ColorL: Channel must be 0");
82  return val;
83  }
84 
86  template <typename IntegerT> Real & operator[](IntegerT channel)
87  {
88  debugAssertM(channel == 0, "ColorL: Channel must be 0");
89  return val;
90  }
91 
93  void fill(Real v) { val = v; }
94 
96  ColorL operator-() const
97  {
98  return ColorL(-val);
99  }
100 
102  ColorL operator+(ColorL const & other) const
103  {
104  return ColorL(val + other.val);
105  }
106 
108  ColorL operator-(ColorL const & other) const
109  {
110  return ColorL(val - other.val);
111  }
112 
114  ColorL operator*(ColorL const & other) const
115  {
116  return ColorL(val * other.val);
117  }
118 
120  ColorL operator*(Real other) const
121  {
122  return ColorL(val * other);
123  }
124 
126  ColorL operator/(ColorL const & other) const
127  {
128  return ColorL(val / other.val);
129  }
130 
132  ColorL operator/(Real other) const
133  {
134  return ColorL(val / other);
135  }
136 
138  ColorL & operator+=(ColorL const & other)
139  {
140  val += other.val;
141  return *this;
142  }
143 
145  ColorL & operator-=(ColorL const & other)
146  {
147  val -= other.val;
148  return *this;
149  }
150 
152  ColorL & operator*=(ColorL const & other)
153  {
154  val *= other.val;
155  return *this;
156  }
157 
159  ColorL & operator/=(ColorL const & other)
160  {
161  val /= other.val;
162  return *this;
163  }
164 
166  ColorL & operator*=(Real other)
167  {
168  val *= other;
169  return *this;
170  }
171 
173  ColorL & operator/=(Real other)
174  {
175  val /= other;
176  return *this;
177  }
178 
180  ColorL max(ColorL const & other) const
181  {
182  return ColorL(std::max(val, other.val));
183  }
184 
186  ColorL min(ColorL const & other) const
187  {
188  return ColorL(std::min(val, other.val));
189  }
190 
192  static ColorL const & zero() { static ColorL const col(0); return col; }
193 
195  std::string toString() const;
196 
197 }; // class ColorL
198 
200 inline ColorL
201 operator*(Real lhs, ColorL const & rhs)
202 {
203  return rhs * lhs;
204 }
205 
206 } // namespace Thea
207 
208 #endif
ColorL operator*(Real lhs, ColorL const &rhs)
Multiply by a scalar.
Definition: ColorL.hpp:201
std::string toString(char const *s)
Convert a C-string to a std::string object, returning an empty string if the input is a null pointer...
Real const * data() const
Get the address of the color value.
Definition: ColorL.hpp:73
Real * data()
Get the address of the color value.
Definition: ColorL.hpp:76
Root namespace for the Thea library.
ColorL & operator-=(ColorL const &other)
Subtract-and-assign.
Definition: ColorL.hpp:145
ColorL & operator*=(Real other)
Multiply by a scalar and assign.
Definition: ColorL.hpp:166
Monochrome luminance value represented as a single byte value in [0, 255], with automatic scaling by ...
Definition: ColorL8.hpp:46
ColorL operator-(ColorL const &other) const
Subtraction operator.
Definition: ColorL.hpp:108
void fill(Real v)
Set the luminance value, for consistency with multi-channel colors.
Definition: ColorL.hpp:93
ColorL operator/(Real other) const
Divide by a scalar.
Definition: ColorL.hpp:132
Real & operator[](IntegerT channel)
Array-style channel access.
Definition: ColorL.hpp:86
Monochrome luminance value in [0, 1], with automatic scaling by 255 when switching between integer (C...
Definition: ColorL.hpp:46
ColorL operator*(Real other) const
Multiply by a scalar.
Definition: ColorL.hpp:120
Real value_type
The numerical type of the luminance value.
Definition: ColorL.hpp:52
ColorL & operator/=(ColorL const &other)
Divide-and-assign.
Definition: ColorL.hpp:159
ColorL & operator+=(ColorL const &other)
Add-and-assign.
Definition: ColorL.hpp:138
Real value() const
The value of the color.
Definition: ColorL.hpp:67
ColorL(Real v)
Initializing constructor.
Definition: ColorL.hpp:58
static ColorL const & zero()
Color with zero luminance (black).
Definition: ColorL.hpp:192
Real & value()
The value of the color.
Definition: ColorL.hpp:70
ColorL()
Default constructor, initializes color to 0.
Definition: ColorL.hpp:55
ColorL operator-() const
Negation operator.
Definition: ColorL.hpp:96
A color with four floating-point channels: red, green and blue, each in [0, 1].
Definition: ColorRgba.hpp:52
ColorL max(ColorL const &other) const
Get the maximum of two color values.
Definition: ColorL.hpp:180
ColorL operator*(ColorL const &other) const
Multiplication operator.
Definition: ColorL.hpp:114
ColorL & operator*=(ColorL const &other)
Multiply-and-assign.
Definition: ColorL.hpp:152
ColorL & operator/=(Real other)
Divide by a scalar and assign.
Definition: ColorL.hpp:173
ColorL operator/(ColorL const &other) const
Division operator.
Definition: ColorL.hpp:126
ColorL min(ColorL const &other) const
Get the minimum of two color values.
Definition: ColorL.hpp:186
Real const & operator[](IntegerT channel) const
Array-style channel access.
Definition: ColorL.hpp:79
ColorL operator+(ColorL const &other) const
Addition operator.
Definition: ColorL.hpp:102
void debugAssertM(CondT const &test, MessageT const &msg)
Check if a test condition is true, and immediately abort the program with an error code if not...
Definition: Common.hpp:52