Repository: godotengine/godot (language: cpp)

Issue:
Game crashes when adding color preset after clearing presets in another control
### Tested versions

- Reproducible in: 4.4 (and previous 4.4.dev builds since saving color palettes was introduced #91604)

### System information

Godot v4.4.stable - Windows 11 (build 26100) - Multi-window, 1 monitor - Vulkan (Forward+) - integrated AMD Radeon 780M Graphics (Advanced Micro Devices, Inc.; 32.0.11039.8001) - AMD Ryzen 7 8845HS w/ Radeon 780M Graphics (16 threads)

### Issue description

If you have multiple `ColorPickers` in the scene and one `ColorPicker` uses saved color palette and it was cleared, trying to add color preset in another `ColorPicker`, if that color was added to the presets before, it would crash the game.

https://github.com/user-attachments/assets/1e5da6c7-255e-4d99-9793-721f06493741

### Steps to reproduce

1. Add 2 or more `ColorPicker`s to the scene
2. Assign different colors to each `ColorPicker`
3. Run the game
4. Open Swatches in each `ColorPicker` and add current color to presets
5. Save presets of one `ColorPicker` as color palette (you will see another issue mentioned in #104223)
6. Clear presets in the `ColorPicker` that you saved the palette in
7. In another `ColorPicker`  press the button to add current color to presets without chaning the color before (it should be the same color, as was added to presets for this `ColorPicker` before
8. The game should crash

### Minimal reproduction project (MRP)

[regression-test-project.zip](https://github.com/user-attachments/files/19270277/regression-test-project.zip)


Discussion / hints:

Fix patch:
diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp
index 426e0092d71b..655988b3d16e 100644
--- a/scene/gui/color_picker.cpp
+++ b/scene/gui/color_picker.cpp
@@ -1138,16 +1138,21 @@ void ColorPicker::add_preset(const Color &p_color) {
 	List<Color>::Element *e = presets.find(p_color);
 	if (e) {
 		presets.move_to_back(e);
-		preset_cache.move_to_back(preset_cache.find(p_color));
 
 		preset_container->move_child(preset_group->get_pressed_button(), preset_container->get_child_count() - 1);
 	} else {
 		presets.push_back(p_color);
-		preset_cache.push_back(p_color);
 
 		_add_preset_button(_get_preset_size(), p_color);
 	}
 
+	List<Color>::Element *cache_e = preset_cache.find(p_color);
+	if (cache_e) {
+		preset_cache.move_to_back(cache_e);
+	} else {
+		preset_cache.push_back(p_color);
+	}
+
 	if (!palette_name->get_text().is_empty()) {
 		palette_name->set_text(vformat("%s*", palette_name->get_text().trim_suffix("*")));
 		palette_name->set_tooltip_text(ETR("The changes to this palette have not been saved to a file."));


Test patch: