15
15
// You should have received a copy of the GNU General Public License
16
16
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
18
- #include < regex>
19
- #include < sstream>
20
-
21
18
#include " gui/menu_color.hpp"
22
19
#include " menu_item.hpp"
23
20
#include " ../util/log.hpp"
@@ -38,14 +35,11 @@ ColorMenu::ColorMenu(Color* color_) :
38
35
add_color_display (color);
39
36
40
37
add_hl ();
41
- add_entry (MNID_COPY , _ (" Copy" ));
38
+ add_entry (MNID_COPY_CLIPBOARD , _ (" Copy from clipboard " ));
42
39
if (Color::s_clipboard_color != nullptr )
43
- add_entry (MNID_PASTE , _ (" Paste" ), *Color::s_clipboard_color);
40
+ add_entry (MNID_PASTE_CLIPBOARD , _ (" Paste from clipboard " ), *Color::s_clipboard_color);
44
41
else
45
- add_entry (MNID_PASTE, _ (" Paste" ), Color (1 .f , 1 .f , 1 .f ));
46
-
47
- add_hl ();
48
- add_entry (MNID_PASTE_CLIPBOARD, _ (" Paste clipboard" ));
42
+ add_entry (MNID_PASTE_CLIPBOARD, _ (" Paste from clipboard" ), Color (1 .f , 1 .f , 1 .f ));
49
43
50
44
add_hl ();
51
45
add_back (_ (" OK" ));
@@ -54,84 +48,53 @@ ColorMenu::ColorMenu(Color* color_) :
54
48
void
55
49
ColorMenu::menu_action (MenuItem& item)
56
50
{
57
- if (item.get_id () == MNID_COPY )
51
+ if (item.get_id () == MNID_COPY_CLIPBOARD )
58
52
{
59
53
if (color)
60
54
{
61
55
Color::s_clipboard_color = std::make_unique<Color>(*color);
62
- MenuItem& menu_paste_item = get_item_by_id (MNID_PASTE );
56
+ MenuItem& menu_paste_item = get_item_by_id (MNID_PASTE_CLIPBOARD );
63
57
menu_paste_item.set_text_color (*color);
58
+
59
+ std::stringstream ss;
60
+ ss << " rgb("
61
+ << static_cast <int >(color->red * 255 .f ) << " ,"
62
+ << static_cast <int >(color->green * 255 .f ) << " ,"
63
+ << static_cast <int >(color->blue * 255 .f ) << " )" ;
64
+
65
+ const std::string clipboard_text = ss.str ();
66
+
67
+ if (SDL_SetClipboardText (clipboard_text.c_str ()) != 0 )
68
+ log_warning << " Failed to set SDL clipboard text: " << SDL_GetError () << std::endl;
64
69
}
65
70
}
66
- else if (item.get_id () == MNID_PASTE)
67
- {
68
- if (Color::s_clipboard_color)
69
- *color = *Color::s_clipboard_color;
70
- }
71
71
else if (item.get_id () == MNID_PASTE_CLIPBOARD)
72
72
{
73
- if (!SDL_HasClipboardText ())
74
- return ;
75
-
76
- const char * clipboard_text = SDL_GetClipboardText ();
77
- if (!clipboard_text)
78
- return ;
79
-
80
- const std::string text (clipboard_text);
81
- SDL_free (const_cast <char *>(clipboard_text));
82
-
83
- Color new_color;
84
- bool is_valid_format = false ;
85
-
86
- // rgb(r,g,b)
87
- const std::regex rgb_format (R"( ^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$)" );
88
- std::smatch rgb_matches;
89
-
90
- if (std::regex_match (text, rgb_matches, rgb_format))
73
+ if (SDL_HasClipboardText ())
91
74
{
92
- const int r = std::stoi (rgb_matches[ 1 ]. str () );
93
- const int g = std::stoi (rgb_matches[ 2 ]. str ());
94
- const int b = std::stoi (rgb_matches[ 3 ]. str ()) ;
75
+ const char * clipboard_text = SDL_GetClipboardText ( );
76
+ if (!clipboard_text)
77
+ return ;
95
78
96
- if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 )
97
- {
98
- new_color = Color (static_cast <float >(r) / 255 .0f , static_cast <float >(g) / 255 .0f , static_cast <float >(b) / 255 .0f , 1 .0f );
99
- is_valid_format = true ;
100
- }
101
- }
102
- else
103
- {
104
- // #rrggbb
105
- const std::regex hex_format (R"( ^\s*#([A-Fa-f0-9]{6})\s*$)" );
106
- std::smatch hex_matches;
79
+ const std::string text (clipboard_text);
80
+ SDL_free (const_cast <char *>(clipboard_text));
107
81
108
- if (std::regex_match (text, hex_matches, hex_format))
109
- {
110
- const std::string hex_value = hex_matches[1 ].str ();
111
- unsigned int hex_color;
112
- std::stringstream ss;
113
- ss << std::hex << hex_value;
114
- ss >> hex_color;
82
+ std::optional<Color> new_color;
83
+ new_color = Color::from_rgb_string (text);
115
84
116
- const float r = ((hex_color >> 16 ) & 0xFF ) / 255 .0f ;
117
- const float g = ((hex_color >> 8 ) & 0xFF ) / 255 .0f ;
118
- const float b = (hex_color & 0xFF ) / 255 .0f ;
85
+ if (!new_color)
86
+ new_color = Color::from_hex_string (text);
119
87
120
- new_color = Color (r, g, b, 1 .0f );
121
- is_valid_format = true ;
88
+ if (new_color)
89
+ {
90
+ *color = *new_color;
91
+ Color::s_clipboard_color = std::make_unique<Color>(*new_color);
92
+ MenuItem& menu_paste_item = get_item_by_id (MNID_PASTE_CLIPBOARD);
93
+ menu_paste_item.set_text_color (*new_color);
122
94
}
95
+ else
96
+ log_warning << " Invalid color format: " << text << " . Supported formats: rgb(r,g,b) and #rrggbb" << std::endl;
123
97
}
124
-
125
- if (is_valid_format)
126
- {
127
- *color = new_color;
128
-
129
- Color::s_clipboard_color = std::make_unique<Color>(new_color);
130
- MenuItem& menu_paste_item = get_item_by_id (MNID_PASTE);
131
- menu_paste_item.set_text_color (new_color);
132
- }
133
- else
134
- log_warning << " Invalid color format: " << text << " . Supported formats: rgb(r,g,b) and #rrggbb" << std::endl;
135
98
}
136
99
}
137
100
0 commit comments