From 468d9965b9849447a8e7eaee7a90635faabe3f4d Mon Sep 17 00:00:00 2001 From: David Capello Date: Sat, 19 May 2012 18:18:56 -0300 Subject: [PATCH] Improve HexColorEntry so we can paste hex colors with # symbol (like CSS colors #ff0000). --- src/widgets/hex_color_entry.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/widgets/hex_color_entry.cpp b/src/widgets/hex_color_entry.cpp index 4badff529..46b143a41 100644 --- a/src/widgets/hex_color_entry.cpp +++ b/src/widgets/hex_color_entry.cpp @@ -25,10 +25,17 @@ #include "gui/theme.h" #include "widgets/hex_color_entry.h" +static inline bool is_hex_digit(char digit) +{ + return ((digit >= '0' && digit <= '9') || + (digit >= 'a' && digit <= 'f') || + (digit >= 'A' && digit <= 'F')); +} + HexColorEntry::HexColorEntry() : Box(JI_HORIZONTAL) , m_label("#") - , m_entry(6, "") + , m_entry(16, "") { addChild(&m_label); addChild(&m_entry); @@ -54,6 +61,10 @@ void HexColorEntry::onEntryChange() std::string text = m_entry.getText(); int r, g, b; + // Remove non hex digits + while (text.size() > 0 && !is_hex_digit(text[0])) + text.erase(0, 1); + // Fill with zeros at the end of the text while (text.size() < 6) text.push_back('0');