📄 rgb.cc
字号:
//
// rgb.cc
//
// $Id: rgb.cc,v 1.1.1.1 2001/02/28 00:28:38 cstolte Exp $
//
#include <sgl/rgb.h>
///
/// RGB
///
int
RGB::operator==(const RGB &r2) const {
return (r2.r == r && r2.g == g && r2.b == b);
}
int
RGB::operator!=(const RGB &r2) const {
return (r2.r != r || r2.g != g || r2.b != b);
}
RGB
RGB::operator-() const {
return RGB(-r, -g, -b);
}
RGB
RGB::operator+(const RGB &r2) const {
return RGB(r + r2.r, g + r2.g, b + r2.b);
}
RGB &
RGB::operator+=(const RGB &r2) {
r += r2.r;
g += r2.g;
b += r2.b;
return *this;
}
RGB
RGB::operator-(const RGB &r2) const {
return RGB(r - r2.r, g - r2.g, b - r2.b);
}
RGB &
RGB::operator-=(const RGB &r2) {
r -= r2.r;
g -= r2.g;
b -= r2.b;
return *this;
}
RGB
RGB::operator*(const RGB &r2) const {
return RGB(r * r2.r, g * r2.g, b * r2.b);
}
RGB &
RGB::operator*=(const RGB &r2) {
r *= r2.r;
g *= r2.g;
b *= r2.b;
return *this;
}
RGB
RGB::operator*(double d) const {
return RGB(r * d, g * d, b * d);
}
RGB &
RGB::operator*=(double d) {
r *= d;
g *= d;
b *= d;
return *this;
}
RGB
operator*(double s, const RGB &c) {
return RGB(s * c.r, s * c.g, s * c.b);
}
RGB
RGB::operator/(double d) const {
return RGB(r / d, g / d, b / d);
}
RGB &
RGB::operator/=(double d) {
r /= d;
g /= d;
b /= d;
return *this;
}
RGB
RGB::clamp(double min, double max) const {
RGB ret(*this);
ret.r = ::clamp(ret.r, min, max);
ret.g = ::clamp(ret.g, min, max);
ret.b = ::clamp(ret.b, min, max);
return ret;
}
int
RGB::out_of_gammut() const {
return (r > 1. || r < 0. || g > 1. || g < 0. || b > 1. || b < 0.);
}
RGB
RGB::scale_to_gammut(double m) const {
double max_component = max(r, max(g, b));
if (max_component > m) {
double scale = m / max_component;
return RGB(r * scale, g * scale, b * scale);
}
else
return *this;
}
RGB::RGB(const ShortRGB &srgb) {
r = double(srgb.r) / 255;
g = double(srgb.g) / 255;
b = double(srgb.b) / 255;
}
RGB
RGB::const_intensity_clip() const {
if (out_of_gammut() == true)
{
double axis_RGB = (r + g + b) / 3.;
assert(axis_RGB > 0.);
if (axis_RGB > 1 - epsilon)
return RGB(1., 1., 1.);
else
{
RGB diff(*this - RGB(axis_RGB, axis_RGB, axis_RGB));
double diff_mult = 1;
if (r > 1.)
diff_mult = min(diff_mult,
((1 - epsilon) - axis_RGB) / diff.r);
else if (r < 0.)
diff_mult = min(diff_mult,
(epsilon - axis_RGB) / diff.r);
if (g > 1.)
diff_mult = min(diff_mult,
((1 - epsilon) - axis_RGB) / diff.g);
else if (g < 0.)
diff_mult = min(diff_mult,
(epsilon - axis_RGB) / diff.g);
if (b > 1.)
diff_mult = min(diff_mult,
((1 - epsilon) - axis_RGB) / diff.b);
else if (b < 0.)
diff_mult = min(diff_mult,
(epsilon - axis_RGB) / diff.b);
return RGB(axis_RGB + diff.r * diff_mult,
axis_RGB + diff.g * diff_mult,
axis_RGB + diff.b * diff_mult);
}
}
else
return *this;
}
///
/// RGBA
//
RGBA::RGBA(const ShortRGBA &rgba) {
r = double(rgba.r) / 255;
g = double(rgba.g) / 255;
b = double(rgba.b) / 255;
a = double(rgba.a) / 255;
}
int
RGBA::operator==(const RGBA &rgba) const {
return (r == rgba.r && g == rgba.g && b == rgba.b && a == rgba.a);
}
int
RGBA::operator!=(const RGBA &rgba) const {
return (r != rgba.r || g != rgba.g || b != rgba.b || a != rgba.a);
}
RGBA
RGBA::operator-() const {
return RGBA(-r, -g, -b, -a);
}
RGBA
RGBA::operator+(const RGBA &rgba) const {
return RGBA(r + rgba.r, g + rgba.g, b + rgba.b, a + rgba.a);
}
RGBA &
RGBA::operator+=(const RGBA &rgba) {
r += rgba.r;
g += rgba.g;
b += rgba.b;
a += rgba.a;
return *this;
}
RGBA
RGBA::operator-(const RGBA &rgba) const {
return RGBA(r - rgba.r, g - rgba.g, b - rgba.b, a - rgba.a);
}
RGBA &
RGBA::operator-=(const RGBA &rgba) {
r -= rgba.r;
g -= rgba.g;
b -= rgba.b;
a -= rgba.a;
return *this;
}
RGBA
RGBA::operator*(double d) const {
return RGBA(r * d, g * d, b * d, a * d);
}
RGBA
RGBA::operator/(double d) const {
return RGBA(r / d, g / d, b / d, a / d);
}
RGBA &
RGBA::operator*=(double d) {
r *= d;
g *= d;
b *= d;
a *= d;
return *this;
}
RGBA &
RGBA::operator/=(double d) {
r /= d;
g /= d;
b /= d;
a /= d;
return *this;
}
RGBA
operator*(double d, const RGBA &rgba) {
return RGBA(rgba.r * d, rgba.g * d, rgba.b * d, rgba.a * d);
}
RGBA &
RGBA::clear() {
r = g = b = a = 0;
return *this;
}
RGBA &
RGBA::over(const RGBA &rgba) {
return *this += (1 - a) * rgba;
}
RGBA &
RGBA::in(const RGBA &rgba) {
return *this *= rgba.a;
}
RGBA &
RGBA::out(const RGBA &rgba) {
return *this *= 1 - rgba.a;
}
RGBA &
RGBA::atop(const RGBA &rgba) {
double orig_a = a;
*this *= rgba.a;
return *this += (1 - orig_a) * rgba;
}
RGBA &
RGBA::xor(const RGBA &rgba) {
double orig_a = a;
*this *= 1 - rgba.a;
return *this += (1 - orig_a) * rgba;
}
RGBA &
RGBA::darken(double d) {
r *= d;
g *= d;
b *= d;
return *this;
}
RGBA &
RGBA::dissolve(double d) {
return *this *= d;
}
///
/// ShortRGB
///
ShortRGB::ShortRGB(const RGB &rgb) {
r = u_char(255 * rgb.r);
g = u_char(255 * rgb.g);
b = u_char(255 * rgb.b);
}
int
ShortRGB::operator==(const ShortRGB &r2) const {
return (r2.r == r && r2.g == g && r2.b == b);
}
int
ShortRGB::operator!=(const ShortRGB &r2) const {
return (r2.r != r || r2.g != g || r2.b != b);
}
ShortRGB
ShortRGB::operator+(const ShortRGB &r2) const {
return ShortRGB(r + r2.r, g + r2.g, b + r2.b);
}
ShortRGB &
ShortRGB::operator+=(const ShortRGB &r2) {
r += r2.r;
g += r2.g;
b += r2.b;
return *this;
}
ShortRGB
ShortRGB::operator-(const ShortRGB &r2) const {
return ShortRGB(r - r2.r, g - r2.g, b - r2.b);
}
ShortRGB &
ShortRGB::operator-=(const ShortRGB &r2) {
r -= r2.r;
g -= r2.g;
b -= r2.b;
return *this;
}
ShortRGB
ShortRGB::operator*(const ShortRGB &r2) const {
return ShortRGB((r * r2.r) / 255, (g * r2.g) / 255, (b * r2.b) / 255);
}
ShortRGB &
ShortRGB::operator*=(const ShortRGB &r2) {
r = (r * r2.r) / 255;
g = (g * r2.g) / 255;
b = (b * r2.b) / 255;
return *this;
}
ShortRGB
ShortRGB::operator*(double d) const {
return ShortRGB(u_char(r * d), u_char(g * d), u_char(b * d));
}
ShortRGB &
ShortRGB::operator*=(double d) {
r = u_char(r * d);
g = u_char(g * d);
b = u_char(b * d);
return *this;
}
ShortRGB
operator*(double s, const ShortRGB &c) {
return ShortRGB(u_char(s * c.r), u_char(s * c.g), u_char(s * c.b));
}
ShortRGB
ShortRGB::operator/(double d) const {
return ShortRGB(u_char(r / d), u_char(g / d), u_char(b / d));
}
ShortRGB &
ShortRGB::operator/=(double d) {
r = u_char(r / d);
g = u_char(g / d);
b = u_char(b / d);
return *this;
}
///
/// ShortRGBA
///
ShortRGBA::ShortRGBA(const RGBA &rgba) {
r = u_char(255. * rgba.r);
g = u_char(255. * rgba.g);
b = u_char(255. * rgba.b);
a = u_char(255. * rgba.a);
}
int
ShortRGBA::operator==(const ShortRGBA &rgba) const {
return (r == rgba.r && g == rgba.g && b == rgba.b && a == rgba.a);
}
int
ShortRGBA::operator!=(const ShortRGBA &rgba) const {
return (r != rgba.r || g != rgba.g || b != rgba.b || a != rgba.a);
}
ShortRGBA
ShortRGBA::operator+(const ShortRGBA &rgba) const {
return RGBA(r + rgba.r, g + rgba.g, b + rgba.b, a + rgba.a);
}
ShortRGBA &
ShortRGBA::operator+=(const ShortRGBA &rgba) {
r += rgba.r;
g += rgba.g;
b += rgba.b;
a += rgba.a;
return *this;
}
ShortRGBA
ShortRGBA::operator-(const ShortRGBA &rgba) const {
return RGBA(r - rgba.r, g - rgba.g, b - rgba.b, a - rgba.a);
}
ShortRGBA &
ShortRGBA::operator-=(const ShortRGBA &rgba) {
r -= rgba.r;
g -= rgba.g;
b -= rgba.b;
a -= rgba.a;
return *this;
}
ShortRGBA
ShortRGBA::operator*(double d) const {
return ShortRGBA(u_char(r * d), u_char(g * d), u_char(b * d),
u_char(a * d));
}
ShortRGBA
ShortRGBA::operator/(double d) const {
return ShortRGBA(u_char(r / d), u_char(g / d), u_char(b / d),
u_char(a / d));
}
ShortRGBA &
ShortRGBA::operator*=(double d) {
r = u_char(r * d);
g = u_char(g * d);
b = u_char(b * d);
a = u_char(a * d);
return *this;
}
ShortRGBA &
ShortRGBA::operator/=(double d) {
r = u_char(r / d);
g = u_char(g / d);
b = u_char(b / d);
a = u_char(a / d);
return *this;
}
ShortRGBA
operator*(double d, const ShortRGBA &rgba) {
return ShortRGBA(u_char(rgba.r * d), u_char(rgba.g * d),
u_char(rgba.b * d), u_char(rgba.a * d));
}
ShortRGBA &
ShortRGBA::clear() {
r = g = b = a = 0;
return *this;
}
ShortRGBA &
ShortRGBA::over(const ShortRGBA &rgba) {
return *this += (1 - A()) * rgba;
}
ShortRGBA &
ShortRGBA::in(const ShortRGBA &rgba) {
return *this *= rgba.A();
}
ShortRGBA &
ShortRGBA::out(const ShortRGBA &rgba) {
return *this *= (1 - rgba.A());
}
ShortRGBA &
ShortRGBA::atop(const ShortRGBA &rgba) {
double orig_a = A();
*this *= 1 - rgba.A();
return *this += (1 - orig_a) * rgba;
}
ShortRGBA &
ShortRGBA::xor(const ShortRGBA &rgba) {
double orig_a = A();
*this *= 1 - rgba.A();
return *this += (1 - orig_a) * rgba;
}
ShortRGBA &
ShortRGBA::darken(double d) {
r = u_char(r * d);
g = u_char(g * d);
b = u_char(b * d);
return *this;
}
ShortRGBA &
ShortRGBA::dissolve(double d) {
return *this *= d;
}
///
/// RGBPrimary
///
RGBPrimary::RGBPrimary(double RGBPrimary[3], const Primary *p)
: primary(p), RGB(RGBPrimary[0], RGBPrimary[1], RGBPrimary[2]) {
}
RGBPrimary::RGBPrimary(const XYZ &XYZ, const Primary *p) {
*this = p->RGB(XYZ);
}
RGBPrimary::RGBPrimary(const RGBPrimary &c, const Primary *p) {
if (p == c.primary)
*this = c;
else
*this = RGBPrimary(c.toXYZ(), p);
}
int
RGBPrimary::operator==(const RGBPrimary &c) const {
// how handle if primary is different? etc...
return (r == c.r && g == c.g && b == c.b);
}
RGBPrimary
RGBPrimary::operator+(const RGBPrimary &c) const {
return RGBPrimary(r+c.r, g+c.g, b+c.b);
}
RGBPrimary &
RGBPrimary::operator+=(const RGBPrimary &c) {
r += c.r;
g += c.g;
b += c.b;
return *this;
}
RGBPrimary
RGBPrimary::operator-(const RGBPrimary &c) const {
return RGBPrimary(r-c.r, g-c.g, b-c.b);
}
RGBPrimary &
RGBPrimary::operator-=(const RGBPrimary &c) {
r -= c.r;
g -= c.g;
b -= c.b;
return *this;
}
RGBPrimary
RGBPrimary::operator*(const RGBPrimary &c) const {
return RGBPrimary(r * c.r, g * c.g, b * c.b);
}
RGBPrimary &
RGBPrimary::operator*=(const RGBPrimary &c) {
r *= c.r;
g *= c.g;
b *= c.b;
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -