update credits
[LibreOffice.git] / cui / source / dialogs / colorpicker.cxx
blob46d3053db8f7567012257d6e61623cb5c850737d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/XComponentContext.hpp>
21 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
22 #include <com/sun/star/beans/XPropertyAccess.hpp>
23 #include <com/sun/star/lang/XInitialization.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/datatransfer/XTransferable.hpp>
26 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
27 #include <com/sun/star/awt/XWindow.hpp>
28 #include <cppuhelper/compbase4.hxx>
29 #include <comphelper/broadcasthelper.hxx>
30 #include <vcl/dialog.hxx>
31 #include <vcl/button.hxx>
32 #include <vcl/fixed.hxx>
33 #include <vcl/edit.hxx>
34 #include <vcl/field.hxx>
35 #include <vcl/bmpacc.hxx>
36 #include <vcl/decoview.hxx>
37 #include <vcl/svapp.hxx>
38 #include <toolkit/helper/vclunohelper.hxx>
39 #include <sot/exchange.hxx>
40 #include <sot/formats.hxx>
41 #include <sax/tools/converter.hxx>
42 #include <basegfx/color/bcolortools.hxx>
43 #include "dialmgr.hxx"
44 #include "colorpicker.hrc"
45 #include <cmath>
46 #include <limits>
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::ui::dialogs;
51 using namespace ::com::sun::star::beans;
52 using namespace ::basegfx;
54 namespace cui
56 const sal_uInt16 COLORMODE_RGB = 0x10;
57 const sal_uInt16 COLORMODE_HSV = 0x20;
58 const sal_uInt16 COLORMODE_CMYK = 0x40;
60 const sal_uInt16 COLORCOMP_RED = 0x10;
61 const sal_uInt16 COLORCOMP_GREEN = 0x11;
62 const sal_uInt16 COLORCOMP_BLUE = 0x12;
64 const sal_uInt16 COLORCOMP_HUE = 0x20;
65 const sal_uInt16 COLORCOMP_SAT = 0x21;
66 const sal_uInt16 COLORCOMP_BRI = 0x22;
68 const sal_uInt16 COLORCOMP_CYAN = 0x40;
69 const sal_uInt16 COLORCOMP_YELLOW = 0x41;
70 const sal_uInt16 COLORCOMP_MAGENTA = 0x42;
71 const sal_uInt16 COLORCOMP_KEY = 0x43;
73 // color space conversion helpers
75 static void RGBtoHSV( double dR, double dG, double dB, double& dH, double& dS, double& dV )
77 BColor result = tools::rgb2hsv( BColor( dR, dG, dB ) );
79 dH = result.getX();
80 dS = result.getY();
81 dV = result.getZ();
84 static void HSVtoRGB(double dH, double dS, double dV, double& dR, double& dG, double& dB )
86 BColor result = tools::hsv2rgb( BColor( dH, dS, dV ) );
88 dR = result.getRed();
89 dG = result.getGreen();
90 dB = result.getBlue();
93 // -----------------------------------------------------------------------
95 // CMYK values from 0 to 1
96 static void CMYKtoRGB( double fCyan, double fMagenta, double fYellow, double fKey, double& dR, double& dG, double& dB )
98 fCyan = (fCyan * ( 1.0 - fKey )) + fKey;
99 fMagenta = (fMagenta * ( 1.0 - fKey )) + fKey;
100 fYellow = (fYellow * ( 1.0 - fKey )) + fKey;
102 dR = std::max( std::min( ( 1.0 - fCyan ), 1.0), 0.0 );
103 dG = std::max( std::min( ( 1.0 - fMagenta ), 1.0), 0.0 );
104 dB = std::max( std::min( ( 1.0 - fYellow ), 1.0), 0.0 );
107 // -----------------------------------------------------------------------
109 // CMY results from 0 to 1
110 static void RGBtoCMYK( double dR, double dG, double dB, double& fCyan, double& fMagenta, double& fYellow, double& fKey )
112 fCyan = 1 - dR;
113 fMagenta = 1 - dG;
114 fYellow = 1 - dB;
116 //CMYK and CMY values from 0 to 1
117 fKey = 1.0;
118 if( fCyan < fKey ) fKey = fCyan;
119 if( fMagenta < fKey ) fKey = fMagenta;
120 if( fYellow < fKey ) fKey = fYellow;
122 if( fKey >= 1.0 )
124 //Black
125 fCyan = 0.0;
126 fMagenta = 0.0;
127 fYellow = 0.0;
129 else
131 fCyan = ( fCyan - fKey ) / ( 1.0 - fKey );
132 fMagenta = ( fMagenta - fKey ) / ( 1.0 - fKey );
133 fYellow = ( fYellow - fKey ) / ( 1.0 - fKey );
137 // ====================================================================
139 class HexColorControl : public Edit
141 public:
142 HexColorControl( Window* pParent, const ResId& rResId );
144 virtual long PreNotify( NotifyEvent& rNEvt );
145 virtual void Paste();
147 void SetColor( sal_Int32 nColor );
148 sal_Int32 GetColor();
150 private:
151 bool ImplProcessKeyInput( const KeyEvent& rKEv );
154 HexColorControl::HexColorControl( Window* pParent, const ResId& rResId )
155 : Edit( pParent, rResId )
157 SetMaxTextLen( 6 );
160 // -----------------------------------------------------------------------
162 void HexColorControl::SetColor( sal_Int32 nColor )
164 OUStringBuffer aBuffer;
165 sax::Converter::convertColor( aBuffer, nColor );
166 SetText( aBuffer.makeStringAndClear().copy(1) );
169 // -----------------------------------------------------------------------
171 sal_Int32 HexColorControl::GetColor()
173 sal_Int32 nColor = -1;
175 OUString aStr( "#" );
176 aStr += GetText();
177 sal_Int32 nLen = aStr.getLength();
178 if( nLen < 7 )
180 static const sal_Char* pNullStr = "000000";
181 aStr += OUString::createFromAscii( &pNullStr[nLen-1] );
184 sax::Converter::convertColor( nColor, aStr );
186 if( nColor == -1 )
187 SetControlBackground( Color( COL_RED ) );
188 else
189 SetControlBackground();
191 return nColor;
194 // -----------------------------------------------------------------------
196 long HexColorControl::PreNotify( NotifyEvent& rNEvt )
198 if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
200 if ( ImplProcessKeyInput( *rNEvt.GetKeyEvent() ) )
201 return 1;
204 return Edit::PreNotify( rNEvt );
207 // -----------------------------------------------------------------------
209 void HexColorControl::Paste()
211 ::com::sun::star::uno::Reference<com::sun::star::datatransfer::clipboard::XClipboard> aClipboard(GetClipboard());
212 if ( aClipboard.is() )
214 ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable > xDataObj;
216 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
220 xDataObj = aClipboard->getContents();
222 catch( const ::com::sun::star::uno::Exception& )
226 Application::AcquireSolarMutex( nRef );
228 if ( xDataObj.is() )
230 ::com::sun::star::datatransfer::DataFlavor aFlavor;
231 SotExchange::GetFormatDataFlavor( SOT_FORMAT_STRING, aFlavor );
234 ::com::sun::star::uno::Any aData = xDataObj->getTransferData( aFlavor );
235 OUString aText;
236 aData >>= aText;
238 if( !aText.isEmpty() && aText.matchAsciiL( "#", 1, 0 ) )
239 aText = aText.copy(1);
241 if( aText.getLength() > 6 )
242 aText = aText.copy( 0, 6 );
244 SetText( aText );
246 catch( const ::com::sun::star::uno::Exception& )
253 // -----------------------------------------------------------------------
255 bool HexColorControl::ImplProcessKeyInput( const KeyEvent& rKEv )
257 const KeyCode& rKeyCode = rKEv.GetKeyCode();
259 if( rKeyCode.GetGroup() == KEYGROUP_ALPHA && !rKeyCode.IsMod1() && !rKeyCode.IsMod2() )
261 if( (rKeyCode.GetCode() < KEY_A) || (rKeyCode.GetCode() > KEY_F) )
262 return true;
264 else if( rKeyCode.GetGroup() == KEYGROUP_NUM )
266 if( rKeyCode.IsShift() )
267 return true;
269 return false;
272 // ====================================================================
274 class ColorPreviewControl : public Control
276 public:
277 ColorPreviewControl( Window* pParent, const ResId& rResId );
279 virtual void Paint( const Rectangle& rRect );
281 void SetColor( const Color& rColor );
282 private:
283 Color maColor;
286 // -----------------------------------------------------------------------
288 ColorPreviewControl::ColorPreviewControl( Window* pParent, const ResId& rResId )
289 : Control( pParent, rResId )
291 SetFillColor( maColor );
292 SetLineColor( maColor );
295 // -----------------------------------------------------------------------
297 void ColorPreviewControl::SetColor( const Color& rCol )
299 if( rCol != maColor )
301 maColor = rCol;
302 SetFillColor( maColor );
303 SetLineColor( maColor );
304 Invalidate();
308 // -----------------------------------------------------------------------
310 void ColorPreviewControl::Paint( const Rectangle& rRect )
312 DrawRect( rRect );
315 // ====================================================================
317 enum ColorMode { HUE, SATURATION, BRIGHTNESS, RED, GREEN, BLUE };
318 const ColorMode DefaultMode = HUE;
320 class ColorFieldControl : public Control
322 public:
323 ColorFieldControl( Window* pParent, const ResId& rResId );
324 ~ColorFieldControl();
326 virtual void MouseMove( const MouseEvent& rMEvt );
327 virtual void MouseButtonDown( const MouseEvent& rMEvt );
328 virtual void MouseButtonUp( const MouseEvent& rMEvt );
329 virtual void KeyInput( const KeyEvent& rKEvt );
330 virtual void Paint( const Rectangle& rRect );
331 virtual void Resize();
333 void UpdateBitmap();
334 void ShowPosition( const Point& rPos, bool bUpdate );
335 void UpdatePosition();
336 void Modify();
338 void SetValues( Color aColor, ColorMode eMode, double x, double y );
339 double GetX();
340 double GetY();
342 void KeyMove( int dx, int dy );
344 void SetModifyHdl( Link& rLink ) { maModifyHdl = rLink; }
346 private:
347 Link maModifyHdl;
348 ColorMode meMode;
349 Color maColor;
350 double mdX;
351 double mdY;
352 Point maPosition;
353 Bitmap* mpBitmap;
354 std::vector< sal_uInt8 > maRGB_Horiz;
355 std::vector< sal_uInt16 > maGrad_Horiz;
356 std::vector< sal_uInt16 > maPercent_Horiz;
357 std::vector< sal_uInt8 > maRGB_Vert;
358 std::vector< sal_uInt16 > maPercent_Vert;
361 // -----------------------------------------------------------------------
363 ColorFieldControl::ColorFieldControl( Window* pParent, const ResId& rResId )
364 : Control( pParent, rResId )
365 , meMode( DefaultMode )
366 , mdX( -1.0 )
367 , mdY( -1.0 )
368 , mpBitmap( 0 )
370 SetControlBackground();
373 // -----------------------------------------------------------------------
375 ColorFieldControl::~ColorFieldControl()
377 delete mpBitmap;
380 // -----------------------------------------------------------------------
382 void ColorFieldControl::UpdateBitmap()
384 const Size aSize( GetOutputSizePixel() );
386 if( mpBitmap && mpBitmap->GetSizePixel() != aSize )
387 delete mpBitmap, mpBitmap = NULL;
389 const sal_Int32 nWidth = aSize.Width();
390 const sal_Int32 nHeight = aSize.Height();
392 if( !mpBitmap )
394 mpBitmap = new Bitmap( aSize, 24 );
396 maRGB_Horiz.resize( nWidth );
397 maGrad_Horiz.resize( nWidth );
398 maPercent_Horiz.resize( nWidth );
400 sal_uInt8* pRGB = &(*maRGB_Horiz.begin());
401 sal_uInt16* pGrad = &(*maGrad_Horiz.begin());
402 sal_uInt16* pPercent = &(*maPercent_Horiz.begin());
404 for( sal_Int32 x = 0; x < nWidth; x++ )
406 *pRGB++ = static_cast< sal_uInt8 >( (x * 256) / nWidth );
407 *pGrad++ = static_cast< sal_uInt16 >( (x * 359) / nWidth );
408 *pPercent++ = static_cast< sal_uInt16 >( (x * 100) / nWidth );
411 maRGB_Vert.resize( nHeight );
412 maPercent_Vert.resize( nHeight );
414 pRGB = &(*maRGB_Vert.begin());
415 pPercent = &(*maPercent_Vert.begin());
417 sal_Int32 y = nHeight;
418 while( y-- )
420 *pRGB++ = static_cast< sal_uInt8 >( (y * 256) / nHeight );
421 *pPercent++ = static_cast< sal_uInt16 >( (y * 100) / nHeight );
425 sal_uInt8* pRGB_Horiz = &(*maRGB_Horiz.begin());
426 sal_uInt16* pGrad_Horiz = &(*maGrad_Horiz.begin());
427 sal_uInt16* pPercent_Horiz = &(*maPercent_Horiz.begin());
428 sal_uInt8* pRGB_Vert = &(*maRGB_Vert.begin());
429 sal_uInt16* pPercent_Vert = &(*maPercent_Vert.begin());
431 BitmapWriteAccess* pWriteAccess = mpBitmap->AcquireWriteAccess();
432 if( pWriteAccess )
434 BitmapColor aBitmapColor( maColor );
436 sal_uInt16 nHue, nSat, nBri;
437 maColor.RGBtoHSB( nHue, nSat, nBri );
439 // this has been unlooped for performance reason, please do not merge back!
441 sal_uInt16 y = nHeight,x;
443 switch( meMode )
445 case HUE:
446 while( y-- )
448 nBri = pPercent_Vert[y];
449 x = nWidth;
450 while( x-- )
452 nSat = pPercent_Horiz[x];
453 pWriteAccess->SetPixel( y, x, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
456 break;
457 case SATURATION:
458 while( y-- )
460 nBri = pPercent_Vert[y];
461 x = nWidth;
462 while( x-- )
464 nHue = pGrad_Horiz[x];
465 pWriteAccess->SetPixel( y, x, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
468 break;
469 case BRIGHTNESS:
470 while( y-- )
472 nSat = pPercent_Vert[y];
473 x = nWidth;
474 while( x-- )
476 nHue = pGrad_Horiz[x];
477 pWriteAccess->SetPixel( y, x, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
480 break;
481 case RED:
482 while( y-- )
484 aBitmapColor.SetGreen( pRGB_Vert[y] );
485 x = nWidth;
486 while( x-- )
488 aBitmapColor.SetBlue( pRGB_Horiz[x] );
489 pWriteAccess->SetPixel( y, x, aBitmapColor );
492 break;
493 case GREEN:
494 while( y-- )
496 aBitmapColor.SetRed( pRGB_Vert[y] );
497 x = nWidth;
498 while( x-- )
500 aBitmapColor.SetBlue( pRGB_Horiz[x] );
501 pWriteAccess->SetPixel( y, x, aBitmapColor );
504 break;
505 case BLUE:
506 while( y-- )
508 aBitmapColor.SetGreen( pRGB_Vert[y] );
509 x = nWidth;
510 while( x-- )
512 aBitmapColor.SetRed( pRGB_Horiz[x] );
513 pWriteAccess->SetPixel( y, x, aBitmapColor );
516 break;
519 mpBitmap->ReleaseAccess( pWriteAccess );
523 // -----------------------------------------------------------------------
525 void ColorFieldControl::ShowPosition( const Point& rPos, bool bUpdate )
527 if( !mpBitmap )
529 UpdateBitmap();
530 Invalidate();
533 const Size aSize( mpBitmap->GetSizePixel() );
535 long nX = rPos.X();
536 long nY = rPos.Y();
537 if( nX < 0L )
538 nX = 0L;
539 else if( nX >= aSize.Width() )
540 nX = aSize.Width() - 1L;
542 if( nY < 0L )
543 nY= 0L;
544 else if( nY >= aSize.Height() )
545 nY = aSize.Height() - 1L;
547 Point aPos = maPosition;
548 maPosition.X() = nX - 5;
549 maPosition.Y() = nY - 5;
550 Invalidate( Rectangle( aPos, Size( 11, 11) ) );
551 Invalidate( Rectangle( maPosition, Size( 11, 11) ) );
553 if( bUpdate )
555 mdX = (double)nX / (double)(aSize.Width()-1);
556 mdY = (double)(aSize.Height()-1-nY) / (double)(aSize.Height()-1);
558 BitmapReadAccess* pReadAccess = mpBitmap->AcquireReadAccess();
559 if( pReadAccess != NULL )
561 // mpBitmap always has a bit count of 24 => use of GetPixel(...) is safe
562 maColor = pReadAccess->GetPixel( nY, nX );
563 mpBitmap->ReleaseAccess( pReadAccess );
564 pReadAccess = NULL;
568 // -----------------------------------------------------------------------
570 void ColorFieldControl::MouseMove( const MouseEvent& rMEvt )
572 if( rMEvt.IsLeft() )
574 ShowPosition( rMEvt.GetPosPixel(), true );
575 Modify();
579 // -----------------------------------------------------------------------
580 void ColorFieldControl::MouseButtonDown( const MouseEvent& rMEvt )
582 if( rMEvt.IsLeft() && !rMEvt.IsShift() )
584 CaptureMouse();
585 ShowPosition( rMEvt.GetPosPixel(), true );
586 Modify();
590 // -----------------------------------------------------------------------
591 void ColorFieldControl::MouseButtonUp( const MouseEvent& )
593 if( IsMouseCaptured() )
594 ReleaseMouse();
597 // -----------------------------------------------------------------------
599 void ColorFieldControl::KeyMove( int dx, int dy )
601 Size aSize( GetOutputSizePixel() );
602 Point aPos(static_cast<long>(mdX * aSize.Width()), static_cast<long>((1.0 - mdY) * aSize.Height()));
603 aPos.X() += dx;
604 aPos.Y() += dy;
605 if( aPos.X() < 0 )
606 aPos.X() += aSize.Width();
607 else if( aPos.X() >= aSize.Width() )
608 aPos.X() -= aSize.Width();
610 if( aPos.Y() < 0 )
611 aPos.Y() += aSize.Height();
612 else if( aPos.Y() >= aSize.Height() )
613 aPos.Y() -= aSize.Height();
615 ShowPosition( aPos, true );
616 Modify();
619 // -----------------------------------------------------------------------
621 void ColorFieldControl::KeyInput( const KeyEvent& rKEvt )
623 bool bShift = rKEvt.GetKeyCode().IsShift();
624 bool bCtrl = rKEvt.GetKeyCode().IsMod1();
625 bool bAlt = rKEvt.GetKeyCode().IsMod2();
627 if ( !bAlt && !bShift )
629 switch( rKEvt.GetKeyCode().GetCode() )
631 case KEY_DOWN: KeyMove( 0, bCtrl ? 5 : 1 ); return;
632 case KEY_UP: KeyMove( 0, bCtrl ? -5 : -1 ); return;
633 case KEY_LEFT: KeyMove( bCtrl ? -5 : -1, 0 ); return;
634 case KEY_RIGHT: KeyMove( bCtrl ? 5 : 1, 0 ); return;
637 Control::KeyInput( rKEvt );
640 // -----------------------------------------------------------------------
642 void ColorFieldControl::Paint( const Rectangle& rRect )
644 if( !mpBitmap )
645 UpdateBitmap();
647 Bitmap aOutputBitmap( *mpBitmap );
649 if( GetBitCount() <= 8 )
650 aOutputBitmap.Dither();
652 DrawBitmap( rRect.TopLeft(), rRect.GetSize(), rRect.TopLeft(), rRect.GetSize(), aOutputBitmap );
654 // draw circle around current color
655 if( maColor.IsDark() )
656 SetLineColor( COL_WHITE );
657 else
658 SetLineColor( COL_BLACK );
660 SetFillColor();
662 DrawEllipse( Rectangle( maPosition, Size( 11, 11) ) );
665 // -----------------------------------------------------------------------
667 void ColorFieldControl::Resize()
669 UpdateBitmap();
670 Control::Resize();
673 // -----------------------------------------------------------------------
675 void ColorFieldControl::Modify()
677 maModifyHdl.Call( this );
680 // -----------------------------------------------------------------------
682 void ColorFieldControl::SetValues( Color aColor, ColorMode eMode, double x, double y )
684 bool bUpdateBitmap = (maColor!= aColor) || (meMode != eMode);
685 if( bUpdateBitmap || (mdX != x) || (mdY != y) )
687 maColor = aColor;
688 meMode = eMode;
689 mdX = x;
690 mdY = y;
692 if( bUpdateBitmap )
693 UpdateBitmap();
694 UpdatePosition();
695 if( bUpdateBitmap )
696 Invalidate();
700 // -----------------------------------------------------------------------
702 double ColorFieldControl::GetX()
704 return mdX;
707 // -----------------------------------------------------------------------
709 double ColorFieldControl::GetY()
711 return mdY;
714 // -----------------------------------------------------------------------
716 void ColorFieldControl::UpdatePosition()
718 Size aSize( GetOutputSizePixel() );
719 ShowPosition( Point(static_cast<long>(mdX * aSize.Width()), static_cast<long>((1.0 - mdY) * aSize.Height())), false );
722 // ====================================================================
724 class ColorSliderControl : public Control
726 public:
727 ColorSliderControl( Window* pParent, const ResId& rResId );
728 ~ColorSliderControl();
730 virtual void MouseMove( const MouseEvent& rMEvt );
731 virtual void MouseButtonDown( const MouseEvent& rMEvt );
732 virtual void MouseButtonUp( const MouseEvent& rMEvt );
733 virtual void KeyInput( const KeyEvent& rKEvt );
734 virtual void Paint( const Rectangle& rRect );
735 virtual void Resize();
737 void UpdateBitmap();
738 void ChangePosition( long nY );
739 void Modify();
741 void SetValue( const Color& rColor, ColorMode eMode, double dValue );
742 double GetValue() const { return mdValue; }
744 void KeyMove( int dy );
746 void SetModifyHdl( Link& rLink ) { maModifyHdl = rLink; }
748 sal_Int16 GetLevel() const { return mnLevel; }
750 private:
751 Link maModifyHdl;
752 Color maColor;
753 ColorMode meMode;
754 Bitmap* mpBitmap;
755 sal_Int16 mnLevel;
756 double mdValue;
759 // -----------------------------------------------------------------------
761 ColorSliderControl::ColorSliderControl( Window* pParent, const ResId& rResId )
762 : Control( pParent, rResId )
763 , meMode( DefaultMode )
764 , mpBitmap( 0 )
765 , mnLevel( 0 )
766 , mdValue( -1.0 )
768 SetControlBackground();
771 // -----------------------------------------------------------------------
773 ColorSliderControl::~ColorSliderControl()
775 delete mpBitmap;
778 // -----------------------------------------------------------------------
780 void ColorSliderControl::UpdateBitmap()
782 Size aSize( 1, GetOutputSizePixel().Height() );
784 if( mpBitmap && mpBitmap->GetSizePixel() != aSize )
785 delete mpBitmap, mpBitmap = NULL;
787 if( !mpBitmap )
788 mpBitmap = new Bitmap( aSize, 24 );
790 BitmapWriteAccess* pWriteAccess = mpBitmap->AcquireWriteAccess();
792 if( pWriteAccess )
794 const long nY = aSize.Height()-1;
796 BitmapColor aBitmapColor( maColor );
798 sal_uInt16 nHue, nSat, nBri;
799 maColor.RGBtoHSB( nHue, nSat, nBri );
801 // this has been unlooped for performance reason, please do not merge back!
803 switch( meMode )
805 case HUE:
806 nSat = 100;
807 nBri = 100;
808 for( long y = 0; y <= nY; y++ )
810 nHue = static_cast< sal_uInt16 >( (359 * y) / nY );
811 aBitmapColor = BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) );
812 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
814 break;
816 case SATURATION:
817 nBri = std::max( (sal_uInt16)32, nBri );
818 for( long y = 0; y <= nY; y++ )
820 nSat = static_cast< sal_uInt16 >( (100 * y) / nY );
821 pWriteAccess->SetPixel( nY-y, 0, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
823 break;
825 case BRIGHTNESS:
826 for( long y = 0; y <= nY; y++ )
828 nBri = static_cast< sal_uInt16 >( (100 * y) / nY );
829 pWriteAccess->SetPixel( nY-y, 0, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
831 break;
833 case RED:
834 for( long y = 0; y <= nY; y++ )
836 aBitmapColor.SetRed( sal_uInt8( ((long)255 * y) / nY ) );
837 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
839 break;
841 case GREEN:
842 for( long y = 0; y <= nY; y++ )
844 aBitmapColor.SetGreen( sal_uInt8( ((long)255 * y) / nY ) );
845 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
847 break;
849 case BLUE:
850 for( long y = 0; y <= nY; y++ )
852 aBitmapColor.SetBlue( sal_uInt8( ((long)255 * y) / nY ) );
853 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
855 break;
858 mpBitmap->ReleaseAccess( pWriteAccess );
862 // -----------------------------------------------------------------------
864 void ColorSliderControl::ChangePosition( long nY )
866 const long nHeight = GetOutputSizePixel().Height() - 1;
868 if( nY < 0L )
869 nY = 0;
870 else if( nY > nHeight )
871 nY = nHeight;
873 mnLevel = nY;
874 mdValue = ((double)(nHeight - nY)) / (double)nHeight;
877 // -----------------------------------------------------------------------
879 void ColorSliderControl::MouseMove( const MouseEvent& rMEvt )
881 if( rMEvt.IsLeft() )
883 ChangePosition( rMEvt.GetPosPixel().Y() );
884 Modify();
888 // -----------------------------------------------------------------------
889 void ColorSliderControl::MouseButtonDown( const MouseEvent& rMEvt )
891 if( rMEvt.IsLeft() && !rMEvt.IsShift() )
893 CaptureMouse();
894 ChangePosition( rMEvt.GetPosPixel().Y() );
895 Modify();
899 // -----------------------------------------------------------------------
900 void ColorSliderControl::MouseButtonUp( const MouseEvent& )
902 if( IsMouseCaptured() )
903 ReleaseMouse();
906 // -----------------------------------------------------------------------
908 void ColorSliderControl::KeyMove( int dy )
910 ChangePosition( mnLevel + dy );
911 Modify();
914 // -----------------------------------------------------------------------
916 void ColorSliderControl::KeyInput( const KeyEvent& rKEvt )
918 if ( !rKEvt.GetKeyCode().IsMod2() && !rKEvt.GetKeyCode().IsShift() )
920 switch( rKEvt.GetKeyCode().GetCode() )
922 case KEY_DOWN: KeyMove( rKEvt.GetKeyCode().IsMod1() ? 5 : 1 ); return;
923 case KEY_UP: KeyMove( rKEvt.GetKeyCode().IsMod1() ? -5 : -1 ); return;
927 Control::KeyInput( rKEvt );
929 // -----------------------------------------------------------------------
931 void ColorSliderControl::Paint( const Rectangle& /*rRect*/ )
933 if( !mpBitmap )
934 UpdateBitmap();
936 const Size aSize( GetOutputSizePixel() );
938 Bitmap aOutputBitmap( *mpBitmap );
940 if( GetBitCount() <= 8 )
941 aOutputBitmap.Dither();
943 Point aPos;
944 int x = aSize.Width();
945 while( x-- )
947 DrawBitmap( aPos, aOutputBitmap );
948 aPos.X() += 1;
952 // -----------------------------------------------------------------------
954 void ColorSliderControl::Resize()
956 UpdateBitmap();
957 Control::Resize();
960 // -----------------------------------------------------------------------
962 void ColorSliderControl::Modify()
964 maModifyHdl.Call( this );
967 // -----------------------------------------------------------------------
969 void ColorSliderControl::SetValue( const Color& rColor, ColorMode eMode, double dValue )
971 bool bUpdateBitmap = (rColor != maColor) || (eMode != meMode);
972 if( bUpdateBitmap || (mdValue != dValue))
974 maColor = rColor;
975 mdValue = dValue;
976 mnLevel = static_cast<sal_Int16>((1.0-dValue) * GetOutputSizePixel().Height());
977 meMode = eMode;
978 if( bUpdateBitmap )
979 UpdateBitmap();
980 Invalidate();
984 // ====================================================================
986 const sal_uInt16 UPDATE_RGB = 0x01;
987 const sal_uInt16 UPDATE_CMYK = 0x02;
988 const sal_uInt16 UPDATE_HSB = 0x04;
989 const sal_uInt16 UPDATE_COLORCHOOSER = 0x08;
990 const sal_uInt16 UPDATE_COLORSLIDER = 0x10;
991 const sal_uInt16 UPDATE_HEX = 0x20;
992 const sal_uInt16 UPDATE_ALL = 0xff;
994 class ColorPickerDialog : public ModalDialog
996 public:
997 ColorPickerDialog( Window* pParent, sal_Int32 nColor, sal_Int16 nMode );
999 void update_color( sal_uInt16 n = UPDATE_ALL );
1001 DECL_LINK( ColorModifyHdl, void * );
1002 DECL_LINK( ModeModifyHdl, void * );
1004 sal_Int32 GetColor() const;
1006 void setColorComponent( sal_uInt16 nComp, double dValue );
1008 private:
1009 Color maPreviousColor;
1010 sal_Int16 mnDialogMode;
1011 ColorMode meMode;
1013 double mdRed, mdGreen, mdBlue;
1014 double mdHue, mdSat, mdBri;
1015 double mdCyan, mdMagenta, mdYellow, mdKey;
1017 private:
1018 ColorFieldControl maColorField;
1019 ColorSliderControl maColorSlider;
1020 ColorPreviewControl maColorPreview;
1021 ColorPreviewControl maColorPrevious;
1023 FixedImage maFISliderLeft;
1024 FixedImage maFISliderRight;
1025 Image maSliderImage;
1027 #if 0
1028 ImageButton maBtnPicker;
1029 #endif
1031 FixedLine maFLRGB;
1033 RadioButton maRBRed;
1034 RadioButton maRBGreen;
1035 RadioButton maRBBlue;
1036 RadioButton maRBHue;
1037 RadioButton maRBSaturation;
1038 RadioButton maRBBrightness;
1040 FixedText maFTRed;
1041 MetricField maMFRed;
1042 FixedText maFTGreen;
1043 MetricField maMFGreen;
1044 FixedText maFTBlue;
1045 MetricField maMFBlue;
1046 FixedText maFTHex;
1047 HexColorControl maEDHex;
1049 FixedLine maFLHSB;
1050 FixedText maFTHue;
1051 MetricField maMFHue;
1052 FixedText maFTSaturation;
1053 MetricField maMFSaturation;
1054 FixedText maFTBrightness;
1055 MetricField maMFBrightness;
1057 FixedLine maFLCMYK;
1058 FixedText maFTCyan;
1059 MetricField maMFCyan;
1060 FixedText maFTMagenta;
1061 MetricField maMFMagenta;
1062 FixedText maFTYellow;
1063 MetricField maMFYellow;
1064 FixedText maFTKey;
1065 MetricField maMFKey;
1067 FixedLine maFLBottmLine;
1068 HelpButton maBTNHelp;
1069 OKButton maBTNOk;
1070 CancelButton maBTNCancel;
1073 // --------------------------------------------------------------------
1075 ColorPickerDialog::ColorPickerDialog( Window* pParent, sal_Int32 nColor, sal_Int16 nMode )
1076 : ModalDialog( pParent, CUI_RES( RID_CUI_DIALOG_COLORPICKER ) )
1077 , maPreviousColor( nColor )
1078 , mnDialogMode( nMode )
1079 , meMode( DefaultMode )
1080 , maColorField( this, CUI_RES( CT_COLORFIELD ) )
1081 , maColorSlider( this, CUI_RES( CT_COLORSLIDER ) )
1082 , maColorPreview( this, CUI_RES( CT_PREVIEW ) )
1083 , maColorPrevious( this, CUI_RES( CT_PREVIOUS ) )
1084 , maFISliderLeft( this, CUI_RES( CT_LEFT_SLIDER ) )
1085 , maFISliderRight( this, CUI_RES( CT_RIGHT_SLIDER ) )
1086 , maSliderImage( CUI_RES( CT_SLIDERIMG ) )
1087 #if 0
1088 , maBtnPicker( this, CUI_RES( PB_PICKER ) )
1089 #endif
1090 , maFLRGB( this, CUI_RES( FL_RGB ) )
1091 , maRBRed( this, CUI_RES( CT_RED ) )
1092 , maRBGreen( this, CUI_RES( CT_GREEN ) )
1093 , maRBBlue( this, CUI_RES( CT_BLUE ) )
1094 , maRBHue( this, CUI_RES( CT_HUE ) )
1095 , maRBSaturation( this, CUI_RES( CT_SATURATION ) )
1096 , maRBBrightness( this, CUI_RES( CT_BRIGHTNESS ) )
1097 , maFTRed( this, CUI_RES( CT_RED ) )
1098 , maMFRed( this, CUI_RES( CT_RED ) )
1099 , maFTGreen( this, CUI_RES( CT_GREEN ) )
1100 , maMFGreen( this, CUI_RES( CT_GREEN ) )
1101 , maFTBlue( this, CUI_RES( CT_BLUE ) )
1102 , maMFBlue( this, CUI_RES( CT_BLUE ) )
1103 , maFTHex( this, CUI_RES( CT_HEX ) )
1104 , maEDHex( this, CUI_RES( CT_HEX ) )
1105 , maFLHSB( this, CUI_RES( FL_HSB ) )
1106 , maFTHue( this, CUI_RES( CT_HUE ) )
1107 , maMFHue( this, CUI_RES( CT_HUE ) )
1108 , maFTSaturation( this, CUI_RES( CT_SATURATION ) )
1109 , maMFSaturation( this, CUI_RES( CT_SATURATION ) )
1110 , maFTBrightness( this, CUI_RES( CT_BRIGHTNESS ) )
1111 , maMFBrightness( this, CUI_RES( CT_BRIGHTNESS ) )
1112 , maFLCMYK( this, CUI_RES( FL_CMYK ) )
1113 , maFTCyan( this, CUI_RES( CT_CYAN ) )
1114 , maMFCyan( this, CUI_RES( CT_CYAN ) )
1115 , maFTMagenta( this, CUI_RES( CT_MAGENTA ) )
1116 , maMFMagenta( this, CUI_RES( CT_MAGENTA ) )
1117 , maFTYellow( this, CUI_RES( CT_YELLOW ) )
1118 , maMFYellow( this, CUI_RES( CT_YELLOW ) )
1119 , maFTKey( this, CUI_RES( CT_KEY ) )
1120 , maMFKey( this, CUI_RES( CT_KEY ) )
1122 , maFLBottmLine( this, CUI_RES( FT_BOTTOMLINE ) )
1123 , maBTNHelp( this, CUI_RES( BTN_HELP ) )
1124 , maBTNOk( this, CUI_RES( BTN_OK ) )
1125 , maBTNCancel( this, CUI_RES( BTN_CANCEL ) )
1127 FreeResource();
1129 Link aLink( LINK( this, ColorPickerDialog, ColorModifyHdl ) );
1130 maColorField.SetModifyHdl( aLink );
1131 maColorSlider.SetModifyHdl( aLink );
1133 maMFRed.SetModifyHdl( aLink );
1134 maMFGreen.SetModifyHdl( aLink );
1135 maMFBlue.SetModifyHdl( aLink );
1137 maMFCyan.SetModifyHdl( aLink );
1138 maMFMagenta.SetModifyHdl( aLink );
1139 maMFYellow.SetModifyHdl( aLink );
1140 maMFKey.SetModifyHdl( aLink );
1142 maMFHue.SetModifyHdl( aLink );
1143 maMFSaturation.SetModifyHdl( aLink );
1144 maMFBrightness.SetModifyHdl( aLink );
1146 maEDHex.SetModifyHdl( aLink );
1148 aLink = LINK( this, ColorPickerDialog, ModeModifyHdl );
1149 maRBRed.SetToggleHdl( aLink );
1150 maRBGreen.SetToggleHdl( aLink );
1151 maRBBlue.SetToggleHdl( aLink );
1152 maRBHue.SetToggleHdl( aLink );
1153 maRBSaturation.SetToggleHdl( aLink );
1154 maRBBrightness.SetToggleHdl( aLink );
1156 Image aSliderImage( maSliderImage );
1158 maFISliderLeft.SetImage( aSliderImage );
1160 BitmapEx aTmpBmp( maSliderImage.GetBitmapEx() );
1161 aTmpBmp.Mirror( BMP_MIRROR_HORZ );
1162 maFISliderRight.SetImage( Image( aTmpBmp ) );
1164 Size aSize( maSliderImage.GetSizePixel() );
1165 maFISliderLeft.SetSizePixel( aSize );
1166 maFISliderRight.SetSizePixel( aSize );
1168 Point aPos( maColorSlider.GetPosPixel() );
1170 aPos.X() -= aSize.Width();
1171 aPos.Y() -= aSize.Height() / 2;
1172 maFISliderLeft.SetPosPixel( aPos );
1174 aPos.X() += aSize.Width() + maColorSlider.GetSizePixel().Width();
1175 maFISliderRight.SetPosPixel( aPos );
1177 Color aColor( nColor );
1179 // modify
1180 if( mnDialogMode == 2 )
1182 maColorPreview.SetSizePixel( maColorPrevious.GetSizePixel() );
1183 maColorPrevious.SetColor( aColor );
1184 maColorPrevious.Show( true );
1187 mdRed = ((double)aColor.GetRed()) / 255.0;
1188 mdGreen = ((double)aColor.GetGreen()) / 255.0;
1189 mdBlue = ((double)aColor.GetBlue()) / 255.0;
1191 RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1192 RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1194 update_color();
1197 // --------------------------------------------------------------------
1199 static int toInt( double dValue, double dRange )
1201 return static_cast< int >( std::floor((dValue * dRange) + 0.5 ) );
1204 sal_Int32 ColorPickerDialog::GetColor() const
1206 return Color( toInt(mdRed,255.0), toInt(mdGreen,255.0), toInt(mdBlue,255.0) ).GetColor();
1209 void ColorPickerDialog::update_color( sal_uInt16 n )
1211 sal_uInt8 nRed = toInt(mdRed,255.0);
1212 sal_uInt8 nGreen = toInt(mdGreen,255.0);
1213 sal_uInt8 nBlue = toInt(mdBlue,255.0);
1215 Color aColor( nRed, nGreen, nBlue );
1217 if( n & UPDATE_RGB ) // update RGB
1219 maMFRed.SetValue( nRed );
1220 maMFGreen.SetValue( nGreen );
1221 maMFBlue.SetValue( nBlue );
1224 if( n & UPDATE_CMYK ) // update CMYK
1226 maMFCyan.SetValue( toInt( mdCyan, 100.0 ) );
1227 maMFMagenta.SetValue( toInt( mdMagenta, 100.0 ) );
1228 maMFYellow.SetValue( toInt( mdYellow, 100.0 ) );
1229 maMFKey.SetValue( toInt( mdKey, 100.0 ) );
1232 if( n & UPDATE_HSB ) // update HSB
1234 maMFHue.SetValue( toInt( mdHue, 1.0 ) );
1235 maMFSaturation.SetValue( toInt( mdSat, 100.0 ) );
1236 maMFBrightness.SetValue( toInt( mdBri, 100.0 ) );
1239 if( n & UPDATE_COLORCHOOSER ) // update Color Chooser 1
1241 switch( meMode )
1243 case HUE: maColorField.SetValues( aColor, meMode, mdSat, mdBri ); break;
1244 case SATURATION: maColorField.SetValues( aColor, meMode, mdHue / 360.0, mdBri ); break;
1245 case BRIGHTNESS: maColorField.SetValues( aColor, meMode, mdHue / 360.0, mdSat ); break;
1246 case RED: maColorField.SetValues( aColor, meMode, mdBlue, mdGreen ); break;
1247 case GREEN: maColorField.SetValues( aColor, meMode, mdBlue, mdRed ); break;
1248 case BLUE: maColorField.SetValues( aColor, meMode, mdRed, mdGreen ); break;
1252 if( n & UPDATE_COLORSLIDER ) // update Color Chooser 2
1254 switch( meMode )
1256 case HUE: maColorSlider.SetValue( aColor, meMode, mdHue / 360.0 ); break;
1257 case SATURATION: maColorSlider.SetValue( aColor, meMode, mdSat ); break;
1258 case BRIGHTNESS: maColorSlider.SetValue( aColor, meMode, mdBri ); break;
1259 case RED: maColorSlider.SetValue( aColor, meMode, mdRed ); break;
1260 case GREEN: maColorSlider.SetValue( aColor, meMode, mdGreen ); break;
1261 case BLUE: maColorSlider.SetValue( aColor, meMode, mdBlue ); break;
1265 if( n & UPDATE_HEX ) // update hex
1267 maEDHex.SetColor( aColor.GetColor() );
1271 Point aPos( 0, maColorSlider.GetLevel() + maColorSlider.GetPosPixel().Y() - 1 );
1273 aPos.X() = maFISliderLeft.GetPosPixel().X();
1274 if( aPos != maFISliderLeft.GetPosPixel() )
1276 maFISliderLeft.SetPosPixel( aPos );
1278 aPos.X() = maFISliderRight.GetPosPixel().X();
1279 maFISliderRight.SetPosPixel( aPos );
1283 maColorPreview.SetColor( aColor );
1286 // --------------------------------------------------------------------
1288 IMPL_LINK( ColorPickerDialog, ColorModifyHdl, void *, p )
1290 sal_uInt16 n = 0;
1292 if( p == &maColorField )
1294 double x = maColorField.GetX();
1295 double y = maColorField.GetY();
1297 switch( meMode )
1299 case HUE: mdSat = x; setColorComponent( COLORCOMP_BRI, y ); break;
1300 case SATURATION: mdHue = x * 360.0; setColorComponent( COLORCOMP_BRI, y ); break;
1301 case BRIGHTNESS: mdHue = x * 360.0; setColorComponent( COLORCOMP_SAT, y ); break;
1302 case RED: mdBlue = x; setColorComponent( COLORCOMP_GREEN, y ); break;
1303 case GREEN: mdBlue = x; setColorComponent( COLORCOMP_RED, y ); break;
1304 case BLUE: mdRed = x; setColorComponent( COLORCOMP_GREEN, y ); break;
1307 n = UPDATE_ALL&~(UPDATE_COLORCHOOSER);
1309 else if( p == &maColorSlider )
1311 double dValue = maColorSlider.GetValue();
1312 switch( meMode )
1314 case HUE: setColorComponent( COLORCOMP_HUE, dValue * 360.0 ); break;
1315 case SATURATION: setColorComponent( COLORCOMP_SAT, dValue ); break;
1316 case BRIGHTNESS: setColorComponent( COLORCOMP_BRI, dValue ); break;
1317 case RED: setColorComponent( COLORCOMP_RED, dValue ); break;
1318 case GREEN: setColorComponent( COLORCOMP_GREEN, dValue ); break;
1319 case BLUE: setColorComponent( COLORCOMP_BLUE, dValue ); break;
1322 n = UPDATE_ALL&~(UPDATE_COLORSLIDER);
1324 else if( p == &maMFRed )
1326 setColorComponent( COLORCOMP_RED, ((double)maMFRed.GetValue()) / 255.0 );
1327 n = UPDATE_ALL&~(UPDATE_RGB);
1329 else if( p == &maMFGreen )
1331 setColorComponent( COLORCOMP_GREEN, ((double)maMFGreen.GetValue()) / 255.0 );
1332 n = UPDATE_ALL&~(UPDATE_RGB);
1334 else if( p == &maMFBlue )
1336 setColorComponent( COLORCOMP_BLUE, ((double)maMFBlue.GetValue()) / 255.0 );
1337 n = UPDATE_ALL&~(UPDATE_RGB);
1339 else if( p == &maMFHue )
1341 setColorComponent( COLORCOMP_HUE, (double)maMFHue.GetValue() );
1342 n = UPDATE_ALL&~(UPDATE_HSB);
1344 else if( p == &maMFSaturation )
1346 setColorComponent( COLORCOMP_SAT, ((double)maMFSaturation.GetValue()) / 100.0 );
1347 n = UPDATE_ALL&~(UPDATE_HSB);
1349 else if( p == &maMFBrightness )
1351 setColorComponent( COLORCOMP_BRI, ((double)maMFBrightness.GetValue()) / 100.0 );
1352 n = UPDATE_ALL&~(UPDATE_HSB);
1354 else if( p == &maMFCyan )
1356 setColorComponent( COLORCOMP_CYAN, ((double)maMFCyan.GetValue()) / 100.0 );
1357 n = UPDATE_ALL&~(UPDATE_CMYK);
1359 else if( p == &maMFMagenta )
1361 setColorComponent( COLORCOMP_MAGENTA, ((double)maMFMagenta.GetValue()) / 100.0 );
1362 n = UPDATE_ALL&~(UPDATE_CMYK);
1364 else if( p == &maMFYellow )
1366 setColorComponent( COLORCOMP_YELLOW, ((double)maMFYellow.GetValue()) / 100.0 );
1367 n = UPDATE_ALL&~(UPDATE_CMYK);
1369 else if( p == &maMFKey )
1371 setColorComponent( COLORCOMP_KEY, ((double)maMFKey.GetValue()) / 100.0 );
1372 n = UPDATE_ALL&~(UPDATE_CMYK);
1374 else if( p == &maEDHex )
1376 sal_Int32 nColor = maEDHex.GetColor();
1378 if( nColor != -1 )
1380 Color aColor( nColor );
1382 if( aColor != GetColor() )
1384 mdRed = ((double)aColor.GetRed()) / 255.0;
1385 mdGreen = ((double)aColor.GetGreen()) / 255.0;
1386 mdBlue = ((double)aColor.GetBlue()) / 255.0;
1388 RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1389 RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1390 n = UPDATE_ALL&~(UPDATE_HEX);
1395 if( n )
1396 update_color( n );
1398 return 0;
1401 // --------------------------------------------------------------------
1403 IMPL_LINK_NOARG(ColorPickerDialog, ModeModifyHdl)
1405 ColorMode eMode = HUE;
1407 if( maRBRed.IsChecked() )
1409 eMode = RED;
1411 else if( maRBGreen.IsChecked() )
1413 eMode = GREEN;
1415 else if( maRBBlue.IsChecked() )
1417 eMode = BLUE;
1419 else if( maRBSaturation.IsChecked() )
1421 eMode = SATURATION;
1423 else if( maRBBrightness.IsChecked() )
1425 eMode = BRIGHTNESS;
1428 if( meMode != eMode )
1430 meMode = eMode;
1431 update_color( UPDATE_COLORCHOOSER | UPDATE_COLORSLIDER );
1434 return 0;
1437 // --------------------------------------------------------------------
1439 void ColorPickerDialog::setColorComponent( sal_uInt16 nComp, double dValue )
1441 switch( nComp )
1443 case COLORCOMP_RED: mdRed = dValue; break;
1444 case COLORCOMP_GREEN: mdGreen = dValue; break;
1445 case COLORCOMP_BLUE: mdBlue = dValue; break;
1446 case COLORCOMP_HUE: mdHue = dValue; break;
1447 case COLORCOMP_SAT: mdSat = dValue; break;
1448 case COLORCOMP_BRI: mdBri = dValue; break;
1449 case COLORCOMP_CYAN: mdCyan = dValue; break;
1450 case COLORCOMP_YELLOW: mdYellow = dValue; break;
1451 case COLORCOMP_MAGENTA: mdMagenta = dValue; break;
1452 case COLORCOMP_KEY: mdKey = dValue; break;
1455 if( nComp & COLORMODE_RGB )
1457 RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1458 RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1460 else if( nComp & COLORMODE_HSV )
1462 HSVtoRGB( mdHue, mdSat, mdBri, mdRed, mdGreen, mdBlue );
1463 RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1465 else
1467 CMYKtoRGB( mdCyan, mdMagenta, mdYellow, mdKey, mdRed, mdGreen, mdBlue );
1468 RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1472 // --------------------------------------------------------------------
1474 typedef ::cppu::WeakComponentImplHelper4< XServiceInfo, XExecutableDialog, XInitialization, XPropertyAccess > ColorPickerBase;
1476 class ColorPicker : protected ::comphelper::OBaseMutex, // Struct for right initalization of mutex member! Must be first of baseclasses.
1477 public ColorPickerBase
1479 public:
1480 ColorPicker( Reference< XComponentContext > const & xContext );
1482 // XInitialization
1483 virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException);
1485 // XInitialization
1486 virtual OUString SAL_CALL getImplementationName( ) throw (RuntimeException);
1487 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException);
1488 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException);
1490 // XPropertyAccess
1491 virtual Sequence< PropertyValue > SAL_CALL getPropertyValues( ) throw (RuntimeException);
1492 virtual void SAL_CALL setPropertyValues( const Sequence< PropertyValue >& aProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException);
1494 // XExecutableDialog
1495 virtual void SAL_CALL setTitle( const OUString& aTitle ) throw (RuntimeException);
1496 virtual sal_Int16 SAL_CALL execute( ) throw (RuntimeException);
1498 private:
1499 Reference< XComponentContext > mxContext;
1500 OUString msTitle;
1501 const OUString msColorKey;
1502 const OUString msModeKey;
1503 sal_Int32 mnColor;
1504 sal_Int16 mnMode;
1505 Reference< ::com::sun::star::awt::XWindow > mxParent;
1508 // --------------------------------------------------------------------
1510 OUString SAL_CALL ColorPicker_getImplementationName()
1512 return OUString( "com.sun.star.cui.ColorPicker" );
1515 // --------------------------------------------------------------------
1517 Reference< XInterface > SAL_CALL ColorPicker_createInstance( Reference< XComponentContext > const & xContext ) SAL_THROW( (Exception) )
1519 return static_cast<XWeak*>( new ColorPicker( xContext ) );
1522 // --------------------------------------------------------------------
1524 Sequence< OUString > SAL_CALL ColorPicker_getSupportedServiceNames() throw( RuntimeException )
1526 Sequence< OUString > seq(1);
1527 seq[0] = OUString( "com.sun.star.ui.dialogs.ColorPicker" );
1528 return seq;
1531 // --------------------------------------------------------------------
1533 ColorPicker::ColorPicker( Reference< XComponentContext > const & xContext )
1534 : ColorPickerBase( m_aMutex )
1535 , mxContext( xContext )
1536 , msColorKey( "Color" )
1537 , msModeKey( "Mode" )
1538 , mnColor( 0 )
1539 , mnMode( 0 )
1543 // --------------------------------------------------------------------
1545 // XInitialization
1546 void SAL_CALL ColorPicker::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
1548 if( aArguments.getLength() == 1 )
1550 aArguments[0] >>= mxParent;
1554 // --------------------------------------------------------------------
1556 // XInitialization
1557 OUString SAL_CALL ColorPicker::getImplementationName( ) throw (RuntimeException)
1559 return ColorPicker_getImplementationName();
1562 // --------------------------------------------------------------------
1564 sal_Bool SAL_CALL ColorPicker::supportsService( const OUString& sServiceName ) throw (RuntimeException)
1566 return sServiceName == "com.sun.star.ui.dialogs.ColorPicker";
1569 // --------------------------------------------------------------------
1571 Sequence< OUString > SAL_CALL ColorPicker::getSupportedServiceNames( ) throw (RuntimeException)
1573 return ColorPicker_getSupportedServiceNames();
1576 // --------------------------------------------------------------------
1578 // XPropertyAccess
1579 Sequence< PropertyValue > SAL_CALL ColorPicker::getPropertyValues( ) throw (RuntimeException)
1581 Sequence< PropertyValue > props(1);
1582 props[0].Name = msColorKey;
1583 props[0].Value <<= mnColor;
1584 return props;
1587 // --------------------------------------------------------------------
1589 void SAL_CALL ColorPicker::setPropertyValues( const Sequence< PropertyValue >& aProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
1591 for( sal_Int32 n = 0; n < aProps.getLength(); n++ )
1593 if( aProps[n].Name.equals( msColorKey ) )
1595 aProps[n].Value >>= mnColor;
1597 else if( aProps[n].Name.equals( msModeKey ) )
1599 aProps[n].Value >>= mnMode;
1604 // --------------------------------------------------------------------
1606 // XExecutableDialog
1607 void SAL_CALL ColorPicker::setTitle( const OUString& sTitle ) throw (RuntimeException)
1609 msTitle = sTitle;
1612 // --------------------------------------------------------------------
1614 sal_Int16 SAL_CALL ColorPicker::execute( ) throw (RuntimeException)
1616 ColorPickerDialog aDlg( VCLUnoHelper::GetWindow( mxParent ), mnColor, mnMode );
1617 sal_Int16 ret = aDlg.Execute();
1618 if( ret )
1619 mnColor = aDlg.GetColor();
1621 return ret;
1624 // --------------------------------------------------------------------
1628 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */