summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/Color.cpp
blob: 5ed6257036f35b77f8230eb09fd95349937f9f18 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Universal Color Format class
*/

#include "Color.h"
#include "Video.h"
#include "Pcx.h"
#include "Utils.h"

// +--------------------------------------------------------------------+

Color          Color::White      = Color(255,255,255);
Color          Color::Black      = Color(  0,  0,  0);
Color          Color::Gray       = Color(128,128,128);
Color          Color::LightGray  = Color(192,192,192);
Color          Color::DarkGray   = Color( 64, 64, 64);
Color          Color::BrightRed  = Color(255,  0,  0);
Color          Color::BrightBlue = Color(  0,  0,255);
Color          Color::BrightGreen= Color(  0,255,  0);
Color          Color::DarkRed    = Color(128,  0,  0);
Color          Color::DarkBlue   = Color(  0,  0,128);
Color          Color::DarkGreen  = Color(  0,128,  0);
Color          Color::Yellow     = Color(255,255,  0);
Color          Color::Cyan       = Color(  0,255,255);
Color          Color::Magenta    = Color(255,  0,255);
Color          Color::Tan        = Color(180,150,120);
Color          Color::Brown      = Color(128,100, 80);
Color          Color::Violet     = Color(128,  0,128);
Color          Color::Orange     = Color(255,150, 20);

bool           Color::standard_format = false;
int            Color::texture_alpha_level = 0;
ColorFormat    Color::format     = ColorFormat(256);
ColorFormat    Color::texture_format[4]  = { ColorFormat(256), ColorFormat(256), ColorFormat(256), ColorFormat(256) };
PALETTEENTRY   Color::palette[256];
BYTE           Color::table[32768];
double         Color::fade = 1.0;
Color          Color::fade_color;
Video*         Color::video = 0;
DWORD          ColorIndex::texture_palettes[4][256];
DWORD          ColorIndex::unfaded_palette[256];
DWORD          ColorIndex::formatted_palette[256];
DWORD          ColorIndex::shade_table[256*256];
BYTE           ColorIndex::blend_table[256*256];
DWORD*         ColorIndex::texture_palette = ColorIndex::texture_palettes[0];

// +--------------------------------------------------------------------+

Color::Color(BYTE index)
{
    PALETTEENTRY* p = &palette[index];

    Set(p->peRed, p->peGreen, p->peBlue);
}

// +--------------------------------------------------------------------+

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;

    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;

    return Color((BYTE) r,(BYTE) g,(BYTE) b);
}

// +--------------------------------------------------------------------+

Color
Color::operator+(const Color& c) const
{
    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);

    return Color(r, g, b);
}

// +--------------------------------------------------------------------+

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);

    return Color(r, g, b);
}

// +--------------------------------------------------------------------+

Color
Color::operator*(double scale) const
{
    int r = static_cast<int>(Red()   * scale); if (r > 255) r = 255;
    int g = static_cast<int>(Green() * scale); if (g > 255) g = 255;
    int b = static_cast<int>(Blue()  * scale); if (b > 255) b = 255;
    int a = static_cast<int>(Alpha() * scale); if (a > 255) a = 255;

    return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) a);
}

Color
Color::dim(double scale) const
{
    int r = static_cast<int>(Red()   * scale);
    int g = static_cast<int>(Green() * scale);
    int b = static_cast<int>(Blue()  * scale);

    return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) Alpha());
}

// +--------------------------------------------------------------------+

DWORD
Color::Formatted() const
{
    if (format.pal) {
        return Index();
    }

    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;

            return (r<<format.rshift)|(g<<format.gshift)|(b<<format.bshift)|(a<<format.ashift);

        }

        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);
        }
    }
}

// +--------------------------------------------------------------------+

Color
Color::Faded() const
{
    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();

        return Color((BYTE) r, (BYTE) g, (BYTE) b, (BYTE) a);

    }

    else {
        return *this;
    }
}

// +--------------------------------------------------------------------+

DWORD
Color::Unfaded() const
{
    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);
    }
}

// +--------------------------------------------------------------------+

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);
    }
}

// +--------------------------------------------------------------------+

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());
}

// +--------------------------------------------------------------------+

DWORD
Color::Shaded(int shade) const
{
    return ShadeColor(shade).Formatted();
}

// +--------------------------------------------------------------------+

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);
    }
}

// +--------------------------------------------------------------------+

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);
}

// +--------------------------------------------------------------------+

DWORD
Color::FormattedBlend(DWORD c1, DWORD c2)
{
    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);

        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);
    }
}

// +--------------------------------------------------------------------+

void
Color::UseVideo(Video* v)
{
    video = v;
}

// +--------------------------------------------------------------------+

void
Color::UseFormat(const ColorFormat& cf)
{
    format = cf;

    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;

        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;
        }
    }
}

// +--------------------------------------------------------------------+

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;
        }
    }
}

// +--------------------------------------------------------------------+

void
Color::WithTextureFormat(int 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;

    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);

        if (d < mindist) {
            mindist = d;
            match   = i;

            if (d < 1.0)
            return match;
        }
    }

    return match;
}

// +--------------------------------------------------------------------+

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();
}

// +--------------------------------------------------------------------+

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);
    }
}

// +--------------------------------------------------------------------+

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;
        }
    }
}

// +--------------------------------------------------------------------+

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);
}

// +--------------------------------------------------------------------+

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;

            ColorIndex::blend_table[src*256+dst] = Color((BYTE)r,(BYTE)g,(BYTE)b).Index();
        }
    }
}

// +--------------------------------------------------------------------+

void
Color::SaveShadeTable(const char* basename)
{
    if (!format.pal)
    return;

    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];

    for (; i < 256; i++)
    for (int j = 0; j < 256; j++)
    *pc++ = (BYTE) 0;

    PcxImage pcx(256, 256, clut, (BYTE*) palette);
    pcx.Save(filename);
}


// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+


ColorValue&
ColorValue::operator+=(const ColorValue& c)
{
    r += c.r;
    g += c.g;
    b += c.b;

    return *this;
}

ColorValue
ColorValue::operator+(const ColorValue& c) const
{
    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);

    return ColorValue(fr, fg, fb);
}

ColorValue
ColorValue::operator*(const ColorValue& c) const
{
    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));
}

ColorValue
ColorValue::dim(double scale) const
{
    return ColorValue((float) (r*scale),
    (float) (g*scale),
    (float) (b*scale));
}

// +--------------------------------------------------------------------+

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));
}