Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: c938708976d2c55085ffd563f18461269b51588f (plain)
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
/*
   math/color.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_MATH_COLOR_H__
#define __INCLUDED_MATH_COLOR_H__

#include <iostream>

namespace math
{

/// a class representing an RGBA color value
class Color
{
public:
	/// create the default color, white
	Color();
	/// create a color from float RGBA value
	Color(float red, float green, float blue, float alpha=1.0f);
	/// create a greyscale color
	Color(const float grey, const float=1.0f);
	/// create a copy from an existing color
	Color(Color const & other);

	/// red channel value
	float red() const;
	/// green channel value
	float green() const;
	/// blue channel value
	float blue() const;
	/// alpha channel value
	float alpha() const;

	/// assignment
	void assign(Color const & other);
	/// assignment
	void assign(float red, float green, float blue, float alpha=1.0f);
	/// assignment
	void assign(float grey, float alpha=1.0f);
	/// assignment operator
	Color const &operator=(Color const & other);

	/// multiply rgb values with scalar value.
	Color operator*(const float scalar) const;

	/// multiply rgb values with scalar value.
	Color & operator*=(const float scalar);

		/// assign a value to an element of this color
	/*! WARNING: range is not checked
	 *  @param index the index of the element to assign to ( 0 <= index < 4 )
	*/
	inline float& operator[](const size_t index) {
		return rgba_data[index];
	}

	/// returns the value of an element of this color
	/*! WARNING: range is not checked
	 *  @param index the index of the element to return ( 0 <= index < 4 )
	 */
	inline float operator[](const size_t index) const {
		return rgba_data[index];
	}


	/// pointer to the internal data
	inline float *ptr() const { return (float *) rgba_data; };

	/// clamp color values to the 0-1 range
	void clamp();

	float &r;
	float &g;
	float &b;
	float &a;

	
private:
	float rgba_data[4];
};

/// write color RGB values to stream
std::ostream &operator<<(std::ostream &os, Color const & color);

/// read color RGB values from stream, alpha is set to 1.0
std::istream &operator>>(std::istream & is, Color & color);

/// scalar-with-color multiplication
Color const operator*(float scalar, Color const & color);

} // namespace math

#endif // __INCLUDED_MATH_COLOR_H__