summaryrefslogtreecommitdiffstats
path: root/src/color
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:50:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:50:49 +0000
commitc853ffb5b2f75f5a889ed2e3ef89b818a736e87a (patch)
tree7d13a0883bb7936b84d6ecdd7bc332b41ed04bee /src/color
parentInitial commit. (diff)
downloadinkscape-c853ffb5b2f75f5a889ed2e3ef89b818a736e87a.tar.xz
inkscape-c853ffb5b2f75f5a889ed2e3ef89b818a736e87a.zip
Adding upstream version 1.3+ds.upstream/1.3+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/color')
-rw-r--r--src/color/CMakeLists.txt11
-rw-r--r--src/color/color-conv.cpp30
-rw-r--r--src/color/color-conv.h24
3 files changed, 65 insertions, 0 deletions
diff --git a/src/color/CMakeLists.txt b/src/color/CMakeLists.txt
new file mode 100644
index 0000000..36b14ad
--- /dev/null
+++ b/src/color/CMakeLists.txt
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+set(color_SRC
+ color-conv.cpp
+
+ # -------
+ # Headers
+ color-conv.h
+)
+
+add_inkscape_source("${color_SRC}")
diff --git a/src/color/color-conv.cpp b/src/color/color-conv.cpp
new file mode 100644
index 0000000..8ce3bcb
--- /dev/null
+++ b/src/color/color-conv.cpp
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <iomanip>
+#include <sstream>
+
+#include "color-conv.h"
+
+namespace Inkscape {
+namespace Util {
+
+std::string rgba_color_to_string(unsigned int rgba) {
+ std::ostringstream ost;
+ ost << "#" << std::setfill ('0') << std::setw(8) << std::hex << rgba;
+ return ost.str();
+}
+
+std::optional<unsigned int> string_to_rgba_color(const char* str) {
+ if (!str || *str != '#') {
+ return std::optional<unsigned int>();
+ }
+ try {
+ return static_cast<unsigned int>(std::stoul(str + 1, nullptr, 16));
+ }
+ catch (...) {
+ return std::optional<unsigned int>();
+ }
+}
+
+}
+} \ No newline at end of file
diff --git a/src/color/color-conv.h b/src/color/color-conv.h
new file mode 100644
index 0000000..0b87704
--- /dev/null
+++ b/src/color/color-conv.h
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2022 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+#ifndef INKSCAPE_UTIL_COLOR_CONV_H
+#define INKSCAPE_UTIL_COLOR_CONV_H
+
+#include <optional>
+#include <string>
+
+namespace Inkscape {
+namespace Util {
+
+// Convert RGBA color to '#rrggbbaa' hex string
+std::string rgba_color_to_string(unsigned int rgba);
+
+// Parse hex string '#rrgbbaa' and return RGBA color
+std::optional<unsigned int> string_to_rgba_color(const char* str);
+
+} } // namespace
+
+#endif