Skip to content

Commit

Permalink
Allow additional hexadecimal color codes in ColorPicker
Browse files Browse the repository at this point in the history
The following formats are now accepted (leading `#` is optional):

- `#1` -> `#111111`
- `godotengine#12` -> `#121212`
- `godotengine#12345` -> `#11223344` (`5` at the end is discarded)
- `#1234567` -> `#123456` (`7` at the end is discarded)
  • Loading branch information
Calinou committed Jan 8, 2024
1 parent f4059d0 commit 760cc5a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,25 @@ void ColorPicker::_html_submitted(const String &p_html) {
return;
}

Color new_color = Color::from_string(p_html.strip_edges(), color);
String html_no_prefix = p_html.strip_edges().trim_prefix("#");
if (html_no_prefix.is_valid_hex_number(false)) {
// Convert invalid HTML color codes that software like Figma supports.
if (html_no_prefix.length() == 1) {
// Turn `#1` into `#111111`.
html_no_prefix = html_no_prefix.repeat(6);
} else if (html_no_prefix.length() == 2) {
// Turn `#12` into `#121212`.
html_no_prefix = html_no_prefix.repeat(3);
} else if (html_no_prefix.length() == 5) {
// Turn `#12345` into `#11223344`.
html_no_prefix = html_no_prefix.left(4);
} else if (html_no_prefix.length() == 7) {
// Turn `#1234567` into `#123456`.
html_no_prefix = html_no_prefix.left(6);
}
}

Color new_color = Color::from_string(html_no_prefix, new_color);

Check failure on line 577 in scene/gui/color_picker.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

variable 'new_color' is uninitialized when passed as a const reference argument here [-Werror,-Wuninitialized-const-reference]

Check failure on line 577 in scene/gui/color_picker.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

variable 'new_color' is uninitialized when passed as a const reference argument here [-Werror,-Wuninitialized-const-reference]

if (!is_editing_alpha()) {
new_color.a = color.a;
Expand Down

0 comments on commit 760cc5a

Please sign in to comment.