/*+----------------------------------------------------------------------------
Ben Landon
CSCI E-235
Color.cpp
*/
#include
#include "gl_common.h"
#include "Color.hpp"
Color::Color (void)
: m_red(1.0), m_green(1.0), m_blue(1.0), m_alpha(1.0)
{
}
Color::Color (const Color& color)
: m_red(color.m_red), m_green(color.m_green),
m_blue(color.m_blue), m_alpha(color.m_alpha)
{
}
Color::Color (float r, float g, float b)
: m_red(r), m_green(g), m_blue(b), m_alpha(1.0)
{
}
Color::Color (float r, float g, float b, float a)
: m_red(r), m_green(g), m_blue(b), m_alpha(a)
{
}
/*+----------------------------------------------------------------------------
Color destructor
*/
Color::~Color ()
{
// Nothing to do, the Color class
// doesn't allocate memory or any
// system resource.
}
/*+----------------------------------------------------------------------------
operator=
Assignment operator for Color objects
*/
const Color& Color::operator= (const Color& color)
{
m_red = color.m_red;
m_green = color.m_green;
m_blue = color.m_blue;
m_alpha = color.m_alpha;
return *this;
}
/*+----------------------------------------------------------------------------
Color::make_current
This method makes this color object the current drawing color
for OpenGL.
*/
void Color::make_current (void) const
{
if (m_alpha != 1.0)
glColor4f(m_red, m_green, m_blue, m_alpha);
else
glColor3f(m_red, m_green, m_blue);
}
/*+----------------------------------------------------------------------------
Color::set_current
This is a static method that takes a color object and sets it
to the current OpenGL color. This is equivalent to the instance
method Color::make_current, but it might be more natural to use
set_current in some cases.
*/
void Color::set_current (const Color& color)
{
color.make_current();
}
/*+-------------------------------------------------------------------
Color::to_float_array
Copy the color data in this object into an array of floats.
This is helpful for use with the "fv" functions in OpenGL
The array must be at least size elements long. Size must
be 3 or 4.
*/
void Color::to_float_array (float *array, int size) const
{
assert(size == 3 || size == 4);
array[0] = m_red;
array[1] = m_green;
array[2] = m_blue;
if (size == 4)
{
array[3] = m_alpha;
}
}
/*+-------------------------------------------------------------------
Color::set
Sets the values in this color object to the RGBA or RGB
values passed in. A defaults to 1.0 if not specified.
These values should be in the range
*/
void Color::set (float r, float g, float b, float a)
{
m_red = r;
m_green = g;
m_blue = b;
m_alpha = a;
}
/*+-------------------------------------------------------------------
Color::set_from_array
Sets the values in this Color object from an array of floats.
The color data in the array is assumed to be in RGB or RGBA
order. The array must be at least size elements long, and size
must be 3 or 4.
If size is 3, then alpha is set to 1.0f;
If size is 4, then alpha is set to array[3].
*/
void Color::set_from_array (const float* array, int size)
{
assert(size == 3 || size == 4);
assert(array != NULL);
m_red = array[0];
m_green = array[1];
m_blue = array[2];
if (size = 3)
m_alpha = 1.0f;
else
m_alpha = array[3];
}
/*+----------------------------------------------------------------------------
make_color_from_uchars
It is often useful to be able to create Color objects from
RGB triples that are values 0...255. This function simply
takes three unsigned characters, normalizes them to floats
from 0 ... 1, and then returns a Color object with those
colors.
*/
Color make_color_from_uchars (unsigned char r,
unsigned char g,
unsigned char b)
{
float red = float(r)/255.0f;
float green = float(g)/255.0f;
float blue = float(b)/255.0f;
return Color(red, green, blue);
}
const Color cyan = make_color_from_uchars(0, 255, 255);
const Color magenta = make_color_from_uchars(255, 0, 255);
const Color yellow = make_color_from_uchars(255, 255, 0);
const Color red = make_color_from_uchars(255, 0, 0);
const Color green = make_color_from_uchars(0, 255, 0);
const Color blue = make_color_from_uchars(0, 0, 255);
const Color gray = make_color_from_uchars(128, 128, 128);
const Color black = make_color_from_uchars(0, 0, 0);
const Color white = make_color_from_uchars(255, 255, 255);