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/awt/XWindow.hpp>
62 template<>
struct typed_flags<
UpdateFlags> : is_typed_flags<UpdateFlags, 0x3f> {};
71 enum class ColorComponent {
88 static void RGBtoHSV(
double dR,
double dG,
double dB,
double& dH,
double& dS,
double& dV )
97 static void HSVtoRGB(
double dH,
double dS,
double dV,
double& dR,
double& dG,
double& dB )
101 dR = result.getRed();
102 dG = result.getGreen();
103 dB = result.getBlue();
107 static void CMYKtoRGB(
double fCyan,
double fMagenta,
double fYellow,
double fKey,
double& dR,
double& dG,
double& dB )
109 fCyan = (fCyan * ( 1.0 - fKey )) + fKey;
110 fMagenta = (fMagenta * ( 1.0 - fKey )) + fKey;
111 fYellow = (fYellow * ( 1.0 - fKey )) + fKey;
113 dR = std::clamp( 1.0 - fCyan, 0.0, 1.0 );
114 dG = std::clamp( 1.0 - fMagenta, 0.0, 1.0 );
115 dB = std::clamp( 1.0 - fYellow, 0.0, 1.0 );
119 static void RGBtoCMYK(
double dR,
double dG,
double dB,
double& fCyan,
double& fMagenta,
double& fYellow,
double& fKey )
127 if( fCyan < fKey ) fKey = fCyan;
128 if( fMagenta < fKey ) fKey = fMagenta;
129 if( fYellow < fKey ) fKey = fYellow;
140 fCyan = ( fCyan - fKey ) / ( 1.0 - fKey );
141 fMagenta = ( fMagenta - fKey ) / ( 1.0 - fKey );
142 fYellow = ( fYellow - fKey ) / ( 1.0 - fKey );
155 ColorPreviewControl()
161 CustomWidgetController::SetDrawingArea(pDrawingArea);
166 void SetColor(
const Color& rCol)
168 if (rCol != m_aColor)
187 enum ColorMode { HUE, SATURATION, BRIGHTNESS, RED, GREEN, BLUE };
208 CustomWidgetController::SetDrawingArea(pDrawingArea);
213 virtual ~ColorFieldControl()
override
219 virtual void Resize()
override;
225 void ShowPosition(
const Point& rPos,
bool bUpdate );
226 void UpdatePosition();
229 void SetValues(
Color aColor, ColorMode
eMode,
double x,
double y);
230 double GetX()
const {
return mdX;}
231 double GetY()
const {
return mdY;}
253 void ColorFieldControl::UpdateBitmap()
255 const Size aSize(GetOutputSizePixel());
260 const sal_Int32 nWidth = aSize.
Width();
261 const sal_Int32 nHeight = aSize.
Height();
263 if (nWidth == 0 || nHeight == 0)
279 for( sal_Int32
x = 0;
x < nWidth;
x++ )
281 *pRGB++ =
static_cast<sal_uInt8>((
x * 256) / nWidth);
282 *pGrad++ =
static_cast<sal_uInt16
>((
x * 359) / nWidth);
283 *pPercent++ =
static_cast<sal_uInt16
>((
x * 100) / nWidth);
292 sal_Int32
y = nHeight;
295 *pRGB++ =
static_cast<sal_uInt8>((y * 256) / nHeight);
296 *pPercent++ =
static_cast<sal_uInt16
>((y * 100) / nHeight);
308 sal_uInt16 nHue, nSat, nBri;
313 sal_uInt16 y = nHeight,
x;
320 nBri = pPercent_Vert[y];
324 nSat = pPercent_Horiz[x];
332 nBri = pPercent_Vert[y];
336 nHue = pGrad_Horiz[x];
344 nSat = pPercent_Vert[y];
348 nHue = pGrad_Horiz[x];
356 aBitmapColor.SetGreen(pRGB_Vert[y]);
360 aBitmapColor.SetBlue(pRGB_Horiz[x]);
368 aBitmapColor.SetRed(pRGB_Vert[y]);
372 aBitmapColor.SetBlue(pRGB_Horiz[x]);
380 aBitmapColor.SetGreen(pRGB_Vert[y]);
384 aBitmapColor.SetRed(pRGB_Horiz[x]);
392 void ColorFieldControl::ShowPosition(
const Point& rPos,
bool bUpdate )
409 else if (nX >= aSize.
Width())
410 nX = aSize.
Width() - 1;
414 else if (nY >= aSize.
Height())
425 mdX = double(nX) / double(aSize.
Width() - 1.0);
426 mdY = double(aSize.
Height() - 1.0 - nY) /
double(aSize.
Height() - 1.0);
432 bool ColorFieldControl::MouseButtonDown(
const MouseEvent& rMEvt)
441 bool ColorFieldControl::MouseMove(
const MouseEvent& rMEvt)
451 bool ColorFieldControl::MouseButtonUp(
const MouseEvent&)
465 Size aSize(GetOutputSizePixel());
480 void ColorFieldControl::Resize()
482 CustomWidgetController::Resize();
487 void ColorFieldControl::Modify()
492 void ColorFieldControl::SetValues(
Color aColor, ColorMode eMode,
double x,
double y )
494 bool bUpdateBitmap = (
maColor!= aColor) || (
meMode != eMode);
495 if( !(bUpdateBitmap || (
mdX != x) || (
mdY != y)) )
510 void ColorFieldControl::UpdatePosition()
512 Size aSize(GetOutputSizePixel());
513 ShowPosition(
Point(static_cast<tools::Long>(
mdX * aSize.
Width()), static_cast<tools::Long>((1.0 -
mdY) * aSize.
Height())),
false);
521 ColorSliderControl();
522 virtual ~ColorSliderControl()
override;
530 virtual void Resize()
override;
536 void SetValue(
const Color& rColor, ColorMode eMode,
double dValue );
541 sal_Int16 GetLevel()
const {
return mnLevel; }
554 ColorSliderControl::ColorSliderControl()
563 CustomWidgetController::SetDrawingArea(pDrawingArea);
567 ColorSliderControl::~ColorSliderControl()
572 void ColorSliderControl::UpdateBitmap()
589 sal_uInt16 nHue, nSat, nBri;
601 nHue =
static_cast<sal_uInt16
>((359 * y) / nY);
607 nBri = std::max(sal_uInt16(32), nBri);
610 nSat =
static_cast<sal_uInt16
>((100 * y) / nY);
618 nBri =
static_cast<sal_uInt16
>((100 * y) / nY);
649 void ColorSliderControl::ChangePosition(
tools::Long nY)
651 const tools::Long nHeight = GetOutputSizePixel().Height() - 1;
655 else if (nY > nHeight)
659 mdValue = double(nHeight - nY) / double(nHeight);
662 bool ColorSliderControl::MouseButtonDown(
const MouseEvent& rMEvt)
670 bool ColorSliderControl::MouseMove(
const MouseEvent& rMEvt)
672 if (IsMouseCaptured())
680 bool ColorSliderControl::MouseButtonUp(
const MouseEvent&)
691 const Size aSize(GetOutputSizePixel());
694 int x = aSize.
Width();
702 void ColorSliderControl::Resize()
704 CustomWidgetController::Resize();
708 void ColorSliderControl::Modify()
713 void ColorSliderControl::SetValue(
const Color& rColor, ColorMode eMode,
double dValue)
715 bool bUpdateBitmap = (rColor !=
maColor) || (eMode !=
meMode);
716 if( bUpdateBitmap || (
mdValue != dValue))
720 mnLevel =
static_cast<sal_Int16
>((1.0-dValue) * GetOutputSizePixel().Height());
769 void update_color(
UpdateFlags n = UpdateFlags::All);
771 DECL_LINK(ColorFieldControlModifydl, ColorFieldControl&,
void);
772 DECL_LINK(ColorSliderControlModifyHdl, ColorSliderControl&,
void);
778 Color GetColor()
const;
780 void setColorComponent(ColorComponent nComp,
double dValue);
792 ColorPickerDialog::ColorPickerDialog(
weld::Window* pParent,
Color nColor, sal_Int16 nDialogMode)
793 : GenericDialogController(pParent,
"cui/ui/colorpickerdialog.ui",
"ColorPicker")
800 ,
m_xRBRed(m_xBuilder->weld_radio_button(
"redRadiobutton"))
801 ,
m_xRBGreen(m_xBuilder->weld_radio_button(
"greenRadiobutton"))
802 ,
m_xRBBlue(m_xBuilder->weld_radio_button(
"blueRadiobutton"))
803 ,
m_xRBHue(m_xBuilder->weld_radio_button(
"hueRadiobutton"))
806 ,
m_xMFRed(m_xBuilder->weld_spin_button(
"redSpinbutton"))
807 ,
m_xMFGreen(m_xBuilder->weld_spin_button(
"greenSpinbutton"))
808 ,
m_xMFBlue(m_xBuilder->weld_spin_button(
"blueSpinbutton"))
809 ,
m_xEDHex(new
weld::HexColorControl(m_xBuilder->weld_entry(
"hexEntry")))
819 m_aColorField.SetModifyHdl(
LINK(
this, ColorPickerDialog, ColorFieldControlModifydl ) );
820 m_aColorSlider.SetModifyHdl(
LINK(
this, ColorPickerDialog, ColorSliderControlModifyHdl ) );
822 int nMargin = (
m_xFISliderLeft->get_preferred_size().Height() + 1) / 2;
827 m_xMFCyan->connect_value_changed( aLink3 );
830 m_xMFKey->connect_value_changed( aLink3 );
832 m_xMFHue->connect_value_changed( aLink3 );
837 m_xMFRed->connect_value_changed(aLink4);
839 m_xMFBlue->connect_value_changed(aLink4);
841 m_xEDHex->connect_changed(
LINK(
this, ColorPickerDialog, ColorModifyEditHdl));
844 m_xRBRed->connect_toggled( aLink2 );
847 m_xRBHue->connect_toggled( aLink2 );
851 Color aColor(nColor);
854 if (nDialogMode == 2)
860 mdRed =
static_cast<double>(aColor.
GetRed()) / 255.0;
870 static int toInt(
double dValue,
double dRange )
872 return static_cast< int >( std::floor((dValue * dRange) + 0.5 ) );
875 Color ColorPickerDialog::GetColor()
const
880 void ColorPickerDialog::update_color(
UpdateFlags n )
886 Color aColor(nRed, nGreen, nBlue);
888 if (n & UpdateFlags::RGB)
895 if (n & UpdateFlags::CMYK)
903 if (n & UpdateFlags::HSB )
910 if (n & UpdateFlags::ColorChooser )
935 if (n & UpdateFlags::ColorSlider)
960 if (n & UpdateFlags::Hex)
969 IMPL_LINK_NOARG(ColorPickerDialog, ColorFieldControlModifydl, ColorFieldControl&,
void)
978 setColorComponent( ColorComponent::Brightness, y );
982 setColorComponent( ColorComponent::Brightness, y );
986 setColorComponent( ColorComponent::Saturation, y );
990 setColorComponent( ColorComponent::Green, y );
994 setColorComponent( ColorComponent::Red, y );
998 setColorComponent( ColorComponent::Green, y );
1002 update_color(UpdateFlags::All & ~UpdateFlags::ColorChooser);
1005 IMPL_LINK_NOARG(ColorPickerDialog, ColorSliderControlModifyHdl, ColorSliderControl&,
void)
1011 setColorComponent( ColorComponent::Hue, dValue * 360.0 );
1014 setColorComponent( ColorComponent::Saturation, dValue );
1017 setColorComponent( ColorComponent::Brightness, dValue );
1020 setColorComponent( ColorComponent::Red, dValue );
1023 setColorComponent( ColorComponent::Green, dValue );
1026 setColorComponent( ColorComponent::Blue, dValue );
1030 update_color(UpdateFlags::All & ~UpdateFlags::ColorSlider);
1039 setColorComponent( ColorComponent::Hue, static_cast<double>(
m_xMFHue->get_value(FieldUnit::DEGREE)) );
1044 setColorComponent( ColorComponent::Saturation, static_cast<double>(
m_xMFSaturation->get_value(FieldUnit::PERCENT)) / 100.0 );
1049 setColorComponent( ColorComponent::Brightness, static_cast<double>(
m_xMFBrightness->get_value(FieldUnit::PERCENT)) / 100.0 );
1054 setColorComponent( ColorComponent::Cyan, static_cast<double>(
m_xMFCyan->get_value(FieldUnit::PERCENT)) / 100.0 );
1059 setColorComponent( ColorComponent::Magenta, static_cast<double>(
m_xMFMagenta->get_value(FieldUnit::PERCENT)) / 100.0 );
1064 setColorComponent( ColorComponent::Yellow, static_cast<double>(
m_xMFYellow->get_value(FieldUnit::PERCENT)) / 100.0 );
1069 setColorComponent( ColorComponent::Key, static_cast<double>(
m_xMFKey->get_value(FieldUnit::PERCENT)) / 100.0 );
1073 if (n != UpdateFlags::NONE)
1083 if (aColor !=
COL_AUTO && aColor != GetColor())
1085 mdRed =
static_cast<double>(aColor.
GetRed()) / 255.0;
1094 if (n != UpdateFlags::NONE)
1104 setColorComponent( ColorComponent::Red, static_cast<double>(
m_xMFRed->get_value()) / 255.0 );
1109 setColorComponent( ColorComponent::Green, static_cast<double>(
m_xMFGreen->get_value()) / 255.0 );
1114 setColorComponent( ColorComponent::Blue, static_cast<double>(
m_xMFBlue->get_value()) / 255.0 );
1118 if (n != UpdateFlags::NONE)
1125 ColorMode eMode = HUE;
1151 update_color(UpdateFlags::ColorChooser | UpdateFlags::ColorSlider);
1155 void ColorPickerDialog::setColorComponent( ColorComponent nComp,
double dValue )
1159 case ColorComponent::Red:
1162 case ColorComponent::Green:
1165 case ColorComponent::Blue:
1168 case ColorComponent::Hue:
1171 case ColorComponent::Saturation:
1174 case ColorComponent::Brightness:
1177 case ColorComponent::Cyan:
1180 case ColorComponent::Yellow:
1183 case ColorComponent::Magenta:
1186 case ColorComponent::Key:
1191 if (nComp == ColorComponent::Red || nComp == ColorComponent::Green || nComp == ColorComponent::Blue)
1196 else if (nComp == ColorComponent::Hue || nComp == ColorComponent::Saturation || nComp == ColorComponent::Brightness)
1208 typedef ::cppu::WeakComponentImplHelper< XServiceInfo, XExecutableDialog, XInitialization, XPropertyAccess >
ColorPickerBase;
1213 public ColorPickerBase
1216 explicit ColorPicker();
1219 virtual void SAL_CALL initialize(
const Sequence< Any >&
aArguments )
override;
1227 virtual Sequence< PropertyValue > SAL_CALL getPropertyValues( )
override;
1228 virtual void SAL_CALL setPropertyValues(
const Sequence< PropertyValue >& aProps )
override;
1231 virtual void SAL_CALL setTitle(
const OUString& aTitle )
override;
1232 virtual sal_Int16 SAL_CALL execute( )
override;
1242 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1244 css::uno::XComponentContext*, css::uno::Sequence<css::uno::Any>
const&)
1246 return cppu::acquire(
new ColorPicker );
1250 constexpr OUStringLiteral
gsColorKey( u
"Color" );
1251 constexpr OUStringLiteral
gsModeKey( u
"Mode" );
1253 ColorPicker::ColorPicker()
1261 void SAL_CALL ColorPicker::initialize(
const Sequence< Any >& aArguments )
1263 if( aArguments.getLength() == 1 )
1270 OUString SAL_CALL ColorPicker::getImplementationName( )
1272 return "com.sun.star.cui.ColorPicker";
1275 sal_Bool SAL_CALL ColorPicker::supportsService(
const OUString& sServiceName )
1282 return {
"com.sun.star.ui.dialogs.ColorPicker" };
1286 Sequence< PropertyValue > SAL_CALL ColorPicker::getPropertyValues( )
1288 Sequence< PropertyValue >
props(1);
1294 void SAL_CALL ColorPicker::setPropertyValues(
const Sequence< PropertyValue >& aProps )
1296 for (
const PropertyValue& rProp : aProps )
1310 void SAL_CALL ColorPicker::setTitle(
const OUString& )
1314 sal_Int16 SAL_CALL ColorPicker::execute()
1317 sal_Int16 ret = xDlg->run();
std::unique_ptr< weld::MetricSpinButton > m_xMFCyan
std::vector< sal_uInt16 > maPercent_Vert
#define LINK(Instance, Class, Member)
ColorPreviewControl m_aColorPrevious
static int toInt(double dValue, double dRange)
ColorPreviewControl m_aColorPreview
std::unique_ptr< weld::HexColorControl > m_xEDHex
std::unique_ptr< weld::RadioButton > m_xRBSaturation
std::unique_ptr< weld::SpinButton > m_xMFGreen
std::unique_ptr< weld::CustomWeld > m_xColorField
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_cui_ColorPicker_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
#define DECL_LINK(Member, ArgType, RetType)
ColorSliderControl m_aColorSlider
std::unique_ptr< weld::RadioButton > m_xRBBlue
std::unique_ptr< weld::RadioButton > m_xRBRed
constexpr::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
std::unique_ptr< weld::CustomWeld > m_xColorPreview
constexpr OUStringLiteral PERCENT(u"Percent")
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
std::unique_ptr< weld::MetricSpinButton > m_xMFSaturation
const ColorMode DefaultMode
IMPL_LINK(ColorPickerDialog, ColorModifySpinHdl, weld::SpinButton &, rEdit, void)
std::unique_ptr< weld::MetricSpinButton > m_xMFHue
std::vector< sal_uInt8 > maRGB_Horiz
std::unique_ptr< weld::CustomWeld > m_xColorPrevious
constexpr OUStringLiteral gsColorKey(u"Color")
Sequence< PropertyValue > aArguments
static void HSVtoRGB(double dH, double dS, double dV, double &dR, double &dG, double &dB)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference< css::awt::XWindow > mxParent
static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness)
sal_uInt8 GetBlue() const
void DrawRect(const tools::Rectangle &rRect)
std::unique_ptr< weld::SpinButton > m_xMFRed
static void RGBtoCMYK(double dR, double dG, double dB, double &fCyan, double &fMagenta, double &fYellow, double &fKey)
::cppu::WeakComponentImplHelper< XServiceInfo, XExecutableDialog, XInitialization, XPropertyAccess > ColorPickerBase
std::unique_ptr< weld::RadioButton > m_xRBGreen
Link< ColorFieldControl &, void > maModifyHdl
bool SetOutputSizePixel(const Size &rNewSize, bool bErase=true)
BColor hsv2rgb(const BColor &rHSVColor)
std::unique_ptr< weld::CustomWeld > m_xColorSlider
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
tools::Long Width() const
static void RGBtoHSV(double dR, double dG, double dB, double &dH, double &dS, double &dV)
std::vector< sal_uInt16 > maPercent_Horiz
VclPtr< VirtualDevice > mxBitmap
Size GetOutputSizePixel() const
std::unique_ptr< weld::MetricSpinButton > m_xMFKey
std::unique_ptr< weld::MetricSpinButton > m_xMFYellow
void DrawPixel(const Point &rPt)
sal_uInt8 GetGreen() const
IMPL_LINK_NOARG(ColorPickerDialog, ModeModifyHdl, weld::ToggleButton &, void)
constexpr OUStringLiteral gsModeKey(u"Mode")
static void CMYKtoRGB(double fCyan, double fMagenta, double fYellow, double fKey, double &dR, double &dG, double &dB)
std::vector< sal_uInt8 > maRGB_Vert
std::unique_ptr< weld::MetricSpinButton > m_xMFBrightness
std::unique_ptr< weld::RadioButton > m_xRBBrightness
static VclPtr< reference_type > Create(Arg &&...arg)
tools::Long Height() const
const Point & GetPosPixel() const
BColor rgb2hsv(const BColor &rRGBColor)
std::vector< sal_uInt16 > maGrad_Horiz
void DrawEllipse(const tools::Rectangle &rRect)
std::unique_ptr< weld::Widget > m_xFISliderRight
void RGBtoHSB(sal_uInt16 &nHue, sal_uInt16 &nSaturation, sal_uInt16 &nBrightness) const
std::unique_ptr< weld::MetricSpinButton > m_xMFMagenta
std::unique_ptr< weld::SpinButton > m_xMFBlue
Color GetPixel(const Point &rPt) const
std::unique_ptr< weld::RadioButton > m_xRBHue
std::unique_ptr< weld::Widget > m_xFISliderLeft
ColorFieldControl m_aColorField
SAL_DLLPRIVATE void DrawOutDev(const Point &, const Size &, const Point &, const Size &, const Printer &)=delete