summaryrefslogtreecommitdiff
path: root/svtools/source/dialogs/colctrl.cxx
blob: eb7c168af6de63976bbfaeae16bf525329c452ec (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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*************************************************************************
 *
 *  $RCSfile: colctrl.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000-09-18 16:58:57 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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 for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/


#ifndef _SV_SALBTYPE_HXX //autogen
#include <vcl/salbtype.hxx>
#endif

#ifndef _SV_BMPACC_HXX //autogen
#include <vcl/bmpacc.hxx>
#endif

#include <colctrl.hxx>

// ----------------
// - ColorControl -
// ----------------

// -----------------------------------------------------------------------
SvColorControl::SvColorControl( Window* pParent, WinBits nStyle ) :
    Control         ( pParent, nStyle ),
    mpBitmap        ( NULL ),
    mpReadAccess    ( NULL ),
    mnLuminance     ( 50 )
{
    Initialize();
}

// -----------------------------------------------------------------------
SvColorControl::SvColorControl( Window* pParent, const ResId& rResId ) :
    Control         ( pParent, rResId ),
    mpBitmap        ( NULL ),
    mpReadAccess    ( NULL ),
    mnLuminance     ( 50 )
{
    Initialize();
}


// -----------------------------------------------------------------------
SvColorControl::~SvColorControl()
{
    if( mpReadAccess )
        mpBitmap->ReleaseAccess( mpReadAccess );

    delete mpBitmap;

}

// -----------------------------------------------------------------------
void SvColorControl::Initialize()
{
    SetLineColor( Color( COL_BLACK ) );
}

// -----------------------------------------------------------------------
void SvColorControl::CreateBitmap()
{
    Size aSize = GetOutputSizePixel();
    Size aBmpSize;
    if( mpBitmap && mpBitmap->GetSizePixel() != aSize )
    {
        delete mpBitmap;
        mpBitmap = NULL;
    }
    if( !mpBitmap )
        mpBitmap = new Bitmap( aSize, 24 );

    BitmapWriteAccess* pWriteAccess = mpBitmap->AcquireWriteAccess();

    USHORT nX = (USHORT) aSize.Width();
    USHORT nY = (USHORT) aSize.Height();

    UINT16 nHue, nSat;
    UINT16 nBri = mnLuminance;
    ColorHSB aColHSB( 0, 0, mnLuminance );

    for( USHORT i = 0; i < nY; i++ )
    {
        nSat = 100 - ( 100.0 * i + 0.5 ) / nY;

        for( USHORT j = 0; j < nX; j++ )
        {
            nHue = ( 360.0 * j + 0.5 ) / nX;

            aColHSB.SetHue( nHue );
            aColHSB.SetSat( nSat );
            Color aCol( aColHSB.GetRGB() );

            pWriteAccess->SetPixel( i, j, BitmapColor( aCol ) );

        }
    }

    /*
     erster Ansatz nur mit RGB
    USHORT n1_6 = (USHORT) ((nX+3) / 6);
    USHORT n2_6 = (USHORT) ((nX*2+3) / 6);
    USHORT n3_6 = (USHORT) ((nX*3+3) / 6);
    USHORT n4_6 = (USHORT) ((nX*4+3) / 6);
    USHORT n5_6 = (USHORT) ((nX*5+3) / 6);
    USHORT n6_6 = nX;

    BitmapColor aBmpColor;
    BYTE cR, cG, cB;
    */
    /*
    for( USHORT i = 0; i < nY; i++ )
    {
        USHORT nK = 100 * i / nY;

        for( USHORT j = 0; j < nX; j++ )
        {
            if( j < n1_6 )
            {
                cR = 255;
                cG = 255 * j / n1_6;
                cB = 0;
            }
            else if( j < n2_6 )
            {
                cR = 255 - 255 * ( j - n1_6 ) / n1_6;
                cG = 255;
                cB = 0;
            }
            else if( j < n3_6 )
            {
                cR = 0;
                cG = 255;
                cB = 255 * (j - n2_6 ) / n1_6;
            }
            else if( j < n4_6 )
            {
                cR = 0;
                cG = 255 - 255 * ( j - n3_6 ) / n1_6;
                cB = 255;
            }
            else if( j < n5_6 )
            {
                cR = 255 * (j - n4_6 ) / n1_6;
                cG = 0;
                cB = 255;
            }
            else if( j < nX )
            {
                cR = 255;
                cG = 0;
                cB = 255 - 255 * ( j - n5_6 ) / n1_6;;
            }

            //cR -= ( cR - mnLuminance * 256 / 100 ) * nK / 100;
            //cG -= ( cG - mnLuminance * 256 / 100 ) * nK / 100;
            //cB -= ( cB - mnLuminance * 256 / 100 ) * nK / 100;

            //aBmpColor.SetRed( cR );
            //aBmpColor.SetGreen( cG );
            //aBmpColor.SetBlue( cB );

            //pWriteAccess->SetPixel( i, j, aBmpColor );

            ColorHSB aColHSB( Color( cR, cG, cB ) );
            aColHSB.SetSat( nK );
            Color aCol( aColHSB.GetRGB() );

            pWriteAccess->SetPixel( i, j, BitmapColor( aCol ) );

        }
    }
    */
    mpBitmap->ReleaseAccess( pWriteAccess );

    mpReadAccess = mpBitmap->AcquireReadAccess();

    SetColor( maColor ); // Anzeige der Position im Control
}

// -----------------------------------------------------------------------
void SvColorControl::ShowPosition( const Point& rPos )
{
    // Explizites Abfragen des Bereichs, da schon mal ein Wert < 0 vorhanden ist
    if( mpBitmap )
    {
        long nX = rPos.X();
        long nY = rPos.Y();
        if( nX < 0L )
            nX = 0L;
        else if( nX >= mpBitmap->GetSizePixel().Width() )
            nX = mpBitmap->GetSizePixel().Width() - 1L;

        if( nY < 0L )
            nY= 0L;
        else if( nY >= mpBitmap->GetSizePixel().Height() )
            nY = mpBitmap->GetSizePixel().Height() - 1L;

        Point aPos = maPosition;
        maPosition.X() = nX - 2;
        maPosition.Y() = nY - 2;
        Invalidate( Rectangle( aPos, Size( 5, 5) ) );
        Invalidate( Rectangle( maPosition, Size( 5, 5) ) );

        maColor = mpReadAccess->GetPixel( nY, nX );
    }
}
// -----------------------------------------------------------------------
void SvColorControl::MouseMove( const MouseEvent& rMEvt )
{
    if( rMEvt.IsLeft() )
    {
        ShowPosition( rMEvt.GetPosPixel() );
        Modify();
    }
}

// -----------------------------------------------------------------------
void SvColorControl::MouseButtonDown( const MouseEvent& rMEvt )
{
    if( rMEvt.IsLeft() && !rMEvt.IsShift() )
    {
        //ShowPointer( FALSE );
        CaptureMouse();
        ShowPosition( rMEvt.GetPosPixel() );
        Modify();
    }
}

// -----------------------------------------------------------------------
void SvColorControl::MouseButtonUp( const MouseEvent& rMEvt )
{
    //ShowPointer( TRUE );
    if( IsMouseCaptured() )
        ReleaseMouse();
}

// -----------------------------------------------------------------------
void SvColorControl::Paint( const Rectangle& rRect )
{
    if( !mpBitmap )
        CreateBitmap();

    // dither bitmap if there only 256 colors available !
    if( GetBitCount() <= 8 )
        mpBitmap->Dither();

    DrawBitmap( rRect.TopLeft(), rRect.GetSize(),
                rRect.TopLeft(), rRect.GetSize(), *mpBitmap );

    // Positions-Control (Fadenkreuz oder Aehnliches)
    Point aPos1( maPosition );
    Point aPos2( maPosition );
    aPos2.X() += 4;
    DrawLine( aPos1, aPos2 );
    aPos2.X() -= 4;
    aPos2.Y() += 4;
    DrawLine( aPos1, aPos2 );
    aPos1.Y() += 4;
    aPos2.X() += 4;
    DrawLine( aPos1, aPos2 );
    aPos1.X() += 4;
    aPos2.Y() -= 4;
    DrawLine( aPos1, aPos2 );
}

// -----------------------------------------------------------------------
void SvColorControl::Resize()
{
    CreateBitmap();

    Control::Resize();
}

// -----------------------------------------------------------------------
void SvColorControl::Modify()
{
    maModifyHdl.Call( this );
}

// -----------------------------------------------------------------------
void SvColorControl::SetColor( const ColorHSB& rCol, BOOL bSetColor )
{
    if( bSetColor )
        maColor = rCol.GetRGB();

    if( mpBitmap )
    {
        USHORT nX = (USHORT) mpBitmap->GetSizePixel().Width();
        USHORT nY = (USHORT) mpBitmap->GetSizePixel().Height();

        INT16 nZ = rCol.GetBri();
        SetLuminance( nZ );

        nX = rCol.GetHue() * nX / 360; // Farbe
        nY = nY - rCol.GetSat() * nY / 100; // Saettigung

        ShowPosition( Point( nX, nY ) );
    }
}

// -----------------------------------------------------------------------
void SvColorControl::SetColor( const Color& rCol )
{
    maColor = rCol;

    if( mpBitmap )
    {
        ColorHSB aColHsb( rCol );
        SetColor( aColHsb, FALSE );
        /*
        BOOL bDone = FALSE;
        USHORT nX = (USHORT) mpBitmap->GetSizePixel().Width();
        USHORT nY = (USHORT) mpBitmap->GetSizePixel().Height();
        USHORT i, j;

        BitmapColor aBmpColor;

        for( i = 0; i < nY && !bDone; i++ )
        {
            for( j = 0; j < nX && !bDone; j++ )
            {
                aBmpColor = mpReadAccess->GetPixel( i, j );

                if( maColor == (Color)aBmpColor )
                    bDone = TRUE;
            }
        }

        if( bDone )
            ShowPosition( Point( j, i ) );
        */
    }
}

// -----------------------------------------------------------------------
void SvColorControl::SetLuminance( short nLum )
{
    if( nLum != mnLuminance &&
        nLum >= 0 && nLum <= 100 )
    {
        mnLuminance = nLum;

        if( mnLuminance < 40 )
            SetLineColor( Color( COL_WHITE ) );
        else
            SetLineColor( Color( COL_BLACK ) );

        CreateBitmap();

        USHORT nX = maPosition.X() + 2;
        USHORT nY = maPosition.Y() + 2;
        maColor = mpReadAccess->GetPixel( nY, nX );

        Invalidate();
    }
}


// -----------------------
// - ColorPreviewControl -
// -----------------------


// -----------------------------------------------------------------------
ColorPreviewControl::ColorPreviewControl( Window* pParent, WinBits nStyle ) :
    Control     ( pParent, nStyle )
{
    SetFillColor( maColor );
    SetLineColor( maColor );
}

// -----------------------------------------------------------------------
ColorPreviewControl::ColorPreviewControl( Window* pParent, const ResId& rResId ) :
    Control     ( pParent, rResId )
{
    SetFillColor( maColor );
    SetLineColor( maColor );
}


// -----------------------------------------------------------------------
ColorPreviewControl::~ColorPreviewControl()
{
}

// -----------------------------------------------------------------------
void ColorPreviewControl::Paint( const Rectangle& rRect )
{
    DrawRect( rRect );
}

// -----------------------------------------------------------------------
void ColorPreviewControl::SetColor( const Color& rCol )
{
    if( rCol != maColor )
    {
        maColor = rCol;
        SetFillColor( maColor );
        SetLineColor( maColor );
        Invalidate();
    }
}


// -----------------------
// - ColorMixingControl -
// -----------------------


// -----------------------------------------------------------------------
ColorMixingControl::ColorMixingControl( Window* pParent, WinBits nStyle,
                                        USHORT nRows, USHORT nColumns ) :
    ValueSet    ( pParent, nStyle ),
    mnRows      ( nRows ),
    mnColumns   ( nColumns )
{
    Initialize();
}

// -----------------------------------------------------------------------
ColorMixingControl::ColorMixingControl( Window* pParent, const ResId& rResId,
                                        USHORT nRows, USHORT nColumns ) :
    ValueSet    ( pParent, rResId ),
    mnRows      ( nRows ),
    mnColumns   ( nColumns )
{
    Initialize();
}


// -----------------------------------------------------------------------
ColorMixingControl::~ColorMixingControl()
{
}

// -----------------------------------------------------------------------
void ColorMixingControl::Initialize()
{
    SetColCount( mnColumns );

    Color aColor;
    String aStr;
    for( USHORT i = 1; i <= mnRows * mnColumns; i++ )
    {
        InsertItem( i, aColor, aStr );
    }

    /*maColor[ 0 ] = Color( COL_LIGHTRED );
    maColor[ 1 ] = Color( COL_LIGHTGREEN );
    maColor[ 2 ] = Color( COL_YELLOW );
    maColor[ 3 ] = Color( COL_LIGHTBLUE );*/

    SetColor( CMC_TOPLEFT, Color( COL_LIGHTRED ) );
    SetColor( CMC_BOTTOMRIGHT, Color( COL_LIGHTBLUE ) );

    SetColor( CMC_TOPRIGHT, Color( COL_LIGHTGREEN ) );
    SetColor( CMC_BOTTOMLEFT, Color( COL_YELLOW ) );

    /*FillColumn( 0 );
    FillColumn( mnColumns - 1 );
    for( i = 0; i < mnRows; i++ )
        FillRow( i );*/
}

// -----------------------------------------------------------------------
Color ColorMixingControl::CalcDifferenceColor( USHORT nCol1, USHORT nCol2,
                                                USHORT nSteps )
{
    // Die Berechnung ist noch etwas ungenau, daher sollte besser mit floats
    // gearbeitet werden...  (muss !!!)
    Color aColor( GetItemColor( nCol1 ) );
    Color aColor2( GetItemColor( nCol2 ) );

    aColor.SetRed( (UINT8) ( ( aColor2.GetRed() - aColor.GetRed() ) / nSteps ) );
    aColor.SetGreen( (UINT8) ( ( aColor2.GetGreen() - aColor.GetGreen() ) / nSteps ) );
    aColor.SetBlue( (UINT8) ( ( aColor2.GetBlue() - aColor.GetBlue() ) / nSteps ) );

    return( aColor );
}

// -----------------------------------------------------------------------
void ColorMixingControl::FillRow( USHORT nRow )
{
    USHORT nCol1 = nRow * mnColumns + 1;
    USHORT nCol2 = ( nRow + 1 ) * mnColumns;
    Color aColor( GetItemColor( nCol1 ) );
    Color aDiffColor( CalcDifferenceColor( nCol1, nCol2, mnColumns - 1 ) );

    for( USHORT i = nCol1 + 1; i < nCol2; i++ )
    {
        aColor.SetRed( aColor.GetRed() + aDiffColor.GetRed() );
        aColor.SetGreen( aColor.GetGreen() + aDiffColor.GetGreen() );
        aColor.SetBlue( aColor.GetBlue() + aDiffColor.GetBlue() );

        SetItemColor( i, aColor );
        SetItemText( i, GetRGBString( aColor ) );
    }
}

// -----------------------------------------------------------------------
void ColorMixingControl::FillColumn( USHORT nColumn )
{
    USHORT nCol1 = nColumn + 1;
    USHORT nCol2 = nColumn + ( mnRows - 1 ) * mnColumns + 1;
    Color aColor( GetItemColor( nCol1 ) );
    Color aDiffColor( CalcDifferenceColor( nCol1, nCol2, mnRows - 1 ) );

    for( USHORT i = nCol1 + mnColumns; i < nCol2; i += mnColumns )
    {
        aColor.SetRed( aColor.GetRed() + aDiffColor.GetRed() );
        aColor.SetGreen( aColor.GetGreen() + aDiffColor.GetGreen() );
        aColor.SetBlue( aColor.GetBlue() + aDiffColor.GetBlue() );

        SetItemColor( i, aColor );
        SetItemText( i, GetRGBString( aColor ) );
    }
}

// -----------------------------------------------------------------------
void ColorMixingControl::SetRows( USHORT nRows )
{
    mnRows = nRows;
}

// -----------------------------------------------------------------------
void ColorMixingControl::SetColumns( USHORT nColumns )
{
    mnColumns = nColumns;
}

// -----------------------------------------------------------------------
void ColorMixingControl::SetColor( CMCPosition ePos, const Color& rCol )
{
    if( rCol != maColor[ ePos ] )
    {
        maColor[ ePos ] = rCol;
        USHORT nPos, nColumn;
        String aStr( GetRGBString( rCol ) );

        switch( ePos )
        {
            case CMC_TOPLEFT:
                nPos = 1;
                nColumn = 0;
            break;

            case CMC_TOPRIGHT:
                nPos = mnColumns;
                nColumn = mnColumns - 1;
            break;

            case CMC_BOTTOMLEFT:
                nPos = ( mnRows - 1 ) * mnColumns + 1;
                nColumn = 0;
            break;

            case CMC_BOTTOMRIGHT:
                nPos = mnRows * mnColumns;
                nColumn = mnColumns - 1;
            break;
        }
        SetItemColor( nPos, rCol );
        SetItemText( nPos, aStr );
        FillColumn( nColumn );

        for( USHORT i = 0; i < mnRows; i++ )
            FillRow( i );
    }
}

// -----------------------------------------------------------------------
String ColorMixingControl::GetRGBString( const Color& rColor )
{
    String  aStr( String::CreateFromInt32(rColor.GetRed()) );
            aStr += ' ';
            aStr += String::CreateFromInt32(rColor.GetGreen());
            aStr += ' ';
            aStr += String::CreateFromInt32(rColor.GetBlue());

    return aStr;
}
// -----------------------------------------------------------------------
CMCPosition ColorMixingControl::GetCMCPosition() const
{
    CMCPosition ePos = CMC_OTHER;
    USHORT nPos = GetSelectItemId();

    if( nPos == 1 )
        ePos = CMC_TOPLEFT;
    else if( nPos == mnColumns )
        ePos = CMC_TOPRIGHT;
    else if( nPos == ( mnRows - 1 ) * mnColumns + 1 )
        ePos = CMC_BOTTOMLEFT;
    else if( nPos == mnRows * mnColumns )
        ePos = CMC_BOTTOMRIGHT;

    return( ePos );
}


// ------------
// - ColorHSB -
// ------------

// Erste Ansaetze gingen auf die Berechnung von Sven Hannover zurueck
// Der jetzige Algorithmus stammt im weitesten Sinne aus dem Foley/VanDam


/**************************************************************************
|*
|*    ColorHSB::ColorHSB()
|*
|*    Beschreibung       RGB nach HSB
|*    Ersterstellung     SOH 02.10.97
|*
**************************************************************************/

ColorHSB::ColorHSB( const Color& rColor )
{
    UINT8 c[3];
    UINT8 cMax, cMin;

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

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

    // Brightness = max(R, G, B);
    mnBri = 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( mnBri > 0 )
        mnSat = cDelta * 100 / cMax;
    else
        mnSat = 0;

    if( mnSat == 0 )
        mnHue = 0; // Default = undefined
    else
    {
        double dHue;

        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;

        mnHue = (UINT16) dHue;
    }
}

/**************************************************************************
|*
|*    ColorHSB::GetRGB()
|*
|*    Beschreibung       HSB nach RGB
|*    Ersterstellung     SOH 02.10.97
|*
**************************************************************************/

Color ColorHSB::GetRGB() const
{
    UINT8 cR,cG,cB;
    UINT8 nB = (UINT8) ( mnBri * 255 / 100 );

    if( mnSat == 0 )
    {
        cR = nB;
        cG = nB;
        cB = nB;
    }
    else
    {
        double dH = mnHue;
        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 - mnSat ) / 100 );
        UINT8 b = (UINT8) ( nB * ( 100 - ( (double)mnSat * f + 0.5 ) ) / 100 );
        UINT8 c = (UINT8) ( nB * ( 100 - ( (double)mnSat * ( 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( Color( cR, cG, cB ) );
}

// ------------
// - ColorCMYK -
// ------------


// -----------------------------------------------------------------------
ColorCMYK::ColorCMYK( const Color& rColor )
{
    mnCyan    = 255 - rColor.GetRed();
    mnMagenta = 255 - rColor.GetGreen();
    mnYellow  = 255 - rColor.GetBlue();

    mnKey = Min( Min( mnCyan, mnMagenta ), mnYellow );

    mnCyan    = mnCyan - mnKey;
    mnMagenta = mnMagenta - mnKey;
    mnYellow  = mnYellow - mnKey;
}

// -----------------------------------------------------------------------
Color ColorCMYK::GetRGB() const
{
    INT16 nTmp = Max( 0, 255 - ( mnCyan + mnKey ) );
    UINT8 cR = (UINT8) nTmp;
          nTmp = Max( 0, 255 - ( mnMagenta + mnKey ) );
    UINT8 cG = (UINT8) nTmp;
          nTmp = Max( 0, 255 - ( mnYellow + mnKey ) );
    UINT8 cB = (UINT8) nTmp;

    return( Color( cR, cG, cB ) );
}