20#include <com/sun/star/uno/XComponentContext.hpp>
21#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
22#include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
23#include <com/sun/star/beans/XPropertyAccess.hpp>
24#include <com/sun/star/lang/XInitialization.hpp>
25#include <com/sun/star/lang/XServiceInfo.hpp>
26#include <com/sun/star/awt/XWindow.hpp>
65 template<>
struct typed_flags<
UpdateFlags> : is_typed_flags<UpdateFlags, 0x3f> {};
74enum class ColorComponent {
91static void RGBtoHSV(
double dR,
double dG,
double dB,
double& dH,
double& dS,
double& dV )
100static void HSVtoRGB(
double dH,
double dS,
double dV,
double& dR,
double& dG,
double& dB )
110static void CMYKtoRGB(
double fCyan,
double fMagenta,
double fYellow,
double fKey,
double& dR,
double& dG,
double& dB )
112 fCyan = (fCyan * ( 1.0 - fKey )) + fKey;
113 fMagenta = (fMagenta * ( 1.0 - fKey )) + fKey;
114 fYellow = (fYellow * ( 1.0 - fKey )) + fKey;
116 dR = std::clamp( 1.0 - fCyan, 0.0, 1.0 );
117 dG = std::clamp( 1.0 - fMagenta, 0.0, 1.0 );
118 dB = std::clamp( 1.0 - fYellow, 0.0, 1.0 );
122static void RGBtoCMYK(
double dR,
double dG,
double dB,
double& fCyan,
double& fMagenta,
double& fYellow,
double& fKey )
130 if( fCyan < fKey ) fKey = fCyan;
131 if( fMagenta < fKey ) fKey = fMagenta;
132 if( fYellow < fKey ) fKey = fYellow;
143 fCyan = ( fCyan - fKey ) / ( 1.0 - fKey );
144 fMagenta = ( fMagenta - fKey ) / ( 1.0 - fKey );
145 fYellow = ( fYellow - fKey ) / ( 1.0 - fKey );
158 ColorPreviewControl()
164 CustomWidgetController::SetDrawingArea(pDrawingArea);
169 void SetColor(
const Color& rCol)
190enum ColorMode { HUE, SATURATION, BRIGHTNESS,
RED,
GREEN,
BLUE };
212 CustomWidgetController::SetDrawingArea(pDrawingArea);
217 virtual ~ColorFieldControl()
override
223 virtual void Resize()
override;
229 void ShowPosition(
const Point& rPos,
bool bUpdate );
230 void UpdatePosition();
233 void SetValues(sal_uInt16 nBaseValue, ColorMode
eMode,
double x,
double y);
234 double GetX()
const {
return mdX;}
235 double GetY()
const {
return mdY;}
257void ColorFieldControl::UpdateBitmap()
259 const Size aSize(GetOutputSizePixel());
264 const sal_Int32 nWidth = aSize.
Width();
265 const sal_Int32 nHeight = aSize.
Height();
267 if (nWidth == 0 || nHeight == 0)
273 mxBitmap->SetOutputSizePixel(aSize);
283 for( sal_Int32
x = 0;
x < nWidth;
x++ )
285 *pRGB++ =
static_cast<sal_uInt8>((
x * 256) / nWidth);
286 *pGrad++ =
static_cast<sal_uInt16
>((
x * 359) / nWidth);
287 *pPercent++ =
static_cast<sal_uInt16
>((
x * 100) / nWidth);
296 sal_Int32
y = nHeight;
299 *pRGB++ =
static_cast<sal_uInt8>((
y * 256) / nHeight);
300 *pPercent++ =
static_cast<sal_uInt16
>((
y * 100) / nHeight);
312 sal_uInt16
y = nHeight,
x;
319 sal_uInt16 nBri = pPercent_Vert[
y];
323 sal_uInt16 nSat = pPercent_Horiz[
x];
331 sal_uInt16 nBri = pPercent_Vert[
y];
335 sal_uInt16 nHue = pGrad_Horiz[
x];
343 sal_uInt16 nSat = pPercent_Vert[
y];
347 sal_uInt16 nHue = pGrad_Horiz[
x];
358 aBitmapColor.
SetGreen(pRGB_Vert[y]);
362 aBitmapColor.
SetBlue(pRGB_Horiz[x]);
374 aBitmapColor.
SetRed(pRGB_Vert[y]);
378 aBitmapColor.
SetBlue(pRGB_Horiz[x]);
390 aBitmapColor.
SetGreen(pRGB_Vert[y]);
394 aBitmapColor.
SetRed(pRGB_Horiz[x]);
405void ColorFieldControl::ShowPosition(
const Point& rPos,
bool bUpdate )
422 else if (nX >= aSize.
Width())
423 nX = aSize.
Width() - 1;
427 else if (nY >= aSize.
Height())
438 mdX = double(nX) / double(aSize.
Width() - 1.0);
439 mdY = double(aSize.
Height() - 1.0 - nY) / double(aSize.
Height() - 1.0);
443bool ColorFieldControl::MouseButtonDown(
const MouseEvent& rMEvt)
452bool ColorFieldControl::MouseMove(
const MouseEvent& rMEvt)
462bool ColorFieldControl::MouseButtonUp(
const MouseEvent&)
477 Size aSize(GetOutputSizePixel());
492void ColorFieldControl::Resize()
494 CustomWidgetController::Resize();
499void ColorFieldControl::Modify()
504void ColorFieldControl::SetValues(sal_uInt16 nBaseValue, ColorMode eMode,
double x,
double y)
507 if (!bUpdateBitmap &&
mdX == x &&
mdY == y)
522void ColorFieldControl::UpdatePosition()
524 Size aSize(GetOutputSizePixel());
533 ColorSliderControl();
534 virtual ~ColorSliderControl()
override;
542 virtual void Resize()
override;
548 void SetValue(
const Color& rColor, ColorMode eMode,
double dValue );
553 sal_Int16 GetLevel()
const {
return mnLevel; }
566ColorSliderControl::ColorSliderControl()
575 CustomWidgetController::SetDrawingArea(pDrawingArea);
579ColorSliderControl::~ColorSliderControl()
584void ColorSliderControl::UpdateBitmap()
594 mxBitmap->SetOutputSizePixel(aSize);
601 sal_uInt16 nHue, nSat, nBri;
613 nHue =
static_cast<sal_uInt16
>((359 *
y) / nY);
619 nBri = std::max(sal_uInt16(32), nBri);
622 nSat =
static_cast<sal_uInt16
>((100 *
y) / nY);
630 nBri =
static_cast<sal_uInt16
>((100 *
y) / nY);
661void ColorSliderControl::ChangePosition(
tools::Long nY)
663 const tools::Long nHeight = GetOutputSizePixel().Height() - 1;
667 else if (nY > nHeight)
671 mdValue = double(nHeight - nY) / double(nHeight);
674bool ColorSliderControl::MouseButtonDown(
const MouseEvent& rMEvt)
682bool ColorSliderControl::MouseMove(
const MouseEvent& rMEvt)
684 if (IsMouseCaptured())
692bool ColorSliderControl::MouseButtonUp(
const MouseEvent&)
703 const Size aSize(GetOutputSizePixel());
714void ColorSliderControl::Resize()
716 CustomWidgetController::Resize();
720void ColorSliderControl::Modify()
727 bool bUpdateBitmap = (rColor !=
maColor) || (eMode !=
meMode);
728 if( bUpdateBitmap || (
mdValue != dValue))
732 mnLevel =
static_cast<sal_Int16
>((1.0-dValue) * GetOutputSizePixel().Height());
781 void update_color(
UpdateFlags n = UpdateFlags::All);
783 DECL_LINK(ColorFieldControlModifydl, ColorFieldControl&,
void);
784 DECL_LINK(ColorSliderControlModifyHdl, ColorSliderControl&,
void);
790 Color GetColor()
const;
792 void setColorComponent(ColorComponent nComp,
double dValue);
804ColorPickerDialog::ColorPickerDialog(
weld::Window* pParent,
Color nColor, sal_Int16 nDialogMode)
812 ,
m_xRBRed(m_xBuilder->weld_radio_button(
"redRadiobutton"))
813 ,
m_xRBGreen(m_xBuilder->weld_radio_button(
"greenRadiobutton"))
814 ,
m_xRBBlue(m_xBuilder->weld_radio_button(
"blueRadiobutton"))
815 ,
m_xRBHue(m_xBuilder->weld_radio_button(
"hueRadiobutton"))
818 ,
m_xMFRed(m_xBuilder->weld_spin_button(
"redSpinbutton"))
819 ,
m_xMFGreen(m_xBuilder->weld_spin_button(
"greenSpinbutton"))
820 ,
m_xMFBlue(m_xBuilder->weld_spin_button(
"blueSpinbutton"))
821 ,
m_xEDHex(new
weld::HexColorControl(m_xBuilder->weld_entry(
"hexEntry")))
831 m_aColorField.SetModifyHdl(
LINK(
this, ColorPickerDialog, ColorFieldControlModifydl ) );
832 m_aColorSlider.SetModifyHdl(
LINK(
this, ColorPickerDialog, ColorSliderControlModifyHdl ) );
834 int nMargin = (
m_xFISliderLeft->get_preferred_size().Height() + 1) / 2;
839 m_xMFCyan->connect_value_changed( aLink3 );
842 m_xMFKey->connect_value_changed( aLink3 );
844 m_xMFHue->connect_value_changed( aLink3 );
849 m_xMFRed->connect_value_changed(aLink4);
851 m_xMFBlue->connect_value_changed(aLink4);
853 m_xEDHex->connect_changed(
LINK(
this, ColorPickerDialog, ColorModifyEditHdl));
856 m_xRBRed->connect_toggled( aLink2 );
859 m_xRBHue->connect_toggled( aLink2 );
863 Color aColor(nColor);
866 if (nDialogMode == 2)
872 mdRed =
static_cast<double>(aColor.
GetRed()) / 255.0;
882static int toInt(
double dValue,
double dRange )
884 return static_cast< int >( std::floor((dValue * dRange) + 0.5 ) );
887Color ColorPickerDialog::GetColor()
const
892void ColorPickerDialog::update_color(
UpdateFlags n )
902 if (n & UpdateFlags::RGB)
909 if (n & UpdateFlags::CMYK)
917 if (n & UpdateFlags::HSB )
919 m_xMFHue->set_value(nHue, FieldUnit::DEGREE);
924 if (n & UpdateFlags::ColorChooser )
949 Color aColor(nRed, nGreen, nBlue);
951 if (n & UpdateFlags::ColorSlider)
976 if (n & UpdateFlags::Hex)
985IMPL_LINK_NOARG(ColorPickerDialog, ColorFieldControlModifydl, ColorFieldControl&,
void)
994 setColorComponent( ColorComponent::Brightness,
y );
998 setColorComponent( ColorComponent::Brightness,
y );
1002 setColorComponent( ColorComponent::Saturation,
y );
1006 setColorComponent( ColorComponent::Green,
y );
1010 setColorComponent( ColorComponent::Red,
y );
1014 setColorComponent( ColorComponent::Green,
y );
1018 update_color(UpdateFlags::All & ~UpdateFlags::ColorChooser);
1027 setColorComponent( ColorComponent::Hue, dValue * 360.0 );
1030 setColorComponent( ColorComponent::Saturation, dValue );
1033 setColorComponent( ColorComponent::Brightness, dValue );
1036 setColorComponent( ColorComponent::Red, dValue );
1039 setColorComponent( ColorComponent::Green, dValue );
1042 setColorComponent( ColorComponent::Blue, dValue );
1046 update_color(UpdateFlags::All & ~UpdateFlags::ColorSlider);
1055 setColorComponent( ColorComponent::Hue,
static_cast<double>(
m_xMFHue->get_value(FieldUnit::DEGREE)) );
1060 setColorComponent( ColorComponent::Saturation,
static_cast<double>(
m_xMFSaturation->get_value(FieldUnit::PERCENT)) / 100.0 );
1065 setColorComponent( ColorComponent::Brightness,
static_cast<double>(
m_xMFBrightness->get_value(FieldUnit::PERCENT)) / 100.0 );
1070 setColorComponent( ColorComponent::Cyan,
static_cast<double>(
m_xMFCyan->get_value(FieldUnit::PERCENT)) / 100.0 );
1075 setColorComponent( ColorComponent::Magenta,
static_cast<double>(
m_xMFMagenta->get_value(FieldUnit::PERCENT)) / 100.0 );
1080 setColorComponent( ColorComponent::Yellow,
static_cast<double>(
m_xMFYellow->get_value(FieldUnit::PERCENT)) / 100.0 );
1085 setColorComponent( ColorComponent::Key,
static_cast<double>(
m_xMFKey->get_value(FieldUnit::PERCENT)) / 100.0 );
1089 if (
n != UpdateFlags::NONE)
1099 if (aColor !=
COL_AUTO && aColor != GetColor())
1101 mdRed =
static_cast<double>(aColor.
GetRed()) / 255.0;
1110 if (
n != UpdateFlags::NONE)
1120 setColorComponent( ColorComponent::Red,
static_cast<double>(
m_xMFRed->get_value()) / 255.0 );
1125 setColorComponent( ColorComponent::Green,
static_cast<double>(
m_xMFGreen->get_value()) / 255.0 );
1130 setColorComponent( ColorComponent::Blue,
static_cast<double>(
m_xMFBlue->get_value()) / 255.0 );
1134 if (
n != UpdateFlags::NONE)
1141 ColorMode
eMode = HUE;
1167 update_color(UpdateFlags::ColorChooser | UpdateFlags::ColorSlider);
1171void ColorPickerDialog::setColorComponent( ColorComponent nComp,
double dValue )
1175 case ColorComponent::Red:
1178 case ColorComponent::Green:
1181 case ColorComponent::Blue:
1184 case ColorComponent::Hue:
1187 case ColorComponent::Saturation:
1190 case ColorComponent::Brightness:
1193 case ColorComponent::Cyan:
1196 case ColorComponent::Yellow:
1199 case ColorComponent::Magenta:
1202 case ColorComponent::Key:
1207 if (nComp == ColorComponent::Red || nComp == ColorComponent::Green || nComp == ColorComponent::Blue)
1212 else if (nComp == ColorComponent::Hue || nComp == ColorComponent::Saturation || nComp == ColorComponent::Brightness)
1224typedef ::comphelper::WeakComponentImplHelper< XServiceInfo, XExecutableDialog, XAsynchronousExecutableDialog, XInitialization, XPropertyAccess >
ColorPickerBase;
1231 explicit ColorPicker();
1234 virtual void SAL_CALL initialize(
const Sequence< Any >&
aArguments )
override;
1242 virtual Sequence< PropertyValue > SAL_CALL getPropertyValues( )
override;
1243 virtual void SAL_CALL setPropertyValues(
const Sequence< PropertyValue >& aProps )
override;
1246 virtual void SAL_CALL setTitle(
const OUString& aTitle )
override;
1247 virtual sal_Int16 SAL_CALL execute( )
override;
1250 virtual void SAL_CALL setDialogTitle(
const OUString& aTitle )
override;
1251 virtual void SAL_CALL startExecuteModal(
const css::uno::Reference< css::ui::dialogs::XDialogClosedListener >& xListener )
override;
1261extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1263 css::uno::XComponentContext*, css::uno::Sequence<css::uno::Any>
const&)
1265 return cppu::acquire(
new ColorPicker );
1272ColorPicker::ColorPicker()
1279void SAL_CALL ColorPicker::initialize(
const Sequence< Any >& aArguments )
1288OUString SAL_CALL ColorPicker::getImplementationName( )
1290 return "com.sun.star.cui.ColorPicker";
1293sal_Bool SAL_CALL ColorPicker::supportsService(
const OUString& sServiceName )
1300 return {
"com.sun.star.ui.dialogs.ColorPicker",
1301 "com.sun.star.ui.dialogs.AsynchronousColorPicker" };
1305Sequence< PropertyValue > SAL_CALL ColorPicker::getPropertyValues( )
1311void SAL_CALL ColorPicker::setPropertyValues(
const Sequence< PropertyValue >& aProps )
1313 for (
const PropertyValue& rProp : aProps )
1327void SAL_CALL ColorPicker::setTitle(
const OUString& )
1331sal_Int16 SAL_CALL ColorPicker::execute()
1334 sal_Int16 ret = xDlg->run();
1341void SAL_CALL ColorPicker::setDialogTitle(
const OUString& )
1345void SAL_CALL ColorPicker::startExecuteModal(
const css::uno::Reference< css::ui::dialogs::XDialogClosedListener >& xListener )
1352 sal_Int16 nRet =
static_cast<sal_Int16
>(nResult);
1353 css::ui::dialogs::DialogClosedEvent
aEvent( *
this, nRet );
1354 xListener->dialogClosed( aEvent );
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
sal_uInt8 GetBlue() const
void RGBtoHSB(sal_uInt16 &nHue, sal_uInt16 &nSaturation, sal_uInt16 &nBrightness) const
void SetGreen(sal_uInt8 nGreen)
void SetRed(sal_uInt8 nRed)
static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness)
sal_uInt8 GetGreen() const
void SetBlue(sal_uInt8 nBlue)
const Point & GetPosPixel() const
void DrawEllipse(const tools::Rectangle &rRect)
void DrawRect(const tools::Rectangle &rRect)
SAL_DLLPRIVATE void DrawOutDev(const Point &, const Size &, const Point &, const Size &, const Printer &)=delete
constexpr tools::Long Y() const
void setX(tools::Long nX)
void setY(tools::Long nY)
tools::Long AdjustX(tools::Long nHorzMove)
constexpr tools::Long X() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
static VclPtr< reference_type > Create(Arg &&... arg)
static bool runAsync(const std::shared_ptr< DialogController > &rController, const std::function< void(sal_Int32)> &)
constexpr ::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
std::unique_ptr< weld::CustomWeld > m_xColorSlider
std::vector< sal_uInt8 > maRGB_Horiz
std::unique_ptr< weld::Widget > m_xFISliderLeft
std::unique_ptr< weld::CustomWeld > m_xColorPrevious
std::unique_ptr< weld::RadioButton > m_xRBGreen
std::unique_ptr< weld::SpinButton > m_xMFGreen
std::unique_ptr< weld::SpinButton > m_xMFBlue
std::unique_ptr< weld::MetricSpinButton > m_xMFYellow
std::unique_ptr< weld::RadioButton > m_xRBBrightness
std::unique_ptr< weld::SpinButton > m_xMFRed
std::unique_ptr< weld::MetricSpinButton > m_xMFBrightness
std::unique_ptr< weld::MetricSpinButton > m_xMFCyan
std::vector< sal_uInt16 > maPercent_Horiz
ColorFieldControl m_aColorField
std::unique_ptr< weld::RadioButton > m_xRBRed
std::unique_ptr< weld::MetricSpinButton > m_xMFHue
std::unique_ptr< weld::HexColorControl > m_xEDHex
std::unique_ptr< weld::MetricSpinButton > m_xMFKey
std::unique_ptr< weld::CustomWeld > m_xColorField
std::vector< sal_uInt16 > maPercent_Vert
std::unique_ptr< weld::CustomWeld > m_xColorPreview
ColorPreviewControl m_aColorPreview
std::unique_ptr< weld::RadioButton > m_xRBSaturation
std::unique_ptr< weld::RadioButton > m_xRBBlue
ColorSliderControl m_aColorSlider
ColorPreviewControl m_aColorPrevious
Link< ColorFieldControl &, void > maModifyHdl
std::unique_ptr< weld::RadioButton > m_xRBHue
std::vector< sal_uInt8 > maRGB_Vert
std::unique_ptr< weld::Widget > m_xFISliderRight
Reference< css::awt::XWindow > mxParent
VclPtr< VirtualDevice > mxBitmap
std::unique_ptr< weld::MetricSpinButton > m_xMFMagenta
std::unique_ptr< weld::MetricSpinButton > m_xMFSaturation
std::vector< sal_uInt16 > maGrad_Horiz
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
virtual void SetValue(tools::Long nNew) override
virtual tools::Long GetValue() const override
Sequence< PropertyValue > aArguments
#define LINK(Instance, Class, Member)
BColor hsv2rgb(const BColor &rHSVColor)
BColor rgb2hsv(const BColor &rRGBColor)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_cui_ColorPicker_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
static void HSVtoRGB(double dH, double dS, double dV, double &dR, double &dG, double &dB)
constexpr int nCenterOffset
constexpr OUStringLiteral gsModeKey(u"Mode")
IMPL_LINK(ColorPickerDialog, ColorModifySpinHdl, weld::SpinButton &, rEdit, void)
static void CMYKtoRGB(double fCyan, double fMagenta, double fYellow, double fKey, double &dR, double &dG, double &dB)
static void RGBtoCMYK(double dR, double dG, double dB, double &fCyan, double &fMagenta, double &fYellow, double &fKey)
static int toInt(double dValue, double dRange)
static void RGBtoHSV(double dR, double dG, double dB, double &dH, double &dS, double &dV)
::comphelper::WeakComponentImplHelper< XServiceInfo, XExecutableDialog, XAsynchronousExecutableDialog, XInitialization, XPropertyAccess > ColorPickerBase
constexpr OUStringLiteral gsColorKey(u"Color")
const ColorMode DefaultMode
IMPL_LINK_NOARG(ColorPickerDialog, ModeModifyHdl, weld::Toggleable &, void)
constexpr OUStringLiteral PERCENT(u"Percent")