summaryrefslogtreecommitdiff
path: root/tools/source/generic/color.cxx
blob: 37e9dedf92591558896f13762069379327223d62 (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_tools.hxx"

#include <stdlib.h>
#include <vos/macros.hxx>
#include <tools/color.hxx>
#include <tools/debug.hxx>
#include <tools/stream.hxx>
#include <tools/rc.hxx>
#include <tools/rcid.h>
#include <tools/resid.hxx>
#ifndef _SV_RC_H
#include <tools/rc.h>
#endif

// -----------
// - Inlines -
// -----------

static inline long _FRound( double fVal )
{
    return( fVal > 0.0 ? (long) ( fVal + 0.5 ) : -(long) ( -fVal + 0.5 ) );
}

// ---------
// - Color -
// ---------

Color::Color( const ResId& rResId )
{
    rResId.SetRT( RSC_COLOR );
    ResMgr* pResMgr = rResId.GetResMgr();
    if ( pResMgr && pResMgr->GetResource( rResId ) )
    {
        // Header ueberspringen
        pResMgr->Increment( sizeof( RSHEADER_TYPE ) );

        // Daten laden
        USHORT nRed     = pResMgr->ReadShort();
        USHORT nGreen   = pResMgr->ReadShort();
        USHORT nBlue    = pResMgr->ReadShort();
        // one more historical ULONG
        pResMgr->ReadLong();

        // RGB-Farbe
        mnColor = RGB_COLORDATA( nRed>>8, nGreen>>8, nBlue>>8 );
    }
    else
    {
        mnColor = RGB_COLORDATA( 0, 0, 0 );
    }
}
UINT8 Color::GetColorError( const Color& rCompareColor ) const
{
    const long nErrAbs = labs( (long) rCompareColor.GetRed() - GetRed() ) +
                         labs( (long) rCompareColor.GetGreen() - GetGreen() ) +
                         labs( (long) rCompareColor.GetBlue() - GetBlue() );

    return (UINT8) _FRound( nErrAbs * 0.3333333333 );
}

// -----------------------------------------------------------------------

void Color::IncreaseLuminance( UINT8 cLumInc )
{
    SetRed( (UINT8) VOS_BOUND( (long) COLORDATA_RED( mnColor ) + cLumInc, 0L, 255L ) );
    SetGreen( (UINT8) VOS_BOUND( (long) COLORDATA_GREEN( mnColor ) + cLumInc, 0L, 255L ) );
    SetBlue( (UINT8) VOS_BOUND( (long) COLORDATA_BLUE( mnColor ) + cLumInc, 0L, 255L ) );
}

// -----------------------------------------------------------------------

void Color::DecreaseLuminance( UINT8 cLumDec )
{
    SetRed( (UINT8) VOS_BOUND( (long) COLORDATA_RED( mnColor ) - cLumDec, 0L, 255L ) );
    SetGreen( (UINT8) VOS_BOUND( (long) COLORDATA_GREEN( mnColor ) - cLumDec, 0L, 255L ) );
    SetBlue( (UINT8) VOS_BOUND( (long) COLORDATA_BLUE( mnColor ) - cLumDec, 0L, 255L ) );
}

// -----------------------------------------------------------------------

void Color::IncreaseContrast( UINT8 cContInc )
{
    if( cContInc)
    {
        const double fM = 128.0 / ( 128.0 - 0.4985 * cContInc );
        const double fOff = 128.0 - fM * 128.0;

        SetRed( (UINT8) VOS_BOUND( _FRound( COLORDATA_RED( mnColor ) * fM + fOff ), 0L, 255L ) );
        SetGreen( (UINT8) VOS_BOUND( _FRound( COLORDATA_GREEN( mnColor ) * fM + fOff ), 0L, 255L ) );
        SetBlue( (UINT8) VOS_BOUND( _FRound( COLORDATA_BLUE( mnColor ) * fM + fOff ), 0L, 255L ) );
    }
}

// -----------------------------------------------------------------------

void Color::DecreaseContrast( UINT8 cContDec )
{
    if( cContDec )
    {
        const double fM = ( 128.0 - 0.4985 * cContDec ) / 128.0;
        const double fOff = 128.0 - fM * 128.0;

        SetRed( (UINT8) VOS_BOUND( _FRound( COLORDATA_RED( mnColor ) * fM + fOff ), 0L, 255L ) );
        SetGreen( (UINT8) VOS_BOUND( _FRound( COLORDATA_GREEN( mnColor ) * fM + fOff ), 0L, 255L ) );
        SetBlue( (UINT8) VOS_BOUND( _FRound( COLORDATA_BLUE( mnColor ) * fM + fOff ), 0L, 255L ) );
    }
}

// -----------------------------------------------------------------------

void Color::Invert()
{
    SetRed( ~COLORDATA_RED( mnColor ) );
    SetGreen( ~COLORDATA_GREEN( mnColor ) );
    SetBlue( ~COLORDATA_BLUE( mnColor ) );
}

// -----------------------------------------------------------------------

BOOL Color::IsDark() const
{
    return GetLuminance() <= 38;
}

// -----------------------------------------------------------------------

BOOL Color::IsBright() const
{
    return GetLuminance() >= 245;
}

// -----------------------------------------------------------------------
// color space conversion
// -----------------------------------------------------------------------

void Color::RGBtoHSB( USHORT& nHue, USHORT& nSat, USHORT& nBri ) const
{
    UINT8 c[3];
    UINT8 cMax, cMin;

    c[0] = GetRed();
    c[1] = GetGreen();
    c[2] = GetBlue();

    cMax = c[0];
    if( c[1] > cMax )
        cMax = c[1];
    if( c[2] > cMax )
        cMax = c[2];

    // Brightness = max(R, G, B);
    nBri = cMax * 100 / 255;

    cMin = c[0];
    if( c[1] < cMin )
        cMin = c[1];
    if( c[2] < cMin )
        cMin = c[2];

    UINT8 cDelta = cMax - cMin;

    // Saturation = max - min / max
    if( nBri > 0 )
        nSat = cDelta * 100 / cMax;
    else
        nSat = 0;

    if( nSat == 0 )
        nHue = 0; // Default = undefined
    else
    {
        double dHue = 0.0;

        if( c[0] == cMax )
        {
            dHue = (double)( c[1] - c[2] ) / (double)cDelta;
        }
        else if( c[1] == cMax )
        {
            dHue = 2.0 + (double)( c[2] - c[0] ) / (double)cDelta;
        }
        else if ( c[2] == cMax )
        {
            dHue = 4.0 + (double)( c[0] - c[1] ) / (double)cDelta;
        }
        dHue *= 60.0;

        if( dHue < 0.0 )
            dHue += 360.0;

        nHue = (UINT16) dHue;
    }
}

ColorData Color::HSBtoRGB( USHORT nHue, USHORT nSat, USHORT nBri )
{
    UINT8 cR=0,cG=0,cB=0;
    UINT8 nB = (UINT8) ( nBri * 255 / 100 );

    if( nSat == 0 )
    {
        cR = nB;
        cG = nB;
        cB = nB;
    }
    else
    {
        double dH = nHue;
        double f;
        UINT16 n;
        if( dH == 360.0 )
            dH = 0.0;

        dH /= 60.0;
        n = (UINT16) dH;
        f = dH - n;

        UINT8 a = (UINT8) ( nB * ( 100 - nSat ) / 100 );
        UINT8 b = (UINT8) ( nB * ( 100 - ( (double)nSat * f + 0.5 ) ) / 100 );
        UINT8 c = (UINT8) ( nB * ( 100 - ( (double)nSat * ( 1.0 - f ) + 0.5 ) ) / 100 );

        switch( n )
        {
            case 0: cR = nB;    cG = c;     cB = a;     break;
            case 1: cR = b;     cG = nB;    cB = a;     break;
            case 2: cR = a;     cG = nB;    cB = c;     break;
            case 3: cR = a;     cG = b;     cB = nB;    break;
            case 4: cR = c;     cG = a;     cB = nB;    break;
            case 5: cR = nB;    cG = a;     cB = b;     break;
        }
    }

    return RGB_COLORDATA( cR, cG, cB );
}

// -----------------------------------------------------------------------

SvStream& Color::Read( SvStream& rIStm, BOOL bNewFormat )
{
    if ( bNewFormat )
        rIStm >> mnColor;
    else
        rIStm >> *this;

    return rIStm;
}

// -----------------------------------------------------------------------

SvStream& Color::Write( SvStream& rOStm, BOOL bNewFormat )
{
    if ( bNewFormat )
        rOStm << mnColor;
    else
        rOStm << *this;

    return rOStm;
}

// -----------------------------------------------------------------------

#define COL_NAME_USER       ((USHORT)0x8000)
#define COL_RED_1B          ((USHORT)0x0001)
#define COL_RED_2B          ((USHORT)0x0002)
#define COL_GREEN_1B        ((USHORT)0x0010)
#define COL_GREEN_2B        ((USHORT)0x0020)
#define COL_BLUE_1B         ((USHORT)0x0100)
#define COL_BLUE_2B         ((USHORT)0x0200)

// -----------------------------------------------------------------------

SvStream& operator>>( SvStream& rIStream, Color& rColor )
{
    DBG_ASSERTWARNING( rIStream.GetVersion(), "Color::>> - Solar-Version not set on rIStream" );

    USHORT      nColorName;
    USHORT      nRed;
    USHORT      nGreen;
    USHORT      nBlue;

    rIStream >> nColorName;

    if ( nColorName & COL_NAME_USER )
    {
        if ( rIStream.GetCompressMode() == COMPRESSMODE_FULL )
        {
            unsigned char   cAry[6];
            USHORT          i = 0;

            nRed    = 0;
            nGreen  = 0;
            nBlue   = 0;

            if ( nColorName & COL_RED_2B )
                i += 2;
            else if ( nColorName & COL_RED_1B )
                i++;
            if ( nColorName & COL_GREEN_2B )
                i += 2;
            else if ( nColorName & COL_GREEN_1B )
                i++;
            if ( nColorName & COL_BLUE_2B )
                i += 2;
            else if ( nColorName & COL_BLUE_1B )
                i++;

            rIStream.Read( cAry, i );
            i = 0;

            if ( nColorName & COL_RED_2B )
            {
                nRed = cAry[i];
                nRed <<= 8;
                i++;
                nRed |= cAry[i];
                i++;
            }
            else if ( nColorName & COL_RED_1B )
            {
                nRed = cAry[i];
                nRed <<= 8;
                i++;
            }
            if ( nColorName & COL_GREEN_2B )
            {
                nGreen = cAry[i];
                nGreen <<= 8;
                i++;
                nGreen |= cAry[i];
                i++;
            }
            else if ( nColorName & COL_GREEN_1B )
            {
                nGreen = cAry[i];
                nGreen <<= 8;
                i++;
            }
            if ( nColorName & COL_BLUE_2B )
            {
                nBlue = cAry[i];
                nBlue <<= 8;
                i++;
                nBlue |= cAry[i];
                i++;
            }
            else if ( nColorName & COL_BLUE_1B )
            {
                nBlue = cAry[i];
                nBlue <<= 8;
                i++;
            }
        }
        else
        {
            rIStream >> nRed;
            rIStream >> nGreen;
            rIStream >> nBlue;
        }

        rColor.mnColor = RGB_COLORDATA( nRed>>8, nGreen>>8, nBlue>>8 );
    }
    else
    {
        static ColorData aColAry[] =
        {
            COL_BLACK,                          // COL_BLACK
            COL_BLUE,                           // COL_BLUE
            COL_GREEN,                          // COL_GREEN
            COL_CYAN,                           // COL_CYAN
            COL_RED,                            // COL_RED
            COL_MAGENTA,                        // COL_MAGENTA
            COL_BROWN,                          // COL_BROWN
            COL_GRAY,                           // COL_GRAY
            COL_LIGHTGRAY,                      // COL_LIGHTGRAY
            COL_LIGHTBLUE,                      // COL_LIGHTBLUE
            COL_LIGHTGREEN,                     // COL_LIGHTGREEN
            COL_LIGHTCYAN,                      // COL_LIGHTCYAN
            COL_LIGHTRED,                       // COL_LIGHTRED
            COL_LIGHTMAGENTA,                   // COL_LIGHTMAGENTA
            COL_YELLOW,                         // COL_YELLOW
            COL_WHITE,                          // COL_WHITE
            COL_WHITE,                          // COL_MENUBAR
            COL_BLACK,                          // COL_MENUBARTEXT
            COL_WHITE,                          // COL_POPUPMENU
            COL_BLACK,                          // COL_POPUPMENUTEXT
            COL_BLACK,                          // COL_WINDOWTEXT
            COL_WHITE,                          // COL_WINDOWWORKSPACE
            COL_BLACK,                          // COL_HIGHLIGHT
            COL_WHITE,                          // COL_HIGHLIGHTTEXT
            COL_BLACK,                          // COL_3DTEXT
            COL_LIGHTGRAY,                      // COL_3DFACE
            COL_WHITE,                          // COL_3DLIGHT
            COL_GRAY,                           // COL_3DSHADOW
            COL_LIGHTGRAY,                      // COL_SCROLLBAR
            COL_WHITE,                          // COL_FIELD
            COL_BLACK                           // COL_FIELDTEXT
        };

        if ( nColorName < (sizeof( aColAry )/sizeof(ColorData)) )
            rColor.mnColor = aColAry[nColorName];
        else
            rColor.mnColor = COL_BLACK;
    }

    return rIStream;
}

// -----------------------------------------------------------------------

SvStream& operator<<( SvStream& rOStream, const Color& rColor )
{
    DBG_ASSERTWARNING( rOStream.GetVersion(), "Color::<< - Solar-Version not set on rOStream" );

    USHORT nColorName   = COL_NAME_USER;
    USHORT nRed         = rColor.GetRed();
    USHORT nGreen       = rColor.GetGreen();
    USHORT nBlue        = rColor.GetBlue();
    nRed    = (nRed<<8) + nRed;
    nGreen  = (nGreen<<8) + nGreen;
    nBlue   = (nBlue<<8) + nBlue;

    if ( rOStream.GetCompressMode() == COMPRESSMODE_FULL )
    {
        unsigned char   cAry[6];
        USHORT          i = 0;

        if ( nRed & 0x00FF )
        {
            nColorName |= COL_RED_2B;
            cAry[i] = (unsigned char)(nRed & 0xFF);
            i++;
            cAry[i] = (unsigned char)((nRed >> 8) & 0xFF);
            i++;
        }
        else if ( nRed & 0xFF00 )
        {
            nColorName |= COL_RED_1B;
            cAry[i] = (unsigned char)((nRed >> 8) & 0xFF);
            i++;
        }
        if ( nGreen & 0x00FF )
        {
            nColorName |= COL_GREEN_2B;
            cAry[i] = (unsigned char)(nGreen & 0xFF);
            i++;
            cAry[i] = (unsigned char)((nGreen >> 8) & 0xFF);
            i++;
        }
        else if ( nGreen & 0xFF00 )
        {
            nColorName |= COL_GREEN_1B;
            cAry[i] = (unsigned char)((nGreen >> 8) & 0xFF);
            i++;
        }
        if ( nBlue & 0x00FF )
        {
            nColorName |= COL_BLUE_2B;
            cAry[i] = (unsigned char)(nBlue & 0xFF);
            i++;
            cAry[i] = (unsigned char)((nBlue >> 8) & 0xFF);
            i++;
        }
        else if ( nBlue & 0xFF00 )
        {
            nColorName |= COL_BLUE_1B;
            cAry[i] = (unsigned char)((nBlue >> 8) & 0xFF);
            i++;
        }

        rOStream << nColorName;
        rOStream.Write( cAry, i );
    }
    else
    {
        rOStream << nColorName;
        rOStream << nRed;
        rOStream << nGreen;
        rOStream << nBlue;
    }

    return rOStream;
}