Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
color-palette-preview.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/*
6 * Authors:
7 * Michael Kowalski
8 * Daniel Boles <dboles.src+inkscape@gmail.com>
9 *
10 * Copyright (C) 2021 Michael Kowalski
11 * Copyright (C) 2023 Daniel Boles
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
16
17#include <utility>
18#include <cairomm/context.h>
19#include <sigc++/functors/mem_fun.h>
20
21namespace Inkscape::UI::Widget {
22
23static constexpr int height = 2;
24static constexpr int dx = 1; // width per color
25
27 : Glib::ObjectBase{"ColorPalettePreview"}
28 , Gtk::DrawingArea{}
29 , _colors{std::move(colors)}
30{
31 set_size_request(-1, height);
32 set_draw_func(sigc::mem_fun(*this, &ColorPalettePreview::draw_func));
33}
34
35void ColorPalettePreview::draw_func(Cairo::RefPtr<Cairo::Context> const &cr, int width, int height)
36{
37 if (_colors.empty()) return;
38 if (width <= 0) return;
39
40 for (int i = 0, px = 0; i < width && px < width; ++i, px += dx) {
41 int const index = i * _colors.size() / width;
42 auto &color = _colors.at(index);
43 cr->set_source_rgb(color.r, color.g, color.b);
44 cr->rectangle(px, 0, dx, height);
45 cr->fill();
46 }
47}
48
49} // namespace Inkscape::UI::Widget
50
51/*
52 Local Variables:
53 mode:c++
54 c-file-style:"stroustrup"
55 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
56 indent-tabs-mode:nil
57 fill-column:99
58 End:
59*/
60// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99:
ColorPalettePreview(std::vector< rgb_t > colors)
Construct with colors that we will preview.
void draw_func(Cairo::RefPtr< Cairo::Context > const &cr, int, int)
A Gtk::DrawingArea to preview color palette menu items by showing a small example of the colors.
Definition desktop.h:50
Custom widgets.
Definition desktop.h:126
static constexpr int height
STL namespace.
int index
double width