Thea
ColorL8.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 Color1uint8uint8.h
19 
20  @maintainer Morgan McGuire, graphics3d.com
21 
22  @created 2007-01-30
23  @edited 2007-01-30
24 
25  Copyright 2000-2007, Morgan McGuire.
26  All rights reserved.
27  */
28 
29 #ifndef __Thea_ColorL8_hpp__
30 #define __Thea_ColorL8_hpp__
31 
32 #include "Common.hpp"
33 #include "Math.hpp"
34 
35 namespace Thea {
36 
37 // Forward declarations
38 class ColorL;
39 class ColorRgba;
40 
46 class THEA_API ColorL8
47 {
48  private:
49  uint8 val;
50 
51  public:
52  typedef uint8 value_type;
53 
55  ColorL8() : val(0) {}
56 
58  explicit ColorL8(uint8 v) : val(v) {}
59 
61  ColorL8(ColorRgba const & other);
62 
64  ColorL8(ColorL const & other);
65 
67  uint8 value() const { return val; }
68 
70  uint8 & value() { return val; }
71 
73  uint8 const * data() const { return &val; }
74 
76  uint8 * data() { return &val; }
77 
79  template <typename IntegerT> uint8 const & operator[](IntegerT channel) const
80  {
81  debugAssertM(channel == 0, "ColorL8: Channel must be 0");
82  return val;
83  }
84 
86  template <typename IntegerT> uint8 & operator[](IntegerT channel)
87  {
88  debugAssertM(channel == 0, "ColorL8: Channel must be 0");
89  return val;
90  }
91 
93  void fill(uint8 v) { val = v; }
94 
96  ColorL8 operator+(ColorL8 const & other) const
97  {
98  return ColorL8((uint8)std::min(255, (int)val + (int)other.val));
99  }
100 
102  ColorL8 operator-(ColorL8 const & other) const
103  {
104  return ColorL8((uint8)std::max(0, (int)val - (int)other.val));
105  }
106 
108  ColorL8 operator*(Real other) const
109  {
110  return ColorL8((uint8)Math::clamp((Real)Math::round(val * other), (Real)0, (Real)255));
111  }
112 
114  ColorL8 operator/(Real other) const
115  {
116  return ColorL8((uint8)Math::clamp((Real)Math::round(val / other), (Real)0, (Real)255));
117  }
118 
120  ColorL8 & operator+=(ColorL8 const & other)
121  {
122  *this = *this + other;
123  return *this;
124  }
125 
127  ColorL8 & operator-=(ColorL8 const & other)
128  {
129  *this = *this - other;
130  return *this;
131  }
132 
134  ColorL8 & operator*=(Real other)
135  {
136  *this = *this * other;
137  return *this;
138  }
139 
141  ColorL8 & operator/=(Real other)
142  {
143  *this = *this / other;
144  return *this;
145  }
146 
148  ColorL8 max(ColorL8 const & other) const
149  {
150  return ColorL8(std::max(val, other.val));
151  }
152 
154  ColorL8 min(ColorL8 const & other) const
155  {
156  return ColorL8(std::min(val, other.val));
157  }
158 
160  static ColorL8 const & zero() { static ColorL8 const col(0); return col; }
161 
163  std::string toString() const;
164 
165 } THEA_END_PACKED_CLASS(1) // class ColorL8
166 
167 
168 inline ColorL8
169 operator*(Real lhs, ColorL8 const & rhs)
170 {
171  return rhs * lhs;
172 }
173 
174 } // namespace Thea
175 
176 #endif
ColorL operator*(Real lhs, ColorL const &rhs)
Multiply by a scalar.
Definition: ColorL.hpp:201
ColorL8 & operator/=(Real other)
Divide by a scalar and assign.
Definition: ColorL8.hpp:141
ColorL8 & operator*=(Real other)
Multiply by a scalar and assign.
Definition: ColorL8.hpp:134
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...
ColorL8 operator*(Real other) const
Multiply by a scalar.
Definition: ColorL8.hpp:108
uint8 & operator[](IntegerT channel)
Array-style channel access.
Definition: ColorL8.hpp:86
Root namespace for the Thea library.
ColorL8 min(ColorL8 const &other) const
Get the minimum of two color values.
Definition: ColorL8.hpp:154
Monochrome luminance value represented as a single byte value in [0, 255], with automatic scaling by ...
Definition: ColorL8.hpp:46
static ColorL8 const & zero()
Color with zero luminance (black).
Definition: ColorL8.hpp:160
uint8 const * data() const
Get the address of the color value.
Definition: ColorL8.hpp:73
ColorL8()
Default constructor, initializes color to 0.
Definition: ColorL8.hpp:55
T clamp(T const &x, U const &lo, V const &hi)
Clamp a number to lie in the range [lo, hi] (inclusive).
Definition: Math.hpp:328
ColorL8 & operator-=(ColorL8 const &other)
Subtract-and-assign.
Definition: ColorL8.hpp:127
#define THEA_END_PACKED_CLASS(byte_align)
Mark end of class that uses tight alignment.
Definition: Platform.hpp:142
uint8 value_type
The numerical type of the luminance value.
Definition: ColorL8.hpp:52
ColorL8 & operator+=(ColorL8 const &other)
Add-and-assign.
Definition: ColorL8.hpp:120
Monochrome luminance value in [0, 1], with automatic scaling by 255 when switching between integer (C...
Definition: ColorL.hpp:46
uint8 & value()
The value of the color.
Definition: ColorL8.hpp:70
#define THEA_BEGIN_PACKED_CLASS(byte_align)
Use tight alignment for a class.
Definition: Platform.hpp:133
uint8 const & operator[](IntegerT channel) const
Array-style channel access.
Definition: ColorL8.hpp:79
ColorL8(uint8 v)
Initializing constructor.
Definition: ColorL8.hpp:58
A color with four floating-point channels: red, green and blue, each in [0, 1].
Definition: ColorRgba.hpp:52
void fill(uint8 v)
Set the luminance value, for consistency with multi-channel colors.
Definition: ColorL8.hpp:93
float round(float x)
Round a real number to the nearest integer using the lrintf() routine.
Definition: Math.hpp:313
ColorL8 operator/(Real other) const
Divide by a scalar.
Definition: ColorL8.hpp:114
ColorL8 operator+(ColorL8 const &other) const
Addition operator.
Definition: ColorL8.hpp:96
ColorL8 max(ColorL8 const &other) const
Get the maximum of two color values.
Definition: ColorL8.hpp:148
uint8 * data()
Get the address of the color value.
Definition: ColorL8.hpp:76
uint8 value() const
The value of the color.
Definition: ColorL8.hpp:67
ColorL8 operator-(ColorL8 const &other) const
Subtraction operator.
Definition: ColorL8.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