Skip to content

Instantly share code, notes, and snippets.

@Wunkolo
Created July 27, 2012 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wunkolo/3190707 to your computer and use it in GitHub Desktop.
Save Wunkolo/3190707 to your computer and use it in GitHub Desktop.
Color class
/*
* Color.cpp
*
* Created on: Jun 5, 2012
* Author: DEElekgolo
*/
#include "Color.hpp"
const Color Color::Black(0.f, 0.f, 0.f);
const Color Color::White(1.0f, 1.0f, 1.0f);
const Color Color::Red(1.0f, 0, 0);
const Color Color::Green(0, 1.0f, 0);
const Color Color::Blue(0, 0, 1.0f);
const Color Color::Yellow(1.0f, 1.0f, 0);
const Color Color::Magenta(1.0f, 0, 1.0f);
const Color Color::Cyan(0, 1.0f, 1.0f);
/*
* Color.hpp
*
* Created on: Jun 5, 2012
* Author: DEElekgolo
*/
#ifndef COLOR_HPP_
#define COLOR_HPP_
#include <math.h>
#include <algorithm>
class Color
{
public:
Color()
{
r = g = b = 0;
a = 1.0f;
}
Color( float fr, float fg, float fb, float fa = 1.0f)
{
r = fr;
g = fg;
b = fb;
a = fa;
}
Color( unsigned char cr, unsigned char cg, unsigned char cb, unsigned char ca = 0xFF)
{
r = (int)cr/(float)255;
g = (int)cg/(float)255;
b = (int)cb/(float)255;
a = (int)ca/(float)255;
}
Color( unsigned int ir, unsigned int ig, unsigned int ib, unsigned int ia = 0xFF)
{
r = (int)ir/(float)255;
g = (int)ig/(float)255;
b = (int)ib/(float)255;
a = (int)ia/(float)255;
}
~Color()
{
}
unsigned int ToRGBA() const
{
return ((int)(r*255) << 24) |
((int)(g*255) << 16) |
((int)(b*255) << 8) |
(int)(a*255);
}
#define RGB15(r,g,b) ((r)|((g)<<5)|((b)<<10))
unsigned int ToRGB15() const
{
return (unsigned int) (RGB15(
(unsigned char)(r*255)>>3,
(unsigned char)(g*255)>>3,
(unsigned char)(b*255)>>3
));
}
float* ToFloatPointer()
{
return (float*)this;
}
void GammaCorrect(float fGamma)
{
r = pow(r,fGamma);
g = pow(r,fGamma);
b = pow(r,fGamma);
}
void Interpolate(Color cColor, float fAmount)
{
r = r + fAmount * (cColor.r - r);
b = b + fAmount * (cColor.b - b);
g = g + fAmount * (cColor.g - g);
a = a + fAmount * (cColor.a - a);
}
void Multiply(Color cColor)
{
r = r * cColor.r;
g = g * cColor.g;
b = b * cColor.b;
}
void Darken (Color cColor)
{
r = std::min(r,cColor.r);
g = std::min(g,cColor.g);
b = std::min(b,cColor.b);
}
void ColorBurn(Color cColor)
{
r = 1.0f - (1.0f - r)/cColor.r;
g = 1.0f - (1.0f - g)/cColor.g;
b = 1.0f - (1.0f - b)/cColor.b;
}
void LinearBurn(Color cColor)
{
r = r + cColor.r - 1.0f;
g = g + cColor.g - 1.0f;
b = b + cColor.b - 1.0f;
}
void Lighten(Color cColor)
{
r = std::max(r,cColor.r);
g = std::max(g,cColor.g);
b = std::max(b,cColor.b);
}
void Screen(Color cColor)
{
r = 1.0f - (1.0f - r) * (1.0f - cColor.r);
g = 1.0f - (1.0f - g) * (1.0f - cColor.g);
b = 1.0f - (1.0f - b) * (1.0f - cColor.b);
}
void ColorDodge(Color cColor)
{
r = r/(1.0f-cColor.r);
g = g/(1.0f-cColor.g);
b = b/(1.0f-cColor.b);
}
void Overlay(Color cColor)
{
r>0.5f ? r = 1.0f - (1.0f-2.0f*(r-0.5f)) * (1.0f-cColor.r) :
r = (2.0f * r) * cColor.r;
g>0.5f ? r = 1.0f - (1.0f-2.0f*(g-0.5f)) * (1.0f-cColor.g) :
g = (2.0f * g) * cColor.g;
b>0.5f ? b = 1.0f - (1.0f-2.0f*(b-0.5f)) * (1.0f-cColor.b) :
b = (2.0f * b) * cColor.b;
}
void SoftLight(Color cColor)
{
cColor.r > 0.5f ? r = 1.0f - (1.0f - r) * (1.0f - (cColor.r - 0.5f)) :
r = r * (cColor.r + 0.5f);
cColor.g > 0.5f ? g = 1.0f - (1.0f - g) * (1.0f - (cColor.g - 0.5f)) :
g = g * (cColor.g + 0.5f);
cColor.b > 0.5f ? b = 1.0f - (1.0f - b) * (1.0f - (cColor.b - 0.5f)) :
b = b * (cColor.b + 0.5f);
}
void HardLight(Color cColor)
{
cColor.r > 0.5f ? r = 1.0f - (1.0f - r) * (1.0f - 2.0f * (cColor.r - 0.5f)) :
r = r * (2.0f * cColor.r);
cColor.g > 0.5f ? g = 1.0f - (1.0f - g) * (1.0f - 2.0f * (cColor.g - 0.5f)) :
g = g * (2.0f * cColor.g);
cColor.b > 0.5f ? b = 1.0f - (1.0f - b) * (1.0f - 2.0f * (cColor.b - 0.5f)) :
b = b * (2.0f * cColor.b);
}
void VividLight(Color cColor)
{
cColor.r > 0.5f ? r = 1.0f - (1.0f - r)/(2.0f*(cColor.r - 0.5f)) :
r = r / (1.0f - 2.0f * cColor.r);
cColor.g > 0.5f ? g = 1.0f - (1.0f - g)/(2.0f*(cColor.g - 0.5f)) :
g = g / (1.0f - 2.0f * cColor.g);
cColor.b > 0.5f ? b = 1.0f - (1.0f - b)/(2.0f*(cColor.b - 0.5f)) :
b = b / (1.0f - 2.0f * cColor.b);
}
void LinearLight(Color cColor)
{
cColor.r > 0.5f ? r = r + 2.0f*(cColor.r - 0.5f) :
r = r + 2.0f*(cColor.r - 1.0f);
cColor.g > 0.5f ? g = g + 2.0f*(cColor.g - 0.5f) :
g = g + 2.0f*(cColor.g - 1.0f);
cColor.b > 0.5f ? b = b + 2.0f*(cColor.b - 0.5f) :
b = b + 2.0f*(cColor.b - 1.0f);
}
void PinLight(Color cColor)
{
cColor.r > 0.5f ? r = std::max(r,2.0f*(cColor.r-0.5f)) :
r = r + 2.0f*(cColor.r - 1.0f);
cColor.g > 0.5f ? g = std::max(r,2.0f*(cColor.g-0.5f)) :
g = g + 2.0f*(cColor.g - 1.0f);
cColor.b > 0.5f ? b = std::max(b,2.0f*(cColor.b-0.5f)) :
b = b + 2.0f*(cColor.b - 1.0f);
}
void Difference(Color cColor)
{
r = abs(r - cColor.r);
g = abs(g - cColor.g);
b = abs(b - cColor.b);
}
void Exclusion(Color cColor)
{
r = 0.5f - 2.0f*(r-0.5f)*(cColor.r-0.5f);
g = 0.5f - 2.0f*(g-0.5f)*(cColor.g-0.5f);
b = 0.5f - 2.0f*(b-0.5f)*(cColor.b-0.5f);
}
bool operator ==( const Color& right)
{
return (r == right.r) &&
(g == right.g) &&
(b == right.b) &&
(a == right.a);
}
bool operator !=( const Color& right)
{
return !(*this == right);
}
Color operator +( const Color& right)
{
return Color(static_cast<unsigned char>(std::min(r + right.r, 1.0f)),
static_cast<unsigned char>(std::min(g + right.g, 1.0f)),
static_cast<unsigned char>(std::min(b + right.b, 1.0f)),
static_cast<unsigned char>(std::min(a + right.a, 1.0f)));
}
Color operator -( const Color& right)
{
return Color(static_cast<unsigned char>(std::min(r - right.r, 1.0f)),
static_cast<unsigned char>(std::min(g - right.g, 1.0f)),
static_cast<unsigned char>(std::min(b - right.b, 1.0f)),
static_cast<unsigned char>(std::min(a - right.a, 1.0f)));
}
Color operator *( const Color& right)
{
return Color(static_cast<unsigned char>(r * right.r / 1.0f),
static_cast<unsigned char>(g * right.g / 1.0f),
static_cast<unsigned char>(b * right.b / 1.0f),
static_cast<unsigned char>(a * right.a / 1.0f));
}
Color operator /( const Color& right)
{
return Color(static_cast<unsigned char>(std::min(r / right.r, 1.0f)),
static_cast<unsigned char>(std::min(g / right.g, 1.0f)),
static_cast<unsigned char>(std::min(b / right.b, 1.0f)),
static_cast<unsigned char>(std::min(a / right.a, 1.0f)));
}
Color& operator +=( const Color& right)
{
r += right.r;
g += right.g;
b += right.b;
a += right.a;
return *this;
}
Color& operator -=( const Color& right)
{
r -= right.r;
g -= right.g;
b -= right.b;
a -= right.a;
return *this;
}
Color& operator *=( const Color& right)
{
r *= right.r;
g *= right.g;
b *= right.b;
a *= right.a;
return *this;
}
Color& operator /=( const Color& right)
{
r /= right.r;
g /= right.g;
b /= right.b;
a /= right.a;
return *this;
}
float r,g,b,a;
static const Color Black; /// Black predefined color
static const Color White; /// White predefined color
static const Color Red; /// Red predefined color
static const Color Green; /// Green predefined color
static const Color Blue; /// Blue predefined color
static const Color Yellow; /// Yellow predefined color
static const Color Magenta; /// Magenta predefined color
static const Color Cyan; /// Cyan predefined color
};
#endif /* COLOR_HPP_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment