Inkscape
Vector Graphics Editor
color-preview.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Author:
4 * Lauris Kaplinski <lauris@kaplinski.com>
5 * Ralf Stephan <ralf@ark.in-berlin.de>
6 *
7 * Copyright (C) 2001-2005 Authors
8 * Copyright (C) 2001 Ximian, Inc.
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
14
15#include <cairomm/context.h>
16#include <sigc++/functors/mem_fun.h>
17#include <gtkmm/drawingarea.h>
18
19#include "display/cairo-utils.h"
20
21namespace Inkscape::UI::Widget {
22
23ColorPreview::ColorPreview(std::uint32_t const rgba)
24 : _rgba{rgba}
25{
26 set_name("ColorPreview");
27 set_draw_func(sigc::mem_fun(*this, &ColorPreview::draw_func));
28}
29
30void
31ColorPreview::setRgba32(std::uint32_t const rgba)
32{
33 if (_rgba == rgba) return;
34
35 _rgba = rgba;
36 queue_draw();
37}
38
39void ColorPreview::draw_func(Cairo::RefPtr<Cairo::Context> const &cr,
40 int const widget_width, int const widget_height)
41{
42 auto const width = widget_width / 2.0, height = widget_height - 1.0;
43 auto x = 0.0;
44 auto y = 0.0;
45 double radius = height / 7.5;
46 double degrees = M_PI / 180.0;
47
48 cairo_new_sub_path (cr->cobj());
49 cairo_line_to(cr->cobj(), width, 0);
50 cairo_line_to(cr->cobj(), width, height);
51 cairo_arc (cr->cobj(), x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
52 cairo_arc (cr->cobj(), x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
53 cairo_close_path (cr->cobj());
54
55 /* Transparent area */
57 cairo_set_source(cr->cobj(), checkers);
58 cr->fill_preserve();
60 cr->fill();
61 cairo_pattern_destroy(checkers);
62
63 /* Solid area */
64 x = width;
65 cairo_new_sub_path (cr->cobj());
66 cairo_arc (cr->cobj(), x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
67 cairo_arc (cr->cobj(), x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
68 cairo_line_to(cr->cobj(), x, height);
69 cairo_line_to(cr->cobj(), x, y);
70 cairo_close_path (cr->cobj());
71 ink_cairo_set_source_rgba32(cr->cobj(), _rgba | 0xff);
72 cr->fill();
73}
74
75} // namespace Inkscape::UI::Widget
76
77/*
78 Local Variables:
79 mode:c++
80 c-file-style:"stroustrup"
81 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
82 indent-tabs-mode:nil
83 fill-column:99
84 End:
85*/
86// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
cairo_pattern_t * ink_cairo_pattern_create_checkerboard(guint32 rgba, bool use_alpha)
void ink_cairo_set_source_rgba32(cairo_t *ct, guint32 rgba)
Cairo integration helpers.
void draw_func(Cairo::RefPtr< Cairo::Context > const &cr, int width, int height)
void setRgba32(std::uint32_t rgba)
_cairo_pattern cairo_pattern_t
Custom widgets.
Definition: desktop.h:127
static constexpr int height
void cairo_line_to(cairo_t *cr, Geom::Point p1)
double width