Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
dragndrop.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Author:
4 * Jon A. Cruz <jon@joncruz.org>
5 * Martin Owens <doctormo@geek-2.com>
6 *
7 * Copyright (C) 2009-2023 author
8 *
9 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10 */
11
12#include "dragndrop.h"
13
14#include <cstring>
15#include <iostream>
16
17#include "colors/color.h"
18
19namespace Inkscape::Colors {
20
24std::vector<char> getMIMEData(Paint const &paint, char const *mime_type)
25{
26 // XML Handles all types of paint
27 if (std::strcmp(mime_type, mimeOSWB_COLOR) == 0) {
28 auto const xml = paint_to_xml_string(paint);
29 return {xml.begin(), xml.end()};
30 }
31
32 // Handle NoColor first
33 if (std::holds_alternative<NoColor>(paint)) {
34 if (std::strcmp(mime_type, mimeTEXT) == 0)
35 return {'n', 'o', 'n', 'e'};
36 if (std::strcmp(mime_type, mimeX_COLOR) == 0)
37 return std::vector<char>(8); // transparent black
38 return {};
39 }
40
41 auto &color = std::get<Color>(paint);
42 if (std::strcmp(mime_type, mimeTEXT) == 0) {
43 auto const str = color.toString();
44 return {str.begin(), str.end()};
45 } else if (std::strcmp(mime_type, mimeX_COLOR) == 0) {
46 // X-color is only ever in RGBA
47 auto const rgb = color.toRGBA();
48 return {
49 (char)SP_RGBA32_R_U(rgb), (char)SP_RGBA32_R_U(rgb), (char)SP_RGBA32_G_U(rgb), (char)SP_RGBA32_G_U(rgb),
50 (char)SP_RGBA32_B_U(rgb), (char)SP_RGBA32_B_U(rgb), (char)SP_RGBA32_A_U(rgb), (char)SP_RGBA32_A_U(rgb),
51 };
52 }
53 return {};
54}
55
59Paint fromMIMEData(std::span<char const> data, char const *mime_type)
60{
61 if (std::strcmp(mime_type, mimeX_COLOR) == 0) {
62 if (data.size() != 8)
63 throw ColorError("Data is the wrong size for color mime type");
64 return Color{SP_RGBA32_U_COMPOSE(data[0], data[2], data[4], data[6])};
65 }
66
67 auto const str = std::string{data.data(), data.size()};
68 if (std::strcmp(mime_type, mimeTEXT) == 0) {
69 if (str == "none") {
70 return NoColor();
71 }
72 if (auto color = Color::parse(str)) {
73 return *color;
74 }
75 }
76 if (std::strcmp(mime_type, mimeOSWB_COLOR) == 0) {
77 return xml_string_to_paint(str, nullptr);
78 }
79 throw ColorError("Unknown color data found");
80}
81
82} // namespace Inkscape::Colors
83
84/*
85 Local Variables:
86 mode:c++
87 c-file-style:"stroustrup"
88 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
89 indent-tabs-mode:nil
90 fill-column:99
91 End:
92*/
93// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
size_t size() const
Definition color.h:42
constexpr uint32_t SP_RGBA32_R_U(uint32_t v)
Definition utils.h:27
constexpr uint32_t SP_RGBA32_G_U(uint32_t v)
Definition utils.h:31
constexpr uint32_t SP_RGBA32_U_COMPOSE(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
Definition utils.h:60
constexpr uint32_t SP_RGBA32_A_U(uint32_t v)
Definition utils.h:39
constexpr uint32_t SP_RGBA32_B_U(uint32_t v)
Definition utils.h:35
A set of useful color modifying functions which do not fit as generic methods on the color class itse...
Definition profile.cpp:24
constexpr auto mimeX_COLOR
Definition dragndrop.h:23
std::vector< char > getMIMEData(Paint const &paint, char const *mime_type)
Convert a paint into a draggable object.
Definition dragndrop.cpp:24
std::variant< NoColor, Color > Paint
Definition xml-color.h:32
constexpr auto mimeOSWB_COLOR
Definition dragndrop.h:22
Paint xml_string_to_paint(std::string const &xmls, SPDocument *doc)
Parse an xml document into a color.
Definition xml-color.cpp:48
constexpr auto mimeTEXT
Definition dragndrop.h:24
Paint fromMIMEData(std::span< char const > data, char const *mime_type)
Convert a dropped object into a color, if possible.
Definition dragndrop.cpp:59
std::string paint_to_xml_string(Paint const &paint)
Turn a color into a color xml document, used for drag and drop.
Definition xml-color.cpp:34
RGB rgb
Definition quantize.cpp:36
static const Point data[]