summaryrefslogtreecommitdiffhomepage
path: root/nGenEx/Color.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'nGenEx/Color.cpp')
-rw-r--r--nGenEx/Color.cpp750
1 files changed, 375 insertions, 375 deletions
diff --git a/nGenEx/Color.cpp b/nGenEx/Color.cpp
index 55ea071..d5f5293 100644
--- a/nGenEx/Color.cpp
+++ b/nGenEx/Color.cpp
@@ -1,15 +1,15 @@
/* Project nGenEx
- Destroyer Studios LLC
- Copyright © 1997-2004. All Rights Reserved.
+ Destroyer Studios LLC
+ Copyright © 1997-2004. All Rights Reserved.
- SUBSYSTEM: nGenEx.lib
- FILE: Color.cpp
- AUTHOR: John DiCamillo
+ SUBSYSTEM: nGenEx.lib
+ FILE: Color.cpp
+ AUTHOR: John DiCamillo
- OVERVIEW
- ========
- Universal Color Format class
+ OVERVIEW
+ ========
+ Universal Color Format class
*/
#include "MemDebug.h"
@@ -21,7 +21,7 @@
// +--------------------------------------------------------------------+
void Print(const char* fmt, ...);
-
+
Color Color::White = Color(255,255,255);
Color Color::Black = Color( 0, 0, 0);
Color Color::Gray = Color(128,128,128);
@@ -61,9 +61,9 @@ DWORD* ColorIndex::texture_palette = ColorIndex::texture_palettes[0];
Color::Color(BYTE index)
{
- PALETTEENTRY* p = &palette[index];
-
- Set(p->peRed, p->peGreen, p->peBlue);
+ PALETTEENTRY* p = &palette[index];
+
+ Set(p->peRed, p->peGreen, p->peBlue);
}
// +--------------------------------------------------------------------+
@@ -71,22 +71,22 @@ Color::Color(BYTE index)
Color&
Color::operator+=(const Color& c)
{
- int r = Red() + c.Red(); if (r > 255) r = 255;
- int g = Green() + c.Green(); if (g > 255) g = 255;
- int b = Blue() + c.Blue(); if (b > 255) b = 255;
+ int r = Red() + c.Red(); if (r > 255) r = 255;
+ int g = Green() + c.Green(); if (g > 255) g = 255;
+ int b = Blue() + c.Blue(); if (b > 255) b = 255;
- Set((BYTE) r, (BYTE) g, (BYTE) b);
- return *this;
+ Set((BYTE) r, (BYTE) g, (BYTE) b);
+ return *this;
}
Color
Color::operator+(DWORD d) const
{
- int r = Red() + ((d & RMask) >> RShift); if (r > 255) r = 255;
- int g = Green() + ((d & GMask) >> GShift); if (g > 255) g = 255;
- int b = Blue() + ((d & BMask) >> BShift); if (b > 255) b = 255;
+ int r = Red() + ((d & RMask) >> RShift); if (r > 255) r = 255;
+ int g = Green() + ((d & GMask) >> GShift); if (g > 255) g = 255;
+ int b = Blue() + ((d & BMask) >> BShift); if (b > 255) b = 255;
- return Color((BYTE) r,(BYTE) g,(BYTE) b);
+ return Color((BYTE) r,(BYTE) g,(BYTE) b);
}
// +--------------------------------------------------------------------+
@@ -94,14 +94,14 @@ Color::operator+(DWORD d) const
Color
Color::operator+(const Color& c) const
{
- float src_alpha = c.fAlpha();
- float dst_alpha = 1.0f - src_alpha;
+ float src_alpha = c.fAlpha();
+ float dst_alpha = 1.0f - src_alpha;
- BYTE r = (BYTE)((fRed() *dst_alpha + c.fRed() *src_alpha)*255.0f);
- BYTE g = (BYTE)((fGreen()*dst_alpha + c.fGreen()*src_alpha)*255.0f);
- BYTE b = (BYTE)((fBlue() *dst_alpha + c.fBlue() *src_alpha)*255.0f);
+ BYTE r = (BYTE)((fRed() *dst_alpha + c.fRed() *src_alpha)*255.0f);
+ BYTE g = (BYTE)((fGreen()*dst_alpha + c.fGreen()*src_alpha)*255.0f);
+ BYTE b = (BYTE)((fBlue() *dst_alpha + c.fBlue() *src_alpha)*255.0f);
- return Color(r, g, b);
+ return Color(r, g, b);
}
// +--------------------------------------------------------------------+
@@ -109,11 +109,11 @@ Color::operator+(const Color& c) const
Color
Color::operator*(const Color& c) const
{
- BYTE r = (BYTE) ((fRed() * c.fRed()) *255.0f);
- BYTE g = (BYTE) ((fGreen() * c.fGreen()) *255.0f);
- BYTE b = (BYTE) ((fBlue() * c.fBlue()) *255.0f);
+ BYTE r = (BYTE) ((fRed() * c.fRed()) *255.0f);
+ BYTE g = (BYTE) ((fGreen() * c.fGreen()) *255.0f);
+ BYTE b = (BYTE) ((fBlue() * c.fBlue()) *255.0f);
- return Color(r, g, b);
+ return Color(r, g, b);
}
// +--------------------------------------------------------------------+
@@ -121,22 +121,22 @@ Color::operator*(const Color& c) const
Color
Color::operator*(double scale) const
{
- int r = fast_f2i(Red() * scale); if (r > 255) r = 255;
- int g = fast_f2i(Green() * scale); if (g > 255) g = 255;
- int b = fast_f2i(Blue() * scale); if (b > 255) b = 255;
- int a = fast_f2i(Alpha() * scale); if (a > 255) a = 255;
+ int r = fast_f2i(Red() * scale); if (r > 255) r = 255;
+ int g = fast_f2i(Green() * scale); if (g > 255) g = 255;
+ int b = fast_f2i(Blue() * scale); if (b > 255) b = 255;
+ int a = fast_f2i(Alpha() * scale); if (a > 255) a = 255;
- return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) a);
+ return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) a);
}
Color
Color::dim(double scale) const
{
- int r = fast_f2i(Red() * scale);
- int g = fast_f2i(Green() * scale);
- int b = fast_f2i(Blue() * scale);
+ int r = fast_f2i(Red() * scale);
+ int g = fast_f2i(Green() * scale);
+ int b = fast_f2i(Blue() * scale);
- return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) Alpha());
+ return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) Alpha());
}
// +--------------------------------------------------------------------+
@@ -144,36 +144,36 @@ Color::dim(double scale) const
DWORD
Color::Formatted() const
{
- if (format.pal) {
- return Index();
- }
+ if (format.pal) {
+ return Index();
+ }
- else {
- if (fade != 1.0) {
- double step = (1.0 - fade);
+ else {
+ if (fade != 1.0) {
+ double step = (1.0 - fade);
- DWORD r = ((int) ((fRed() - (fRed() - fade_color.fRed()) * step)*255.0)) >> format.rdown;
- DWORD g = ((int) ((fGreen() - (fGreen() - fade_color.fGreen())* step)*255.0)) >> format.gdown;
- DWORD b = ((int) ((fBlue() - (fBlue() - fade_color.fBlue()) * step)*255.0)) >> format.bdown;
- DWORD a = Alpha()>>format.adown;
+ DWORD r = ((int) ((fRed() - (fRed() - fade_color.fRed()) * step)*255.0)) >> format.rdown;
+ DWORD g = ((int) ((fGreen() - (fGreen() - fade_color.fGreen())* step)*255.0)) >> format.gdown;
+ DWORD b = ((int) ((fBlue() - (fBlue() - fade_color.fBlue()) * step)*255.0)) >> format.bdown;
+ DWORD a = Alpha()>>format.adown;
- return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);
+ return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);
- }
+ }
- else if (standard_format) {
- return rgba;
- }
+ else if (standard_format) {
+ return rgba;
+ }
- else {
- DWORD r = Red() >>format.rdown;
- DWORD g = Green()>>format.gdown;
- DWORD b = Blue() >>format.bdown;
- DWORD a = Alpha()>>format.adown;
-
- return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);
- }
- }
+ else {
+ DWORD r = Red() >>format.rdown;
+ DWORD g = Green()>>format.gdown;
+ DWORD b = Blue() >>format.bdown;
+ DWORD a = Alpha()>>format.adown;
+
+ return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);
+ }
+ }
}
// +--------------------------------------------------------------------+
@@ -181,21 +181,21 @@ Color::Formatted() const
Color
Color::Faded() const
{
- if (fade != 1.0) {
- double step = (1.0 - fade);
+ if (fade != 1.0) {
+ double step = (1.0 - fade);
- DWORD r = ((int) ((fRed() - (fRed() - fade_color.fRed()) * step)*255.0));
- DWORD g = ((int) ((fGreen() - (fGreen() - fade_color.fGreen())* step)*255.0));
- DWORD b = ((int) ((fBlue() - (fBlue() - fade_color.fBlue()) * step)*255.0));
- DWORD a = Alpha();
+ DWORD r = ((int) ((fRed() - (fRed() - fade_color.fRed()) * step)*255.0));
+ DWORD g = ((int) ((fGreen() - (fGreen() - fade_color.fGreen())* step)*255.0));
+ DWORD b = ((int) ((fBlue() - (fBlue() - fade_color.fBlue()) * step)*255.0));
+ DWORD a = Alpha();
- return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) a);
+ return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) a);
- }
+ }
- else {
- return *this;
- }
+ else {
+ return *this;
+ }
}
// +--------------------------------------------------------------------+
@@ -203,21 +203,21 @@ Color::Faded() const
DWORD
Color::Unfaded() const
{
- if (standard_format) {
- return rgba;
- }
+ if (standard_format) {
+ return rgba;
+ }
- if (format.pal) {
- return Index();
- }
- else {
- DWORD r = Red() >>format.rdown;
- DWORD g = Green()>>format.gdown;
- DWORD b = Blue() >>format.bdown;
- DWORD a = Alpha()>>format.adown;
-
- return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);
- }
+ if (format.pal) {
+ return Index();
+ }
+ else {
+ DWORD r = Red() >>format.rdown;
+ DWORD g = Green()>>format.gdown;
+ DWORD b = Blue() >>format.bdown;
+ DWORD a = Alpha()>>format.adown;
+
+ return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);
+ }
}
// +--------------------------------------------------------------------+
@@ -225,41 +225,41 @@ Color::Unfaded() const
DWORD
Color::TextureFormat(int keep_alpha) const
{
- if (texture_format[texture_alpha_level].pal) {
- return Index();
- }
- else if (rgba == 0) {
- return 0;
- }
- else {
- ColorFormat& tf = texture_format[texture_alpha_level];
-
- DWORD r = Red();
- DWORD g = Green();
- DWORD b = Blue();
- DWORD a = 0;
-
- if (keep_alpha) {
- a = Alpha()>>tf.adown;
- }
-
- else if (texture_alpha_level == 1) {
- // transparent:
- a = 255>>tf.adown;
- }
-
- else if (texture_alpha_level == 2) {
- // translucent:
- if (r || g || b)
- a = ((r+g+b+255)>>2)>>tf.adown;
- }
-
- r = r >>tf.rdown;
- g = g >>tf.gdown;
- b = b >>tf.bdown;
-
- return (r<<tf.rshift)|(g<<tf.gshift)|(b<<tf.bshift)|(a<<tf.ashift);
- }
+ if (texture_format[texture_alpha_level].pal) {
+ return Index();
+ }
+ else if (rgba == 0) {
+ return 0;
+ }
+ else {
+ ColorFormat& tf = texture_format[texture_alpha_level];
+
+ DWORD r = Red();
+ DWORD g = Green();
+ DWORD b = Blue();
+ DWORD a = 0;
+
+ if (keep_alpha) {
+ a = Alpha()>>tf.adown;
+ }
+
+ else if (texture_alpha_level == 1) {
+ // transparent:
+ a = 255>>tf.adown;
+ }
+
+ else if (texture_alpha_level == 2) {
+ // translucent:
+ if (r || g || b)
+ a = ((r+g+b+255)>>2)>>tf.adown;
+ }
+
+ r = r >>tf.rdown;
+ g = g >>tf.gdown;
+ b = b >>tf.bdown;
+
+ return (r<<tf.rshift)|(g<<tf.gshift)|(b<<tf.bshift)|(a<<tf.ashift);
+ }
}
// +--------------------------------------------------------------------+
@@ -267,26 +267,26 @@ Color::TextureFormat(int keep_alpha) const
Color
Color::ShadeColor(int shade) const
{
- double fr = fRed(), sr = fr;
- double fg = fGreen(), sg = fg;
- double fb = fBlue(), sb = fb;
- double range = SHADE_LEVELS;
-
- // first shade:
- if (shade < SHADE_LEVELS) { // shade towards black
- sr = fr * (shade/range);
- sg = fg * (shade/range);
- sb = fb * (shade/range);
- }
- else if (shade > SHADE_LEVELS) { // shade towards white
- double step = (shade - range)/range;
-
- sr = fr - (fr - 1.0) * step;
- sg = fg - (fg - 1.0) * step;
- sb = fb - (fb - 1.0) * step;
- }
-
- return Color((BYTE) (sr*255.0), (BYTE) (sg*255.0), (BYTE) (sb*255.0), (BYTE) Alpha());
+ double fr = fRed(), sr = fr;
+ double fg = fGreen(), sg = fg;
+ double fb = fBlue(), sb = fb;
+ double range = SHADE_LEVELS;
+
+ // first shade:
+ if (shade < SHADE_LEVELS) { // shade towards black
+ sr = fr * (shade/range);
+ sg = fg * (shade/range);
+ sb = fb * (shade/range);
+ }
+ else if (shade > SHADE_LEVELS) { // shade towards white
+ double step = (shade - range)/range;
+
+ sr = fr - (fr - 1.0) * step;
+ sg = fg - (fg - 1.0) * step;
+ sb = fb - (fb - 1.0) * step;
+ }
+
+ return Color((BYTE) (sr*255.0), (BYTE) (sg*255.0), (BYTE) (sb*255.0), (BYTE) Alpha());
}
// +--------------------------------------------------------------------+
@@ -294,7 +294,7 @@ Color::ShadeColor(int shade) const
DWORD
Color::Shaded(int shade) const
{
- return ShadeColor(shade).Formatted();
+ return ShadeColor(shade).Formatted();
}
// +--------------------------------------------------------------------+
@@ -302,22 +302,22 @@ Color::Shaded(int shade) const
Color
Color::Unformat(DWORD formatted_color)
{
- if (format.pal) {
- return Color((BYTE) formatted_color);
- }
- else if (standard_format) {
- Color c;
- c.Set(formatted_color);
- return c;
- }
- else {
- BYTE r = (BYTE) ((formatted_color & format.rmask)>>format.rshift) << format.rdown;
- BYTE g = (BYTE) ((formatted_color & format.gmask)>>format.gshift) << format.gdown;
- BYTE b = (BYTE) ((formatted_color & format.bmask)>>format.bshift) << format.bdown;
- BYTE a = (BYTE) ((formatted_color & format.amask)>>format.ashift) << format.adown;
-
- return Color(r,g,b,a);
- }
+ if (format.pal) {
+ return Color((BYTE) formatted_color);
+ }
+ else if (standard_format) {
+ Color c;
+ c.Set(formatted_color);
+ return c;
+ }
+ else {
+ BYTE r = (BYTE) ((formatted_color & format.rmask)>>format.rshift) << format.rdown;
+ BYTE g = (BYTE) ((formatted_color & format.gmask)>>format.gshift) << format.gdown;
+ BYTE b = (BYTE) ((formatted_color & format.bmask)>>format.bshift) << format.bdown;
+ BYTE a = (BYTE) ((formatted_color & format.amask)>>format.ashift) << format.adown;
+
+ return Color(r,g,b,a);
+ }
}
// +--------------------------------------------------------------------+
@@ -325,12 +325,12 @@ Color::Unformat(DWORD formatted_color)
Color
Color::Scale(const Color& c1, const Color& c2, double scale)
{
- BYTE r = (BYTE) ((c1.fRed() + (c2.fRed() - c1.fRed() )*scale) * 255);
- BYTE g = (BYTE) ((c1.fGreen() + (c2.fGreen() - c1.fGreen())*scale) * 255);
- BYTE b = (BYTE) ((c1.fBlue() + (c2.fBlue() - c1.fBlue() )*scale) * 255);
- BYTE a = (BYTE) ((c1.fAlpha() + (c2.fAlpha() - c1.fAlpha())*scale) * 255);
-
- return Color(r,g,b,a);
+ BYTE r = (BYTE) ((c1.fRed() + (c2.fRed() - c1.fRed() )*scale) * 255);
+ BYTE g = (BYTE) ((c1.fGreen() + (c2.fGreen() - c1.fGreen())*scale) * 255);
+ BYTE b = (BYTE) ((c1.fBlue() + (c2.fBlue() - c1.fBlue() )*scale) * 255);
+ BYTE a = (BYTE) ((c1.fAlpha() + (c2.fAlpha() - c1.fAlpha())*scale) * 255);
+
+ return Color(r,g,b,a);
}
// +--------------------------------------------------------------------+
@@ -338,22 +338,22 @@ Color::Scale(const Color& c1, const Color& c2, double scale)
DWORD
Color::FormattedBlend(DWORD c1, DWORD c2)
{
- if (format.pal) {
- return ColorIndex::blend_table[(BYTE) c1 * 256 + (BYTE) c2];
- }
- else {
- ColorFormat& cf = format;
+ if (format.pal) {
+ return ColorIndex::blend_table[(BYTE) c1 * 256 + (BYTE) c2];
+ }
+ else {
+ ColorFormat& cf = format;
- DWORD r = (c1 & cf.rmask) + (c2 & cf.rmask);
- DWORD g = (c1 & cf.gmask) + (c2 & cf.gmask);
- DWORD b = (c1 & cf.bmask) + (c2 & cf.bmask);
+ DWORD r = (c1 & cf.rmask) + (c2 & cf.rmask);
+ DWORD g = (c1 & cf.gmask) + (c2 & cf.gmask);
+ DWORD b = (c1 & cf.bmask) + (c2 & cf.bmask);
- if (r & ~cf.rmask) r = cf.rmask;
- if (g & ~cf.gmask) g = cf.gmask;
- if (b & ~cf.bmask) b = cf.bmask;
+ if (r & ~cf.rmask) r = cf.rmask;
+ if (g & ~cf.gmask) g = cf.gmask;
+ if (b & ~cf.bmask) b = cf.bmask;
- return (DWORD) (r|g|b);
- }
+ return (DWORD) (r|g|b);
+ }
}
// +--------------------------------------------------------------------+
@@ -361,7 +361,7 @@ Color::FormattedBlend(DWORD c1, DWORD c2)
void
Color::UseVideo(Video* v)
{
- video = v;
+ video = v;
}
// +--------------------------------------------------------------------+
@@ -369,30 +369,30 @@ Color::UseVideo(Video* v)
void
Color::UseFormat(const ColorFormat& cf)
{
- format = cf;
+ format = cf;
- if (format.rmask == RMask && format.gmask == GMask && format.bmask == BMask)
- standard_format = true;
- else
- standard_format = false;
+ if (format.rmask == RMask && format.gmask == GMask && format.bmask == BMask)
+ standard_format = true;
+ else
+ standard_format = false;
- if (cf.pal) {
- for (int i = 0; i < 256; i++) {
- ColorIndex::formatted_palette[i] = i;
- ColorIndex::unfaded_palette[i] = i;
- }
- }
- else {
- double old_fade = fade;
+ if (cf.pal) {
+ for (int i = 0; i < 256; i++) {
+ ColorIndex::formatted_palette[i] = i;
+ ColorIndex::unfaded_palette[i] = i;
+ }
+ }
+ else {
+ double old_fade = fade;
- for (int i = 0; i < 256; i++) {
- ColorIndex::formatted_palette[i] = Color(i).Formatted();
+ for (int i = 0; i < 256; i++) {
+ ColorIndex::formatted_palette[i] = Color(i).Formatted();
- fade = 1.0;
- ColorIndex::unfaded_palette[i] = Color(i).Formatted();
- fade = old_fade;
- }
- }
+ fade = 1.0;
+ ColorIndex::unfaded_palette[i] = Color(i).Formatted();
+ fade = old_fade;
+ }
+ }
}
// +--------------------------------------------------------------------+
@@ -400,23 +400,23 @@ Color::UseFormat(const ColorFormat& cf)
void
Color::UseTextureFormat(const ColorFormat& cf, int alpha_level)
{
- texture_format[alpha_level] = cf;
-
- if (cf.pal) {
- for (int i = 0; i < 256; i++) {
- ColorIndex::texture_palettes[alpha_level][i] = i;
- }
- }
- else {
- double old_fade = fade;
-
- for (int i = 0; i < 256; i++) {
- int old_texture_alpha_level = texture_alpha_level;
- texture_alpha_level = alpha_level;
- ColorIndex::texture_palettes[alpha_level][i] = Color(i).TextureFormat();
- texture_alpha_level = old_texture_alpha_level;
- }
- }
+ texture_format[alpha_level] = cf;
+
+ if (cf.pal) {
+ for (int i = 0; i < 256; i++) {
+ ColorIndex::texture_palettes[alpha_level][i] = i;
+ }
+ }
+ else {
+ double old_fade = fade;
+
+ for (int i = 0; i < 256; i++) {
+ int old_texture_alpha_level = texture_alpha_level;
+ texture_alpha_level = alpha_level;
+ ColorIndex::texture_palettes[alpha_level][i] = Color(i).TextureFormat();
+ texture_alpha_level = old_texture_alpha_level;
+ }
+ }
}
// +--------------------------------------------------------------------+
@@ -424,36 +424,36 @@ Color::UseTextureFormat(const ColorFormat& cf, int alpha_level)
void
Color::WithTextureFormat(int alpha_level)
{
- texture_alpha_level = alpha_level;
- ColorIndex::texture_palette = ColorIndex::texture_palettes[alpha_level];
+ texture_alpha_level = alpha_level;
+ ColorIndex::texture_palette = ColorIndex::texture_palettes[alpha_level];
}
// +--------------------------------------------------------------------+
static BYTE MatchRGB(PALETTEENTRY* pal, BYTE r, BYTE g, BYTE b)
{
- double mindist = 100000000.0;
- BYTE match = 0;
+ double mindist = 100000000.0;
+ BYTE match = 0;
- for (int i = 0; i < 256; i++) {
- PALETTEENTRY* p = pal++;
+ for (int i = 0; i < 256; i++) {
+ PALETTEENTRY* p = pal++;
- double dr = p->peRed - r;
- double dg = p->peGreen - g;
- double db = p->peBlue - b;
-
- double d = (dr*dr) + (dg*dg) + (db*db);
+ double dr = p->peRed - r;
+ double dg = p->peGreen - g;
+ double db = p->peBlue - b;
+
+ double d = (dr*dr) + (dg*dg) + (db*db);
- if (d < mindist) {
- mindist = d;
- match = i;
-
- if (d < 1.0)
- return match;
- }
- }
+ if (d < mindist) {
+ mindist = d;
+ match = i;
+
+ if (d < 1.0)
+ return match;
+ }
+ }
- return match;
+ return match;
}
// +--------------------------------------------------------------------+
@@ -461,39 +461,39 @@ static BYTE MatchRGB(PALETTEENTRY* pal, BYTE r, BYTE g, BYTE b)
void
Color::SetPalette(PALETTEENTRY* pal, int palsize, BYTE* invpal)
{
- for (int i = 0; i < palsize; i++)
- palette[i] = pal[i];
-
- if (invpal) {
- for (int i = 0; i < 32768; i++)
- table[i] = invpal[i];
- }
- else {
- for (int i = 0; i < 32768; i++) {
- BYTE r = (i >> 10) & 0x1f;
- BYTE g = (i >> 5) & 0x1f;
- BYTE b = (i ) & 0x1f;
-
- Color c(r<<3, g<<3, b<<3);
-
- table[i] = MatchRGB(palette, r<<3, g<<3, b<<3);
- }
- }
-
- // set up formatted palette:
- UseFormat(format);
-
- for (int i = 0; i < 4; i++)
- UseTextureFormat(texture_format[i], i);
-
- // set up shade table:
- double old_fade = fade;
- fade = 1.0;
- BuildShadeTable();
- fade = old_fade;
-
- // and blend table:
- BuildBlendTable();
+ for (int i = 0; i < palsize; i++)
+ palette[i] = pal[i];
+
+ if (invpal) {
+ for (int i = 0; i < 32768; i++)
+ table[i] = invpal[i];
+ }
+ else {
+ for (int i = 0; i < 32768; i++) {
+ BYTE r = (i >> 10) & 0x1f;
+ BYTE g = (i >> 5) & 0x1f;
+ BYTE b = (i ) & 0x1f;
+
+ Color c(r<<3, g<<3, b<<3);
+
+ table[i] = MatchRGB(palette, r<<3, g<<3, b<<3);
+ }
+ }
+
+ // set up formatted palette:
+ UseFormat(format);
+
+ for (int i = 0; i < 4; i++)
+ UseTextureFormat(texture_format[i], i);
+
+ // set up shade table:
+ double old_fade = fade;
+ fade = 1.0;
+ BuildShadeTable();
+ fade = old_fade;
+
+ // and blend table:
+ BuildBlendTable();
}
// +--------------------------------------------------------------------+
@@ -501,15 +501,15 @@ Color::SetPalette(PALETTEENTRY* pal, int palsize, BYTE* invpal)
void
Color::SavePalette(const char* basename)
{
- char filename[256];
-
- sprintf_s(filename, "%s.ipl", basename);
- FILE* f;
- fopen_s(&f, filename, "wb");
- if (f) {
- fwrite(table, sizeof(table), 1, f);
- fclose(f);
- }
+ char filename[256];
+
+ sprintf_s(filename, "%s.ipl", basename);
+ FILE* f;
+ fopen_s(&f, filename, "wb");
+ if (f) {
+ fwrite(table, sizeof(table), 1, f);
+ fclose(f);
+ }
}
// +--------------------------------------------------------------------+
@@ -517,45 +517,45 @@ Color::SavePalette(const char* basename)
void
Color::SetFade(double f, Color c, int build_shade)
{
- static int shade_built = 0;
-
- if (fade == f && fade_color == c && (build_shade ? shade_built : 1))
- return;
-
- fade = f;
- fade_color = c;
-
- // set up formatted palette:
- UseFormat(format);
-
- // if this is a paletted video mode,
- // modify the video palette as well:
- if (format.pal && video) {
- PALETTEENTRY fade_palette[256];
-
- double step = (1.0 - fade);
- for (int i = 0; i < 256; i++) {
- PALETTEENTRY& entry = fade_palette[i];
- ColorIndex c = ColorIndex(i);
-
- entry.peRed = ((int) ((c.fRed() - (c.fRed() - fade_color.fRed()) * step)*255.0));
- entry.peGreen = ((int) ((c.fGreen() - (c.fGreen() - fade_color.fGreen())* step)*255.0));
- entry.peBlue = ((int) ((c.fBlue() - (c.fBlue() - fade_color.fBlue()) * step)*255.0));
- entry.peFlags = 0;
- }
- }
-
- // otherwise, we need to re-compute
- // the shade table:
- else {
- if (build_shade) {
- BuildShadeTable();
- shade_built = 1;
- }
- else {
- shade_built = 0;
- }
- }
+ static int shade_built = 0;
+
+ if (fade == f && fade_color == c && (build_shade ? shade_built : 1))
+ return;
+
+ fade = f;
+ fade_color = c;
+
+ // set up formatted palette:
+ UseFormat(format);
+
+ // if this is a paletted video mode,
+ // modify the video palette as well:
+ if (format.pal && video) {
+ PALETTEENTRY fade_palette[256];
+
+ double step = (1.0 - fade);
+ for (int i = 0; i < 256; i++) {
+ PALETTEENTRY& entry = fade_palette[i];
+ ColorIndex c = ColorIndex(i);
+
+ entry.peRed = ((int) ((c.fRed() - (c.fRed() - fade_color.fRed()) * step)*255.0));
+ entry.peGreen = ((int) ((c.fGreen() - (c.fGreen() - fade_color.fGreen())* step)*255.0));
+ entry.peBlue = ((int) ((c.fBlue() - (c.fBlue() - fade_color.fBlue()) * step)*255.0));
+ entry.peFlags = 0;
+ }
+ }
+
+ // otherwise, we need to re-compute
+ // the shade table:
+ else {
+ if (build_shade) {
+ BuildShadeTable();
+ shade_built = 1;
+ }
+ else {
+ shade_built = 0;
+ }
+ }
}
// +--------------------------------------------------------------------+
@@ -563,9 +563,9 @@ Color::SetFade(double f, Color c, int build_shade)
void
Color::BuildShadeTable()
{
- for (int shade = 0; shade < SHADE_LEVELS*2; shade++)
- for (int index = 0; index < 256; index++)
- ColorIndex::shade_table[shade*256+index] = Color(index).Shaded(shade);
+ for (int shade = 0; shade < SHADE_LEVELS*2; shade++)
+ for (int index = 0; index < 256; index++)
+ ColorIndex::shade_table[shade*256+index] = Color(index).Shaded(shade);
}
// +--------------------------------------------------------------------+
@@ -573,22 +573,22 @@ Color::BuildShadeTable()
void
Color::BuildBlendTable()
{
- for (int src = 0; src < 256; src++) {
- for (int dst = 0; dst < 256; dst++) {
- ColorIndex src_clr = ColorIndex(src);
- ColorIndex dst_clr = ColorIndex(dst);
-
- int r = src_clr.Red() + dst_clr.Red();
- int g = src_clr.Green() + dst_clr.Green();
- int b = src_clr.Blue() + dst_clr.Blue();
-
- if (r>255) r=255;
- if (g>255) g=255;
- if (b>255) b=255;
+ for (int src = 0; src < 256; src++) {
+ for (int dst = 0; dst < 256; dst++) {
+ ColorIndex src_clr = ColorIndex(src);
+ ColorIndex dst_clr = ColorIndex(dst);
+
+ int r = src_clr.Red() + dst_clr.Red();
+ int g = src_clr.Green() + dst_clr.Green();
+ int b = src_clr.Blue() + dst_clr.Blue();
+
+ if (r>255) r=255;
+ if (g>255) g=255;
+ if (b>255) b=255;
- ColorIndex::blend_table[src*256+dst] = Color((BYTE)r,(BYTE)g,(BYTE)b).Index();
- }
- }
+ ColorIndex::blend_table[src*256+dst] = Color((BYTE)r,(BYTE)g,(BYTE)b).Index();
+ }
+ }
}
// +--------------------------------------------------------------------+
@@ -596,25 +596,25 @@ Color::BuildBlendTable()
void
Color::SaveShadeTable(const char* basename)
{
- if (!format.pal)
- return;
+ if (!format.pal)
+ return;
+
+ char filename[256];
+ sprintf_s(filename, "%s_clut.pcx", basename);
- char filename[256];
- sprintf_s(filename, "%s_clut.pcx", basename);
+ BYTE clut[256*256];
+ BYTE* pc = clut;
+ int i;
+ for (i = 0; i < SHADE_LEVELS*2; i++)
+ for (int j = 0; j < 256; j++)
+ *pc++ = (BYTE) ColorIndex::shade_table[i*256+j];
- BYTE clut[256*256];
- BYTE* pc = clut;
- int i;
- for (i = 0; i < SHADE_LEVELS*2; i++)
- for (int j = 0; j < 256; j++)
- *pc++ = (BYTE) ColorIndex::shade_table[i*256+j];
-
- for (; i < 256; i++)
- for (int j = 0; j < 256; j++)
- *pc++ = (BYTE) 0;
+ for (; i < 256; i++)
+ for (int j = 0; j < 256; j++)
+ *pc++ = (BYTE) 0;
- PcxImage pcx(256, 256, clut, (BYTE*) palette);
- pcx.Save(filename);
+ PcxImage pcx(256, 256, clut, (BYTE*) palette);
+ pcx.Save(filename);
}
@@ -626,47 +626,47 @@ Color::SaveShadeTable(const char* basename)
ColorValue&
ColorValue::operator+=(const ColorValue& c)
{
- r += c.r;
- g += c.g;
- b += c.b;
+ r += c.r;
+ g += c.g;
+ b += c.b;
- return *this;
+ return *this;
}
ColorValue
ColorValue::operator+(const ColorValue& c) const
{
- float src_alpha = c.a;
- float dst_alpha = 1.0f - a;
+ float src_alpha = c.a;
+ float dst_alpha = 1.0f - a;
- float fr = (r * dst_alpha) + (c.r * src_alpha);
- float fg = (g * dst_alpha) + (c.g * src_alpha);
- float fb = (b * dst_alpha) + (c.b * src_alpha);
+ float fr = (r * dst_alpha) + (c.r * src_alpha);
+ float fg = (g * dst_alpha) + (c.g * src_alpha);
+ float fb = (b * dst_alpha) + (c.b * src_alpha);
- return ColorValue(fr, fg, fb);
+ return ColorValue(fr, fg, fb);
}
ColorValue
ColorValue::operator*(const ColorValue& c) const
{
- return ColorValue(r*c.r, g*c.g, b*c.b);
+ return ColorValue(r*c.r, g*c.g, b*c.b);
}
ColorValue
ColorValue::operator*(double scale) const
{
- return ColorValue((float) (r*scale),
- (float) (g*scale),
- (float) (b*scale),
- (float) (a*scale));
+ return ColorValue((float) (r*scale),
+ (float) (g*scale),
+ (float) (b*scale),
+ (float) (a*scale));
}
ColorValue
ColorValue::dim(double scale) const
{
- return ColorValue((float) (r*scale),
- (float) (g*scale),
- (float) (b*scale));
+ return ColorValue((float) (r*scale),
+ (float) (g*scale),
+ (float) (b*scale));
}
// +--------------------------------------------------------------------+
@@ -676,8 +676,8 @@ inline BYTE bclamp(float x) { return (BYTE) ((x<0) ? 0 : (x>255) ? 255 : x); }
Color
ColorValue::ToColor() const
{
- return Color(bclamp(r * 255.0f),
- bclamp(g * 255.0f),
- bclamp(b * 255.0f),
- bclamp(a * 255.0f));
+ return Color(bclamp(r * 255.0f),
+ bclamp(g * 255.0f),
+ bclamp(b * 255.0f),
+ bclamp(a * 255.0f));
}