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
|
/*
This file is part of the MinGfx Project.
Copyright (c) 2017,2018 Regents of the University of Minnesota.
All Rights Reserved.
Original Author(s) of this File:
Dan Keefe, 2018, University of Minnesota
Author(s) of Significant Updates/Modifications to the File:
...
*/
#ifndef SRC_COLOR_H_
#define SRC_COLOR_H_
#include <iostream>
#include <vector>
namespace mingfx {
/** Represents a 4-component (R,G,B,A) color, stored internally in a float array
to be compatable with OpenGL. Example usage:
~~~
Color black(0, 0, 0);
Color white(1, 1, 1);
Color gray(0.5, 0.5, 0.5);
Color tranparent_red(1.0, 0.0, 0.0, 0.5);
Color orange(1.0, 0.65, 0.0);
float red_component_of_orange = orange[0];
float green_component_of_orange = orange[1];
float blue_component_of_orange = orange[2];
float alpha_component_of_orange = orange[3];
// Use value_ptr() to send the array of floats directly to OpenGL commands
glColor4fv(orange.value_ptr());
~~~
*/
class Color {
public:
/// Defaults to black
Color();
/// Constructs a color. Alpha defaults to 1.0 (completely opaque)
Color(float red, float green, float blue, float alpha=1.0);
/// Constructs a point given a pointer to float array
Color(float *p);
/// Constructs a point given a 3 or 4-element vector of floats
Color(const std::vector<float> &vals);
/// Copy constructor
Color(const Color& p);
/// Color destructor
virtual ~Color();
/// Check for equality
bool operator==(const Color& p) const;
/// Check for inequality
bool operator!=(const Color& p) const;
/// Assignment operator
Color& operator=(const Color& p);
/// Accesses the ith component of the color, stored in RGBA order.
float operator[](const int i) const;
/// Accesses the ith coordinate of the color, stored in RGBA order.
float& operator[](const int i);
/// Returns a const pointer to the raw data array
const float * value_ptr() const;
std::vector<float> ToVector() const;
/// Linear interpolation between this color and another. Alpha=0.0 returns
/// this color, and alpha=1.0 returns the other color, other values blend
/// between the two via a linear interpolation on each color channel.
Color Lerp(const Color &b, float alpha) const;
/// Linear interpolation between two colors. Alpha=0.0 returns 'a' and
/// alpha=1.0 returns 'b', other values blend between the two via a linear
/// interpolation on each color channel.
static Color Lerp(const Color &a, const Color &b, float alpha);
private:
float c[4];
};
std::ostream & operator<< ( std::ostream &os, const Color &c);
std::istream & operator>> ( std::istream &is, Color &c);
} // namespace
#endif
|