Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
color-picker.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Lauris Kaplinski <lauris@kaplinski.com>
5 * bulia byak <buliabyak@users.sf.net>
6 * Ralf Stephan <ralf@ark.in-berlin.de>
7 * Abhishek Sharma
8 *
9 * Copyright (C) Authors 2000-2005
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include "color-picker.h"
15#include <glibmm/ustring.h>
16#include <gtkmm/enums.h>
17#include <gtkmm/menubutton.h>
18#include <gtkmm/cssprovider.h>
19#include <gtkmm/widget.h>
20#include <utility>
21
22#include "desktop.h"
23#include "document-undo.h"
24#include "inkscape.h"
25#include "ui/util.h"
28
29static bool _in_use = false;
30
31namespace Inkscape::UI::Widget {
32
33ColorPicker::ColorPicker(Glib::ustring title,
34 Glib::ustring const &tip,
35 Colors::Color const &initial,
36 bool const undo,
37 bool use_transparency)
38 : _preview(Gtk::make_managed<ColorPreview>(initial.toRGBA()))
39 , _title(std::move(title))
40 , _undo(undo)
41 , _colors(std::make_shared<Colors::ColorSet>(nullptr, use_transparency))
42{
43 // set tooltip if given, otherwise leave original tooltip in place (from external button)
44 if (!tip.empty()) {
45 set_tooltip_text(tip);
46 }
47
48 _colors->set(initial);
49 _construct();
50}
51
52ColorPicker::ColorPicker(BaseObjectType *cobject, Glib::RefPtr<Gtk::Builder> const &,
53 Glib::ustring title, bool use_transparency)
54 : Gtk::MenuButton(cobject)
55 , _preview(Gtk::make_managed<ColorPreview>(0x0))
56 , _title(std::move(title))
57 , _colors(std::make_shared<Colors::ColorSet>(nullptr, use_transparency))
58{
59 _construct();
60}
61
63 // match min height with that of the current theme button and enforce square shape for our color picker
64 Gtk::Button button;
65 auto height = button.measure(Gtk::Orientation::VERTICAL).sizes.minimum;
66 set_name("ColorPicker");
68
70 set_child(*_preview);
71
72 // postpone color selector creation until popup is open
73 _popup.signal_show().connect([this](){
74 if (!_color_selector) {
75 _color_selector = Gtk::make_managed<ColorNotebook>(_colors);
77 _color_selector->set_margin(4);
78 _popup.set_child(*_color_selector);
79 }
80 });
81 set_popover(_popup);
82
83 _colors->signal_changed.connect(sigc::mem_fun(*this, &ColorPicker::_onSelectedColorChanged));
84 _colors->signal_released.connect(sigc::mem_fun(*this, &ColorPicker::_onSelectedColorChanged));
85}
86
88
89void ColorPicker::setTitle(Glib::ustring title) {
90 _title = std::move(title);
91}
92
94{
95 if (_in_use) return;
96
97 _updating = true;
98 set_preview(color.toRGBA());
99 _colors->set(color);
100 _updating = false;
101}
102
104 popup();
105}
106
108 popdown();
109}
110
112{
113 if (_updating || _in_use)
114 return;
115
116 auto color = _colors->get();
117 if (!color) return;
118
119 set_preview(color->toRGBA());
120
121 if (_undo && SP_ACTIVE_DESKTOP) {
122 DocumentUndo::done(SP_ACTIVE_DESKTOP->getDocument(), /* TODO: annotate */ "color-picker.cpp:122", "");
123 }
124
125 _in_use = true;
126 _changed_signal.emit(*color);
127 on_changed(*color);
128 _in_use = false;
129}
130
132
133void ColorPicker::set_preview(std::uint32_t rgba)
134{
135 bool has_alpha = _colors->getAlphaConstraint().value_or(true);
136 _preview->setRgba32(has_alpha ? rgba : rgba | 0xff);
137}
138
139} // namespace Inkscape::UI::Widget
140
141/*
142 Local Variables:
143 mode:c++
144 c-file-style:"stroustrup"
145 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
146 indent-tabs-mode:nil
147 fill-column:99
148 End:
149*/
150// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
void undo(SPDocument *document)
uint32_t toRGBA(double opacity=1.0) const
Return an sRGB conversion of the color in RGBA int32 format.
Definition color.cpp:117
static void done(SPDocument *document, Glib::ustring const &event_description, Glib::ustring const &undo_icon, unsigned int object_modified_tag=0)
void set_label(const Glib::ustring &label)
ColorPicker(Glib::ustring title, Glib::ustring const &tip, Colors::Color const &initial, bool undo, bool use_transparency=true)
void set_preview(std::uint32_t rgba)
void setTitle(Glib::ustring title)
virtual void on_changed(Colors::Color const &)
void setColor(Colors::Color const &)
sigc::signal< void(Colors::Color const &)> _changed_signal
std::shared_ptr< Colors::ColorSet > _colors
A color preview widget, used within a picker button and style indicator.
void setRgba32(std::uint32_t rgba)
A notebook with RGB, CMYK, CMS, HSL, and Wheel pages.
static bool _in_use
Color picker button and window.
Editable view implementation.
TODO: insert short description here.
Definition desktop.h:50
Custom widgets.
Definition desktop.h:126
static constexpr int height
STL namespace.
void restrict_minsize_to_square(Gtk::Widget &widget, int min_size_px)
Definition util.cpp:537