From 39e0785f5f21aee9e17c9fce3fe5d5d5e37e1f7d Mon Sep 17 00:00:00 2001 From: Ay1nDas Date: Wed, 5 Nov 2025 01:28:09 +0530 Subject: [PATCH] Fix accecpt hex value in Fill & Stroke RGBA field without # Add a condition to add #. But, first check if it is a valid hex with 'looksLikeHex()'. Fixes https://gitlab.com/inkscape/inkscape/-/issues/5500 --- src/ui/widget/color-entry.cpp | 29 ++++++++++++++++++++++++++++- src/ui/widget/color-entry.h | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ui/widget/color-entry.cpp b/src/ui/widget/color-entry.cpp index 3b519cc9b3..e214c865ec 100644 --- a/src/ui/widget/color-entry.cpp +++ b/src/ui/widget/color-entry.cpp @@ -57,7 +57,14 @@ void ColorEntry::on_changed() return; } - auto new_color = Colors::Color::parse(get_text()); + auto text = get_text(); + // If it looks like a plain hex string (e.g., "ff00ff") add a '#' + // so both "ff00ff" and "#ff00ff" are accepted. + if (looksLikeHex(text)) { + text = "#" + text; + } + + auto new_color = Colors::Color::parse(text); _updatingrgba = true; if (new_color) { @@ -109,6 +116,26 @@ void ColorEntry::_onColorChanged() } } +// Checks if a text looks like a hex color code +// for example, returns true for "fff" or "ff00ff", +// but false for actual hex codes (like "#fff" or "#ff00ff") +// or any invalid hex strings. +bool ColorEntry::looksLikeHex(const Glib::ustring &text) const +{ + if (text.empty() || text[0] == '#') { + return false; + } + + auto len = text.size(); + if (len != 3 && len != 4 && len != 6 && len != 8) { + return false; + } + + return std::all_of(text.begin(), text.end(), [](unsigned char c) { + return std::isxdigit(c); + }); +} + } // namespace /* diff --git a/src/ui/widget/color-entry.h b/src/ui/widget/color-entry.h index 9dbb82e9dd..72afbb2fef 100644 --- a/src/ui/widget/color-entry.h +++ b/src/ui/widget/color-entry.h @@ -32,6 +32,7 @@ protected: private: void _onColorChanged(); void _inputCheck(guint pos, const gchar * /*chars*/, guint /*n_chars*/); + bool looksLikeHex(const Glib::ustring &text) const; std::shared_ptr _colors; bool _updating; -- GitLab