diff --git a/resources/filament_mixing/standard_color_recipes.json b/resources/filament_mixing/standard_color_recipes.json new file mode 100644 index 0000000000..280be054b2 --- /dev/null +++ b/resources/filament_mixing/standard_color_recipes.json @@ -0,0 +1,14705 @@ +{ + "_comment": "Simulated values (source=filament_mixer) are generated by FilamentMixer, a degree-4 polynomial regression trained to approximate Mixbox behavior (Mean Delta-E ~2.07). This file does not use Mixbox source code, binaries, or data files. See src/libslic3r/FilamentMixerModel.hpp.", + "entries": [ + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 48.12, + 36.72, + -25.69 + ], + "measured_rgb": "#9A5B9E", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 48.038, + 34.252, + -26.762 + ], + "measured_rgb": "#955DA0", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 47.957, + 31.783, + -27.833 + ], + "measured_rgb": "#905FA1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 47.875, + 29.315, + -28.905 + ], + "measured_rgb": "#8B61A3", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 47.793, + 26.847, + -29.977 + ], + "measured_rgb": "#8563A4", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 47.712, + 24.378, + -31.048 + ], + "measured_rgb": "#8065A6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 47.63, + 21.91, + -32.12 + ], + "measured_rgb": "#7967A7", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 48.247, + 19.212, + -32.842 + ], + "measured_rgb": "#756AAA", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 48.863, + 16.513, + -33.563 + ], + "measured_rgb": "#706DAD", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 49.48, + 13.815, + -34.285 + ], + "measured_rgb": "#6B71B0", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 50.097, + 11.117, + -35.007 + ], + "measured_rgb": "#6574B3", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 50.713, + 8.418, + -35.728 + ], + "measured_rgb": "#5F77B6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 51.33, + 5.72, + -36.45 + ], + "measured_rgb": "#587AB8", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 72.05, + -32.59, + 59.71 + ], + "measured_rgb": "#96BE39", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 70.497, + -34.185, + 54.833 + ], + "measured_rgb": "#8CBB41", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 68.943, + -35.78, + 49.957 + ], + "measured_rgb": "#82B748", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 67.39, + -37.375, + 45.08 + ], + "measured_rgb": "#78B44E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 65.837, + -38.97, + 40.203 + ], + "measured_rgb": "#6DB054", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 64.283, + -40.565, + 35.327 + ], + "measured_rgb": "#61AD5A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 62.73, + -42.16, + 30.45 + ], + "measured_rgb": "#53AA5F", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 62.037, + -42.202, + 26.398 + ], + "measured_rgb": "#4DA865", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 61.343, + -42.243, + 22.347 + ], + "measured_rgb": "#45A66B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 60.65, + -42.285, + 18.295 + ], + "measured_rgb": "#3CA471", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 59.957, + -42.327, + 14.243 + ], + "measured_rgb": "#32A376", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 59.263, + -42.368, + 10.192 + ], + "measured_rgb": "#23A17B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 58.57, + -42.41, + 6.14 + ], + "measured_rgb": "#089F81", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 76.96, + -13.8, + -20.22 + ], + "measured_rgb": "#86C7E3", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 75.9, + -13.61, + -21.202 + ], + "measured_rgb": "#81C4E1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 74.84, + -13.42, + -22.183 + ], + "measured_rgb": "#7DC1E0", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 73.78, + -13.23, + -23.165 + ], + "measured_rgb": "#79BEDF", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 72.72, + -13.04, + -24.147 + ], + "measured_rgb": "#75BBDE", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 71.66, + -12.85, + -25.128 + ], + "measured_rgb": "#70B8DD", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 70.6, + -12.66, + -26.11 + ], + "measured_rgb": "#6CB6DB", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 67.377, + -9.625, + -27.845 + ], + "measured_rgb": "#69ABD6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 64.153, + -6.59, + -29.58 + ], + "measured_rgb": "#65A1D0", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 60.93, + -3.555, + -31.315 + ], + "measured_rgb": "#6298CA", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 57.707, + -0.52, + -33.05 + ], + "measured_rgb": "#5F8EC4", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 54.483, + 2.515, + -34.785 + ], + "measured_rgb": "#5B84BE", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 51.26, + 5.55, + -36.52 + ], + "measured_rgb": "#577AB8", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 68.59, + 15.52, + 56.99 + ], + "measured_rgb": "#DB9B3C", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 66.767, + 18.537, + 52.082 + ], + "measured_rgb": "#D99443", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 64.943, + 21.553, + 47.173 + ], + "measured_rgb": "#D78D48", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 63.12, + 24.57, + 42.265 + ], + "measured_rgb": "#D4864E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 61.297, + 27.587, + 37.357 + ], + "measured_rgb": "#D28053", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 59.473, + 30.603, + 32.448 + ], + "measured_rgb": "#CF7958", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 57.65, + 33.62, + 27.54 + ], + "measured_rgb": "#CC725C", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 57.01, + 35.452, + 24.22 + ], + "measured_rgb": "#CC6E61", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 56.37, + 37.283, + 20.9 + ], + "measured_rgb": "#CB6B65", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 55.73, + 39.115, + 17.58 + ], + "measured_rgb": "#CB6869", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 55.09, + 40.947, + 14.26 + ], + "measured_rgb": "#CA656D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 54.45, + 42.778, + 10.94 + ], + "measured_rgb": "#CA6271", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 53.81, + 44.61, + 7.62 + ], + "measured_rgb": "#C95E75", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 72.63, + 29.26, + -12.43 + ], + "measured_rgb": "#DDA0CA", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 71.607, + 29.98, + -12.407 + ], + "measured_rgb": "#DB9CC7", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 70.583, + 30.7, + -12.383 + ], + "measured_rgb": "#DA99C4", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 69.56, + 31.42, + -12.36 + ], + "measured_rgb": "#D896C1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 68.537, + 32.14, + -12.337 + ], + "measured_rgb": "#D692BE", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 67.513, + 32.86, + -12.313 + ], + "measured_rgb": "#D48FBB", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 66.49, + 33.58, + -12.29 + ], + "measured_rgb": "#D38CB9", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 64.777, + 36.348, + -12.727 + ], + "measured_rgb": "#D285B5", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 63.063, + 39.117, + -13.163 + ], + "measured_rgb": "#D17EB1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 61.35, + 41.885, + -13.6 + ], + "measured_rgb": "#D077AD", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 59.637, + 44.653, + -14.037 + ], + "measured_rgb": "#CF70A9", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 57.923, + 47.422, + -14.473 + ], + "measured_rgb": "#CE69A6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 56.21, + 50.19, + -14.91 + ], + "measured_rgb": "#CD61A2", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 89.77, + -11.57, + 41.5 + ], + "measured_rgb": "#E8E691", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 89.41, + -11.307, + 43.647 + ], + "measured_rgb": "#E8E58C", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 89.05, + -11.043, + 45.793 + ], + "measured_rgb": "#E9E487", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 88.69, + -10.78, + 47.94 + ], + "measured_rgb": "#E9E281", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 88.33, + -10.517, + 50.087 + ], + "measured_rgb": "#EAE17C", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 87.97, + -10.253, + 52.233 + ], + "measured_rgb": "#EAE077", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 87.61, + -9.99, + 54.38 + ], + "measured_rgb": "#EADF71", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 87.398, + -9.86, + 57.192 + ], + "measured_rgb": "#EBDE6B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 87.187, + -9.73, + 60.003 + ], + "measured_rgb": "#ECDD64", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 86.975, + -9.6, + 62.815 + ], + "measured_rgb": "#ECDC5D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 86.763, + -9.47, + 65.627 + ], + "measured_rgb": "#EDDB56", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 86.552, + -9.34, + 68.438 + ], + "measured_rgb": "#EDDB4E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 86.34, + -9.21, + 71.25 + ], + "measured_rgb": "#EDDA46", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 58.92, + -7.07, + 33.53 + ], + "measured_rgb": "#969052", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 56.835, + -4.933, + 27.242 + ], + "measured_rgb": "#918A59", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 55.45, + -1.15, + 24.56 + ], + "measured_rgb": "#92845A", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 54.072, + 1.273, + 19.464 + ], + "measured_rgb": "#907F60", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 53.38, + 4.68, + 18.05 + ], + "measured_rgb": "#937C61", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 52.131, + 7.12, + 11.6 + ], + "measured_rgb": "#907869", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 51.41, + 10.52, + 8.37 + ], + "measured_rgb": "#92746D", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 50.768, + 12.107, + 5.298 + ], + "measured_rgb": "#917170", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 49.97, + 15.33, + 2.21 + ], + "measured_rgb": "#926E74", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 57.345, + -9.33, + 27.302 + ], + "measured_rgb": "#8B8D59", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 56.135, + -6.58, + 23.635 + ], + "measured_rgb": "#8A895D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 54.463, + -2.851, + 18.678 + ], + "measured_rgb": "#8A8362", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 53.385, + 0.29, + 15.782 + ], + "measured_rgb": "#8A7E65", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 52.613, + 3.31, + 13.936 + ], + "measured_rgb": "#8C7B66", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 51.603, + 6.16, + 8.38 + ], + "measured_rgb": "#8B776D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 51.038, + 8.243, + 5.013 + ], + "measured_rgb": "#8B7571", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 50.674, + 10.886, + 3.9 + ], + "measured_rgb": "#8E7272", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 56.98, + -14.34, + 24.74 + ], + "measured_rgb": "#7F8F5D", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 55.123, + -8.998, + 18.633 + ], + "measured_rgb": "#818863", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 53.19, + -3.76, + 11.71 + ], + "measured_rgb": "#81806B", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 52.698, + -0.693, + 12.101 + ], + "measured_rgb": "#857D69", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 51.52, + 1.39, + 8.81 + ], + "measured_rgb": "#83796C", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 51.074, + 5.2, + 5.16 + ], + "measured_rgb": "#867671", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 50.1, + 8.05, + -1.71 + ], + "measured_rgb": "#84737A", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 55.514, + -13.663, + 18.858 + ], + "measured_rgb": "#798B64", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 54.392, + -10.32, + 15.045 + ], + "measured_rgb": "#7A8768", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 52.967, + -5.5, + 10.567 + ], + "measured_rgb": "#7C816C", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 51.84, + -2.31, + 6.725 + ], + "measured_rgb": "#7C7C70", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 51.083, + 0.928, + 4.271 + ], + "measured_rgb": "#7E7972", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 50.849, + 3.236, + 3.172 + ], + "measured_rgb": "#817774", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 55.17, + -16.33, + 16.79 + ], + "measured_rgb": "#728B66", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 53.931, + -11.167, + 12.925 + ], + "measured_rgb": "#76866A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 52.23, + -6.85, + 6.94 + ], + "measured_rgb": "#758071", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 51.497, + -3.06, + 4.368 + ], + "measured_rgb": "#797C73", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 50.42, + -0.02, + -0.56 + ], + "measured_rgb": "#777879", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 54.193, + -15.753, + 11.043 + ], + "measured_rgb": "#6C896E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 53.26, + -12.21, + 7.178 + ], + "measured_rgb": "#6E8573", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 52.327, + -8.667, + 3.312 + ], + "measured_rgb": "#6F8177", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 51.389, + -4.229, + 1.238 + ], + "measured_rgb": "#747D78", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 54.15, + -18.72, + 9.16 + ], + "measured_rgb": "#648A71", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 52.967, + -12.623, + 4.053 + ], + "measured_rgb": "#698577", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 51.49, + -6.94, + -4.18 + ], + "measured_rgb": "#697F82", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 53.675, + -16.828, + 2.661 + ], + "measured_rgb": "#61887B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 53.17, + -14.343, + 1.343 + ], + "measured_rgb": "#63867C", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 53.47, + -16.97, + -4.04 + ], + "measured_rgb": "#578886", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 60.62, + 16.41, + -27.19 + ], + "measured_rgb": "#978BC2", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 58.416, + 18.423, + -27.936 + ], + "measured_rgb": "#9484BD", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 56.19, + 22.63, + -28.47 + ], + "measured_rgb": "#967BB8", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 55.817, + 22.511, + -27.942 + ], + "measured_rgb": "#957AB6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 56.48, + 23.36, + -26.2 + ], + "measured_rgb": "#9A7BB5", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 54.683, + 24.228, + -27.087 + ], + "measured_rgb": "#9676B2", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 53.86, + 25.93, + -26.83 + ], + "measured_rgb": "#9773AF", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 52.924, + 27.03, + -27.166 + ], + "measured_rgb": "#966FAD", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 51.71, + 29.47, + -27.22 + ], + "measured_rgb": "#976AAA", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 60.566, + 13.416, + -27.029 + ], + "measured_rgb": "#918CC2", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 58.437, + 16.227, + -28.148 + ], + "measured_rgb": "#9085BE", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 55.09, + 20.811, + -29.603 + ], + "measured_rgb": "#8E7AB7", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 54.78, + 21.542, + -29.157 + ], + "measured_rgb": "#8F78B6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 54.433, + 22.913, + -28.35 + ], + "measured_rgb": "#9276B3", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 53.707, + 23.395, + -28.23 + ], + "measured_rgb": "#9174B1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 53.303, + 23.898, + -28.057 + ], + "measured_rgb": "#9173B0", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 52.606, + 25.673, + -27.911 + ], + "measured_rgb": "#9270AE", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 62.64, + 7.61, + -25.75 + ], + "measured_rgb": "#8C95C5", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 57.776, + 12.715, + -29.283 + ], + "measured_rgb": "#8586BE", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 54.3, + 18.26, + -31.18 + ], + "measured_rgb": "#857AB8", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 53.449, + 19.947, + -30.78 + ], + "measured_rgb": "#8776B5", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 52.15, + 21.92, + -30.78 + ], + "measured_rgb": "#8772B1", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 52.733, + 22.562, + -29.373 + ], + "measured_rgb": "#8B72B0", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 52.34, + 22.37, + -29.11 + ], + "measured_rgb": "#8A72AF", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 58.326, + 9.338, + -30.016 + ], + "measured_rgb": "#7E89C1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 56.387, + 12.275, + -30.918 + ], + "measured_rgb": "#7F83BD", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 53.575, + 16.404, + -32.238 + ], + "measured_rgb": "#7E79B8", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 52.46, + 18.41, + -32.043 + ], + "measured_rgb": "#7F75B4", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 51.78, + 19.563, + -31.891 + ], + "measured_rgb": "#8072B2", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 52.036, + 21.038, + -30.619 + ], + "measured_rgb": "#8572B1", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 55.95, + 8.13, + -33.38 + ], + "measured_rgb": "#7084C0", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 54.999, + 11.835, + -32.553 + ], + "measured_rgb": "#7880BC", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 52.66, + 15.1, + -33.36 + ], + "measured_rgb": "#7778B7", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 51.95, + 17.29, + -32.751 + ], + "measured_rgb": "#7B74B4", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 50.73, + 18.36, + -32.85 + ], + "measured_rgb": "#7A71B1", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 56.176, + 7.293, + -32.738 + ], + "measured_rgb": "#7085BF", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 54.497, + 10.53, + -33.105 + ], + "measured_rgb": "#727FBB", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 52.207, + 15.028, + -33.565 + ], + "measured_rgb": "#7677B6", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 51.837, + 15.861, + -33.386 + ], + "measured_rgb": "#7775B5", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 58.08, + 3.22, + -31.73 + ], + "measured_rgb": "#6C8CC3", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 54.626, + 9.807, + -32.928 + ], + "measured_rgb": "#7180BB", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 51.3, + 15.67, + -33.95 + ], + "measured_rgb": "#7474B4", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 56.067, + 5.637, + -32.746 + ], + "measured_rgb": "#6B86BF", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 54.872, + 8.214, + -33.064 + ], + "measured_rgb": "#6E82BC", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 55.02, + 5.77, + -33.45 + ], + "measured_rgb": "#6883BD", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 72.25, + -37.06, + 24.67 + ], + "measured_rgb": "#76C283", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 71.136, + -38.009, + 27.818 + ], + "measured_rgb": "#73BF7A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 71.21, + -38.72, + 34.12 + ], + "measured_rgb": "#77BF6E", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 70.097, + -39.222, + 34.128 + ], + "measured_rgb": "#73BD6B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 69.96, + -39.61, + 37.44 + ], + "measured_rgb": "#74BC64", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 69.533, + -39.725, + 38.622 + ], + "measured_rgb": "#74BB61", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 70.07, + -39.32, + 41.87 + ], + "measured_rgb": "#78BC5C", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 69.807, + -38.943, + 42.092 + ], + "measured_rgb": "#79BB5A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 70.1, + -37.86, + 43.47 + ], + "measured_rgb": "#7DBC58", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 70.176, + -37.819, + 21.824 + ], + "measured_rgb": "#6BBD82", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 69.947, + -38.248, + 24.663 + ], + "measured_rgb": "#6CBC7D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 69.443, + -39.038, + 29.554 + ], + "measured_rgb": "#6DBB72", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 69.12, + -39.335, + 30.823 + ], + "measured_rgb": "#6DBA6F", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 68.702, + -39.682, + 32.737 + ], + "measured_rgb": "#6DB96A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 68.57, + -40.245, + 36.555 + ], + "measured_rgb": "#6EB962", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 68.759, + -40.381, + 40.397 + ], + "measured_rgb": "#71B95B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 69.094, + -39.751, + 41.165 + ], + "measured_rgb": "#74BA5B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 68.33, + -38.15, + 16.14 + ], + "measured_rgb": "#5EB888", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 68.196, + -38.822, + 20.824 + ], + "measured_rgb": "#61B87F", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 68.0, + -39.06, + 23.72 + ], + "measured_rgb": "#64B779", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 67.702, + -39.731, + 26.834 + ], + "measured_rgb": "#64B673", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 67.31, + -39.95, + 28.01 + ], + "measured_rgb": "#64B570", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 67.165, + -41.047, + 33.805 + ], + "measured_rgb": "#66B564", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 66.94, + -42.1, + 38.9 + ], + "measured_rgb": "#67B559", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 66.828, + -39.823, + 17.494 + ], + "measured_rgb": "#56B482", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 66.665, + -40.218, + 19.873 + ], + "measured_rgb": "#58B47D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 66.196, + -41.017, + 23.343 + ], + "measured_rgb": "#58B375", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 66.203, + -41.143, + 26.033 + ], + "measured_rgb": "#5BB370", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 66.233, + -41.326, + 29.073 + ], + "measured_rgb": "#5EB36B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 66.539, + -41.536, + 32.664 + ], + "measured_rgb": "#62B465", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 65.49, + -41.1, + 16.47 + ], + "measured_rgb": "#4CB180", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 65.258, + -41.626, + 19.645 + ], + "measured_rgb": "#4FB17A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 64.84, + -42.56, + 23.16 + ], + "measured_rgb": "#4FB072", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 64.848, + -42.5, + 25.195 + ], + "measured_rgb": "#52B06E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 64.66, + -43.0, + 29.24 + ], + "measured_rgb": "#55AF66", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 64.347, + -41.829, + 15.957 + ], + "measured_rgb": "#45AE7E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 64.113, + -42.238, + 17.53 + ], + "measured_rgb": "#46AE7B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 63.979, + -42.717, + 20.384 + ], + "measured_rgb": "#48AE75", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 64.149, + -42.788, + 22.598 + ], + "measured_rgb": "#4CAE71", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 63.44, + -42.15, + 13.87 + ], + "measured_rgb": "#3DAC80", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 63.204, + -42.248, + 14.638 + ], + "measured_rgb": "#3DAC7E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 62.68, + -43.14, + 16.62 + ], + "measured_rgb": "#3CAA79", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 63.005, + -41.008, + 11.148 + ], + "measured_rgb": "#3BAB83", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 62.993, + -41.544, + 12.664 + ], + "measured_rgb": "#3CAB81", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Cyan", + "rgb": "#0086D6" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 62.36, + -39.43, + 6.74 + ], + "measured_rgb": "#36A98A", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 70.73, + 16.88, + 29.74 + ], + "measured_rgb": "#DBA178", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 69.381, + 17.858, + 34.612 + ], + "measured_rgb": "#DB9C6B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 69.48, + 16.27, + 40.49 + ], + "measured_rgb": "#DB9D60", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 68.666, + 17.873, + 45.073 + ], + "measured_rgb": "#DC9A56", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 69.27, + 16.88, + 51.38 + ], + "measured_rgb": "#DE9C4A", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 68.885, + 17.481, + 51.935 + ], + "measured_rgb": "#DE9A48", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 69.9, + 15.64, + 54.97 + ], + "measured_rgb": "#DF9E44", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 68.967, + 17.607, + 54.28 + ], + "measured_rgb": "#DF9A43", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 68.51, + 18.92, + 54.92 + ], + "measured_rgb": "#DF9841", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 68.098, + 21.004, + 29.415 + ], + "measured_rgb": "#DA9772", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 67.933, + 20.422, + 33.605 + ], + "measured_rgb": "#DA966A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 68.071, + 19.105, + 40.002 + ], + "measured_rgb": "#DA975E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 67.248, + 20.468, + 43.348 + ], + "measured_rgb": "#DB9456", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 66.701, + 21.497, + 46.382 + ], + "measured_rgb": "#DC924E", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 67.485, + 19.923, + 49.455 + ], + "measured_rgb": "#DD954A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 68.282, + 18.367, + 52.279 + ], + "measured_rgb": "#DD9846", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 68.339, + 18.505, + 52.939 + ], + "measured_rgb": "#DE9845", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 65.63, + 25.71, + 24.9 + ], + "measured_rgb": "#D88D74", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 65.949, + 23.927, + 32.271 + ], + "measured_rgb": "#D98F68", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 65.89, + 22.83, + 39.29 + ], + "measured_rgb": "#D98F5A", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 65.294, + 24.002, + 41.295 + ], + "measured_rgb": "#DA8D55", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 64.35, + 25.89, + 42.23 + ], + "measured_rgb": "#DA8951", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 66.085, + 22.364, + 46.975 + ], + "measured_rgb": "#DB904C", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 66.42, + 21.28, + 49.24 + ], + "measured_rgb": "#DB9148", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 62.967, + 30.239, + 26.0 + ], + "measured_rgb": "#D7826C", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 63.57, + 28.218, + 30.77 + ], + "measured_rgb": "#D78565", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 63.934, + 26.355, + 36.534 + ], + "measured_rgb": "#D8875B", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 64.015, + 25.97, + 38.727 + ], + "measured_rgb": "#D88857", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 63.753, + 26.364, + 40.092 + ], + "measured_rgb": "#D88754", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 64.808, + 24.427, + 43.305 + ], + "measured_rgb": "#D98B50", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 59.7, + 36.79, + 22.33 + ], + "measured_rgb": "#D5746B", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 61.215, + 32.341, + 28.641 + ], + "measured_rgb": "#D67C63", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 63.06, + 27.54, + 36.56 + ], + "measured_rgb": "#D78459", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 62.983, + 27.508, + 36.188 + ], + "measured_rgb": "#D68459", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 62.76, + 27.62, + 36.83 + ], + "measured_rgb": "#D68358", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 60.188, + 34.818, + 23.527 + ], + "measured_rgb": "#D4776A", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 60.885, + 32.692, + 27.032 + ], + "measured_rgb": "#D57B65", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 61.837, + 29.803, + 31.746 + ], + "measured_rgb": "#D57F5F", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 62.068, + 29.258, + 33.017 + ], + "measured_rgb": "#D5805D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 59.98, + 34.97, + 21.22 + ], + "measured_rgb": "#D3776D", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 60.398, + 33.292, + 25.022 + ], + "measured_rgb": "#D37967", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 60.8, + 31.47, + 28.02 + ], + "measured_rgb": "#D37C63", + "source": "measured" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 59.099, + 36.827, + 20.252 + ], + "measured_rgb": "#D3736D", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 59.751, + 34.909, + 23.141 + ], + "measured_rgb": "#D37669", + "source": "interpolated" + }, + { + "mode": "CMYW", + "material": "PLA Basic", + "components": [ + { + "key": "Magenta", + "rgb": "#EC008C" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 57.81, + 39.76, + 17.5 + ], + "measured_rgb": "#D26D6E", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 62.49, + 27.88, + 52.76 + ], + "measured_rgb": "#D98237", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 60.433, + 30.793, + 50.433 + ], + "measured_rgb": "#D67A38", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 58.377, + 33.707, + 48.107 + ], + "measured_rgb": "#D47238", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 56.32, + 36.62, + 45.78 + ], + "measured_rgb": "#D16B38", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 54.263, + 39.533, + 43.453 + ], + "measured_rgb": "#CE6338", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 52.207, + 42.447, + 41.127 + ], + "measured_rgb": "#CB5A38", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 50.15, + 45.36, + 38.8 + ], + "measured_rgb": "#C85237", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 49.213, + 46.43, + 38.045 + ], + "measured_rgb": "#C64E37", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 48.277, + 47.5, + 37.29 + ], + "measured_rgb": "#C44B36", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 47.34, + 48.57, + 36.535 + ], + "measured_rgb": "#C34735", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 46.403, + 49.64, + 35.78 + ], + "measured_rgb": "#C14335", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 45.467, + 50.71, + 35.025 + ], + "measured_rgb": "#BF3F34", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 44.53, + 51.78, + 34.27 + ], + "measured_rgb": "#BD3B33", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 29.46, + 3.51, + -19.32 + ], + "measured_rgb": "#384563", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 29.34, + 4.79, + -16.368 + ], + "measured_rgb": "#3E445E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 29.22, + 6.07, + -13.417 + ], + "measured_rgb": "#44435A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 29.1, + 7.35, + -10.465 + ], + "measured_rgb": "#484155", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 28.98, + 8.63, + -7.513 + ], + "measured_rgb": "#4D4050", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 28.86, + 9.91, + -4.562 + ], + "measured_rgb": "#503F4B", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 28.74, + 11.19, + -1.61 + ], + "measured_rgb": "#543E47", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 29.12, + 13.203, + 0.292 + ], + "measured_rgb": "#583D45", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 29.5, + 15.217, + 2.193 + ], + "measured_rgb": "#5D3D43", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 29.88, + 17.23, + 4.095 + ], + "measured_rgb": "#623C41", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 30.26, + 19.243, + 5.997 + ], + "measured_rgb": "#663B3F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 30.64, + 21.257, + 7.898 + ], + "measured_rgb": "#6A3B3D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 31.02, + 23.27, + 9.8 + ], + "measured_rgb": "#6E3A3B", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 60.85, + 40.5, + 16.46 + ], + "measured_rgb": "#DC7478", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 59.283, + 41.47, + 17.302 + ], + "measured_rgb": "#D96F72", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 57.717, + 42.44, + 18.143 + ], + "measured_rgb": "#D66A6D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 56.15, + 43.41, + 18.985 + ], + "measured_rgb": "#D26568", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 54.583, + 44.38, + 19.827 + ], + "measured_rgb": "#CF6063", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 53.017, + 45.35, + 20.668 + ], + "measured_rgb": "#CC5B5E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 51.45, + 46.32, + 21.51 + ], + "measured_rgb": "#C95558", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 50.295, + 46.967, + 23.548 + ], + "measured_rgb": "#C75152", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 49.14, + 47.613, + 25.587 + ], + "measured_rgb": "#C54D4C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 47.985, + 48.26, + 27.625 + ], + "measured_rgb": "#C24946", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 46.83, + 48.907, + 29.663 + ], + "measured_rgb": "#C04540", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 45.675, + 49.553, + 31.702 + ], + "measured_rgb": "#BE413A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 44.52, + 50.2, + 33.74 + ], + "measured_rgb": "#BB3D34", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 36.34, + -18.37, + -11.18 + ], + "measured_rgb": "#155E67", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 37.912, + -19.985, + -6.787 + ], + "measured_rgb": "#206264", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 39.483, + -21.6, + -2.393 + ], + "measured_rgb": "#286760", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 41.055, + -23.215, + 2.0 + ], + "measured_rgb": "#2F6B5D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 42.627, + -24.83, + 6.393 + ], + "measured_rgb": "#356F59", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 44.198, + -26.445, + 10.787 + ], + "measured_rgb": "#3A7456", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 45.77, + -28.06, + 15.18 + ], + "measured_rgb": "#3F7852", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 48.035, + -28.535, + 19.455 + ], + "measured_rgb": "#477E50", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 50.3, + -29.01, + 23.73 + ], + "measured_rgb": "#50844E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 52.565, + -29.485, + 28.005 + ], + "measured_rgb": "#588A4C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 54.83, + -29.96, + 32.28 + ], + "measured_rgb": "#5F9049", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 57.095, + -30.435, + 36.555 + ], + "measured_rgb": "#679646", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 59.36, + -30.91, + 40.83 + ], + "measured_rgb": "#6E9C43", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 88.94, + -14.1, + 49.31 + ], + "measured_rgb": "#E5E57F", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 88.612, + -13.948, + 52.973 + ], + "measured_rgb": "#E6E477", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 88.283, + -13.797, + 56.637 + ], + "measured_rgb": "#E6E26E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 87.955, + -13.645, + 60.3 + ], + "measured_rgb": "#E7E165", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 87.627, + -13.493, + 63.963 + ], + "measured_rgb": "#E8E05C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 87.298, + -13.342, + 67.627 + ], + "measured_rgb": "#E8DF52", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 86.97, + -13.19, + 71.29 + ], + "measured_rgb": "#E9DE47", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 86.717, + -12.727, + 71.763 + ], + "measured_rgb": "#E9DD45", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 86.463, + -12.263, + 72.237 + ], + "measured_rgb": "#E9DC43", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 86.21, + -11.8, + 72.71 + ], + "measured_rgb": "#E9DB41", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 85.957, + -11.337, + 73.183 + ], + "measured_rgb": "#E9DA3F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 85.703, + -10.873, + 73.657 + ], + "measured_rgb": "#E9D93D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 85.45, + -10.41, + 74.13 + ], + "measured_rgb": "#EAD83B", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 80 + ], + "measured_lab": [ + 64.3, + -4.33, + -32.03 + ], + "measured_rgb": "#68A1D5", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 75 + ], + "measured_lab": [ + 61.86, + -3.247, + -33.64 + ], + "measured_rgb": "#619AD1", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 70 + ], + "measured_lab": [ + 59.42, + -2.163, + -35.25 + ], + "measured_rgb": "#5993CD", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 65 + ], + "measured_lab": [ + 56.98, + -1.08, + -36.86 + ], + "measured_rgb": "#528DC9", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 60 + ], + "measured_lab": [ + 54.54, + 0.003, + -38.47 + ], + "measured_rgb": "#4A86C5", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 55 + ], + "measured_lab": [ + 52.1, + 1.087, + -40.08 + ], + "measured_rgb": "#427FC1", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 50 + ], + "measured_lab": [ + 49.66, + 2.17, + -41.69 + ], + "measured_rgb": "#3979BD", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 45 + ], + "measured_lab": [ + 47.967, + 3.17, + -42.525 + ], + "measured_rgb": "#3474BA", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 40 + ], + "measured_lab": [ + 46.273, + 4.17, + -43.36 + ], + "measured_rgb": "#2F6FB6", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 65, + 35 + ], + "measured_lab": [ + 44.58, + 5.17, + -44.195 + ], + "measured_rgb": "#2A6BB3", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 70, + 30 + ], + "measured_lab": [ + 42.887, + 6.17, + -45.03 + ], + "measured_rgb": "#2566B0", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 75, + 25 + ], + "measured_lab": [ + 41.193, + 7.17, + -45.865 + ], + "measured_rgb": "#1F62AD", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 80, + 20 + ], + "measured_lab": [ + 39.5, + 8.17, + -46.7 + ], + "measured_rgb": "#175DAA", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 31.91, + 0.15, + -2.61 + ], + "measured_rgb": "#494B4F", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 33.44, + 0.855, + 2.003 + ], + "measured_rgb": "#514E4C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 35.38, + -0.41, + 5.94 + ], + "measured_rgb": "#57534A", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 35.64, + 1.093, + 7.858 + ], + "measured_rgb": "#5B5347", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 36.29, + 0.41, + 9.25 + ], + "measured_rgb": "#5C5547", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 37.468, + 2.187, + 12.217 + ], + "measured_rgb": "#635645", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 38.64, + 1.69, + 14.18 + ], + "measured_rgb": "#665944", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 40.133, + 3.466, + 17.156 + ], + "measured_rgb": "#6E5C43", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 42.17, + 4.75, + 20.85 + ], + "measured_rgb": "#776041", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 31.95, + 3.178, + 0.199 + ], + "measured_rgb": "#504A4B", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 33.03, + 2.825, + 2.678 + ], + "measured_rgb": "#544C4A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 34.485, + 2.602, + 6.069 + ], + "measured_rgb": "#594F48", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 35.25, + 3.28, + 8.383 + ], + "measured_rgb": "#5D5146", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 36.043, + 3.661, + 10.318 + ], + "measured_rgb": "#615244", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 37.473, + 4.46, + 13.22 + ], + "measured_rgb": "#675543", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 39.044, + 4.952, + 16.087 + ], + "measured_rgb": "#6D5842", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 39.826, + 4.901, + 17.277 + ], + "measured_rgb": "#6F5A42", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 30.91, + 6.56, + 0.53 + ], + "measured_rgb": "#534548", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 32.258, + 5.695, + 3.2 + ], + "measured_rgb": "#574947", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 33.92, + 5.0, + 6.85 + ], + "measured_rgb": "#5C4D45", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 34.678, + 6.296, + 8.956 + ], + "measured_rgb": "#614E44", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 35.41, + 8.12, + 11.49 + ], + "measured_rgb": "#664E41", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 37.46, + 7.938, + 14.712 + ], + "measured_rgb": "#6D5341", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 39.55, + 7.62, + 17.96 + ], + "measured_rgb": "#735840", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 31.482, + 8.343, + 3.323 + ], + "measured_rgb": "#594545", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 32.495, + 8.078, + 5.33 + ], + "measured_rgb": "#5C4844", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 33.939, + 8.505, + 8.552 + ], + "measured_rgb": "#624B43", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 34.88, + 9.587, + 10.82 + ], + "measured_rgb": "#674C41", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 35.457, + 10.859, + 12.473 + ], + "measured_rgb": "#6B4D40", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 37.115, + 9.671, + 14.811 + ], + "measured_rgb": "#6E5140", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 31.04, + 10.39, + 4.11 + ], + "measured_rgb": "#5B4343", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 32.583, + 10.243, + 6.938 + ], + "measured_rgb": "#604742", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 34.11, + 10.36, + 9.83 + ], + "measured_rgb": "#654A41", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 35.023, + 11.606, + 11.92 + ], + "measured_rgb": "#6A4B40", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 36.08, + 14.87, + 15.11 + ], + "measured_rgb": "#734B3D", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 31.752, + 12.508, + 6.458 + ], + "measured_rgb": "#614341", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 32.888, + 12.963, + 8.565 + ], + "measured_rgb": "#654640", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 34.023, + 13.418, + 10.672 + ], + "measured_rgb": "#6A4840", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 34.834, + 13.824, + 12.307 + ], + "measured_rgb": "#6D493F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 31.33, + 14.17, + 6.7 + ], + "measured_rgb": "#624140", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 33.096, + 14.688, + 9.628 + ], + "measured_rgb": "#69453F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 35.07, + 16.93, + 13.62 + ], + "measured_rgb": "#72483E", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 32.354, + 16.62, + 9.312 + ], + "measured_rgb": "#69423E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 32.996, + 16.524, + 10.244 + ], + "measured_rgb": "#6B433E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 32.98, + 19.7, + 11.64 + ], + "measured_rgb": "#70413C", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 62.23, + 35.29, + 25.26 + ], + "measured_rgb": "#DC7C6C", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 61.534, + 35.748, + 26.888 + ], + "measured_rgb": "#DB7A67", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 62.26, + 34.42, + 28.24 + ], + "measured_rgb": "#DB7D66", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 61.158, + 35.561, + 30.821 + ], + "measured_rgb": "#DA795F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 61.75, + 34.7, + 32.79 + ], + "measured_rgb": "#DC7B5D", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 60.502, + 35.282, + 34.869 + ], + "measured_rgb": "#D97756", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 60.7, + 34.27, + 37.47 + ], + "measured_rgb": "#D97852", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 60.206, + 34.426, + 39.029 + ], + "measured_rgb": "#D8774E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 60.1, + 33.81, + 42.27 + ], + "measured_rgb": "#D87747", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 60.271, + 37.504, + 25.308 + ], + "measured_rgb": "#D97567", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 60.112, + 37.532, + 27.162 + ], + "measured_rgb": "#D97463", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 59.954, + 37.561, + 29.018 + ], + "measured_rgb": "#D97460", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 59.465, + 37.562, + 31.432 + ], + "measured_rgb": "#D8735A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 59.055, + 37.325, + 33.396 + ], + "measured_rgb": "#D77256", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 59.055, + 36.875, + 34.347 + ], + "measured_rgb": "#D77254", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 59.367, + 35.876, + 36.068 + ], + "measured_rgb": "#D77451", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 59.55, + 35.359, + 37.618 + ], + "measured_rgb": "#D7744F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 58.47, + 39.69, + 23.5 + ], + "measured_rgb": "#D66E66", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 57.918, + 40.28, + 27.642 + ], + "measured_rgb": "#D66C5D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 57.49, + 40.73, + 31.65 + ], + "measured_rgb": "#D76A55", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 56.315, + 41.212, + 32.738 + ], + "measured_rgb": "#D46750", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 56.36, + 40.4, + 33.05 + ], + "measured_rgb": "#D36850", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 56.883, + 39.276, + 34.064 + ], + "measured_rgb": "#D36A4F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 57.41, + 38.13, + 34.08 + ], + "measured_rgb": "#D46D50", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 55.689, + 42.794, + 28.026 + ], + "measured_rgb": "#D36457", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 55.607, + 42.722, + 29.887 + ], + "measured_rgb": "#D36454", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 55.526, + 42.651, + 31.749 + ], + "measured_rgb": "#D36350", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 55.095, + 42.505, + 33.515 + ], + "measured_rgb": "#D2624C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 54.828, + 42.215, + 34.274 + ], + "measured_rgb": "#D1624A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 55.474, + 41.194, + 34.226 + ], + "measured_rgb": "#D2654C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 52.99, + 45.97, + 30.69 + ], + "measured_rgb": "#CF594C", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 54.026, + 44.398, + 31.429 + ], + "measured_rgb": "#D15E4D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 53.48, + 44.5, + 33.71 + ], + "measured_rgb": "#D05C48", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 53.875, + 43.798, + 34.292 + ], + "measured_rgb": "#D05E48", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 53.05, + 44.39, + 35.65 + ], + "measured_rgb": "#CF5B44", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 52.053, + 47.001, + 31.923 + ], + "measured_rgb": "#CE5548", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 51.938, + 46.902, + 33.508 + ], + "measured_rgb": "#CE5545", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 51.974, + 46.493, + 35.433 + ], + "measured_rgb": "#CE5642", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 52.243, + 45.967, + 35.487 + ], + "measured_rgb": "#CE5742", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 51.23, + 48.13, + 31.57 + ], + "measured_rgb": "#CD5247", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 51.072, + 48.014, + 34.379 + ], + "measured_rgb": "#CD5242", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 50.05, + 49.01, + 38.06 + ], + "measured_rgb": "#CC4D39", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 50.014, + 48.998, + 33.907 + ], + "measured_rgb": "#CB4E40", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 50.106, + 48.917, + 34.856 + ], + "measured_rgb": "#CB4E3F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 48.46, + 50.2, + 35.77 + ], + "measured_rgb": "#C84839", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 46.44, + 9.95, + -5.84 + ], + "measured_rgb": "#7B6978", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 43.093, + 9.297, + -6.861 + ], + "measured_rgb": "#706171", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 40.15, + 7.97, + -8.83 + ], + "measured_rgb": "#655B6D", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 39.33, + 7.278, + -9.51 + ], + "measured_rgb": "#61596C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 39.65, + 5.81, + -11.22 + ], + "measured_rgb": "#5E5B70", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 36.105, + 6.125, + -11.934 + ], + "measured_rgb": "#555368", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 33.79, + 5.58, + -14.05 + ], + "measured_rgb": "#4D4E66", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 32.584, + 5.717, + -14.126 + ], + "measured_rgb": "#4A4B63", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 30.83, + 5.37, + -15.57 + ], + "measured_rgb": "#444761", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 44.996, + 10.637, + -4.941 + ], + "measured_rgb": "#796573", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 42.687, + 9.97, + -5.912 + ], + "measured_rgb": "#71606F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 39.862, + 8.788, + -7.563 + ], + "measured_rgb": "#675A6A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 38.19, + 8.055, + -8.48 + ], + "measured_rgb": "#615668", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 37.5, + 7.445, + -9.22 + ], + "measured_rgb": "#5E5567", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 34.875, + 6.985, + -10.533 + ], + "measured_rgb": "#554F63", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 33.355, + 6.882, + -11.161 + ], + "measured_rgb": "#514C60", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 32.484, + 6.31, + -12.739 + ], + "measured_rgb": "#4C4A60", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 45.86, + 11.99, + -3.07 + ], + "measured_rgb": "#7E6672", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 42.708, + 11.107, + -4.343 + ], + "measured_rgb": "#745F6C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 38.3, + 9.97, + -5.91 + ], + "measured_rgb": "#665564", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 37.05, + 8.832, + -7.45 + ], + "measured_rgb": "#605363", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 34.66, + 8.47, + -7.96 + ], + "measured_rgb": "#594D5E", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 33.568, + 8.272, + -8.298 + ], + "measured_rgb": "#564B5C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 31.4, + 8.08, + -8.9 + ], + "measured_rgb": "#504658", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 44.09, + 12.48, + -2.568 + ], + "measured_rgb": "#7B616D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 41.16, + 11.92, + -3.262 + ], + "measured_rgb": "#725B67", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 38.23, + 11.36, + -3.958 + ], + "measured_rgb": "#6A5461", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 35.265, + 10.333, + -5.157 + ], + "measured_rgb": "#604E5B", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 33.946, + 9.492, + -6.243 + ], + "measured_rgb": "#5B4B5A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 33.394, + 8.879, + -7.239 + ], + "measured_rgb": "#584A5A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 45.25, + 13.53, + -1.37 + ], + "measured_rgb": "#80636E", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 40.543, + 13.197, + -1.734 + ], + "measured_rgb": "#745863", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 35.23, + 12.19, + -2.7 + ], + "measured_rgb": "#644C57", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 34.455, + 11.074, + -3.972 + ], + "measured_rgb": "#604B58", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 32.87, + 10.7, + -4.06 + ], + "measured_rgb": "#5B4854", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 40.503, + 15.308, + 0.332 + ], + "measured_rgb": "#78565F", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 37.998, + 14.625, + -0.243 + ], + "measured_rgb": "#70515A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 35.493, + 13.942, + -0.818 + ], + "measured_rgb": "#694C55", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 34.5, + 12.689, + -2.045 + ], + "measured_rgb": "#644A55", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 38.26, + 17.77, + 2.61 + ], + "measured_rgb": "#774F56", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 36.514, + 16.453, + 1.608 + ], + "measured_rgb": "#704C54", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 33.25, + 15.01, + 0.49 + ], + "measured_rgb": "#65464E", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 36.912, + 18.292, + 3.339 + ], + "measured_rgb": "#754C52", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 36.228, + 17.339, + 2.496 + ], + "measured_rgb": "#714B52", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Red", + "rgb": "#C12E1F" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 35.37, + 20.0, + 5.16 + ], + "measured_rgb": "#74474C", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 20, + 60 + ], + "measured_lab": [ + 61.06, + -24.52, + 4.52 + ], + "measured_rgb": "#629F8B", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 25, + 55 + ], + "measured_lab": [ + 57.848, + -25.117, + 4.243 + ], + "measured_rgb": "#589783", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 30, + 50 + ], + "measured_lab": [ + 54.77, + -24.86, + 1.44 + ], + "measured_rgb": "#4D8F80", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 35, + 45 + ], + "measured_lab": [ + 52.842, + -24.709, + 1.617 + ], + "measured_rgb": "#498A7B", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 40, + 40 + ], + "measured_lab": [ + 51.03, + -23.9, + 0.04 + ], + "measured_rgb": "#448579", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 45, + 35 + ], + "measured_lab": [ + 50.331, + -22.636, + -1.173 + ], + "measured_rgb": "#448279", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 50, + 30 + ], + "measured_lab": [ + 50.33, + -19.96, + -4.63 + ], + "measured_rgb": "#46827F", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 55, + 25 + ], + "measured_lab": [ + 47.473, + -21.21, + -4.144 + ], + "measured_rgb": "#3C7B77", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 20, + 60, + 20 + ], + "measured_lab": [ + 44.4, + -21.12, + -5.79 + ], + "measured_rgb": "#317372", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 20, + 55 + ], + "measured_lab": [ + 59.761, + -26.071, + 8.356 + ], + "measured_rgb": "#609C80", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 25, + 50 + ], + "measured_lab": [ + 57.712, + -25.973, + 6.767 + ], + "measured_rgb": "#59977E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 30, + 45 + ], + "measured_lab": [ + 54.838, + -25.724, + 4.421 + ], + "measured_rgb": "#4F8F7B", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 35, + 40 + ], + "measured_lab": [ + 52.725, + -25.367, + 3.373 + ], + "measured_rgb": "#498A77", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 40, + 35 + ], + "measured_lab": [ + 51.078, + -24.654, + 2.192 + ], + "measured_rgb": "#458575", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 45, + 30 + ], + "measured_lab": [ + 49.633, + -24.048, + 1.07 + ], + "measured_rgb": "#418173", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 50, + 25 + ], + "measured_lab": [ + 48.295, + -23.241, + -0.276 + ], + "measured_rgb": "#3E7D72", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 25, + 55, + 20 + ], + "measured_lab": [ + 47.321, + -22.711, + -1.654 + ], + "measured_rgb": "#3B7B72", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 20, + 50 + ], + "measured_lab": [ + 60.51, + -27.72, + 13.78 + ], + "measured_rgb": "#649F79", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 25, + 45 + ], + "measured_lab": [ + 56.832, + -27.292, + 9.998 + ], + "measured_rgb": "#579576", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 30, + 40 + ], + "measured_lab": [ + 54.51, + -26.79, + 7.33 + ], + "measured_rgb": "#4F8F75", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 35, + 35 + ], + "measured_lab": [ + 52.699, + -26.542, + 6.207 + ], + "measured_rgb": "#498A72", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 40, + 30 + ], + "measured_lab": [ + 50.59, + -25.92, + 4.68 + ], + "measured_rgb": "#448470", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 45, + 25 + ], + "measured_lab": [ + 48.934, + -25.459, + 3.313 + ], + "measured_rgb": "#3F806E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 30, + 50, + 20 + ], + "measured_lab": [ + 46.58, + -26.41, + 4.19 + ], + "measured_rgb": "#377A67", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 20, + 45 + ], + "measured_lab": [ + 59.54, + -27.398, + 13.802 + ], + "measured_rgb": "#629C76", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 25, + 40 + ], + "measured_lab": [ + 57.05, + -27.815, + 12.345 + ], + "measured_rgb": "#599573", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 30, + 35 + ], + "measured_lab": [ + 53.733, + -28.082, + 10.13 + ], + "measured_rgb": "#4D8D6E", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 35, + 30 + ], + "measured_lab": [ + 51.412, + -28.053, + 9.243 + ], + "measured_rgb": "#46876A", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 40, + 25 + ], + "measured_lab": [ + 50.144, + -27.794, + 8.631 + ], + "measured_rgb": "#438468", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 35, + 45, + 20 + ], + "measured_lab": [ + 48.907, + -27.218, + 6.971 + ], + "measured_rgb": "#3F8068", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 20, + 40 + ], + "measured_lab": [ + 61.06, + -26.66, + 15.28 + ], + "measured_rgb": "#699F77", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 25, + 35 + ], + "measured_lab": [ + 56.013, + -28.529, + 13.977 + ], + "measured_rgb": "#56936D", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 30, + 30 + ], + "measured_lab": [ + 52.12, + -30.09, + 12.99 + ], + "measured_rgb": "#478965", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 35, + 25 + ], + "measured_lab": [ + 50.654, + -29.184, + 11.401 + ], + "measured_rgb": "#438564", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 40, + 40, + 20 + ], + "measured_lab": [ + 48.43, + -29.41, + 11.97 + ], + "measured_rgb": "#3E805E", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 20, + 35 + ], + "measured_lab": [ + 59.968, + -26.563, + 15.444 + ], + "measured_rgb": "#679D74", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 25, + 30 + ], + "measured_lab": [ + 56.752, + -28.317, + 15.672 + ], + "measured_rgb": "#5A956C", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 30, + 25 + ], + "measured_lab": [ + 53.538, + -30.073, + 15.901 + ], + "measured_rgb": "#4E8D64", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 45, + 35, + 20 + ], + "measured_lab": [ + 50.997, + -30.209, + 14.208 + ], + "measured_rgb": "#458660", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 20, + 30 + ], + "measured_lab": [ + 62.09, + -24.71, + 15.38 + ], + "measured_rgb": "#70A17A", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 25, + 25 + ], + "measured_lab": [ + 56.579, + -28.785, + 18.097 + ], + "measured_rgb": "#5B9467", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 50, + 30, + 20 + ], + "measured_lab": [ + 51.74, + -31.81, + 19.04 + ], + "measured_rgb": "#48895A", + "source": "measured" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 20, + 25 + ], + "measured_lab": [ + 59.237, + -28.888, + 22.914 + ], + "measured_rgb": "#669B65", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 55, + 25, + 20 + ], + "measured_lab": [ + 56.854, + -29.771, + 21.59 + ], + "measured_rgb": "#5D9562", + "source": "interpolated" + }, + { + "mode": "RYBW", + "material": "PLA Basic", + "components": [ + { + "key": "Yellow", + "rgb": "#F4EE2A" + }, + { + "key": "Blue", + "rgb": "#0A2989" + }, + { + "key": "White", + "rgb": "#FFFFFF" + } + ], + "ratios": [ + 60, + 20, + 20 + ], + "measured_lab": [ + 57.68, + -32.73, + 32.07 + ], + "measured_rgb": "#609850", + "source": "measured" + } + ] +} diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index 333f43a68c..b8e537c5aa 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -179,6 +179,17 @@ set(lisbslic3r_sources Fill/Lightning/Layer.hpp Fill/Lightning/TreeNode.cpp Fill/Lightning/TreeNode.hpp + FilamentMixer.cpp + FilamentMixer.hpp + FilamentMixerModel.hpp + ColorDecomposeRecipe.cpp + ColorDecomposeRecipe.hpp + TexturePainting.hpp + TexturePainting.cpp + TextureToColor/TextureToColor.hpp + TextureToColor/TextureToColor.cpp + TextureToColor/ColorUtils.hpp + TextureToColor/ColorUtils.cpp Flow.cpp Flow.hpp FlushVolCalc.cpp @@ -194,6 +205,7 @@ set(lisbslic3r_sources format.hpp Format/OBJ.cpp Format/OBJ.hpp + Format/ResourcePathUtils.hpp Format/objparser.cpp Format/objparser.hpp Format/SL1.cpp @@ -549,7 +561,7 @@ target_compile_definitions(libslic3r PUBLIC -DUSE_TBB -DTBB_USE_CAPTURED_EXCEPTI if (USE_SLIC3R_CONSOLE_LOG) target_compile_definitions(libslic3r PRIVATE $<$:SLIC3R_CONSOLE_LOG>) endif() -target_include_directories(libslic3r PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(libslic3r PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/TextureToColor PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(libslic3r SYSTEM PUBLIC ${EXPAT_INCLUDE_DIRS}) # Find the OCCT and related libraries diff --git a/src/libslic3r/ColorDecomposeRecipe.cpp b/src/libslic3r/ColorDecomposeRecipe.cpp new file mode 100644 index 0000000000..8e5de9c06f --- /dev/null +++ b/src/libslic3r/ColorDecomposeRecipe.cpp @@ -0,0 +1,361 @@ +#include "ColorDecomposeRecipe.hpp" + +#include "FilamentMixer.hpp" +#include "Utils.hpp" +#include "nlohmann/json.hpp" + +#include +#include +#include +#include +#include +#include + +namespace Slic3r { +namespace { + +struct LabColor { + double l{0.0}; + double a{0.0}; + double b{0.0}; +}; + +struct StandardRecipeEntry { + ColorDecomposeRecipeMode mode{ColorDecomposeRecipeMode::CMYW}; + std::string material; + std::string source; + std::vector component_keys; + std::vector component_hexes; + std::vector ratios; + std::string measured_hex; + LabColor measured_lab; +}; + +static double srgb_to_linear(double v) +{ + v /= 255.0; + return v <= 0.04045 ? v / 12.92 : std::pow((v + 0.055) / 1.055, 2.4); +} + +static double xyz_to_lab_component(double v) +{ + constexpr double eps = 216.0 / 24389.0; + constexpr double kappa = 24389.0 / 27.0; + return v > eps ? std::cbrt(v) : (kappa * v + 16.0) / 116.0; +} + +static LabColor rgb_to_lab(const ColorDecomposeRgb& rgb) +{ + const double r = srgb_to_linear(rgb.r); + const double g = srgb_to_linear(rgb.g); + const double b = srgb_to_linear(rgb.b); + + const double x = (0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / 0.95047; + const double y = (0.2126729 * r + 0.7151522 * g + 0.0721750 * b); + const double z = (0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / 1.08883; + + const double fx = xyz_to_lab_component(x); + const double fy = xyz_to_lab_component(y); + const double fz = xyz_to_lab_component(z); + + return {116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz)}; +} + +static double delta_e76(const LabColor& a, const LabColor& b) +{ + return std::sqrt(std::pow(a.l - b.l, 2.0) + std::pow(a.a - b.a, 2.0) + std::pow(a.b - b.b, 2.0)); +} + +static bool material_matches(const std::string& a, const std::string& b) +{ + if (a.empty() || b.empty()) + return false; + return a == b || a == b + " Basic" || b == a + " Basic"; +} + +static std::vector> ratio_grid(size_t n) +{ + std::vector> out; + if (n == 2) { + for (int a = 20; a <= 80; a += 5) + out.push_back({a, 100 - a}); + } else if (n == 3) { + for (int a = 20; a <= 60; a += 5) + for (int b = 20; b <= 80 - a; b += 5) { + const int c = 100 - a - b; + if (c >= 20) + out.push_back({a, b, c}); + } + } + return out; +} + +static ColorDecomposeRecipeMode parse_mode(const std::string& s) +{ + if (s == "RYBW" || s == "RGBY") + return ColorDecomposeRecipeMode::RYBW; + return ColorDecomposeRecipeMode::CMYW; +} + +static std::vector load_standard_entries() +{ + std::vector entries; + const std::string path = resources_dir() + "/filament_mixing/standard_color_recipes.json"; + std::ifstream ifs(path); + if (!ifs) + return entries; + + nlohmann::json root = nlohmann::json::parse(ifs, nullptr, false); + if (root.is_discarded() || !root.contains("entries") || !root["entries"].is_array()) + return entries; + + for (const auto& item : root["entries"]) { + if (!item.is_object()) + continue; + StandardRecipeEntry entry; + entry.mode = parse_mode(item.value("mode", "CMYW")); + entry.material = item.value("material", ""); + entry.source = item.value("source", ""); + entry.measured_hex = item.value("measured_rgb", ""); + + if (item.contains("components") && item["components"].is_array()) { + for (const auto& comp : item["components"]) { + if (comp.is_object()) { + entry.component_keys.push_back(comp.value("key", "")); + entry.component_hexes.push_back(comp.value("rgb", "")); + } + } + } + if (item.contains("ratios") && item["ratios"].is_array()) { + for (const auto& ratio : item["ratios"]) { + if (ratio.is_number_integer()) + entry.ratios.push_back(ratio.get()); + } + } + if (item.contains("measured_lab") && item["measured_lab"].is_array() && item["measured_lab"].size() >= 3) { + entry.measured_lab = { + item["measured_lab"][0].get(), + item["measured_lab"][1].get(), + item["measured_lab"][2].get() + }; + } else { + ColorDecomposeRgb measured_rgb; + if (!color_decompose_hex_to_rgb(entry.measured_hex, measured_rgb)) + continue; + entry.measured_lab = rgb_to_lab(measured_rgb); + } + + if (entry.component_hexes.size() >= 2 && entry.component_hexes.size() == entry.ratios.size() && + !entry.measured_hex.empty()) + entries.push_back(std::move(entry)); + } + return entries; +} + +static const std::vector& standard_entries() +{ + static const std::vector entries = load_standard_entries(); + return entries; +} + +static void evaluate_candidate(const ColorDecomposeRgb& target, + const std::vector& hexes, + const std::vector& ratios, + const std::vector& indices, + ColorDecomposeRecipeMode mode, + double& best_score, + ColorDecomposeRecipeResult& best) +{ + const std::string mixed = blend_color_multi(hexes, ratios); + ColorDecomposeRgb mixed_rgb; + if (!color_decompose_hex_to_rgb(mixed, mixed_rgb)) + return; + + const double score = delta_e76(rgb_to_lab(target), rgb_to_lab(mixed_rgb)); + if (score >= best_score) + return; + + best_score = score; + best.valid = true; + best.mode = mode; + best.matched_color_hex = mixed; + best.components.clear(); + for (size_t i = 0; i < hexes.size(); ++i) { + ColorDecomposeRecipeComponent comp; + comp.color_hex = hexes[i]; + comp.ratio = ratios[i]; + comp.filament_index = i < indices.size() ? indices[i] : 0; + best.components.push_back(comp); + } +} + +} // namespace + +std::string color_decompose_rgb_to_hex(const ColorDecomposeRgb& rgb) +{ + char buf[8]; + std::snprintf(buf, sizeof(buf), "#%02X%02X%02X", rgb.r, rgb.g, rgb.b); + return std::string(buf); +} + +bool color_decompose_hex_to_rgb(const std::string& hex, ColorDecomposeRgb& out) +{ + if (hex.size() < 7 || hex[0] != '#') + return false; + unsigned r = 0, g = 0, b = 0; + if (std::sscanf(hex.c_str(), "#%02x%02x%02x", &r, &g, &b) != 3) + return false; + out = {static_cast(r), static_cast(g), static_cast(b)}; + return true; +} + +ColorDecomposeRecipeResult recommend_from_physical_filaments( + const ColorDecomposeRgb& target, + const std::vector& physical_filaments, + const std::string& preferred_material_type) +{ + std::vector candidates; + for (const auto& filament : physical_filaments) { + if (filament.is_mixed) + continue; + ColorDecomposeRgb ignored; + if (!color_decompose_hex_to_rgb(filament.color_hex, ignored)) + continue; + if (preferred_material_type.empty() || material_matches(filament.type, preferred_material_type)) + candidates.push_back(filament); + } + if (candidates.size() < 2) + candidates = physical_filaments; + candidates.erase(std::remove_if(candidates.begin(), candidates.end(), [](const auto& filament) { + if (filament.is_mixed) + return true; + ColorDecomposeRgb ignored; + return !color_decompose_hex_to_rgb(filament.color_hex, ignored); + }), candidates.end()); + + constexpr size_t kMaxCandidates = 8; + if (candidates.size() > kMaxCandidates) { + const LabColor target_lab = rgb_to_lab(target); + std::sort(candidates.begin(), candidates.end(), + [&target_lab](const ColorDecomposePhysicalFilament& a, const ColorDecomposePhysicalFilament& b) { + ColorDecomposeRgb rgb_a, rgb_b; + color_decompose_hex_to_rgb(a.color_hex, rgb_a); + color_decompose_hex_to_rgb(b.color_hex, rgb_b); + return delta_e76(target_lab, rgb_to_lab(rgb_a)) + < delta_e76(target_lab, rgb_to_lab(rgb_b)); + }); + candidates.resize(kMaxCandidates); + } + + ColorDecomposeRecipeResult best; + double best_score = std::numeric_limits::max(); + + for (size_t i = 0; i < candidates.size(); ++i) { + for (size_t j = i + 1; j < candidates.size(); ++j) { + const std::vector hexes = {candidates[i].color_hex, candidates[j].color_hex}; + const std::vector indices = {candidates[i].filament_index, candidates[j].filament_index}; + for (const auto& ratios : ratio_grid(2)) + evaluate_candidate(target, hexes, ratios, indices, ColorDecomposeRecipeMode::MaterialList, best_score, best); + + for (size_t k = j + 1; k < candidates.size(); ++k) { + const std::vector hexes3 = {candidates[i].color_hex, candidates[j].color_hex, candidates[k].color_hex}; + const std::vector indices3 = {candidates[i].filament_index, candidates[j].filament_index, candidates[k].filament_index}; + for (const auto& ratios : ratio_grid(3)) + evaluate_candidate(target, hexes3, ratios, indices3, ColorDecomposeRecipeMode::MaterialList, best_score, best); + } + } + } + + return best; +} + +ColorDecomposeRecipeResult lookup_standard_recipe( + const ColorDecomposeRgb& target, + ColorDecomposeRecipeMode mode, + const std::string& preferred_material_type) +{ + const LabColor target_lab = rgb_to_lab(target); + ColorDecomposeRecipeResult best; + double best_score = std::numeric_limits::max(); + + auto consider = [&](bool require_material_match) { + for (const StandardRecipeEntry& entry : standard_entries()) { + if (entry.mode != mode) + continue; + if (require_material_match && !material_matches(entry.material, preferred_material_type)) + continue; + if (!require_material_match && !preferred_material_type.empty() && material_matches(entry.material, preferred_material_type)) + continue; + + const double score = delta_e76(target_lab, entry.measured_lab); + if (score >= best_score) + continue; + + best_score = score; + best.valid = true; + best.mode = mode; + best.matched_color_hex = entry.measured_hex; + best.components.clear(); + for (size_t i = 0; i < entry.component_hexes.size(); ++i) { + ColorDecomposeRecipeComponent comp; + comp.color_hex = entry.component_hexes[i]; + comp.base_color = i < entry.component_keys.size() ? entry.component_keys[i] : ""; + comp.ratio = entry.ratios[i]; + comp.filament_index = 0; + best.components.push_back(comp); + } + } + }; + + consider(true); + if (!best.valid) + consider(false); + return best; +} + +std::string lookup_measured_blend_color(const std::vector& component_hexes, + const std::vector& ratios) +{ + if (component_hexes.size() < 2 || component_hexes.size() != ratios.size()) + return {}; + + auto normalize_hex = [](const std::string& hex) -> std::string { + ColorDecomposeRgb rgb; + if (!color_decompose_hex_to_rgb(hex, rgb)) + return {}; + char buf[8]; + std::snprintf(buf, sizeof(buf), "#%02X%02X%02X", rgb.r, rgb.g, rgb.b); + return std::string(buf); + }; + + std::vector norm_hexes; + norm_hexes.reserve(component_hexes.size()); + for (const auto& h : component_hexes) { + std::string n = normalize_hex(h); + if (n.empty()) + return {}; + norm_hexes.push_back(std::move(n)); + } + + for (const StandardRecipeEntry& entry : standard_entries()) { + if (entry.source != "measured" && entry.source != "interpolated") + continue; + if (entry.component_hexes.size() != norm_hexes.size()) + continue; + if (entry.ratios != ratios) + continue; + + bool match = true; + for (size_t i = 0; i < norm_hexes.size(); ++i) { + if (normalize_hex(entry.component_hexes[i]) != norm_hexes[i]) { + match = false; + break; + } + } + if (match) + return entry.measured_hex; + } + return {}; +} + +} // namespace Slic3r diff --git a/src/libslic3r/ColorDecomposeRecipe.hpp b/src/libslic3r/ColorDecomposeRecipe.hpp new file mode 100644 index 0000000000..146bf322a2 --- /dev/null +++ b/src/libslic3r/ColorDecomposeRecipe.hpp @@ -0,0 +1,64 @@ +#ifndef SLIC3R_COLOR_DECOMPOSE_RECIPE_HPP +#define SLIC3R_COLOR_DECOMPOSE_RECIPE_HPP + +#include +#include + +namespace Slic3r { + +enum class ColorDecomposeRecipeMode { + MaterialList, + CMYW, + RYBW +}; + +struct ColorDecomposeRgb { + unsigned char r{0}; + unsigned char g{0}; + unsigned char b{0}; +}; + +struct ColorDecomposePhysicalFilament { + std::string color_hex; + std::string name; + std::string type; + bool is_mixed{false}; + unsigned int filament_index{0}; // 1-based physical filament index +}; + +struct ColorDecomposeRecipeComponent { + std::string color_hex; + std::string base_color; + int ratio{0}; + unsigned int filament_index{0}; // 1-based for physical filaments, 0 for standard base colors +}; + +struct ColorDecomposeRecipeResult { + bool valid{false}; + ColorDecomposeRecipeMode mode{ColorDecomposeRecipeMode::MaterialList}; + std::string matched_color_hex; + std::vector components; +}; + +std::string color_decompose_rgb_to_hex(const ColorDecomposeRgb& rgb); +bool color_decompose_hex_to_rgb(const std::string& hex, ColorDecomposeRgb& out); + +ColorDecomposeRecipeResult recommend_from_physical_filaments( + const ColorDecomposeRgb& target, + const std::vector& physical_filaments, + const std::string& preferred_material_type); + +ColorDecomposeRecipeResult lookup_standard_recipe( + const ColorDecomposeRgb& target, + ColorDecomposeRecipeMode mode, + const std::string& preferred_material_type); + +// Look up the measured blend color for an exact (component_hexes, ratios) match +// in the standard color recipe table. Returns the measured hex color if found +// with reliable source data ("measured" or "interpolated"), empty string otherwise. +std::string lookup_measured_blend_color(const std::vector& component_hexes, + const std::vector& ratios); + +} // namespace Slic3r + +#endif // SLIC3R_COLOR_DECOMPOSE_RECIPE_HPP diff --git a/src/libslic3r/FilamentMixer.cpp b/src/libslic3r/FilamentMixer.cpp new file mode 100644 index 0000000000..6514e6d3d2 --- /dev/null +++ b/src/libslic3r/FilamentMixer.cpp @@ -0,0 +1,554 @@ +#include "FilamentMixer.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "ColorDecomposeRecipe.hpp" +#include "FilamentMixerModel.hpp" +#include "LocalesUtils.hpp" + +namespace Slic3r { +namespace { + +inline float clamp01(float x) +{ + return std::max(0.0f, std::min(1.0f, x)); +} + +inline float srgb_to_linear(float x) +{ + return (x >= 0.04045f) ? std::pow((x + 0.055f) / 1.055f, 2.4f) : x / 12.92f; +} + +inline float linear_to_srgb(float x) +{ + return (x >= 0.0031308f) ? (1.055f * std::pow(x, 1.0f / 2.4f) - 0.055f) : (12.92f * x); +} + +inline unsigned char to_u8(float x) +{ + const float clamped = clamp01(x); + return static_cast(clamped * 255.0f + 0.5f); +} + +inline float to_f01(unsigned char x) +{ + return static_cast(x) / 255.0f; +} + +} // namespace + +void filament_mixer_lerp(unsigned char r1, unsigned char g1, unsigned char b1, + unsigned char r2, unsigned char g2, unsigned char b2, + float t, + unsigned char* out_r, unsigned char* out_g, unsigned char* out_b) +{ + ::filament_mixer::lerp(r1, g1, b1, r2, g2, b2, t, out_r, out_g, out_b); +} + +void filament_mixer_lerp_float(float r1, float g1, float b1, + float r2, float g2, float b2, + float t, + float* out_r, float* out_g, float* out_b) +{ + unsigned char ur = 0, ug = 0, ub = 0; + filament_mixer_lerp(to_u8(r1), to_u8(g1), to_u8(b1), + to_u8(r2), to_u8(g2), to_u8(b2), + t, &ur, &ug, &ub); + *out_r = to_f01(ur); + *out_g = to_f01(ug); + *out_b = to_f01(ub); +} + +void filament_mixer_lerp_linear_float(float r1, float g1, float b1, + float r2, float g2, float b2, + float t, + float* out_r, float* out_g, float* out_b) +{ + const float sr1 = linear_to_srgb(clamp01(r1)); + const float sg1 = linear_to_srgb(clamp01(g1)); + const float sb1 = linear_to_srgb(clamp01(b1)); + const float sr2 = linear_to_srgb(clamp01(r2)); + const float sg2 = linear_to_srgb(clamp01(g2)); + const float sb2 = linear_to_srgb(clamp01(b2)); + + float out_sr = 0.0f, out_sg = 0.0f, out_sb = 0.0f; + filament_mixer_lerp_float(sr1, sg1, sb1, sr2, sg2, sb2, t, &out_sr, &out_sg, &out_sb); + + *out_r = srgb_to_linear(clamp01(out_sr)); + *out_g = srgb_to_linear(clamp01(out_sg)); + *out_b = srgb_to_linear(clamp01(out_sb)); +} + +static bool parse_hex(const std::string &hex, unsigned char &r, unsigned char &g, unsigned char &b) +{ + if (hex.size() < 7 || hex[0] != '#') return false; + unsigned rv = 0, gv = 0, bv = 0; + if (std::sscanf(hex.c_str(), "#%02x%02x%02x", &rv, &gv, &bv) != 3) return false; + r = (unsigned char)rv; g = (unsigned char)gv; b = (unsigned char)bv; + return true; +} + +std::string blend_color(const std::string& hex_a, const std::string& hex_b, float ratio_b) +{ + unsigned char r1 = 128, g1 = 128, b1 = 128; + unsigned char r2 = 128, g2 = 128, b2 = 128; + parse_hex(hex_a, r1, g1, b1); + parse_hex(hex_b, r2, g2, b2); + + unsigned char mr = 0, mg = 0, mb = 0; + filament_mixer_lerp(r1, g1, b1, r2, g2, b2, ratio_b, &mr, &mg, &mb); + + char buf[8]; + std::snprintf(buf, sizeof(buf), "#%02X%02X%02X", mr, mg, mb); + return std::string(buf); +} + +std::string blend_color_multi(const std::vector &hex_colors, + const std::vector &weights) +{ + if (hex_colors.size() >= 2 && hex_colors.size() == weights.size()) { + std::string measured = lookup_measured_blend_color(hex_colors, weights); + if (!measured.empty()) + return measured; + } + + if (hex_colors.empty()) + return "#000000"; + if (hex_colors.size() == 1) { + unsigned char cr = 128, cg = 128, cb = 128; + parse_hex(hex_colors.front(), cr, cg, cb); + char buf[8]; + std::snprintf(buf, sizeof(buf), "#%02X%02X%02X", cr, cg, cb); + return std::string(buf); + } + + assert(hex_colors.size() == weights.size()); + + unsigned char r = 128, g = 128, b = 128; + int accumulated = 0; + + for (size_t i = 0; i < hex_colors.size() && i < weights.size(); ++i) { + if (weights[i] <= 0) + continue; + unsigned char cr = 128, cg = 128, cb = 128; + parse_hex(hex_colors[i], cr, cg, cb); + if (accumulated == 0) { + r = cr; g = cg; b = cb; + accumulated = weights[i]; + } else { + const int new_total = accumulated + weights[i]; + const float t = static_cast(weights[i]) / static_cast(new_total); + filament_mixer_lerp(r, g, b, cr, cg, cb, t, &r, &g, &b); + accumulated = new_total; + } + } + + if (accumulated == 0) + return "#000000"; + + char buf[8]; + std::snprintf(buf, sizeof(buf), "#%02X%02X%02X", r, g, b); + return std::string(buf); +} + +std::vector parse_mixed_components(const std::string &str) +{ + std::vector components; + if (str.empty()) + return components; + std::istringstream ss(str); + std::string token; + while (std::getline(ss, token, ',')) { + try { + int val = std::stoi(token); + if (val >= 0) + components.push_back(static_cast(val)); + } catch (...) {} + } + return components; +} + +namespace { + +// Parse a token that may represent a finite double or "use default" (empty / "nan"). +// Returns NaN on either explicit sentinel or any parse error. +inline double parse_tangent_token(const std::string& tok) +{ + if (tok.empty()) return std::numeric_limits::quiet_NaN(); + std::string lower(tok.size(), '\0'); + std::transform(tok.begin(), tok.end(), lower.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + if (lower == "nan") return std::numeric_limits::quiet_NaN(); + try { + const double v = std::stod(tok); + if (!std::isfinite(v)) return std::numeric_limits::quiet_NaN(); + return v; + } catch (...) { + return std::numeric_limits::quiet_NaN(); + } +} + +// Split a "a,b,c,d" segment on commas, preserving empty tokens (so "0.5,0.4,," yields +// {"0.5","0.4","",""}). Used by the gradient-curve parser to distinguish NaN tangents +// from a malformed segment. +inline std::vector split_commas(const std::string& seg) +{ + std::vector out; + size_t start = 0; + while (true) { + const size_t comma = seg.find(',', start); + if (comma == std::string::npos) { + out.emplace_back(seg.substr(start)); + return out; + } + out.emplace_back(seg.substr(start, comma - start)); + start = comma + 1; + } +} + +} // namespace + +// Default Fritsch-Carlson PCHIP tangents for a sorted-by-x anchor list. m has size n +// matching the anchor count; for n == 1 the tangent is 0; for n == 2 both endpoint +// tangents equal the single secant (degenerates to linear). +std::vector compute_pchip_default_tangents(const std::vector& pts) +{ + const size_t n = pts.size(); + std::vector m(n, 0.0); + if (n < 2) return m; + + std::vector d(n - 1); + for (size_t i = 0; i + 1 < n; ++i) { + const double h = std::max(1e-12, pts[i + 1].x - pts[i].x); + d[i] = (pts[i + 1].y - pts[i].y) / h; + } + + m[0] = d[0]; + m[n - 1] = d[n - 2]; + for (size_t i = 1; i + 1 < n; ++i) + m[i] = 0.5 * (d[i - 1] + d[i]); + + // Fritsch-Carlson monotonic guard: kill flats then rescale steep tangents so the + // resulting cubic never overshoots [min, max] of the surrounding anchors. + for (size_t i = 0; i + 1 < n; ++i) { + if (d[i] == 0.0) { + m[i] = 0.0; + m[i + 1] = 0.0; + continue; + } + const double a = m[i] / d[i]; + const double b = m[i + 1] / d[i]; + const double s = a * a + b * b; + if (s > 9.0) { + const double tau = 3.0 / std::sqrt(s); + m[i] = tau * a * d[i]; + m[i + 1] = tau * b * d[i]; + } + } + return m; +} + +GradientCurve parse_gradient_curve(const std::string& s) +{ + GradientCurve curve; + if (s.empty()) + return curve; + + CNumericLocalesSetter c_locale_setter; + std::istringstream ss(s); + std::string segment; + while (std::getline(ss, segment, '|')) { + if (segment.empty()) + continue; + const auto fields = split_commas(segment); + // 2-field legacy form -> (x, y), tangents stay NaN. + // 4-field form -> (x, y, m_in, m_out), empty / "nan" tokens preserved as NaN. + if (fields.size() != 2 && fields.size() != 4) { + BOOST_LOG_TRIVIAL(warning) << "parse_gradient_curve: ignoring malformed segment \"" + << segment << "\" (expected 2 or 4 comma-separated fields, got " + << fields.size() << ")"; + continue; + } + try { + double x = std::stod(fields[0]); + double y = std::stod(fields[1]); + x = std::max(0.0, std::min(1.0, x)); + y = std::max(kGradientMinRatio, std::min(kGradientMaxRatio, y)); + GradientAnchor a; + a.x = x; + a.y = y; + if (fields.size() == 4) { + a.m_in = parse_tangent_token(fields[2]); + a.m_out = parse_tangent_token(fields[3]); + } + curve.points.push_back(a); + } catch (const std::exception& e) { + BOOST_LOG_TRIVIAL(warning) << "parse_gradient_curve: ignoring unparseable segment \"" + << segment << "\": " << e.what(); + } + } + + if (curve.points.size() < 2) { + if (!curve.points.empty()) + BOOST_LOG_TRIVIAL(warning) << "parse_gradient_curve: only " + << curve.points.size() << " valid point(s), need at least 2; discarding"; + curve.points.clear(); + return curve; + } + + std::sort(curve.points.begin(), curve.points.end(), + [](const GradientAnchor& a, const GradientAnchor& b) { + return a.x < b.x; + }); + return curve; +} + +std::string serialize_gradient_curve(const GradientCurve& c) +{ + if (c.points.empty()) + return std::string{}; + + CNumericLocalesSetter c_locale_setter; + std::string out; + char buf[128]; + for (size_t i = 0; i < c.points.size(); ++i) { + if (i > 0) out += '|'; + const auto& a = c.points[i]; + const bool has_in = std::isfinite(a.m_in); + const bool has_out = std::isfinite(a.m_out); + if (has_in || has_out) { + // Emit empty tokens for NaN slots so the legacy parser would still split + // four fields; the new parser interprets empty tokens as "use PCHIP default". + char in_buf[32] = {0}; + char out_buf[32] = {0}; + if (has_in) std::snprintf(in_buf, sizeof(in_buf), "%.4f", a.m_in); + if (has_out) std::snprintf(out_buf, sizeof(out_buf), "%.4f", a.m_out); + std::snprintf(buf, sizeof(buf), "%.4f,%.4f,%s,%s", + a.x, a.y, in_buf, out_buf); + } else { + // 4-field form is only emitted when at least one tangent is finite; the + // 2-field form is emitted otherwise so the JSON payload stays minimal + // and remains readable by older clients that only know (x, y) pairs. + std::snprintf(buf, sizeof(buf), "%.4f,%.4f", a.x, a.y); + } + out += buf; + } + return out; +} + +double sample_gradient_curve(const GradientCurve& c, double t) +{ + const auto& pts = c.points; + if (pts.size() < 2) + return 0.5; + if (t <= pts.front().x) + return pts.front().y; + if (t >= pts.back().x) + return pts.back().y; + + // PCHIP defaults are computed for every call; control point counts are typically + // tiny (< 16) so the allocation cost is negligible compared to any actual rendering + // or G-code work that drives the sampler. + const std::vector m_def = compute_pchip_default_tangents(pts); + const size_t n = pts.size(); + + // Linear scan to locate the interval [pts[i].x, pts[i+1].x] containing t. Cheap + // and avoids the upper_bound boilerplate; n is small. + for (size_t i = 1; i < n; ++i) { + const double x0 = pts[i - 1].x; + const double x1 = pts[i].x; + if (t > x1) continue; + + const double y0 = pts[i - 1].y; + const double y1 = pts[i].y; + const double h = std::max(1e-12, x1 - x0); + const double m_left = std::isfinite(pts[i - 1].m_out) ? pts[i - 1].m_out : m_def[i - 1]; + const double m_right = std::isfinite(pts[i].m_in) ? pts[i].m_in : m_def[i]; + + const double u = (t - x0) / h; + const double u2 = u * u; + const double u3 = u2 * u; + const double h00 = 2.0 * u3 - 3.0 * u2 + 1.0; + const double h10 = u3 - 2.0 * u2 + u; + const double h01 = -2.0 * u3 + 3.0 * u2; + const double h11 = u3 - u2; + double y = h00 * y0 + h10 * h * m_left + + h01 * y1 + h11 * h * m_right; + // Defensive clamp in case tangent overrides on legacy curves push the + // single-segment Hermite slightly outside the anchor band. + if (y < kGradientMinRatio) y = kGradientMinRatio; + if (y > kGradientMaxRatio) y = kGradientMaxRatio; + return y; + } + return pts.back().y; +} + +std::vector parse_mixed_ratios(const std::string &str, size_t n_components) +{ + CNumericLocalesSetter c_locale_setter; + std::vector ratios; + if (!str.empty()) { + std::istringstream ss(str); + std::string token; + while (std::getline(ss, token, ',')) { + try { + double val = std::stod(token); + if (val > 0.0) + ratios.push_back(val); + } catch (...) {} + } + } + + if (ratios.size() != n_components || n_components == 0) { + ratios.assign(n_components, n_components > 0 ? 1.0 / n_components : 0.0); + return ratios; + } + + double sum = std::accumulate(ratios.begin(), ratios.end(), 0.0); + if (sum > 0.0 && std::abs(sum - 1.0) > 1e-6) { + for (double &r : ratios) + r /= sum; + } + return ratios; +} + +bool has_any_mixed_filament(const std::vector &is_mixed) +{ + for (unsigned char v : is_mixed) + if (v) return true; + return false; +} + +std::vector check_mixed_filament_integrity( + const std::vector &is_mixed, + const std::vector &comp_strs, + size_t num_physical) +{ + std::vector broken; + for (size_t i = 0; i < is_mixed.size(); ++i) { + if (!is_mixed[i]) continue; + if (i >= comp_strs.size() || comp_strs[i].empty()) { + broken.push_back(i); + continue; + } + auto comps = parse_mixed_components(comp_strs[i]); + if (comps.size() < 2) { + broken.push_back(i); + continue; + } + for (unsigned int c : comps) { + if (c < 1 || c > num_physical) { + broken.push_back(i); + break; + } + } + } + return broken; +} + +std::vector expand_mixed_filaments( + const std::vector &extruders_0based, + const std::vector &is_mixed, + const std::vector &comp_strs) +{ + std::vector result; + for (unsigned int ext : extruders_0based) { + if (ext < is_mixed.size() && is_mixed[ext] && ext < comp_strs.size()) { + auto comps = parse_mixed_components(comp_strs[ext]); + for (unsigned int c : comps) + if (c >= 1) result.push_back(c - 1); + } else { + result.push_back(ext); + } + } + std::sort(result.begin(), result.end()); + result.erase(std::unique(result.begin(), result.end()), result.end()); + return result; +} + +void remap_mixed_components_on_delete( + const std::vector &is_mixed, + std::vector &comp_strs, + unsigned int del_1based) +{ + for (size_t i = 0; i < is_mixed.size(); ++i) { + if (!is_mixed[i]) continue; + if (i >= comp_strs.size() || comp_strs[i].empty()) continue; + + auto comps = parse_mixed_components(comp_strs[i]); + std::ostringstream ss; + for (size_t j = 0; j < comps.size(); ++j) { + if (j > 0) ss << ','; + if (comps[j] == del_1based) + ss << 0; + else if (comps[j] > del_1based) + ss << (comps[j] - 1); + else + ss << comps[j]; + } + comp_strs[i] = ss.str(); + } +} + +std::vector check_mixed_filament_type_consistency( + const std::vector &is_mixed, + const std::vector &comp_strs, + const std::vector &filament_types) +{ + std::vector result; + for (size_t i = 0; i < is_mixed.size(); ++i) { + if (!is_mixed[i]) continue; + if (i >= comp_strs.size() || comp_strs[i].empty()) continue; + auto comps = parse_mixed_components(comp_strs[i]); + if (comps.size() < 2) continue; + + std::string ref_type; + bool mismatch = false; + for (unsigned int c : comps) { + if (c == 0) continue; // sentinel for deleted component + size_t idx = static_cast(c) - 1; // 1-based -> 0-based + if (idx >= filament_types.size()) continue; + if (ref_type.empty()) + ref_type = filament_types[idx]; + else if (filament_types[idx] != ref_type) { + mismatch = true; + break; + } + } + if (mismatch) + result.push_back(i); + } + return result; +} + +void expand_mixed_slots_in_unprintables( + std::vector> &unprintables, + const std::vector &is_mixed, + const std::vector &comp_strs) +{ + for (auto &unprintable_set : unprintables) { + std::set expanded; + for (int fid : unprintable_set) { + if (fid >= 0 && (size_t)fid < is_mixed.size() && is_mixed[fid] + && (size_t)fid < comp_strs.size()) { + auto comps = parse_mixed_components(comp_strs[fid]); + for (unsigned int c : comps) + if (c >= 1) expanded.insert((int)(c - 1)); + } else { + expanded.insert(fid); + } + } + unprintable_set = std::move(expanded); + } +} + +} // namespace Slic3r diff --git a/src/libslic3r/FilamentMixer.hpp b/src/libslic3r/FilamentMixer.hpp new file mode 100644 index 0000000000..2ca4182066 --- /dev/null +++ b/src/libslic3r/FilamentMixer.hpp @@ -0,0 +1,144 @@ +#ifndef SLIC3R_FILAMENT_MIXER_HPP +#define SLIC3R_FILAMENT_MIXER_HPP + +#include +#include +#include +#include +#include + +namespace Slic3r { + +// Photoshop-style gradient curve control point in [0,1] x [0,1]. +// (x, y) is the anchor position; (m_in, m_out) are optional cubic Hermite tangent +// overrides. NaN means "use the PCHIP-computed default", which is the case for plain +// anchors loaded from old 2-field 3MF projects or freshly added via a quick click. +// A press-and-drag on a curve segment populates m_out of its left anchor and m_in of +// its right anchor so the segment bends without inserting a new anchor. +struct GradientAnchor { + double x = 0.0; + double y = 0.0; + double m_in = std::numeric_limits::quiet_NaN(); + double m_out = std::numeric_limits::quiet_NaN(); +}; + +// Sorted list of GradientAnchor; x in [0,1], y in [kGradientMinRatio, kGradientMaxRatio]. +// Empty means "no custom curve" (callers should fall back to the linear range). +struct GradientCurve { + std::vector points; + bool empty() const { return points.empty(); } +}; + +// Reserved blend ratio range. Anchor y values (= component 0's ratio) are constrained +// to this band so the mixed filament never reaches pure 0% / 100% of either physical +// component, which keeps both extruders flowing and avoids degenerate transitions. +// Both the editor and the sampler enforce this clamp. +constexpr double kGradientMinRatio = 0.1; +constexpr double kGradientMaxRatio = 0.9; + +// Parse "x0,y0[,m_in0,m_out0]|x1,y1[,m_in1,m_out1]|..." into a GradientCurve. +// (Anchors are pipe-separated; the fields within an anchor are comma-separated.) +// Accepts both the legacy 2-field form (tangents -> NaN) and the new 4-field form +// (empty token or "nan" preserved as NaN). Returns an empty curve when the input is +// empty or unparsable. Points are clamped to [0,1] for (x, y) and re-sorted by x. +GradientCurve parse_gradient_curve(const std::string& s); + +// Serialize a GradientCurve back to a string. Emits 4 fields per anchor when any +// tangent override is finite; emits 2 fields when both tangents are NaN so unchanged +// projects stay byte-identical with the legacy format. Returns "" when empty. +std::string serialize_gradient_curve(const GradientCurve& c); + +// Sample the curve at t in [0,1] using cubic Hermite with Fritsch-Carlson PCHIP +// default tangents, optionally overridden per anchor via m_in / m_out. Returns the +// clamped end values when t is outside the control point range. Returns 0.5 when the +// curve has fewer than 2 points (a safety fallback; callers should check empty()). +double sample_gradient_curve(const GradientCurve& c, double t); + +// Compute Fritsch-Carlson PCHIP default tangents for a sorted-by-x anchor list. +// Result size == pts.size(). Useful for callers that need to know what tangent the +// sampler would synthesize when m_in / m_out are NaN (e.g. the GUI's segment-bend +// interaction that inserts a virtual anchor and reads back the surrounding tangents). +std::vector compute_pchip_default_tangents(const std::vector& pts); + +void filament_mixer_lerp(unsigned char r1, unsigned char g1, unsigned char b1, + unsigned char r2, unsigned char g2, unsigned char b2, + float t, + unsigned char* out_r, unsigned char* out_g, unsigned char* out_b); + +void filament_mixer_lerp_float(float r1, float g1, float b1, + float r2, float g2, float b2, + float t, + float* out_r, float* out_g, float* out_b); + +void filament_mixer_lerp_linear_float(float r1, float g1, float b1, + float r2, float g2, float b2, + float t, + float* out_r, float* out_g, float* out_b); + +// Blend two hex colors ("#RRGGBB") by ratio (0.0 ~ 1.0 for color_b). +// Returns "#RRGGBB" string. +std::string blend_color(const std::string& hex_a, const std::string& hex_b, float ratio_b); + +// Blend N hex colors by integer weights using polynomial pigment mixing. +// Pairwise accumulation via filament_mixer_lerp. Returns "#RRGGBB". +std::string blend_color_multi(const std::vector &hex_colors, + const std::vector &weights); + +// Parse comma-separated 1-based component IDs, e.g. "1,3" → {1, 3}. +std::vector parse_mixed_components(const std::string &str); + +// Parse comma-separated ratio values, e.g. "0.7,0.3" → {0.7, 0.3}. +// Returns equal ratios (1/n each) when str is empty or invalid. +// Normalizes so the sum equals 1.0. +std::vector parse_mixed_ratios(const std::string &str, size_t n_components); + +// Returns true if any element in is_mixed is true. +// ConfigOptionBools stores values as std::vector. +bool has_any_mixed_filament(const std::vector &is_mixed); + +// Check which mixed filament slots have broken component references. +// Returns 0-based indices of mixed slots whose components reference +// filaments beyond num_physical (i.e., deleted filaments). +std::vector check_mixed_filament_integrity( + const std::vector &is_mixed, + const std::vector &comp_strs, + size_t num_physical); + +// Expand mixed filament slots in an extruder list to their physical components. +// Input/output are 0-based indices. Non-mixed slots pass through unchanged. +// Result is sorted and deduplicated. +std::vector expand_mixed_filaments( + const std::vector &extruders_0based, + const std::vector &is_mixed, + const std::vector &comp_strs); + +// Remap mixed filament component references after a physical filament is deleted. +// del_1based: the 1-based index of the deleted physical filament. +// For each mixed slot: +// - if component == del_1based -> replace with 0 (sentinel for deleted/unselected) +// - if component > del_1based -> decrement by 1 +void remap_mixed_components_on_delete( + const std::vector &is_mixed, + std::vector &comp_strs, + unsigned int del_1based); + +// Check which mixed filament slots have type-mismatched components. +// filament_types: type strings for physical filaments (0-based, size == num_physical). +// Component IDs in comp_strs are 1-based; the function converts to 0-based to look up types. +// Returns 0-based config indices of mixed slots with mismatched component types. +std::vector check_mixed_filament_type_consistency( + const std::vector &is_mixed, + const std::vector &comp_strs, + const std::vector &filament_types); + +// Expand mixed-slot IDs in geometric unprintable sets to their physical component IDs. +// Each set entry that corresponds to a mixed slot is replaced by the slot's component +// IDs (0-based). Non-mixed entries pass through unchanged. +void expand_mixed_slots_in_unprintables( + std::vector> &unprintables, + const std::vector &is_mixed, + const std::vector &comp_strs); + +} // namespace Slic3r + +#endif // SLIC3R_FILAMENT_MIXER_HPP diff --git a/src/libslic3r/FilamentMixerModel.hpp b/src/libslic3r/FilamentMixerModel.hpp new file mode 100644 index 0000000000..89b299471b --- /dev/null +++ b/src/libslic3r/FilamentMixerModel.hpp @@ -0,0 +1,819 @@ +/* + * FilamentMixer — Header-only C++ pigment color mixer + * + * Filament mixer implementation using a degree-4 polynomial regression + * trained to approximate Mixbox behavior (Mean Delta-E ~2.07). + * This library does not include Mixbox source code, binaries, or data files. + * + * Usage: + * #include "FilamentMixerModel.hpp" + * + * unsigned char r, g, b; + * filament_mixer::lerp(0, 33, 133, 252, 211, 0, 0.5f, &r, &g, &b); + * // r=47, g=141, b=56 (blue + yellow → green) + * + * No dependencies beyond the C++ standard library. + * + * MIT License + * + * Copyright (c) 2026 Justin Hayes + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef FILAMENT_MIXER_MODEL_HPP +#define FILAMENT_MIXER_MODEL_HPP + +#include +#include +#include + +namespace filament_mixer { +namespace detail { + +// BEGIN AUTO-GENERATED COEFFICIENTS +// Auto-generated by scripts/export_poly_coefficients.py +// Do not edit manually. +// Degree-4 polynomial, 330 features, 7 inputs + +static const int POLY_DEGREE = 4; +static const int N_FEATURES = 330; +static const int N_INPUTS = 7; + +static const int POWERS[330][7] = { + {0, 0, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0}, + {0, 0, 0, 1, 0, 0, 0}, + {0, 0, 0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0, 1, 0}, + {0, 0, 0, 0, 0, 0, 1}, + {2, 0, 0, 0, 0, 0, 0}, + {1, 1, 0, 0, 0, 0, 0}, + {1, 0, 1, 0, 0, 0, 0}, + {1, 0, 0, 1, 0, 0, 0}, + {1, 0, 0, 0, 1, 0, 0}, + {1, 0, 0, 0, 0, 1, 0}, + {1, 0, 0, 0, 0, 0, 1}, + {0, 2, 0, 0, 0, 0, 0}, + {0, 1, 1, 0, 0, 0, 0}, + {0, 1, 0, 1, 0, 0, 0}, + {0, 1, 0, 0, 1, 0, 0}, + {0, 1, 0, 0, 0, 1, 0}, + {0, 1, 0, 0, 0, 0, 1}, + {0, 0, 2, 0, 0, 0, 0}, + {0, 0, 1, 1, 0, 0, 0}, + {0, 0, 1, 0, 1, 0, 0}, + {0, 0, 1, 0, 0, 1, 0}, + {0, 0, 1, 0, 0, 0, 1}, + {0, 0, 0, 2, 0, 0, 0}, + {0, 0, 0, 1, 1, 0, 0}, + {0, 0, 0, 1, 0, 1, 0}, + {0, 0, 0, 1, 0, 0, 1}, + {0, 0, 0, 0, 2, 0, 0}, + {0, 0, 0, 0, 1, 1, 0}, + {0, 0, 0, 0, 1, 0, 1}, + {0, 0, 0, 0, 0, 2, 0}, + {0, 0, 0, 0, 0, 1, 1}, + {0, 0, 0, 0, 0, 0, 2}, + {3, 0, 0, 0, 0, 0, 0}, + {2, 1, 0, 0, 0, 0, 0}, + {2, 0, 1, 0, 0, 0, 0}, + {2, 0, 0, 1, 0, 0, 0}, + {2, 0, 0, 0, 1, 0, 0}, + {2, 0, 0, 0, 0, 1, 0}, + {2, 0, 0, 0, 0, 0, 1}, + {1, 2, 0, 0, 0, 0, 0}, + {1, 1, 1, 0, 0, 0, 0}, + {1, 1, 0, 1, 0, 0, 0}, + {1, 1, 0, 0, 1, 0, 0}, + {1, 1, 0, 0, 0, 1, 0}, + {1, 1, 0, 0, 0, 0, 1}, + {1, 0, 2, 0, 0, 0, 0}, + {1, 0, 1, 1, 0, 0, 0}, + {1, 0, 1, 0, 1, 0, 0}, + {1, 0, 1, 0, 0, 1, 0}, + {1, 0, 1, 0, 0, 0, 1}, + {1, 0, 0, 2, 0, 0, 0}, + {1, 0, 0, 1, 1, 0, 0}, + {1, 0, 0, 1, 0, 1, 0}, + {1, 0, 0, 1, 0, 0, 1}, + {1, 0, 0, 0, 2, 0, 0}, + {1, 0, 0, 0, 1, 1, 0}, + {1, 0, 0, 0, 1, 0, 1}, + {1, 0, 0, 0, 0, 2, 0}, + {1, 0, 0, 0, 0, 1, 1}, + {1, 0, 0, 0, 0, 0, 2}, + {0, 3, 0, 0, 0, 0, 0}, + {0, 2, 1, 0, 0, 0, 0}, + {0, 2, 0, 1, 0, 0, 0}, + {0, 2, 0, 0, 1, 0, 0}, + {0, 2, 0, 0, 0, 1, 0}, + {0, 2, 0, 0, 0, 0, 1}, + {0, 1, 2, 0, 0, 0, 0}, + {0, 1, 1, 1, 0, 0, 0}, + {0, 1, 1, 0, 1, 0, 0}, + {0, 1, 1, 0, 0, 1, 0}, + {0, 1, 1, 0, 0, 0, 1}, + {0, 1, 0, 2, 0, 0, 0}, + {0, 1, 0, 1, 1, 0, 0}, + {0, 1, 0, 1, 0, 1, 0}, + {0, 1, 0, 1, 0, 0, 1}, + {0, 1, 0, 0, 2, 0, 0}, + {0, 1, 0, 0, 1, 1, 0}, + {0, 1, 0, 0, 1, 0, 1}, + {0, 1, 0, 0, 0, 2, 0}, + {0, 1, 0, 0, 0, 1, 1}, + {0, 1, 0, 0, 0, 0, 2}, + {0, 0, 3, 0, 0, 0, 0}, + {0, 0, 2, 1, 0, 0, 0}, + {0, 0, 2, 0, 1, 0, 0}, + {0, 0, 2, 0, 0, 1, 0}, + {0, 0, 2, 0, 0, 0, 1}, + {0, 0, 1, 2, 0, 0, 0}, + {0, 0, 1, 1, 1, 0, 0}, + {0, 0, 1, 1, 0, 1, 0}, + {0, 0, 1, 1, 0, 0, 1}, + {0, 0, 1, 0, 2, 0, 0}, + {0, 0, 1, 0, 1, 1, 0}, + {0, 0, 1, 0, 1, 0, 1}, + {0, 0, 1, 0, 0, 2, 0}, + {0, 0, 1, 0, 0, 1, 1}, + {0, 0, 1, 0, 0, 0, 2}, + {0, 0, 0, 3, 0, 0, 0}, + {0, 0, 0, 2, 1, 0, 0}, + {0, 0, 0, 2, 0, 1, 0}, + {0, 0, 0, 2, 0, 0, 1}, + {0, 0, 0, 1, 2, 0, 0}, + {0, 0, 0, 1, 1, 1, 0}, + {0, 0, 0, 1, 1, 0, 1}, + {0, 0, 0, 1, 0, 2, 0}, + {0, 0, 0, 1, 0, 1, 1}, + {0, 0, 0, 1, 0, 0, 2}, + {0, 0, 0, 0, 3, 0, 0}, + {0, 0, 0, 0, 2, 1, 0}, + {0, 0, 0, 0, 2, 0, 1}, + {0, 0, 0, 0, 1, 2, 0}, + {0, 0, 0, 0, 1, 1, 1}, + {0, 0, 0, 0, 1, 0, 2}, + {0, 0, 0, 0, 0, 3, 0}, + {0, 0, 0, 0, 0, 2, 1}, + {0, 0, 0, 0, 0, 1, 2}, + {0, 0, 0, 0, 0, 0, 3}, + {4, 0, 0, 0, 0, 0, 0}, + {3, 1, 0, 0, 0, 0, 0}, + {3, 0, 1, 0, 0, 0, 0}, + {3, 0, 0, 1, 0, 0, 0}, + {3, 0, 0, 0, 1, 0, 0}, + {3, 0, 0, 0, 0, 1, 0}, + {3, 0, 0, 0, 0, 0, 1}, + {2, 2, 0, 0, 0, 0, 0}, + {2, 1, 1, 0, 0, 0, 0}, + {2, 1, 0, 1, 0, 0, 0}, + {2, 1, 0, 0, 1, 0, 0}, + {2, 1, 0, 0, 0, 1, 0}, + {2, 1, 0, 0, 0, 0, 1}, + {2, 0, 2, 0, 0, 0, 0}, + {2, 0, 1, 1, 0, 0, 0}, + {2, 0, 1, 0, 1, 0, 0}, + {2, 0, 1, 0, 0, 1, 0}, + {2, 0, 1, 0, 0, 0, 1}, + {2, 0, 0, 2, 0, 0, 0}, + {2, 0, 0, 1, 1, 0, 0}, + {2, 0, 0, 1, 0, 1, 0}, + {2, 0, 0, 1, 0, 0, 1}, + {2, 0, 0, 0, 2, 0, 0}, + {2, 0, 0, 0, 1, 1, 0}, + {2, 0, 0, 0, 1, 0, 1}, + {2, 0, 0, 0, 0, 2, 0}, + {2, 0, 0, 0, 0, 1, 1}, + {2, 0, 0, 0, 0, 0, 2}, + {1, 3, 0, 0, 0, 0, 0}, + {1, 2, 1, 0, 0, 0, 0}, + {1, 2, 0, 1, 0, 0, 0}, + {1, 2, 0, 0, 1, 0, 0}, + {1, 2, 0, 0, 0, 1, 0}, + {1, 2, 0, 0, 0, 0, 1}, + {1, 1, 2, 0, 0, 0, 0}, + {1, 1, 1, 1, 0, 0, 0}, + {1, 1, 1, 0, 1, 0, 0}, + {1, 1, 1, 0, 0, 1, 0}, + {1, 1, 1, 0, 0, 0, 1}, + {1, 1, 0, 2, 0, 0, 0}, + {1, 1, 0, 1, 1, 0, 0}, + {1, 1, 0, 1, 0, 1, 0}, + {1, 1, 0, 1, 0, 0, 1}, + {1, 1, 0, 0, 2, 0, 0}, + {1, 1, 0, 0, 1, 1, 0}, + {1, 1, 0, 0, 1, 0, 1}, + {1, 1, 0, 0, 0, 2, 0}, + {1, 1, 0, 0, 0, 1, 1}, + {1, 1, 0, 0, 0, 0, 2}, + {1, 0, 3, 0, 0, 0, 0}, + {1, 0, 2, 1, 0, 0, 0}, + {1, 0, 2, 0, 1, 0, 0}, + {1, 0, 2, 0, 0, 1, 0}, + {1, 0, 2, 0, 0, 0, 1}, + {1, 0, 1, 2, 0, 0, 0}, + {1, 0, 1, 1, 1, 0, 0}, + {1, 0, 1, 1, 0, 1, 0}, + {1, 0, 1, 1, 0, 0, 1}, + {1, 0, 1, 0, 2, 0, 0}, + {1, 0, 1, 0, 1, 1, 0}, + {1, 0, 1, 0, 1, 0, 1}, + {1, 0, 1, 0, 0, 2, 0}, + {1, 0, 1, 0, 0, 1, 1}, + {1, 0, 1, 0, 0, 0, 2}, + {1, 0, 0, 3, 0, 0, 0}, + {1, 0, 0, 2, 1, 0, 0}, + {1, 0, 0, 2, 0, 1, 0}, + {1, 0, 0, 2, 0, 0, 1}, + {1, 0, 0, 1, 2, 0, 0}, + {1, 0, 0, 1, 1, 1, 0}, + {1, 0, 0, 1, 1, 0, 1}, + {1, 0, 0, 1, 0, 2, 0}, + {1, 0, 0, 1, 0, 1, 1}, + {1, 0, 0, 1, 0, 0, 2}, + {1, 0, 0, 0, 3, 0, 0}, + {1, 0, 0, 0, 2, 1, 0}, + {1, 0, 0, 0, 2, 0, 1}, + {1, 0, 0, 0, 1, 2, 0}, + {1, 0, 0, 0, 1, 1, 1}, + {1, 0, 0, 0, 1, 0, 2}, + {1, 0, 0, 0, 0, 3, 0}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 1, 2}, + {1, 0, 0, 0, 0, 0, 3}, + {0, 4, 0, 0, 0, 0, 0}, + {0, 3, 1, 0, 0, 0, 0}, + {0, 3, 0, 1, 0, 0, 0}, + {0, 3, 0, 0, 1, 0, 0}, + {0, 3, 0, 0, 0, 1, 0}, + {0, 3, 0, 0, 0, 0, 1}, + {0, 2, 2, 0, 0, 0, 0}, + {0, 2, 1, 1, 0, 0, 0}, + {0, 2, 1, 0, 1, 0, 0}, + {0, 2, 1, 0, 0, 1, 0}, + {0, 2, 1, 0, 0, 0, 1}, + {0, 2, 0, 2, 0, 0, 0}, + {0, 2, 0, 1, 1, 0, 0}, + {0, 2, 0, 1, 0, 1, 0}, + {0, 2, 0, 1, 0, 0, 1}, + {0, 2, 0, 0, 2, 0, 0}, + {0, 2, 0, 0, 1, 1, 0}, + {0, 2, 0, 0, 1, 0, 1}, + {0, 2, 0, 0, 0, 2, 0}, + {0, 2, 0, 0, 0, 1, 1}, + {0, 2, 0, 0, 0, 0, 2}, + {0, 1, 3, 0, 0, 0, 0}, + {0, 1, 2, 1, 0, 0, 0}, + {0, 1, 2, 0, 1, 0, 0}, + {0, 1, 2, 0, 0, 1, 0}, + {0, 1, 2, 0, 0, 0, 1}, + {0, 1, 1, 2, 0, 0, 0}, + {0, 1, 1, 1, 1, 0, 0}, + {0, 1, 1, 1, 0, 1, 0}, + {0, 1, 1, 1, 0, 0, 1}, + {0, 1, 1, 0, 2, 0, 0}, + {0, 1, 1, 0, 1, 1, 0}, + {0, 1, 1, 0, 1, 0, 1}, + {0, 1, 1, 0, 0, 2, 0}, + {0, 1, 1, 0, 0, 1, 1}, + {0, 1, 1, 0, 0, 0, 2}, + {0, 1, 0, 3, 0, 0, 0}, + {0, 1, 0, 2, 1, 0, 0}, + {0, 1, 0, 2, 0, 1, 0}, + {0, 1, 0, 2, 0, 0, 1}, + {0, 1, 0, 1, 2, 0, 0}, + {0, 1, 0, 1, 1, 1, 0}, + {0, 1, 0, 1, 1, 0, 1}, + {0, 1, 0, 1, 0, 2, 0}, + {0, 1, 0, 1, 0, 1, 1}, + {0, 1, 0, 1, 0, 0, 2}, + {0, 1, 0, 0, 3, 0, 0}, + {0, 1, 0, 0, 2, 1, 0}, + {0, 1, 0, 0, 2, 0, 1}, + {0, 1, 0, 0, 1, 2, 0}, + {0, 1, 0, 0, 1, 1, 1}, + {0, 1, 0, 0, 1, 0, 2}, + {0, 1, 0, 0, 0, 3, 0}, + {0, 1, 0, 0, 0, 2, 1}, + {0, 1, 0, 0, 0, 1, 2}, + {0, 1, 0, 0, 0, 0, 3}, + {0, 0, 4, 0, 0, 0, 0}, + {0, 0, 3, 1, 0, 0, 0}, + {0, 0, 3, 0, 1, 0, 0}, + {0, 0, 3, 0, 0, 1, 0}, + {0, 0, 3, 0, 0, 0, 1}, + {0, 0, 2, 2, 0, 0, 0}, + {0, 0, 2, 1, 1, 0, 0}, + {0, 0, 2, 1, 0, 1, 0}, + {0, 0, 2, 1, 0, 0, 1}, + {0, 0, 2, 0, 2, 0, 0}, + {0, 0, 2, 0, 1, 1, 0}, + {0, 0, 2, 0, 1, 0, 1}, + {0, 0, 2, 0, 0, 2, 0}, + {0, 0, 2, 0, 0, 1, 1}, + {0, 0, 2, 0, 0, 0, 2}, + {0, 0, 1, 3, 0, 0, 0}, + {0, 0, 1, 2, 1, 0, 0}, + {0, 0, 1, 2, 0, 1, 0}, + {0, 0, 1, 2, 0, 0, 1}, + {0, 0, 1, 1, 2, 0, 0}, + {0, 0, 1, 1, 1, 1, 0}, + {0, 0, 1, 1, 1, 0, 1}, + {0, 0, 1, 1, 0, 2, 0}, + {0, 0, 1, 1, 0, 1, 1}, + {0, 0, 1, 1, 0, 0, 2}, + {0, 0, 1, 0, 3, 0, 0}, + {0, 0, 1, 0, 2, 1, 0}, + {0, 0, 1, 0, 2, 0, 1}, + {0, 0, 1, 0, 1, 2, 0}, + {0, 0, 1, 0, 1, 1, 1}, + {0, 0, 1, 0, 1, 0, 2}, + {0, 0, 1, 0, 0, 3, 0}, + {0, 0, 1, 0, 0, 2, 1}, + {0, 0, 1, 0, 0, 1, 2}, + {0, 0, 1, 0, 0, 0, 3}, + {0, 0, 0, 4, 0, 0, 0}, + {0, 0, 0, 3, 1, 0, 0}, + {0, 0, 0, 3, 0, 1, 0}, + {0, 0, 0, 3, 0, 0, 1}, + {0, 0, 0, 2, 2, 0, 0}, + {0, 0, 0, 2, 1, 1, 0}, + {0, 0, 0, 2, 1, 0, 1}, + {0, 0, 0, 2, 0, 2, 0}, + {0, 0, 0, 2, 0, 1, 1}, + {0, 0, 0, 2, 0, 0, 2}, + {0, 0, 0, 1, 3, 0, 0}, + {0, 0, 0, 1, 2, 1, 0}, + {0, 0, 0, 1, 2, 0, 1}, + {0, 0, 0, 1, 1, 2, 0}, + {0, 0, 0, 1, 1, 1, 1}, + {0, 0, 0, 1, 1, 0, 2}, + {0, 0, 0, 1, 0, 3, 0}, + {0, 0, 0, 1, 0, 2, 1}, + {0, 0, 0, 1, 0, 1, 2}, + {0, 0, 0, 1, 0, 0, 3}, + {0, 0, 0, 0, 4, 0, 0}, + {0, 0, 0, 0, 3, 1, 0}, + {0, 0, 0, 0, 3, 0, 1}, + {0, 0, 0, 0, 2, 2, 0}, + {0, 0, 0, 0, 2, 1, 1}, + {0, 0, 0, 0, 2, 0, 2}, + {0, 0, 0, 0, 1, 3, 0}, + {0, 0, 0, 0, 1, 2, 1}, + {0, 0, 0, 0, 1, 1, 2}, + {0, 0, 0, 0, 1, 0, 3}, + {0, 0, 0, 0, 0, 4, 0}, + {0, 0, 0, 0, 0, 3, 1}, + {0, 0, 0, 0, 0, 2, 2}, + {0, 0, 0, 0, 0, 1, 3}, + {0, 0, 0, 0, 0, 0, 4} +}; + +static const double COEF[330][3] = { + {8.70954844857314666e-12, 1.27926950848359881e-09, -2.06865474316332923e-09}, + {1.05783308354771544e+00, -8.02119209663359686e-03, -7.88705651445470723e-02}, + {1.35905954452774837e-02, 8.71267975138422468e-01, 1.04898760410704936e-01}, + {-4.16452026099768252e-02, 1.75465381596434100e-02, 1.00224594702931546e+00}, + {4.50321316661211821e-02, -7.11409155427628892e-02, 3.91232300778902690e-03}, + {1.76675507851922452e-02, -1.32709276116036640e-01, 6.36935270589509828e-02}, + {-5.23434830565911030e-02, 3.77681739012521722e-02, -2.08691145087504179e-02}, + {-2.33722556520224792e-03, -1.57542611462692145e-03, -3.05158628452478807e-03}, + {-8.87678609044812990e-04, 3.83194388837734693e-04, 1.37779212442523083e-03}, + {-2.11519042076831979e-03, 5.82337362515735358e-04, 2.24055108941204821e-04}, + {4.61545125563611917e-04, 7.72869451707915893e-04, -1.10800630143346882e-03}, + {1.05937484157345879e-03, -3.14448681732842211e-04, -1.75129182446198098e-03}, + {1.49045689016363055e-03, -2.09220860101674106e-04, 5.93100338908187697e-04}, + {-3.51246656293852696e-04, -8.20743017485394289e-04, 5.71854064480802862e-04}, + {-9.18204643629581319e-01, -2.27788122702773155e-01, 6.39980793022790623e-02}, + {9.24243491377523679e-05, 7.32841332381495400e-04, -1.55219718415109450e-03}, + {7.13695056804217989e-04, -8.46467621879685712e-05, 6.50202947442505750e-04}, + {1.66640864747485983e-03, -1.24492362771216523e-04, 2.68236502346156410e-04}, + {-7.20253644860527516e-04, 7.81434220384157334e-04, 1.12661089007361367e-03}, + {-6.83033334365238206e-05, 7.27742627159490762e-04, -1.78048843835204584e-03}, + {-3.13431571993316588e-02, -8.57604034845650287e-01, -2.57225920656276863e-01}, + {-6.47867200595898341e-05, -1.16688982572457655e-03, 1.14174511750260031e-03}, + {-5.00713925613324338e-04, -6.87598082111323477e-04, 6.20598069880440176e-04}, + {-8.56716727659588957e-05, 9.74478786593559361e-04, -1.65892838405139512e-03}, + {6.53468478750158263e-04, 7.51662000672516676e-04, -6.73196326298856570e-04}, + {-4.42539011000103941e-02, -2.01965359697350230e-02, -9.94663493761314355e-01}, + {-7.39107395392403087e-04, 5.28870828612476996e-04, 1.00947183860234540e-03}, + {-2.06577300933763214e-03, 9.60215813758718011e-04, -3.27993888180819421e-04}, + {3.47783280638377555e-04, 8.41824316850705743e-04, -8.87458944147930993e-04}, + {1.20960551709587905e+00, -7.07660818059813873e-02, -8.56332806008946491e-03}, + {2.11116509318935269e-04, 7.68490846994171776e-04, -1.63228995491542417e-03}, + {6.47698075356516103e-04, -4.20589129268072884e-04, 1.18354001300614896e-03}, + {-2.78795945253848716e-02, 1.22199201000304547e+00, -2.07383075858847743e-01}, + {-5.32457386680677347e-05, -9.58027320315790677e-04, 9.89667309649038679e-04}, + {-9.03932426306289782e-02, -4.00969232187064692e-02, 1.26285611182120072e+00}, + {-2.19453630740322871e-03, -1.21893190049422620e-03, -1.92293368093085417e-03}, + {1.72950845415964505e-06, -8.93952511560151819e-09, -6.14874900641340649e-06}, + {8.02644554976326974e-06, -6.42543741723487294e-06, -6.07103419227907060e-06}, + {3.20307552755319525e-06, -4.83533743093466500e-06, 9.13563764113473065e-07}, + {-2.18105804067510178e-06, 6.19595552598436322e-07, 5.21392855381760945e-06}, + {-2.43310123604345563e-06, 2.17201813434465818e-06, 1.94098874242362718e-07}, + {-1.56293672065252465e-06, 3.95256011818110372e-06, 1.68792962079201969e-06}, + {-1.37567295252127852e-03, 3.59746071987262106e-04, 7.38927139000157259e-05}, + {4.27822004137219658e-06, -8.80187479967658548e-07, 2.29453131891411977e-06}, + {7.68758937964332534e-06, 2.40909410585557829e-07, 4.69351234070854509e-06}, + {-2.87166709944317033e-06, 7.60223902901142716e-07, 4.57864913314467992e-06}, + {-4.01295140267654560e-06, 2.65929275888376483e-06, -2.36575067819565221e-06}, + {2.32693030513910805e-07, 2.28814396769890308e-06, 1.83526107699893970e-07}, + {-2.18213927011287265e-03, 1.65013083920367864e-03, 2.31992998847323087e-04}, + {-7.70829764693697905e-06, 4.23888841240673345e-07, 7.30018322002944087e-06}, + {-1.23111329452911533e-06, 1.50076529718910084e-06, -1.91139744928209288e-06}, + {-1.68872756433485760e-06, 1.03254236824697979e-06, -1.72081108163607555e-06}, + {1.64276928199709460e-06, -4.96350219553231067e-07, -1.46349385185670297e-06}, + {1.12731767057843682e-03, 5.03104281148445223e-04, 1.36398977654308994e-03}, + {-1.05449609518089293e-06, -4.06952115309007489e-07, 3.53062441379482783e-06}, + {-1.98745923822574166e-06, 4.98021943693208180e-07, 3.92645061370218429e-06}, + {-1.55569377977005097e-07, -4.00262856484093037e-07, -2.49609122397048688e-06}, + {2.18005022830924673e-03, -4.10275057064835439e-05, -2.59776311836759947e-04}, + {5.41337439827552225e-07, -1.88603932528607146e-06, -2.06428606152470051e-06}, + {-6.03243799807140491e-06, -3.75067864464502022e-06, -3.05702776851046742e-06}, + {2.30038011634901016e-03, -1.32581161861259635e-03, -1.07680096899188406e-03}, + {4.46773877910556887e-06, 1.85008408528524772e-08, -2.72851357570281713e-06}, + {-1.49177636513049289e-03, -1.91426739654176659e-04, -1.71206384332753194e-03}, + {2.31661325589237743e-02, 2.26540538563063554e-01, 5.42330337046266139e-02}, + {-1.40563059963100256e-06, -4.50551806294901061e-06, 8.87542894832671347e-06}, + {-1.66780916452391459e-06, 4.12065434881171526e-06, -3.55865035776836702e-06}, + {2.71536622051954390e-07, -3.08564858926584692e-06, -1.52164363662402047e-06}, + {2.66659632027280158e-06, -1.19436686895073481e-06, -3.25738306279285683e-06}, + {-1.43666282346327501e-06, -2.51923473623639690e-06, 5.21205120344175876e-06}, + {2.82954522469612199e-04, -1.59147454710008968e-03, 1.27685773978167098e-03}, + {-3.99471240294241303e-06, 9.97323772325767188e-08, -5.28196823261495307e-06}, + {-6.39858432699424995e-06, -4.59897864440506933e-06, -2.39736149785715891e-06}, + {2.89457420106498109e-06, -3.10427512149489757e-06, 9.75553221437691631e-07}, + {-8.96518259720091581e-07, -5.53996694461914366e-06, 1.03733964032237669e-05}, + {8.82130497168875905e-04, -2.33618402105562365e-03, 1.35100410641244379e-03}, + {-2.14088521029685841e-06, 2.59005410360388117e-06, -9.78713171504927426e-08}, + {-4.50668337071552516e-06, 3.58808570076458002e-06, -1.56159349007541082e-06}, + {-1.52345101244247272e-06, 2.21066768791959578e-06, -2.19555898547246775e-06}, + {2.07334042074768356e-03, -1.56333498489329517e-03, -5.53762940364141767e-04}, + {2.22151748134440108e-06, -4.74729938900429749e-07, -3.46744150304684889e-06}, + {2.95389009221172505e-06, -2.96312023445686329e-06, -9.00385068308695580e-07}, + {-6.47780848348620771e-04, 2.38772263398574292e-03, -8.93908589731968019e-04}, + {9.69501567645025819e-07, 2.41432205872957328e-06, 5.56908291093893837e-07}, + {-6.33392066185247586e-04, 2.38613844267241120e-03, -1.05383725637261472e-03}, + {6.76250135616376785e-02, -5.57799579151454852e-02, 1.83393652374666566e-01}, + {3.53986894266120067e-06, 5.92996717102502093e-06, -7.32378536156402804e-06}, + {5.69667193362453916e-06, 1.20219201908705218e-06, -4.56663805956276925e-06}, + {7.11494218295222192e-07, 2.93069858359131137e-06, 1.23210839732268429e-07}, + {-3.41917893741799928e-06, -1.47435291776966751e-06, 1.07397354370819542e-06}, + {7.30931882734254710e-04, 1.15433149094644884e-03, -2.40026982569019722e-03}, + {-1.22780859907432871e-06, 2.29287908084027789e-06, 1.84270754640877832e-06}, + {7.71579140080615178e-07, 2.92378122615943208e-06, -1.91800935486416413e-07}, + {-3.76107279903559188e-07, -1.83159743461489867e-06, 8.17089655984204466e-07}, + {-1.10830882430058061e-03, -5.10908079549339251e-04, -1.77835176235151705e-03}, + {-1.26839781743699406e-06, -2.86942252006448415e-06, 4.47464983859263005e-06}, + {-1.44518716284694482e-06, -7.03360635528004451e-06, 1.04898109513258675e-05}, + {-4.98687888007460470e-04, 1.86990180752567262e-03, -1.24341018156770089e-03}, + {-2.90479801332704790e-06, -9.24272269110706229e-07, 7.56354222045119151e-07}, + {-1.16451534008294149e-03, -2.34216801827852273e-03, 4.91479264672447288e-03}, + {-7.70970926241258958e-02, 9.35855573900774423e-02, 1.50623807158846906e-01}, + {1.14039905307547484e-06, -1.80664235182388840e-07, -5.15527441317074897e-06}, + {7.50559587697416375e-06, -6.23982034686780714e-06, -5.01245198064126721e-06}, + {2.37840954889385892e-06, -4.15663063190341991e-06, 1.93118829429697603e-06}, + {-1.54903048110950777e-03, 2.65832194444263125e-04, 5.34401520444913940e-04}, + {4.00040634507183718e-06, -2.43965474694277443e-06, 2.88683251413283937e-06}, + {7.72301916160400559e-06, -9.54300275625495457e-07, 5.50777546561020959e-06}, + {-2.28103126593574368e-03, 1.02658341009706066e-03, 1.22010567464172614e-03}, + {-6.32818026002207601e-06, 9.83088209200334157e-07, 5.24316808343458507e-06}, + {1.37175660779395581e-03, 4.01188715721313943e-04, 7.59370199245276625e-04}, + {-3.33184694847917573e-01, 7.82846225823195241e-02, -9.94270054263078074e-02}, + {-1.70108770909324636e-06, -5.10749831734438279e-06, 9.80267482880020635e-06}, + {-1.79301365419055891e-06, 4.44839673308561508e-06, -3.83837422072638712e-06}, + {1.71911692904483371e-04, -1.56077480341044431e-03, 1.30725115579017584e-03}, + {-3.55763938679129477e-06, 1.20558966207589408e-06, -5.94340114624253291e-06}, + {1.02325453537648178e-03, -1.52640960762801372e-03, 3.10973117856692537e-04}, + {3.81842873295820109e-03, -3.02114884453467680e-01, 2.78264587142456665e-01}, + {3.46123498726202961e-06, 5.05929187103208375e-06, -6.85764673719752027e-06}, + {4.47228353489932293e-04, 9.60672217798415784e-04, -2.19382758010531077e-03}, + {2.22711833124298791e-01, -4.14141995162802465e-02, -4.27998216564745015e-01}, + {-1.78271151817048783e-03, -9.81039111371464307e-04, -1.37513011841553174e-03}, + {3.35305394560947434e-10, -1.26710751613412498e-09, 3.54248685940916630e-09}, + {-9.26917423371698135e-09, 6.21190912597491263e-09, 1.86942252233812667e-08}, + {-1.56687696151180944e-09, -5.44315731376698864e-09, 1.93822974337010123e-09}, + {7.52897716393974292e-10, -3.48923168136394679e-10, -5.94217786087369859e-10}, + {2.52116855170569920e-10, -2.48216903975251313e-09, 1.01699001303634518e-09}, + {3.72215577457146729e-09, 4.51910314724912610e-10, -6.15361639422218332e-09}, + {-2.62088816666700142e-07, 3.23631086683010168e-07, 8.85302852722882894e-07}, + {-1.30537319842360944e-08, 1.46808588619151692e-08, 2.67574040702101001e-09}, + {-1.23991327621864045e-08, 2.61298349069072344e-08, -4.58919307373337193e-09}, + {5.03079244928983371e-09, -6.73783119575777079e-10, -1.13935871848269699e-08}, + {9.09065785148488459e-09, -1.04304054004966673e-08, -3.23123813816827976e-09}, + {9.55627910137479830e-10, -1.41129563591135820e-08, -1.75594400131373618e-09}, + {-1.05549669436946769e-07, 8.47284096194811896e-08, 6.70761880091491625e-07}, + {-5.92079330008488114e-10, 6.31702118392141188e-09, -4.51534448719925763e-09}, + {-1.04033970327321867e-09, 4.67775485013532943e-09, 2.79348504744758586e-09}, + {5.38758108958869997e-09, -9.55380699552144108e-09, 6.16488249338686956e-11}, + {1.12057409185073453e-09, -3.00645183748393663e-09, -2.14940637510707688e-09}, + {-6.27004681934967278e-07, 8.59159786402940127e-07, 2.73192537668387470e-07}, + {7.36784189214745311e-10, -8.12761968838060511e-10, -2.43226564583531868e-09}, + {1.25546123497244366e-09, -6.98609614602219153e-10, -5.29894812750786315e-09}, + {-8.88351475714088679e-10, 1.37132565025677167e-09, 1.92497813869541012e-09}, + {6.10992637326349119e-07, -6.13496367368217277e-07, -2.19901889726877020e-06}, + {-8.59090437677068053e-11, 2.72772732179404898e-09, 1.54554039011323141e-09}, + {-4.58798915525804318e-10, 4.54384851966693759e-09, 3.63189350816028877e-09}, + {9.93115786933340683e-08, 1.63700862245048928e-07, -1.71397937400244449e-07}, + {-1.62985361318312982e-09, -3.10762126448649312e-09, 1.76193495557419588e-09}, + {6.27207737564569601e-07, -1.49343052365004934e-06, 8.16168870109573730e-08}, + {1.42518738380244172e-03, -3.47531891583186285e-04, -2.98661838800559913e-04}, + {8.98157254125564464e-09, -8.24242643235328920e-09, -5.34769730234363472e-09}, + {-2.17776999489327494e-08, -4.47141107473569832e-09, -1.10218517090920898e-08}, + {3.19614509858290319e-09, -3.32861183754973311e-09, 9.92016746526047655e-11}, + {-2.91660393059167689e-09, 5.59829099744391101e-09, 1.70080685646389895e-09}, + {1.22479524179014421e-09, 9.20737683318684219e-09, -1.10618757209746121e-10}, + {7.70594587548882257e-09, -1.33267446898667659e-06, 4.52812675308736368e-07}, + {9.46080642993951670e-09, -1.95483249032513129e-08, -1.23592694620255905e-08}, + {-2.02330094345448686e-09, 1.18198534293512125e-10, 2.34746776184291406e-09}, + {4.00839940406516604e-09, -4.80716730311137042e-09, 5.25802457129742606e-09}, + {-2.53115202408782380e-09, 2.05563177591017165e-10, 5.46003270374129102e-09}, + {3.24841319972028232e-08, -1.24284705839720552e-06, 4.97326549863015555e-07}, + {1.37729661009444726e-09, -1.67903983772088594e-09, -5.62083748989472554e-09}, + {-3.53256937590806785e-10, 4.49320892992322030e-09, -4.02300486673778934e-09}, + {2.48976475547557641e-09, -6.97256366533061112e-09, 1.43185084622299286e-09}, + {-4.38617299338556199e-09, 9.45081248826811111e-08, -2.91197460585562728e-07}, + {3.24429103026879773e-09, -1.71647943601749287e-09, 2.71076100455402980e-09}, + {3.86933235105302309e-09, -2.82628156988984358e-09, 8.24455756442965537e-09}, + {-7.46614068323353530e-07, 1.27696340529665289e-06, 6.88413034833322557e-07}, + {-5.78118683480788320e-09, 1.34319005917760137e-09, -1.15898873831454807e-09}, + {4.42686972671260670e-07, 6.41810588767341775e-07, -1.16058405342719939e-08}, + {2.24399192788231686e-03, -1.35129336477888174e-03, -7.39944244498236844e-04}, + {7.47869199901884940e-09, -2.68762612165573955e-09, -7.41584788022109365e-09}, + {1.80867308283150230e-09, -2.21500551234043996e-09, 1.86995768869380186e-09}, + {-5.05514829302056157e-09, 4.74048706539109688e-09, 2.52998993977016085e-09}, + {1.32441967115592973e-09, 5.70339246663831290e-09, 7.13448300437846683e-10}, + {1.19767475292940212e-06, 6.72445227582811568e-07, -1.97500319605841551e-06}, + {-1.70612399208458498e-09, 1.07145120553653328e-09, 1.73225882249550267e-09}, + {1.15369127445807962e-09, -5.80362996549510513e-09, 9.33515653667171819e-10}, + {3.38692740520230018e-09, 3.72531013675958533e-09, -3.18062756687886861e-09}, + {1.14787653780236421e-06, -1.84917201319622368e-06, -2.44834286920736499e-07}, + {1.45558928799083276e-09, 1.12720083267348059e-09, 9.00940544390493869e-10}, + {2.09654001104286891e-09, 4.92913422578400429e-09, 3.04938074791039071e-10}, + {3.54033623213741155e-07, 1.07259516691213860e-06, -6.03027205987524684e-07}, + {-2.72038239157446071e-09, -1.60070143945256760e-09, 6.03853855807301443e-10}, + {-2.03235662485238069e-06, -1.03151962834260348e-06, 1.99637918628457062e-06}, + {-1.26261175077493210e-03, -4.98503988506484859e-04, -1.03875859619143593e-03}, + {6.43182729298530376e-10, 8.01776645076301975e-10, -1.83589794755523172e-09}, + {4.01805119037978997e-09, -5.63673552278487477e-10, -1.09102650663883693e-08}, + {-1.48648961195707585e-09, 5.01067861508053269e-09, 2.99132781045319263e-09}, + {-8.91404754824534629e-07, 7.49163968581634775e-07, 2.12542215183124383e-06}, + {2.38642574451608525e-09, -3.47605810802065207e-09, 3.86935566920598717e-10}, + {-2.80031986488182838e-09, -4.25160427697246490e-11, 2.24182921879090280e-09}, + {-1.26991357818351247e-07, -1.45348284568834647e-07, 5.68792533226815389e-07}, + {1.39227229745131353e-09, -1.84849578699353145e-09, 2.24967258190267305e-09}, + {-1.15462500328497586e-06, 1.84347590761761086e-06, 3.64918716654494962e-07}, + {-2.09357112083411985e-03, 1.60820400301404873e-05, 2.27418117008655948e-04}, + {-1.04484803378768198e-08, 4.86043558178828050e-09, 2.00996588123336650e-09}, + {1.44040971927772432e-08, 1.42223015309195233e-09, 1.99778974613318283e-09}, + {-1.62414574166394599e-07, -1.31976785339561840e-06, 4.43918084507000099e-07}, + {3.73061943836905385e-09, 1.00036822436866402e-08, -1.05450977117005351e-09}, + {-2.06551932971539565e-07, -9.72167971235462190e-07, 4.28861904300768815e-07}, + {-2.16051814014425313e-03, 1.48780488507118812e-03, 7.79940397419977911e-04}, + {-4.80544204428667854e-09, -1.09870773590259319e-09, 6.58876991984844174e-09}, + {1.31575045692056136e-06, 4.32430764481131318e-07, -1.55255090541518703e-06}, + {1.28823975640215602e-03, 4.04521283440268135e-04, 1.76186984141882253e-03}, + {-1.09767251093991436e-01, -4.94112205838347640e-02, -5.43102978164306804e-02}, + {7.93691223854864347e-10, 1.54639511196208446e-08, -1.71518303448969789e-08}, + {2.56523843833456056e-09, -2.31047392329486456e-09, -4.29758133398648601e-09}, + {-9.87725901069325118e-09, 4.28127375218245732e-09, 2.02888056355376989e-09}, + {3.21762172461603768e-10, -5.82937505211322815e-09, 3.88293127512318037e-09}, + {1.63250610252241302e-09, -7.02161705168347083e-09, 3.46592492032893329e-09}, + {-1.44272117683086343e-07, -4.40408510988914148e-07, 5.92746408872857344e-07}, + {2.71961467235293242e-09, -1.47466668633244868e-08, 2.89637452632884873e-08}, + {1.47637712476396399e-08, 1.16406781783262581e-09, 2.04904540557215853e-09}, + {-5.53709807865621073e-09, 7.05512286092169205e-09, 1.56159114805820565e-09}, + {5.29268649740455288e-09, 2.10616986628942016e-08, -3.03219004488264332e-08}, + {1.79978890693655025e-07, 7.95085399132693105e-07, -4.78366567607801940e-07}, + {-4.03847393894152251e-10, 2.90357085597214848e-09, 1.12992165623992946e-09}, + {2.99031871486832301e-09, -1.37951879780606745e-09, 2.41048263988075107e-09}, + {1.26882357398550027e-09, 1.30631467101793852e-09, 7.99574240151201820e-10}, + {-1.41169562567489137e-08, 1.27148955713198356e-06, -2.89386439707162157e-07}, + {-2.68794415198003733e-09, 8.73673404455654889e-10, 2.89557382238125882e-09}, + {-4.90264437380538709e-09, 1.89207244316591527e-09, 2.25393465003165261e-09}, + {-3.58274654665979853e-08, 2.91386646529383231e-07, -4.98477764412919022e-08}, + {1.65722165851311942e-09, -1.11673743863338615e-09, -4.14131162695952071e-09}, + {-1.47751280626939874e-07, -2.41471865000848773e-07, -8.53552350049691100e-07}, + {-2.24352957583577790e-04, 1.60900273524284708e-03, -1.32260753549593617e-03}, + {2.05497643901431104e-09, 1.38702982710459111e-08, -3.09887516689033582e-09}, + {3.39770491949997755e-09, 9.41613393506957053e-09, -7.09844738544518350e-10}, + {7.86209687630989862e-10, 1.93556837224662104e-10, -6.58630930350234678e-09}, + {-6.86841181152253455e-10, -5.57194149153339424e-09, 1.41214109156129197e-09}, + {2.59516074158083754e-07, 1.30703181255419770e-06, -4.02454784192984860e-07}, + {-5.79425202262839889e-10, 4.05071760856134944e-09, 3.02384985106929349e-09}, + {4.00677924866643664e-09, -2.25614611715219127e-09, 7.52819043214891792e-09}, + {2.34003759425061020e-09, 5.27462258592681366e-09, -2.05723854618256041e-10}, + {2.29340174767722615e-07, 1.05507868574435809e-06, -4.45904844964539748e-07}, + {-3.91634245866523401e-09, 1.07849931763048801e-09, 1.85542686770290288e-09}, + {-6.62166513287765213e-09, 3.86355018811013196e-09, -1.87861701195224384e-09}, + {1.32112240848469842e-07, 4.39339645861430705e-08, -1.59384598983486336e-06}, + {2.02488462108796341e-09, -1.48427112267590644e-09, -4.32055485832805175e-09}, + {-4.27701540045566375e-07, -1.46229443391283215e-06, -2.38186369433401879e-07}, + {-9.86744509368740232e-04, 1.91104095070606826e-03, -8.17774843405986713e-04}, + {2.06891823117949514e-10, -2.64060942556376688e-09, 1.86419366055012858e-09}, + {8.33785634979378187e-09, -1.00697171434571686e-08, -2.84106664583116952e-09}, + {5.07057938692323518e-09, -9.56246298811080919e-09, -6.33399999117045809e-11}, + {-6.78808357162941078e-08, -2.21612941845184680e-07, 9.42031624998063144e-08}, + {-3.04300065007145903e-09, 5.64120231083542478e-09, 1.65718606892628628e-09}, + {3.76240642807612602e-09, -4.58941407446844529e-09, 5.06162500801821125e-09}, + {7.25149885354159363e-07, -1.18149759075966698e-06, -6.82406347277120240e-07}, + {-4.84358128605144600e-09, 4.56893046833772853e-09, 2.67044331092591847e-09}, + {-2.54939737986958903e-07, -1.06106228658746360e-06, 5.04013386790069795e-07}, + {-2.17097468872509735e-03, 1.41624400187313607e-03, 8.11305605779899562e-04}, + {2.24635331169675823e-10, -6.02144184513875302e-09, 4.15827878380570226e-09}, + {-4.55408258326350790e-09, 6.20319154376325343e-09, 2.08760821823750220e-09}, + {2.10871853867367065e-07, -4.29346688506603014e-07, 1.15683623843482186e-07}, + {1.00732072683129559e-09, 3.88267751283422058e-11, -6.73798626615873530e-09}, + {5.34506627847264326e-09, -8.01262819982717645e-08, 1.60888846226225901e-06}, + {5.83419066552946048e-04, -2.36474094848551555e-03, 8.79373865688287898e-04}, + {-4.85158746510450101e-10, -6.78789624508624456e-09, 4.95385649168511577e-09}, + {3.47485142271342085e-07, 5.60944792101468470e-07, -4.35887910682497548e-07}, + {5.75824910919892421e-04, -2.18618554413632388e-03, 1.22736498224538170e-03}, + {-2.51838883195707221e-02, -8.23487774284355212e-02, 3.33658831723806573e-02}, + {-8.70167529698484543e-09, -1.37080219501928280e-08, 1.80728228771354082e-08}, + {-4.67111571644807100e-09, -2.72041008123058425e-09, 7.06648883852523113e-09}, + {7.26183221906172727e-10, -6.77816339167414128e-09, 4.52883232651690726e-09}, + {5.28852302228433047e-09, 6.47161005340457507e-09, -8.67298467766008940e-09}, + {-2.25465519365641853e-07, -6.46057585221293529e-07, 3.48151143400587948e-07}, + {-1.30051025504229756e-09, -3.25062288891730944e-09, 2.01775679498084060e-09}, + {-5.12724809831333062e-09, 9.33902577666956280e-10, -6.96327353416625883e-10}, + {-3.10810940873373909e-09, -7.49756534634826721e-10, 6.87357185058523612e-10}, + {-1.52109221995821997e-06, -4.22908767925417317e-07, 1.38629667568307413e-06}, + {1.42955317028459206e-09, -7.02968461219199980e-10, -3.81617160094549490e-09}, + {2.53707400921232562e-09, -1.60727622877665510e-09, -4.18765366827500429e-09}, + {-2.14750738948554787e-07, -6.40554276953864132e-07, 3.76128531993924486e-07}, + {3.83073214815787821e-09, 4.50296289838947317e-10, 2.29523194894554194e-09}, + {4.76340728555735282e-07, 6.83235613037347367e-07, -4.72205395646296822e-07}, + {-6.10651996176347607e-04, -1.06790499934057291e-03, 2.29083496655867842e-03}, + {3.95497823379997726e-09, 1.38236928154400474e-09, -6.26218820548585242e-09}, + {1.11904936705986557e-09, -1.37869946362223494e-08, -9.34049783699042457e-10}, + {1.25499246411697740e-09, -2.73635453185150368e-09, -2.91506864740637139e-09}, + {-3.59882924006599270e-07, 1.32511373732895413e-06, -1.55110207063907657e-07}, + {1.07068498511608823e-09, 8.92087770321126072e-09, 2.62826524433101838e-10}, + {-2.69316546841480431e-09, 9.61138280075601870e-10, 5.19946977139973399e-09}, + {-5.92563579700916554e-07, -1.05071339539294234e-06, 1.56249964602256375e-07}, + {1.32198180180509439e-09, 5.16087961255351502e-09, 8.46339526239248130e-10}, + {2.07323220008381881e-06, 1.02309267446332522e-06, -2.07661522726165781e-06}, + {1.31402366846389393e-03, 3.78229792813366064e-04, 1.77496793932758741e-03}, + {8.59301428624004160e-10, -6.83071707530125138e-09, 3.36249680876754553e-09}, + {5.27310424491833629e-09, 2.09999085065692981e-08, -3.10459945807028959e-08}, + {-8.88666080375855039e-08, 4.60897593930476024e-07, 7.41576575386676540e-07}, + {-4.85540663230921155e-10, -5.58243438975036810e-09, 7.40450811775872353e-10}, + {4.03141117225058743e-07, 1.52035531639227450e-06, 9.06206514897367477e-08}, + {5.61075629915620496e-04, -2.05847905628765053e-03, 1.12849817492909434e-03}, + {5.11216541321246609e-09, 7.26292920250060092e-09, -8.97145741030058730e-09}, + {-4.26211688914213127e-07, -7.03366608210270750e-07, 6.27995585866791828e-07}, + {1.15309052943982646e-03, 2.34474318844151959e-03, -4.91856748507475423e-03}, + {1.01104427799588961e-01, -4.22361682938472982e-02, -1.88750007538552200e-01}, + {3.94738332298860684e-10, -7.81372397340440727e-10, 4.06815717224340290e-09}, + {-8.61483928638051566e-09, 5.37427180535843263e-09, 1.81738104426676372e-08}, + {-8.48011268844706123e-10, -5.33803143354383280e-09, 2.99703953494934172e-10}, + {3.89154099408092063e-07, -2.44166311268514957e-07, -8.03240371135063858e-07}, + {-1.20249536439409610e-08, 1.48908931921210019e-08, 1.88292573199966284e-09}, + {-1.16401289163015065e-08, 2.57866422936903206e-08, -5.27022399332555125e-09}, + {1.37065399911928676e-07, 2.16494406102361175e-08, -7.63924557662179482e-07}, + {-6.94754161319199870e-10, 6.65038621394664631e-09, -4.31779645371221932e-09}, + {4.72542155592614588e-07, -7.58546986886782931e-07, -2.35913417925837088e-07}, + {1.46133817312113241e-03, -3.25193103208258009e-04, -3.06625181254991741e-04}, + {9.35794082672593210e-09, -7.92923574022275091e-09, -5.41426242728348939e-09}, + {-2.15279239157428748e-08, -4.16754339024882903e-09, -1.12896482995505920e-08}, + {2.60645369870582400e-10, 1.44616071127263122e-06, -3.63334053799999057e-07}, + {9.17105741349288905e-09, -2.02295233654725681e-08, -1.20002956877085509e-08}, + {-1.27759226226098477e-07, 1.28193771791124470e-06, -5.83097827522305323e-07}, + {2.26880791869919426e-03, -1.34042850080092401e-03, -7.65092051285704835e-04}, + {7.03374036792325796e-09, -2.53508958270032281e-09, -7.66132998708535240e-09}, + {-9.71978722189015265e-07, -5.57836512454779054e-07, 1.96329328074063003e-06}, + {-1.26115140811304343e-03, -4.81792074617704632e-04, -1.06803272537897391e-03}, + {1.19419564863885497e-01, 5.07766738901840875e-02, 4.87642090320925953e-02}, + {1.14090414893297520e-09, 1.56073433760228752e-08, -1.78054684078429726e-08}, + {3.03285130343056153e-09, -1.58615337531031741e-09, -4.94928394101368241e-09}, + {2.64483280249840080e-07, 2.97155396291660413e-07, -5.41608085095034164e-07}, + {2.68757552324139226e-09, -1.41400907649469332e-08, 2.93255796729452456e-08}, + {-2.11094617584561828e-07, -6.56355695552793272e-07, 3.72180321686621518e-07}, + {-2.55073452371079590e-04, 1.57943859317488818e-03, -1.29154484940938240e-03}, + {1.40049266628139435e-09, 1.40747080656922208e-08, -2.58792021839981956e-09}, + {-2.12330362681090179e-07, -1.30522733223815968e-06, 5.84417623253341567e-07}, + {-9.33144849909676392e-04, 1.90305575962152547e-03, -8.35564417983726418e-04}, + {1.81624805201406961e-02, 6.84911174969819458e-02, -2.28291882522520390e-02}, + {-8.25231299961259879e-09, -1.40227519596081152e-08, 1.78809529925716415e-08}, + {1.90689491530449118e-07, 7.01057736002264065e-07, -4.26430629252294580e-07}, + {-5.85146839837499930e-04, -1.07311215649546045e-03, 2.31986890222730339e-03}, + {-1.05962397073886522e-01, 5.51532131360410807e-02, 1.87542648909451215e-01}, + {-1.37499370823599516e-03, -8.49619409242363438e-04, -1.18180356709159952e-03} +}; + +static const double INTERCEPT[3] = { + -1.29208772400146188e+00, + 6.62251952866635918e+00, + -1.35908984683965173e-01 +}; +// END AUTO-GENERATED COEFFICIENTS + +inline void compute_poly_features(const double x[7], double out[330]) { + for (int i = 0; i < N_FEATURES; ++i) { + double val = 1.0; + for (int j = 0; j < N_INPUTS; ++j) { + if (POWERS[i][j] != 0) { + double base = x[j]; + int exp = POWERS[i][j]; + // Fast integer exponentiation (max exp = 4) + double p = 1.0; + for (int e = 0; e < exp; ++e) + p *= base; + val *= p; + } + } + out[i] = val; + } +} + +} // namespace detail + +struct RGB { + unsigned char r, g, b; +}; + +/** + * Mix two RGB colors using polynomial pigment mixing. + * + * This performs polynomial pigment-style RGB interpolation. + * + * @param r1,g1,b1 First color (0-255) + * @param r2,g2,b2 Second color (0-255) + * @param t Mixing ratio: 0.0 = all color1, 1.0 = all color2 + * @param out_r,out_g,out_b Output color (0-255) + */ +inline void lerp(unsigned char r1, unsigned char g1, unsigned char b1, + unsigned char r2, unsigned char g2, unsigned char b2, + float t, + unsigned char* out_r, unsigned char* out_g, unsigned char* out_b) { + // Clamp t + if (t <= 0.0f) { + *out_r = r1; *out_g = g1; *out_b = b1; + return; + } + if (t >= 1.0f) { + *out_r = r2; *out_g = g2; *out_b = b2; + return; + } + + double x[7] = { + static_cast(r1), static_cast(g1), static_cast(b1), + static_cast(r2), static_cast(g2), static_cast(b2), + static_cast(t) + }; + + double features[330]; + detail::compute_poly_features(x, features); + + // Dot product: features @ COEF + INTERCEPT + for (int c = 0; c < 3; ++c) { + double sum = detail::INTERCEPT[c]; + for (int i = 0; i < detail::N_FEATURES; ++i) { + sum += features[i] * detail::COEF[i][c]; + } + // Clamp to [0, 255] and truncate (matches numpy astype(int) behavior) + int val = static_cast(sum); + if (val < 0) val = 0; + if (val > 255) val = 255; + + if (c == 0) *out_r = static_cast(val); + else if (c == 1) *out_g = static_cast(val); + else *out_b = static_cast(val); + } +} + +/** + * Convenience overload returning an RGB struct. + */ +inline RGB lerp(unsigned char r1, unsigned char g1, unsigned char b1, + unsigned char r2, unsigned char g2, unsigned char b2, + float t) { + RGB result; + lerp(r1, g1, b1, r2, g2, b2, t, &result.r, &result.g, &result.b); + return result; +} + +} // namespace filament_mixer + +#endif // FILAMENT_MIXER_MODEL_HPP diff --git a/src/libslic3r/Format/OBJ.cpp b/src/libslic3r/Format/OBJ.cpp index 71f7d1e7e2..50826924f4 100644 --- a/src/libslic3r/Format/OBJ.cpp +++ b/src/libslic3r/Format/OBJ.cpp @@ -1,6 +1,8 @@ #include "../libslic3r.h" #include "../Model.hpp" #include "../TriangleMesh.hpp" +#include "../TexturePainting.hpp" +#include "ResourcePathUtils.hpp" #include "OBJ.hpp" #include "objparser.hpp" @@ -21,7 +23,7 @@ namespace Slic3r { -bool load_obj(const char *path, TriangleMesh *meshptr, ObjInfo& obj_info, std::string &message) +bool load_obj(const char *path, TriangleMesh *meshptr, ObjInfo& obj_info, std::string &message, ObjParser::MtlData *out_mtl) { if (meshptr == nullptr) return false; @@ -98,6 +100,7 @@ bool load_obj(const char *path, TriangleMesh *meshptr, ObjInfo& obj_info, std::s its.indices.reserve(num_faces + num_quads); if (exist_mtl) { obj_info.is_single_mtl = data.usemtls.size() == 1 && mtl_data.new_mtl_unmap.size() == 1; + obj_info.usemtls = data.usemtls; obj_info.face_colors.reserve(num_faces + num_quads); } bool has_color = data.has_vertex_color; @@ -210,14 +213,17 @@ bool load_obj(const char *path, TriangleMesh *meshptr, ObjInfo& obj_info, std::s } if (meshptr->volume() < 0) meshptr->flip_triangles(); + // Hand the parsed material table back so callers can build a TexturedMesh from it. + if (out_mtl) + *out_mtl = mtl_data; return true; } -bool load_obj(const char *path, Model *model, ObjInfo& obj_info, std::string &message, const char *object_name_in) +bool load_obj(const char *path, Model *model, ObjInfo& obj_info, std::string &message, const char *object_name_in, ObjParser::MtlData *out_mtl) { TriangleMesh mesh; - bool ret = load_obj(path, &mesh, obj_info, message); + bool ret = load_obj(path, &mesh, obj_info, message, out_mtl); if (ret) { std::string object_name; @@ -232,6 +238,144 @@ bool load_obj(const char *path, Model *model, ObjInfo& obj_info, std::string &me return ret; } +bool obj_to_textured_mesh( + const ObjInfo& obj_info, + const indexed_triangle_set& its, + const ObjParser::MtlData& mtl_data, + const std::string& obj_directory, + TexturedMesh& out) +{ + if (its.vertices.empty() || its.indices.empty() || !obj_info.has_uv_png) + return false; + + const size_t nv = its.vertices.size(); + const size_t nf = its.indices.size(); + + // 1. Copy vertices + out.vertices.resize(nv); + for (size_t i = 0; i < nv; ++i) + out.vertices[i] = {its.vertices[i].x(), its.vertices[i].y(), its.vertices[i].z()}; + + // 2. Copy face indices + out.indices.resize(nf); + for (size_t i = 0; i < nf; ++i) + out.indices[i] = {its.indices[i][0], its.indices[i][1], its.indices[i][2]}; + + // 3. Build per-face UV (uv_coords + uv_indices) + // OBJ UV convention: V=0 at bottom (OpenGL); texture sampling expects V=0 at top (like glTF/OpenCV). + // Flip V here so downstream code works uniformly. + if (!obj_info.uvs.empty()) { + const size_t uv_face_count = obj_info.uvs.size(); + out.uv_coords.resize(uv_face_count * 3); + out.uv_indices.resize(nf); + for (size_t fi = 0; fi < nf; ++fi) { + if (fi < uv_face_count) { + int base = static_cast(fi * 3); + out.uv_coords[base + 0] = {obj_info.uvs[fi][0].x(), 1.f - obj_info.uvs[fi][0].y()}; + out.uv_coords[base + 1] = {obj_info.uvs[fi][1].x(), 1.f - obj_info.uvs[fi][1].y()}; + out.uv_coords[base + 2] = {obj_info.uvs[fi][2].x(), 1.f - obj_info.uvs[fi][2].y()}; + out.uv_indices[fi] = {base, base + 1, base + 2}; + } else { + out.uv_indices[fi] = {0, 0, 0}; + } + } + } + + // 4. Build material list and load textures from disk + // Map: material name -> material index + std::map mtl_name_to_idx; + for (size_t i = 0; i < mtl_data.mtl_orders.size(); ++i) + mtl_name_to_idx[mtl_data.mtl_orders[i]] = static_cast(i); + + const int num_materials = static_cast(mtl_data.mtl_orders.size()); + out.material_colors.resize(num_materials, {1.f, 1.f, 1.f, 1.f}); + out.material_texture_map.resize(num_materials, -1); + + // Map: texture filename -> index in out.textures + std::map png_to_tex_idx; + + for (int mi = 0; mi < num_materials; ++mi) { + const std::string& name = mtl_data.mtl_orders[mi]; + auto it = mtl_data.new_mtl_unmap.find(name); + if (it == mtl_data.new_mtl_unmap.end()) + continue; + const auto& mtl = *(it->second); + + // Material color from Kd + out.material_colors[mi] = {mtl.Kd[0], mtl.Kd[1], mtl.Kd[2], mtl.Tr}; + + // Texture from map_Kd + if (mtl.map_Kd.empty()) + continue; + + auto tex_it = png_to_tex_idx.find(mtl.map_Kd); + if (tex_it != png_to_tex_idx.end()) { + out.material_texture_map[mi] = tex_it->second; + continue; + } + + // Resolve texture file path. + const boost::filesystem::path requested_tex_path(mtl.map_Kd); + const boost::filesystem::path tex_path = requested_tex_path.is_absolute() ? + resource_path::resolve_existing_path_case_insensitive(requested_tex_path, "obj_to_textured_mesh: map_Kd") : + resource_path::resolve_existing_relative_path_case_insensitive( + boost::filesystem::path(obj_directory), requested_tex_path, "obj_to_textured_mesh: map_Kd"); + + if (tex_path.empty()) { + BOOST_LOG_TRIVIAL(warning) << "obj_to_textured_mesh: texture not found: " << requested_tex_path; + continue; + } + + // Read raw file bytes + boost::nowide::ifstream file(tex_path.string(), std::ios::binary | std::ios::ate); + if (!file.is_open()) + continue; + auto file_size = file.tellg(); + if (file_size <= 0) + continue; + file.seekg(0, std::ios::beg); + + TextureImage ti; + ti.data.resize(static_cast(file_size)); + file.read(reinterpret_cast(ti.data.data()), file_size); + ti.width = -1; + ti.height = -1; + ti.channels = 0; + + int new_idx = static_cast(out.textures.size()); + out.textures.push_back(std::move(ti)); + png_to_tex_idx[mtl.map_Kd] = new_idx; + out.material_texture_map[mi] = new_idx; + } + + // 5. Build per-face material_ids from usemtls ranges + out.material_ids.resize(nf, -1); + if (!obj_info.usemtls.empty()) { + for (size_t fi = 0; fi < nf; ++fi) { + int face_idx = static_cast(fi); + for (size_t k = 0; k < obj_info.usemtls.size(); ++k) { + const auto& um = obj_info.usemtls[k]; + if (face_idx >= um.face_start && face_idx <= um.face_end) { + auto name_it = mtl_name_to_idx.find(um.name); + if (name_it != mtl_name_to_idx.end()) + out.material_ids[fi] = name_it->second; + break; + } + } + } + } + + if (out.textures.empty()) { + BOOST_LOG_TRIVIAL(warning) << "obj_to_textured_mesh: no textures loaded"; + return false; + } + + BOOST_LOG_TRIVIAL(info) << "obj_to_textured_mesh: " << nf << " faces, " + << out.textures.size() << " textures, " + << num_materials << " materials"; + return true; +} + bool store_obj(const char *path, TriangleMesh *mesh) { //FIXME returning false even if write failed. diff --git a/src/libslic3r/Format/OBJ.hpp b/src/libslic3r/Format/OBJ.hpp index 2d4370c99a..7338fe0813 100644 --- a/src/libslic3r/Format/OBJ.hpp +++ b/src/libslic3r/Format/OBJ.hpp @@ -1,6 +1,7 @@ #ifndef slic3r_Format_OBJ_hpp_ #define slic3r_Format_OBJ_hpp_ #include "libslic3r/Color.hpp" +#include "objparser.hpp" #include namespace Slic3r { @@ -18,6 +19,7 @@ struct ObjInfo { std::map pngs; std::unordered_map uv_map_pngs; bool has_uv_png{false}; + std::vector usemtls; // material spans, for texture import }; struct ObjDialogInOut @@ -32,8 +34,18 @@ struct ObjDialogInOut std::string lost_material_name{""}; }; typedef std::function ObjImportColorFn; -extern bool load_obj(const char *path, TriangleMesh *mesh, ObjInfo &vertex_colors, std::string &message); -extern bool load_obj(const char *path, Model *model, ObjInfo &vertex_colors, std::string &message, const char *object_name = nullptr); +extern bool load_obj(const char *path, TriangleMesh *mesh, ObjInfo &vertex_colors, std::string &message, ObjParser::MtlData *out_mtl = nullptr); +extern bool load_obj(const char *path, Model *model, ObjInfo &vertex_colors, std::string &message, const char *object_name = nullptr, ObjParser::MtlData *out_mtl = nullptr); + +struct TexturedMesh; +// Build a TexturedMesh (vertices + per-face UVs + decoded texture images) from a parsed OBJ +// plus its material table, so the texture-to-color importer can sample face colours. +extern bool obj_to_textured_mesh( + const ObjInfo& obj_info, + const indexed_triangle_set& its, + const ObjParser::MtlData& mtl_data, + const std::string& obj_directory, + TexturedMesh& out); extern bool store_obj(const char *path, TriangleMesh *mesh); extern bool store_obj(const char *path, ModelObject *model); diff --git a/src/libslic3r/Format/ResourcePathUtils.hpp b/src/libslic3r/Format/ResourcePathUtils.hpp new file mode 100644 index 0000000000..d82b92bd45 --- /dev/null +++ b/src/libslic3r/Format/ResourcePathUtils.hpp @@ -0,0 +1,240 @@ +#ifndef slic3r_Format_ResourcePathUtils_hpp_ +#define slic3r_Format_ResourcePathUtils_hpp_ + +#include +#include +#include +#include +#include + +#include +#include + +namespace Slic3r { +namespace resource_path { + +inline std::string ascii_lower_copy(const std::string& value) +{ + std::string lowered; + lowered.reserve(value.size()); + for (unsigned char ch : value) + lowered.push_back(static_cast(std::tolower(ch))); + return lowered; +} + +inline boost::filesystem::path portable_path_copy(const boost::filesystem::path& value) +{ + std::string portable = value.string(); + std::replace(portable.begin(), portable.end(), '\\', '/'); + return boost::filesystem::path(portable); +} + +inline int hex_digit_value(char ch) +{ + if (ch >= '0' && ch <= '9') return ch - '0'; + if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; + if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; + return -1; +} + +// Byte-level percent decoding. Per RFC 3986 the %XX byte stream is expected to be +// UTF-8 when produced from URIs / Assimp aiString; this function performs no +// transcoding, so callers must treat both input and output as raw UTF-8 bytes. +inline std::string percent_decode_copy(const std::string& value) +{ + std::string decoded; + decoded.reserve(value.size()); + for (std::size_t i = 0; i < value.size(); ++i) { + if (value[i] == '%' && i + 2 < value.size()) { + const int hi = hex_digit_value(value[i + 1]); + const int lo = hex_digit_value(value[i + 2]); + if (hi >= 0 && lo >= 0) { + decoded.push_back(static_cast((hi << 4) | lo)); + i += 2; + continue; + } + } + decoded.push_back(value[i]); + } + return decoded; +} + +inline std::string strip_file_uri_prefix_copy(const std::string& value) +{ + const std::string lower = ascii_lower_copy(value); + if (lower.rfind("file://", 0) != 0) + return value; + + std::string path = value.substr(7); + if (ascii_lower_copy(path).rfind("localhost/", 0) == 0) + path.erase(0, std::string("localhost").size()); + else if (!path.empty() && path.front() != '/') + path = "//" + path; + + // file:///C:/... should become C:/..., while file:///tmp/... keeps /tmp/... + if (path.size() >= 3 && path[0] == '/' && std::isalpha(static_cast(path[1])) && path[2] == ':') + path.erase(path.begin()); + return path; +} + +inline bool file_uri_has_remote_authority(const std::string& value) +{ + const std::string lower = ascii_lower_copy(value); + if (lower.rfind("file://", 0) != 0) + return false; + + const std::string path = value.substr(7); + if (path.empty() || path.front() == '/') + return false; + + const std::size_t slash = path.find('/'); + const std::string authority = path.substr(0, slash); + return ascii_lower_copy(authority) != "localhost"; +} + +inline bool looks_like_windows_absolute_path(const boost::filesystem::path& path) +{ + const std::string portable = portable_path_copy(path).string(); + return portable.size() >= 3 + && std::isalpha(static_cast(portable[0])) + && portable[1] == ':' + && portable[2] == '/'; +} + +inline boost::filesystem::path filename_from_portable_path(const boost::filesystem::path& value) +{ + const boost::filesystem::path portable = portable_path_copy(value); + return portable.filename(); +} + +inline boost::filesystem::path find_child_case_insensitive( + const boost::filesystem::path& directory, + const boost::filesystem::path& requested_name, + const char* context) +{ + if (!boost::filesystem::exists(directory) || !boost::filesystem::is_directory(directory)) + return {}; + + const std::string requested_lower = ascii_lower_copy(requested_name.filename().string()); + std::vector matches; + + boost::system::error_code ec; + for (boost::filesystem::directory_iterator it(directory, ec), end; !ec && it != end; it.increment(ec)) { + if (ascii_lower_copy(it->path().filename().string()) == requested_lower) + matches.push_back(it->path()); + } + + if (matches.size() == 1) + return matches.front(); + + if (matches.size() > 1) { + BOOST_LOG_TRIVIAL(warning) << context << ": ambiguous case-insensitive resource match for " + << requested_name << " in " << directory; + } + + return {}; +} + +inline boost::filesystem::path resolve_existing_path_case_insensitive( + const boost::filesystem::path& requested_path, + const char* context = "resource_path") +{ + const boost::filesystem::path normalized_path = portable_path_copy(requested_path); + + if (normalized_path.empty()) + return {}; + + if (boost::filesystem::exists(normalized_path)) + return normalized_path; + + boost::filesystem::path current; + bool initialized = false; + + for (const boost::filesystem::path& part : normalized_path) { + if (part == normalized_path.root_name() || part == normalized_path.root_directory()) { + current /= part; + initialized = true; + continue; + } + + if (!initialized) { + current = boost::filesystem::current_path(); + initialized = true; + } + + boost::filesystem::path exact = current / part; + if (boost::filesystem::exists(exact)) { + current = exact; + continue; + } + + boost::filesystem::path matched = find_child_case_insensitive(current, part, context); + if (matched.empty()) + return {}; + + BOOST_LOG_TRIVIAL(info) << context << ": resolved resource path case-insensitively from " + << exact << " to " << matched; + current = matched; + } + + return boost::filesystem::exists(current) ? current : boost::filesystem::path(); +} + +inline boost::filesystem::path resolve_existing_relative_path_case_insensitive( + const boost::filesystem::path& base_dir, + const boost::filesystem::path& resource_path, + const char* context = "resource_path") +{ + const boost::filesystem::path requested = resource_path.is_absolute() ? resource_path : base_dir / resource_path; + return resolve_existing_path_case_insensitive(requested, context); +} + +// Resolve a resource path that originated outside our own code (e.g. a glTF/FBX +// material texture reference or a file:// URI inside a 3MF descriptor). +// +// `raw_path` is expected to be UTF-8 regardless of host platform: file URIs are +// UTF-8 by spec, and Assimp aiString uses UTF-8 internally. Cross-platform +// correctness on Windows additionally relies on the process having called +// boost::nowide::nowide_filesystem() during startup (see src/BambuStudio.cpp), +// which imbues boost::filesystem::path with a UTF-8 codecvt so that +// `path(std::string)` constructs from UTF-8 byte sequences. Callers that bypass +// the main entry point (standalone CLI tools, unit tests) must reproduce that +// setup themselves before invoking this helper. +inline boost::filesystem::path resolve_external_resource_path( + const boost::filesystem::path& base_dir, + const std::string& raw_path, + const char* context = "resource_path", + bool allow_basename_fallback = true) +{ + if (raw_path.empty()) + return {}; + + const bool remote_file_uri = file_uri_has_remote_authority(raw_path); + const std::string decoded_path = percent_decode_copy(strip_file_uri_prefix_copy(raw_path)); + const boost::filesystem::path requested = portable_path_copy(boost::filesystem::path(decoded_path)); + + boost::filesystem::path resolved = (requested.is_absolute() || looks_like_windows_absolute_path(requested)) ? + resolve_existing_path_case_insensitive(requested, context) : + resolve_existing_relative_path_case_insensitive(base_dir, requested, context); + if (!resolved.empty()) + return resolved; + + if (!allow_basename_fallback || remote_file_uri) + return {}; + + const boost::filesystem::path basename = filename_from_portable_path(requested); + if (basename.empty()) + return {}; + + resolved = resolve_existing_relative_path_case_insensitive(base_dir, basename, context); + if (!resolved.empty()) { + BOOST_LOG_TRIVIAL(info) << context << ": resolved resource by basename from " + << requested << " to " << resolved; + } + return resolved; +} + +} // namespace resource_path +} // namespace Slic3r + +#endif /* slic3r_Format_ResourcePathUtils_hpp_ */ diff --git a/src/libslic3r/Format/objparser.cpp b/src/libslic3r/Format/objparser.cpp index 82bf2b4963..6ee117adc9 100644 --- a/src/libslic3r/Format/objparser.cpp +++ b/src/libslic3r/Format/objparser.cpp @@ -394,6 +394,7 @@ static bool mtl_parseline(const char *line, MtlData &data) ObjNewMtl new_mtl; cur_mtl_name = line; data.new_mtl_unmap[cur_mtl_name] = std::make_shared(); + data.mtl_orders.emplace_back(cur_mtl_name); break; } case 'm': { diff --git a/src/libslic3r/Format/objparser.hpp b/src/libslic3r/Format/objparser.hpp index 48493de3de..58afd015a8 100644 --- a/src/libslic3r/Format/objparser.hpp +++ b/src/libslic3r/Format/objparser.hpp @@ -122,6 +122,9 @@ struct MtlData // Version of the data structure for load / store in the private binary format. int version; std::unordered_map> new_mtl_unmap; + // Material names in declaration order. new_mtl_unmap is unordered, but OBJ material + // indices are positional, so texture import needs the original order. + std::vector mtl_orders; }; extern bool objparse(const char *path, ObjData &data); extern bool mtlparse(const char *path, MtlData &data); diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 18d805936e..ba26f7f0da 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -6557,6 +6557,318 @@ LayerResult GCode::process_layer( } } } + + // Mixed-color sublayer extrusion: if this extruder is a component of a mixed sublayer + // group, extrude the mixed slot's geometry at the appropriate sub-Z with scaled flow. + // Ported from BambuStudio's 混色耗材 feature; adapted to Orca's InstanceVisit-based + // instance loop and its finer-grained per-role region filament options. + for (const auto &grp : layer_tools.mixed_sub_layer_groups) { + int sub_idx = -1; + for (size_t k = 0; k < grp.components_0based.size(); ++k) { + if (grp.components_0based[k] == extruder_id) { + sub_idx = static_cast(k); + break; + } + } + if (sub_idx < 0) + continue; + + auto mixed_instances_it = filament_to_print_instances.find(grp.mixed_slot_0based); + if (mixed_instances_it == filament_to_print_instances.end() || mixed_instances_it->second.first.empty()) + continue; + + double lh = grp.layer_height > 0. ? grp.layer_height : static_cast(height); + double cumulative_h = 0.0; + for (int i = 0; i < sub_idx; ++i) + cumulative_h += grp.sub_heights[i]; + double default_sub_h = grp.sub_heights[sub_idx]; + double default_sub_z = print_z - lh + cumulative_h + default_sub_h; + + m_sub_layer_flow_ratio = default_sub_h / lh; + m_sub_layer_height = default_sub_h; + m_nominal_z = default_sub_z; + + gcode += this->set_extruder(extruder_id, default_sub_z); + + for (InstanceToPrint &instance_to_print : mixed_instances_it->second.first) { + const bool use_per_volume = grp.is_gradient + && !grp.per_volume_gradient.empty() + && std::any_of(grp.per_volume_gradient.begin(), grp.per_volume_gradient.end(), + [&](const auto &kv) { return kv.first.obj == &instance_to_print.print_object; }); + + // --- Shared instance preamble (mirrors Orca's main instance loop) --- + const LayerToPrint &layer_to_print = layers[instance_to_print.layer_id]; + const auto &inst = instance_to_print.print_object.instances()[instance_to_print.instance_id]; + + bool object_layer_over_raft = layer_to_print.object_layer && layer_to_print.object_layer->id() > 0 && + instance_to_print.print_object.slicing_parameters().raft_layers() == layer_to_print.object_layer->id(); + m_config.apply(print.default_region_config()); + m_config.apply(instance_to_print.print_object.config(), true); + m_layer = layer_to_print.layer(); + m_object_layer_over_raft = object_layer_over_raft; + if (m_config.reduce_crossing_wall) + m_avoid_crossing_perimeters.init_layer(*m_layer); + + if (this->config().gcode_label_objects) { + gcode += std::string("; printing object ") + instance_to_print.print_object.model_object()->name + + " id:" + std::to_string(instance_to_print.print_object.get_id()) + " copy " + + std::to_string(inst.id) + "\n"; + } + if (m_enable_exclude_object) { + if (is_BBL_Printer()) { + m_writer.set_object_start_str( + std::string("; start printing object, unique label id: ") + + std::to_string(instance_to_print.label_object_id) + "\n" + "M624 " + + _encode_label_ids_to_base64({instance_to_print.label_object_id}) + "\n"); + } else { + const auto gflavor = print.config().gcode_flavor.value; + if (gflavor == gcfKlipper) { + m_writer.set_object_start_str(std::string("EXCLUDE_OBJECT_START NAME=") + + get_instance_name(&instance_to_print.print_object, inst.id) + "\n"); + } else if (gflavor == gcfMarlinLegacy || gflavor == gcfMarlinFirmware || gflavor == gcfRepRapFirmware) { + m_writer.set_object_start_str(std::string("M486 S") + std::to_string(inst.unique_id) + "\n"); + } + } + } + + m_extrusion_quality_estimator.set_current_object(&instance_to_print.print_object); + + const Point &offset = inst.shift; + std::pair this_object_copy(&instance_to_print.print_object, offset); + if (m_last_obj_copy != this_object_copy) + m_avoid_crossing_perimeters.use_external_mp_once(); + m_last_obj_copy = this_object_copy; + this->set_origin(unscale(offset)); + + // --- Build emission plan --- + // Each entry represents one travel_to_z + extrude pass. Per-object mode produces + // exactly 1 entry (all regions, single sub_z); per-volume mode produces N entries + // for tagged volumes plus an optional entry for untagged residue. + struct SubLayerEmitEntry { + double sub_h; + double sub_z; + std::function region_filter; + bool skip = false; + }; + std::vector emit_plan; + + auto compute_sub_zh = [&](double r1, double r2, double &out_sub_h, double &out_sub_z) { + std::vector sub_heights_local(grp.components_0based.size()); + for (size_t ci = 0; ci < grp.components_0based.size(); ++ci) + sub_heights_local[ci] = (static_cast(ci) == grp.gradient_first_sorted_idx) ? r1 * lh : r2 * lh; + double cum = 0.0; + for (int ci = 0; ci < sub_idx; ++ci) + cum += sub_heights_local[ci]; + out_sub_h = sub_heights_local[sub_idx]; + out_sub_z = print_z - lh + cum + out_sub_h; + }; + + auto gradient_ratios = [](const auto &g) -> std::pair { + double t = (g.total_layers > 0) ? (2.0 * g.current_idx + 1.0) / (2.0 * g.total_layers) : 0.5; + // Custom curve wins over linear range when present; OFF path stays bit-identical. + double r1 = g.curve.empty() + ? (g.gradient_start + (g.gradient_end - g.gradient_start) * t) + : sample_gradient_curve(g.curve, t); + return {r1, 1.0 - r1}; + }; + + // Orca splits BBS's three role filaments into five; a region belongs to the slot + // when any of its roles is assigned to it. + auto region_uses_slot = [](const PrintRegionConfig &rcfg, unsigned int slot_1b) { + return (unsigned int)rcfg.outer_wall_filament_id.value == slot_1b + || (unsigned int)rcfg.inner_wall_filament_id.value == slot_1b + || (unsigned int)rcfg.sparse_infill_filament_id.value == slot_1b + || (unsigned int)rcfg.internal_solid_filament_id.value == slot_1b + || (unsigned int)rcfg.top_surface_filament_id.value == slot_1b + || (unsigned int)rcfg.bottom_surface_filament_id.value == slot_1b; + }; + + double obj_sub_z = default_sub_z; + + if (use_per_volume) { + const PrintObject *po = &instance_to_print.print_object; + const unsigned int slot_1b = grp.mixed_slot_0based + 1; + + // Discover tagged volumes and untagged presence for this instance. + std::set tagged_volumes_present; + bool has_untagged_for_slot = false; + for (ObjectByExtruder::Island &island : instance_to_print.object_by_extruder.islands) { + for (size_t r = 0; r < island.by_region.size(); ++r) { + const auto ®ion = island.by_region[r]; + if (region.perimeters.empty() && region.infills.empty()) + continue; + const PrintRegion &pr = print.get_print_region(r); + if (!region_uses_slot(pr.config(), slot_1b)) + continue; + ObjectID vid = pr.gradient_volume_id(); + if (vid.valid()) + tagged_volumes_present.insert(vid); + else + has_untagged_for_slot = true; + } + } + + // One entry per tagged volume. + for (const ObjectID &target_vid : tagged_volumes_present) { + auto vg_it = grp.per_volume_gradient.find({po, target_vid}); + if (vg_it == grp.per_volume_gradient.end()) + continue; + const auto &vg = vg_it->second; + auto [r1, r2] = gradient_ratios(vg); + + bool vol_no_split = false; + bool skip_entry = false; + const size_t n = grp.components_0based.size(); + if (n == 2 && vg.current_idx + 1 == vg.total_layers) { + const size_t dom_idx = (r1 >= r2) ? 0 : 1; + const unsigned int first_sorted_comp = grp.components_0based[grp.gradient_first_sorted_idx]; + const unsigned int other_comp = grp.components_0based[1 - grp.gradient_first_sorted_idx]; + const unsigned int dom_0b = (dom_idx == 0) ? first_sorted_comp : other_comp; + const unsigned int oth_0b = (dom_idx == 0) ? other_comp : first_sorted_comp; + if (dom_0b < oth_0b) { + vol_no_split = true; + if (extruder_id != dom_0b) + skip_entry = true; + } + } + + double vol_sub_h = default_sub_h; + double vol_sub_z = default_sub_z; + if (vol_no_split) { + vol_sub_h = lh; + vol_sub_z = print_z; + } else { + compute_sub_zh(r1, r2, vol_sub_h, vol_sub_z); + } + + emit_plan.push_back({vol_sub_h, vol_sub_z, + [target_vid, &print](size_t r) { + return print.get_print_region(r).gradient_volume_id() == target_vid; + }, + skip_entry}); + } + + // Optional entry for untagged regions (modifier / painted / fuzzy_skin). + if (has_untagged_for_slot) { + double obj_sub_h = default_sub_h; + auto og_it = grp.per_object_gradient.find(po); + if (og_it != grp.per_object_gradient.end()) { + auto [r1, r2] = gradient_ratios(og_it->second); + compute_sub_zh(r1, r2, obj_sub_h, obj_sub_z); + } + emit_plan.push_back({obj_sub_h, obj_sub_z, + [&print](size_t r) { + return !print.get_print_region(r).gradient_volume_id().valid(); + }, + false}); + } + } else { + // Legacy per-object path: single entry, no region filter. + double legacy_sub_h = default_sub_h; + obj_sub_z = default_sub_z; + if (grp.is_gradient) { + auto og_it = grp.per_object_gradient.find(&instance_to_print.print_object); + if (og_it != grp.per_object_gradient.end()) { + auto [r1, r2] = gradient_ratios(og_it->second); + compute_sub_zh(r1, r2, legacy_sub_h, obj_sub_z); + } + } + emit_plan.push_back({legacy_sub_h, obj_sub_z, nullptr, false}); + } + + // --- Unified emission loop --- + auto plan_has_infill = [](const std::vector &by_region) { + for (const auto &r : by_region) + if (!r.infills.empty()) + return true; + return false; + }; + + for (auto &entry : emit_plan) { + if (entry.skip) + continue; + m_sub_layer_flow_ratio = entry.sub_h / lh; + m_sub_layer_height = entry.sub_h; + m_nominal_z = entry.sub_z; + // Use the same lazy-Z mechanism as change_layer(): set the flag so travel_to + // fires even when m_last_pos coincides with the first extrusion point, + // ensuring Z reaches sub_z via the combined XY+Z move. + m_need_change_layer_lift_z = true; + + for (ObjectByExtruder::Island &island : instance_to_print.object_by_extruder.islands) { + const auto &src = island.by_region; + std::vector subset_storage; + if (entry.region_filter) { + subset_storage.resize(src.size()); + for (size_t r = 0; r < src.size(); ++r) + if (entry.region_filter(r)) + subset_storage[r] = src[r]; + } + const auto &by_region_specific = entry.region_filter ? subset_storage : src; + + // Orca resolves infill-first per region inside extrude_perimeters() + // (unlike BBS, which branches on a single global flag), so mirror the + // main instance loop's ordering exactly. + gcode += this->extrude_perimeters(print, by_region_specific, first_layer, false); + if (!has_wipe_tower && need_insert_timelapse_gcode_for_traditional + && printer_structure == PrinterStructure::psI3 + && !has_insert_timelapse_gcode && plan_has_infill(by_region_specific)) { + gcode += this->retract(false, false, auto_lift_type, true); + gcode += insert_timelapse_gcode(); + has_insert_timelapse_gcode = true; + } + gcode += this->extrude_infill(print, by_region_specific, false); + gcode += this->extrude_perimeters(print, by_region_specific, first_layer, true); + // ironing + gcode += this->extrude_infill(print, by_region_specific, true); + } + } + + // --- Shared support --- + if (instance_to_print.object_by_extruder.support && !instance_to_print.object_by_extruder.support->empty()) { + if (use_per_volume) { + m_nominal_z = obj_sub_z; + gcode += m_writer.travel_to_z(obj_sub_z, "restore Z for support"); + } + ExtrusionRole support_role = instance_to_print.object_by_extruder.support_extrusion_role; + gcode += this->extrude_support(*instance_to_print.object_by_extruder.support, support_role); + // Make sure ironing is the last (Orca names this role erIroning, not erSupportIroning). + if (support_role == erMixed || support_role == erSupportMaterialInterface) + gcode += this->extrude_support(*instance_to_print.object_by_extruder.support, erIroning); + } + + // --- Shared instance footer (mirrors Orca's main instance loop) --- + if (!m_writer.is_object_start_str_empty()) { + m_writer.set_object_start_str(""); + } else if (m_enable_exclude_object) { + if (is_BBL_Printer()) { + m_writer.set_object_end_str(std::string("; stop printing object, unique label id: ") + + std::to_string(instance_to_print.label_object_id) + "\n" + + "M625\n"); + } else { + const auto gflavor = print.config().gcode_flavor.value; + if (gflavor == gcfKlipper) { + m_writer.set_object_end_str(std::string("EXCLUDE_OBJECT_END NAME=") + + get_instance_name(&instance_to_print.print_object, inst.id) + "\n"); + } else if (gflavor == gcfMarlinLegacy || gflavor == gcfMarlinFirmware || gflavor == gcfRepRapFirmware) { + m_writer.set_object_end_str(std::string("M486 S-1\n")); + } + } + } + } + + m_sub_layer_flow_ratio = 0.0; + m_sub_layer_height = 0.0; + } + // Flush any pending object end label before leaving the sublayer block, otherwise the + // wipe tower's add_object_end_labels may consume it into a local temp string and the + // M625 would be lost for BBL printers. + if (!layer_tools.mixed_sub_layer_groups.empty()) { + m_writer.add_object_end_labels(gcode); + m_nominal_z = print_z; + gcode += m_writer.travel_to_z(print_z, "restore Z after sublayers"); + } + } if (first_layer) { for (auto iter = by_extruder.begin(); iter != by_extruder.end(); ++iter) { @@ -7634,6 +7946,15 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description, } } + // Mixed-color sublayer: this path belongs to one sub-layer of a split layer, so scale the + // flow down to that sub-layer's share of the nominal layer height and report the sub-height + // as the effective extrusion height. Inert (ratio == 0) outside the sublayer emission block. + float effective_height = path.height; + if (m_sub_layer_flow_ratio > 0.0) { + _mm3_per_mm *= m_sub_layer_flow_ratio; + effective_height = static_cast(m_sub_layer_height); + } + // Effective extrusion length per distance unit = (filament_flow_ratio/cross_section) * mm3_per_mm / print flow ratio // m_writer.extruder()->e_per_mm3() below is (filament flow ratio / cross-sectional area) double e_per_mm = m_writer.filament()->e_per_mm3() * _mm3_per_mm; @@ -7933,8 +8254,8 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description, gcode += buf; } - if (last_was_wipe_tower || std::abs(m_last_height - path.height) > EPSILON) { - m_last_height = path.height; + if (last_was_wipe_tower || std::abs(m_last_height - effective_height) > EPSILON) { + m_last_height = effective_height; sprintf(buf, ";%s%g\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Height).c_str(), m_last_height); gcode += buf; } diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 6bdb04a8a9..990bf0fee7 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -747,6 +747,11 @@ private: Print* m_curr_print = nullptr; unsigned int m_toolchange_count; coordf_t m_nominal_z; + // Mixed-color sublayer state. Non-zero only while emitting a mixed slot's sub-layer: + // scales extrusion flow to the sub-layer's share of the nominal layer height, and + // reports that sub-height as the effective extrusion height. Reset to 0 afterwards. + double m_sub_layer_flow_ratio = 0.0; + double m_sub_layer_height = 0.0; bool m_need_change_layer_lift_z = false; int m_start_gcode_filament = -1; std::string m_filament_instances_code; diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index f37025d4e7..19f8fddb93 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -7,6 +7,8 @@ #include "GCode/ToolOrderUtils.hpp" #include "FilamentGroupUtils.hpp" #include "MultiNozzleUtils.hpp" +#include "FilamentMixer.hpp" +#include "LocalesUtils.hpp" #include "Utils.hpp" #include "I18N.hpp" @@ -22,8 +24,13 @@ #endif #include +#include #include #include +#include +#include +#include +#include #include #include @@ -402,7 +409,9 @@ void ToolOrdering::sort_and_build_data(const Print& print, unsigned int first_ex // if first extruder is -1, we can decide the first layer tool order before doing reorder function // so we shouldn't reorder first layer in reorder function bool reorder_first_layer = (first_extruder != (unsigned int)(-1)); + this->resolve_mixed_filaments(print.config()); reorder_extruders_for_minimum_flush_volume(reorder_first_layer); + this->enforce_mixed_component_order(); m_sorted = true; double max_layer_height = 0.; @@ -422,6 +431,9 @@ void ToolOrdering::sort_and_build_data(const Print& print, unsigned int first_ex this->fill_wipe_tower_partitions(print.config(), object_bottom_z, max_layer_height); if (this->insert_wipe_tower_extruder()) { reorder_extruders_for_minimum_flush_volume(reorder_first_layer); + // Orca reorders a second time here (BBS has no such path); re-enforce so the + // mixed sub-layer component order survives the extra pass. + this->enforce_mixed_component_order(); this->fill_wipe_tower_partitions(print.config(), object_bottom_z, max_layer_height); } @@ -433,7 +445,9 @@ void ToolOrdering::sort_and_build_data(const PrintObject& object , unsigned int // if first extruder is -1, we can decide the first layer tool order before doing reorder function // so we shouldn't reorder first layer in reorder function bool reorder_first_layer = (first_extruder != (unsigned int)(-1)); + this->resolve_mixed_filaments(object.print()->config()); reorder_extruders_for_minimum_flush_volume(reorder_first_layer); + this->enforce_mixed_component_order(); m_sorted = true; double max_layer_height = calc_max_layer_height(object.print()->config(), object.config().layer_height); @@ -441,6 +455,9 @@ void ToolOrdering::sort_and_build_data(const PrintObject& object , unsigned int this->fill_wipe_tower_partitions(object.print()->config(), object.layers().front()->print_z - object.layers().front()->height, max_layer_height); if (this->insert_wipe_tower_extruder()) { reorder_extruders_for_minimum_flush_volume(reorder_first_layer); + // Orca reorders a second time here (BBS has no such path); re-enforce so the + // mixed sub-layer component order survives the extra pass. + this->enforce_mixed_component_order(); this->fill_wipe_tower_partitions(object.print()->config(), object.layers().front()->print_z - object.layers().front()->height, max_layer_height); } @@ -723,6 +740,38 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto it_per_layer_extruder_override = per_layer_extruder_switches.begin(); unsigned int extruder_override = 0; + // Pre-compute 1-based IDs of mixed filament slots for per-object tracking. + // mixed_slots_1based covers ALL mixed slots (needed by calc_slot_lh for + // accurate layer height when a slot skips layers). gradient_slots_1based + // and per_part_slots_1based are subsets for gradient-specific logic. + std::set mixed_slots_1based; + std::set gradient_slots_1based; + std::set per_part_slots_1based; + { + const PrintConfig &cfg = object.print()->config(); + const auto &is_mixed = cfg.filament_is_mixed.values; + const auto &grad_flags = cfg.filament_mixed_gradient.values; + const auto &per_part_flags = cfg.filament_mixed_gradient_per_part.values; + const auto &comp_strs = cfg.filament_mixed_components.values; + for (size_t i = 0; i < is_mixed.size(); ++i) { + if (!is_mixed[i]) + continue; + auto comps = parse_mixed_components(i < comp_strs.size() ? comp_strs[i] : ""); + if (comps.size() < 2) + continue; + mixed_slots_1based.insert(static_cast(i + 1)); + // Gradient/per-part are only defined for 2-component slots; keep their + // tracking limited to them (mirrors the is_gradient guard at resolve time). + if (comps.size() != 2) + continue; + if (i >= grad_flags.size() || !grad_flags[i]) + continue; + gradient_slots_1based.insert(static_cast(i + 1)); + if (i < per_part_flags.size() && per_part_flags[i]) + per_part_slots_1based.insert(static_cast(i + 1)); + } + } + // BBS: collect first layer extruders of an object's wall, which will be used by brim generator int layerCount = 0; std::vector firstLayerExtruders; @@ -732,6 +781,9 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto for (auto layer : object.layers()) { LayerTools &layer_tools = this->tools_for_layer(layer->print_z); + m_object_all_layer_indices[&object].push_back( + static_cast(&layer_tools - m_layer_tools.data())); + // Override extruder with the next for (; it_per_layer_extruder_override != per_layer_extruder_switches.end() && it_per_layer_extruder_override->first < layer->print_z + EPSILON; ++ it_per_layer_extruder_override) extruder_override = (int)it_per_layer_extruder_override->second; @@ -739,6 +791,9 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto // Store the current extruder override (set to zero if no overriden), so that layer_tools.wiping_extrusions().is_overridable_and_mark() will use it. layer_tools.extruder_override = extruder_override; + // Snapshot extruders before this object's regions to track new additions. + const size_t ext_snapshot = layer_tools.extruders.size(); + // What extruders are required to print this object layer? for (const LayerRegion *layerm : layer->regions()) { const PrintRegion ®ion = layerm->region(); @@ -805,6 +860,54 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto if (has_internal_solid || has_top_solid_surface || has_bottom_surface || has_infill) layer_tools.has_object = true; } + + // Record mixed slot usage for this object at this layer. + // All mixed slots are tracked (not just gradient) so that calc_slot_lh + // can compute accurate layer heights even when a slot skips layers. + if (!mixed_slots_1based.empty()) { + size_t layer_idx = static_cast(&layer_tools - m_layer_tools.data()); + std::set seen; + for (size_t ei = ext_snapshot; ei < layer_tools.extruders.size(); ++ei) { + unsigned int ext_1based = layer_tools.extruders[ei]; + if (mixed_slots_1based.count(ext_1based) && seen.insert(ext_1based).second) + m_mixed_object_layers[ext_1based - 1][&object].push_back(layer_idx); + } + } + + // Per-part gradient: walk LayerRegions and record which (slot, ModelVolume) pairs + // contributed to this layer. Only regions tagged by PrintApply.cpp's get_create_region + // (i.e. gradient_volume_id().valid()) are considered, so this loop is a strict no-op + // unless per_part_gradient is enabled for at least one slot AND the corresponding + // ModelObject has >=2 model-part volumes using that slot. The per-object pass above is + // unaffected — both run the same layer's data through orthogonal containers. + if (!per_part_slots_1based.empty()) { + size_t layer_idx = static_cast(&layer_tools - m_layer_tools.data()); + std::set> vol_seen; + for (const LayerRegion *layerm : layer->regions()) { + if (layerm->slices.empty()) + continue; + const PrintRegion ®ion = layerm->region(); + ObjectID vol_id = region.gradient_volume_id(); + if (! vol_id.valid()) + continue; + const PrintRegionConfig &rcfg = region.config(); + // Orca splits BBS's three role slots into five; cover them all so a mixed + // slot used by any role is tracked. + const unsigned int role_slots[5] = { + static_cast(rcfg.outer_wall_filament_id.value), + static_cast(rcfg.inner_wall_filament_id.value), + static_cast(rcfg.sparse_infill_filament_id.value), + static_cast(rcfg.top_surface_filament_id.value), + static_cast(rcfg.bottom_surface_filament_id.value), + }; + for (unsigned int ext_1based : role_slots) { + if (ext_1based >= 1 + && per_part_slots_1based.count(ext_1based) + && vol_seen.insert({ext_1based, vol_id}).second) + m_gradient_volume_layers[ext_1based - 1][{&object, vol_id}].push_back(layer_idx); + } + } + } layerCount++; } @@ -1945,6 +2048,594 @@ MultiNozzleUtils::LayeredNozzleGroupResult ToolOrdering::build_sequential_group_ return result ? *result : MultiNozzleUtils::LayeredNozzleGroupResult(); } +static double snap_to_simple_fraction(double r, int max_denom = 10) +{ + double best_r = r; + double best_err = 1.0; + for (int q = 1; q <= max_denom; ++q) { + int p = (int)std::round(r * q); + if (p < 0) p = 0; + if (p > q) p = q; + double candidate = (double)p / q; + double err = std::abs(candidate - r); + if (err < best_err) { + best_err = err; + best_r = candidate; + } + } + return best_r; +} + +void ToolOrdering::resolve_mixed_filaments(const PrintConfig &config) +{ + const auto &is_mixed = config.filament_is_mixed.values; + const auto &comp_strs = config.filament_mixed_components.values; + const auto &ratio_strs = config.filament_mixed_sublayer_ratios.values; + + if (!has_any_mixed_filament(is_mixed)) + return; + + const bool sublayer_enabled = config.enable_mixed_color_sublayer.value; + + struct SlotInfo { + std::vector components; // 1-based + std::vector ratios; + std::vector accum; // deficit accumulator (integer, unit: 1e-6 mm) + }; + std::vector slots(is_mixed.size()); + for (size_t i = 0; i < is_mixed.size(); ++i) { + if (!is_mixed[i]) + continue; + slots[i].components = parse_mixed_components(i < comp_strs.size() ? comp_strs[i] : ""); + if (slots[i].components.size() < 2) { + slots[i].components.clear(); + continue; + } + for (unsigned int cid : slots[i].components) { + unsigned int idx0 = cid - 1; + if (idx0 >= is_mixed.size() || (idx0 < is_mixed.size() && is_mixed[idx0])) { + slots[i].components.clear(); + break; + } + } + if (slots[i].components.empty()) + continue; + slots[i].ratios = parse_mixed_ratios( + i < ratio_strs.size() ? ratio_strs[i] : "", slots[i].components.size()); + if (!sublayer_enabled) { + for (double &r : slots[i].ratios) + r = snap_to_simple_fraction(r); + double sum = 0; + for (double r : slots[i].ratios) sum += r; + if (sum > 0) + for (double &r : slots[i].ratios) r /= sum; + } + slots[i].accum.assign(slots[i].components.size(), 0LL); + } + + // Parse gradient settings per slot + const auto &gradient_flags = config.filament_mixed_gradient.values; + const auto &gradient_range_strs = config.filament_mixed_gradient_range.values; + const auto &gradient_curve_strs = config.filament_mixed_gradient_curve.values; + struct GradientInfo { + double start = 0.10; + double end_val = 0.90; + GradientCurve curve; // empty -> use linear (start, end_val); non-empty wins + }; + std::vector is_gradient(is_mixed.size(), false); + std::vector gradient_info(is_mixed.size()); + for (size_t i = 0; i < is_mixed.size(); ++i) { + if (!is_mixed[i] || slots[i].components.size() != 2) + continue; + if (i >= gradient_flags.size() || !gradient_flags[i]) + continue; + is_gradient[i] = true; + if (i < gradient_range_strs.size() && !gradient_range_strs[i].empty()) { + CNumericLocalesSetter c_locale_setter; + float v0 = 0, v1 = 0; + if (std::sscanf(gradient_range_strs[i].c_str(), "%f,%f", &v0, &v1) == 2 && + v0 > 0 && v0 < 1.0 && v1 > 0 && v1 < 1.0) { + gradient_info[i].start = v0; + gradient_info[i].end_val = v1; + } + } + if (i < gradient_curve_strs.size() && !gradient_curve_strs[i].empty()) + gradient_info[i].curve = parse_gradient_curve(gradient_curve_strs[i]); + } + + // Pass 1: identify continuous runs for each gradient slot (Per-Run). + // A "run" is a maximal sequence of consecutive layers where the slot appears. + struct GradientRunInfo { + std::vector run_lengths; + int current_run = -1; + size_t current_idx = 0; + bool prev_appeared = false; + bool last_absent_was_relevant = false; + }; + std::map gradient_runs; + for (size_t i = 0; i < is_mixed.size(); ++i) + if (is_gradient[i]) gradient_runs[static_cast(i)] = {}; + + // Build per-slot sets of all layer indices where any slot-owning object has a + // layer. Used by gradient run detection (a gap is real only if the slot is + // absent at a layer belonging to one of its own objects) and by calc_slot_lh + // to keep prev_relevant_z_for_slot current even when a slot skips many layers. + std::map> slot_relevant_layers; + for (auto &[slot_idx, obj_map] : m_mixed_object_layers) { + for (auto &[obj, _] : obj_map) { + auto it = m_object_all_layer_indices.find(obj); + if (it != m_object_all_layer_indices.end()) + slot_relevant_layers[slot_idx].insert(it->second.begin(), it->second.end()); + } + } + + if (!gradient_runs.empty()) { + for (size_t li = 0; li < m_layer_tools.size(); ++li) { + if (li == 0) continue; + const auto < = m_layer_tools[li]; + for (auto &[slot, run] : gradient_runs) { + bool here = std::find(lt.extruders.begin(), lt.extruders.end(), slot) != lt.extruders.end(); + if (here) { + bool real_gap = false; + if (!run.prev_appeared && !run.run_lengths.empty()) { + real_gap = run.last_absent_was_relevant; + } + if (run.run_lengths.empty() || real_gap) + run.run_lengths.push_back(0); + run.run_lengths.back()++; + run.last_absent_was_relevant = false; + } else if (!run.run_lengths.empty()) { + auto rel_it = slot_relevant_layers.find(slot); + if (rel_it != slot_relevant_layers.end() && rel_it->second.count(li)) + run.last_absent_was_relevant = true; + } + run.prev_appeared = here; + } + } + for (auto &[slot, run] : gradient_runs) { + run.current_run = -1; + run.current_idx = 0; + run.prev_appeared = false; + run.last_absent_was_relevant = false; + } + } + + // Per-object gradient: pre-compute per-object runs (respecting Z gaps within each object). + struct PerObjRunState { + std::vector run_start_offsets; // index into layer_indices where each run starts + std::vector run_lengths; + int current_run = -1; + size_t current_idx = 0; + }; + + // Detect whether a gap between two consecutive gradient-slot appearances is a + // real run break. A gap is real only if the object has its own layer inside the + // gap that does NOT use the gradient slot (i.e. the slot was genuinely absent). + // Uses lower_bound to skip global indices that don't belong to the object. + auto has_real_gap = [](size_t prev_idx, size_t cur_idx, + const std::set& obj_set, + const std::set& slot_set) -> bool { + for (auto it = obj_set.lower_bound(prev_idx + 1); + it != obj_set.end() && *it < cur_idx; ++it) { + if (!slot_set.count(*it)) + return true; + } + return false; + }; + + // Segment a sorted list of layer indices into runs, using has_real_gap to decide + // where to break. Shared by the per-object and per-volume paths below. + auto segment_runs = [&](const std::vector& layer_indices, + const std::set& obj_set, + const std::set& slot_set) -> PerObjRunState { + PerObjRunState st; + for (size_t i = 0; i < layer_indices.size(); ++i) { + bool new_run = (i == 0) || + has_real_gap(layer_indices[i - 1], layer_indices[i], obj_set, slot_set); + if (new_run) { + st.run_start_offsets.push_back(i); + st.run_lengths.push_back(0); + } + st.run_lengths.back()++; + } + return st; + }; + + std::map> per_obj_runs; + for (auto &[slot, obj_map] : m_mixed_object_layers) { + if (slot >= is_gradient.size() || !is_gradient[slot]) + continue; + for (auto &[obj, layer_indices] : obj_map) { + sort_remove_duplicates(layer_indices); + // Erase layer 0 — this mutation is also relied upon by the Pass 2 binary_search below. + if (!layer_indices.empty() && layer_indices.front() == 0) + layer_indices.erase(layer_indices.begin()); + + const auto &all_obj_layers = m_object_all_layer_indices[obj]; + std::set all_obj_set(all_obj_layers.begin(), all_obj_layers.end()); + std::set grad_set(layer_indices.begin(), layer_indices.end()); + + per_obj_runs[slot][obj] = segment_runs(layer_indices, all_obj_set, grad_set); + } + } + + // Per-volume gradient: mirror the per-object run-segmentation logic above for + // m_gradient_volume_layers. When per_part_gradient is off (or no qualifying volume exists), + // m_gradient_volume_layers is empty and per_vol_runs ends up empty too — so all subsequent + // checks of `per_vol_runs.find(slot) != end()` will fail and the legacy per-object path + // remains the only path taken. + using VolumeKey = LayerTools::MixedSubLayerGroup::VolumeKey; + std::map> per_vol_runs; + for (auto &[slot, vol_map] : m_gradient_volume_layers) { + if (slot >= is_gradient.size() || !is_gradient[slot]) + continue; + for (auto &[vkey, layer_indices] : vol_map) { + sort_remove_duplicates(layer_indices); + if (!layer_indices.empty() && layer_indices.front() == 0) + layer_indices.erase(layer_indices.begin()); + + const auto &all_obj_layers = m_object_all_layer_indices[vkey.obj]; + std::set all_obj_set(all_obj_layers.begin(), all_obj_layers.end()); + std::set vol_grad_set(layer_indices.begin(), layer_indices.end()); + + per_vol_runs[slot][vkey] = segment_runs(layer_indices, all_obj_set, vol_grad_set); + } + } + // Pass 2: resolve per layer + coordf_t prev_print_z = 0.; + // Track last print_z per mixed slot so that layer height is computed from the + // slot's own previous appearance, not from a global Z that may include layers + // belonging only to other objects with different layer heights. + std::map prev_print_z_for_slot; + // Track the last Z where a slot-owning object had ANY layer (regardless of + // whether the slot was present). Used to detect genuine gaps: if the slot was + // absent but its owner objects had layers, prev_relevant_z advances while + // prev_print_z_for_slot stays stale. Taking the max of both gives correct lh. + std::map prev_relevant_z_for_slot; + + // Compute the effective layer height for a mixed slot by choosing the best + // reference Z among: (1) the slot's own last Z, (2) the last Z where the + // slot's owning object had any layer, (3) the global previous Z as fallback + // when the slot appears for the first time. + auto calc_slot_lh = [&](unsigned int ext, coordf_t print_z) -> double { + auto slot_pz_it = prev_print_z_for_slot.find(ext); + auto rel_pz_it = prev_relevant_z_for_slot.find(ext); + coordf_t base_z = prev_print_z; + if (slot_pz_it != prev_print_z_for_slot.end()) { + base_z = slot_pz_it->second; + if (rel_pz_it != prev_relevant_z_for_slot.end()) + base_z = std::max(base_z, rel_pz_it->second); + } + double lh = print_z - base_z; + return (lh > 0.) ? lh : 0.2; // 0.2mm safety fallback; should not trigger in normal operation + }; + + for (LayerTools < : m_layer_tools) { + size_t layer_idx = static_cast(< - m_layer_tools.data()); + + // Update gradient run state (skip first layer to match counting). + if (layer_idx > 0) { + for (auto &[slot, run] : gradient_runs) { + bool here = std::find(lt.extruders.begin(), lt.extruders.end(), slot) != lt.extruders.end(); + if (here) { + if (!run.prev_appeared) { + if (run.last_absent_was_relevant || run.current_run < 0) { + run.current_run++; + run.current_idx = 0; + } + } + run.last_absent_was_relevant = false; + } else { + auto rel_it = slot_relevant_layers.find(slot); + if (rel_it != slot_relevant_layers.end() && rel_it->second.count(layer_idx)) + run.last_absent_was_relevant = true; + } + run.prev_appeared = here; + } + } + + std::vector new_extruders; + for (unsigned int ext : lt.extruders) { + if (ext >= slots.size() || slots[ext].components.empty()) { + new_extruders.push_back(ext); + continue; + } + auto &s = slots[ext]; + + // Skip sublayer splitting for the first layer to preserve bed adhesion. + if (sublayer_enabled && layer_idx > 0) { + double lh = calc_slot_lh(ext, lt.print_z); + size_t n = s.components.size(); + + std::vector sub_heights; + bool gradient_last_no_split = false; + unsigned int gradient_last_dominant_0b = 0; + if (is_gradient[ext] && n == 2) { + auto gr_it = gradient_runs.find(ext); + if (gr_it != gradient_runs.end() && gr_it->second.current_run >= 0 && + static_cast(gr_it->second.current_run) < gr_it->second.run_lengths.size()) { + auto &run = gr_it->second; + size_t N = run.run_lengths[run.current_run]; + size_t idx = run.current_idx++; + double t = (N > 0) ? (2.0 * idx + 1.0) / (2.0 * N) : 0.5; + // Custom curve wins over linear range when present; OFF path stays bit-identical. + double r1 = gradient_info[ext].curve.empty() + ? (gradient_info[ext].start + (gradient_info[ext].end_val - gradient_info[ext].start) * t) + : sample_gradient_curve(gradient_info[ext].curve, t); + double r2 = 1.0 - r1; + sub_heights.push_back(r1 * lh); + sub_heights.push_back(r2 * lh); + // The sublayer split path sorts components by physical ID ascending; + // the higher-ID component ends up on top (visible surface). If the + // gradient's dominant component has the lower physical ID, splitting + // would put the non-dominant color on the visible top surface. In + // that case, skip the split and print this final run-layer as pure + // dominant color to preserve the gradient appearance. + if (idx == N - 1) { + // When r1 == r2 (exactly 50/50), component[0] is treated as dominant. + size_t dominant = (r1 >= r2) ? 0 : 1; + unsigned int dom_0b = s.components[dominant] - 1; + unsigned int oth_0b = s.components[1 - dominant] - 1; + if (dom_0b < oth_0b) { + gradient_last_no_split = true; + gradient_last_dominant_0b = dom_0b; + } + } + } else { + for (double r : s.ratios) + sub_heights.push_back(r * lh); + } + } else { + for (double r : s.ratios) + sub_heights.push_back(r * lh); + } + + // Per-part gradient: when this slot has any qualifying volume, the global + // no-split short-circuit must NOT bypass MixedSubLayerGroup creation — each + // volume needs its own no-split decision in GCode.cpp (a per-volume "last + // run-layer" can occur on a different layer index than the per-object one). We + // still keep the per-object short-circuit when per_vol_runs[ext] is empty, which + // covers the legacy path bit-identically. + bool per_vol_active_for_slot = per_vol_runs.find(ext) != per_vol_runs.end() + && !per_vol_runs[ext].empty(); + + if (gradient_last_no_split && !per_vol_active_for_slot) { + lt.mixed_filament_resolution[ext] = gradient_last_dominant_0b; + new_extruders.push_back(gradient_last_dominant_0b); + prev_print_z_for_slot[ext] = lt.print_z; + continue; + } + + LayerTools::MixedSubLayerGroup grp; + grp.mixed_slot_0based = ext; + grp.layer_height = lh; + grp.is_gradient = is_gradient[ext]; + for (size_t k = 0; k < s.components.size(); ++k) { + unsigned int comp_0based = s.components[k] - 1; + grp.components_0based.push_back(comp_0based); + } + grp.sub_heights = sub_heights; + + // Write gradient metadata (run-aware). Both per_object_gradient and + // per_volume_gradient are populated independently from their own run-state + // machines; the GCode emitter chooses per-region: + // - tagged region (gradient_volume_id valid) -> per_volume_gradient[{obj, vol}] + // - untagged region (modifier / painted / etc.) -> per_object_gradient[obj] + // Populating both keeps the per-object run state correct even when per-volume + // takes over for the same (slot, obj), and lets untagged geometry (which is + // explicitly NOT split per-volume in v1 per the design doc) keep its legacy + // per-object gradient ratios. + if (grp.is_gradient) { + auto vol_runs_slot_it = per_vol_runs.find(ext); + if (vol_runs_slot_it != per_vol_runs.end()) { + auto vol_slot_it = m_gradient_volume_layers.find(ext); + for (auto &[vkey, st] : vol_runs_slot_it->second) { + auto &layer_indices = vol_slot_it->second[vkey]; + if (!std::binary_search(layer_indices.begin(), layer_indices.end(), layer_idx)) + continue; + if (st.current_run < 0 || + st.current_idx >= st.run_lengths[st.current_run]) { + st.current_run++; + st.current_idx = 0; + } + size_t run_N = st.run_lengths[st.current_run]; + size_t run_idx = st.current_idx++; + grp.per_volume_gradient[vkey] = { + run_N, + run_idx, + gradient_info[ext].start, + gradient_info[ext].end_val, + gradient_info[ext].curve, + }; + } + } + + auto runs_slot_it = per_obj_runs.find(ext); + if (runs_slot_it != per_obj_runs.end()) { + auto slot_it = m_mixed_object_layers.find(ext); + for (auto &[obj, st] : runs_slot_it->second) { + auto &layer_indices = slot_it->second[obj]; + if (!std::binary_search(layer_indices.begin(), layer_indices.end(), layer_idx)) + continue; + if (st.current_run < 0 || + st.current_idx >= st.run_lengths[st.current_run]) { + st.current_run++; + st.current_idx = 0; + } + size_t run_N = st.run_lengths[st.current_run]; + size_t run_idx = st.current_idx++; + grp.per_object_gradient[obj] = { + run_N, + run_idx, + gradient_info[ext].start, + gradient_info[ext].end_val, + gradient_info[ext].curve, + }; + } + } + } + + if (grp.components_0based.size() > 1) { + unsigned int first_comp_0based = s.components[0] - 1; + std::vector idx(grp.components_0based.size()); + std::iota(idx.begin(), idx.end(), 0); + std::sort(idx.begin(), idx.end(), [&](size_t a, size_t b) { + return grp.components_0based[a] < grp.components_0based[b]; + }); + std::vector sorted_comps; + std::vector sorted_heights; + for (size_t i : idx) { + sorted_comps.push_back(grp.components_0based[i]); + sorted_heights.push_back(grp.sub_heights[i]); + } + grp.components_0based = std::move(sorted_comps); + grp.sub_heights = std::move(sorted_heights); + if (grp.is_gradient) { + for (size_t i = 0; i < grp.components_0based.size(); ++i) { + if (grp.components_0based[i] == first_comp_0based) { + grp.gradient_first_sorted_idx = static_cast(i); + break; + } + } + } + } + + for (unsigned int comp : grp.components_0based) + new_extruders.push_back(comp); + lt.mixed_sub_layer_groups.push_back(std::move(grp)); + prev_print_z_for_slot[ext] = lt.print_z; + } else { + // Deficit Round-Robin: pick one component per layer. + // Weight by layer height so volume ratios stay accurate + // even with adaptive layer heights. + double lh = calc_slot_lh(ext, lt.print_z); + long long lh_i = std::llround(lh * 1e6); + + // For 2-component gradient on the first layer, use the gradient's + // starting ratio instead of the configured mixing ratio so the + // selected filament matches the gradient's "from" end. + // Only affects the first layer; when sublayer splitting is enabled + // (required for gradient), layers 1+ take the sublayer path and + // do not touch the DRR accumulator. + if (layer_idx == 0 && is_gradient[ext] && s.components.size() == 2) { + double r0 = gradient_info[ext].start; + s.accum[0] += std::llround(r0 * lh_i); + s.accum[1] += std::llround((1.0 - r0) * lh_i); + } else { + for (size_t k = 0; k < s.ratios.size(); ++k) + s.accum[k] += std::llround(s.ratios[k] * lh_i); + } + size_t sel = 0; + for (size_t k = 1; k < s.accum.size(); ++k) + if (s.accum[k] > s.accum[sel]) + sel = k; + s.accum[sel] -= lh_i; + unsigned int resolved = s.components[sel] - 1; + lt.mixed_filament_resolution[ext] = resolved; + new_extruders.push_back(resolved); + prev_print_z_for_slot[ext] = lt.print_z; + } + } + lt.extruders = new_extruders; + sort_remove_duplicates(lt.extruders); + + // Update prev_relevant_z: for each slot that has relevant-layer tracking, + // advance if the current layer belongs to a slot-owning object. + for (auto &[slot, rel_set] : slot_relevant_layers) { + if (rel_set.count(layer_idx)) + prev_relevant_z_for_slot[slot] = lt.print_z; + } + + prev_print_z = lt.print_z; + } +} + +void ToolOrdering::enforce_mixed_component_order() +{ + for (LayerTools < : m_layer_tools) { + if (lt.mixed_sub_layer_groups.empty()) + continue; + + // Build a set of extruders present in lt.extruders for fast lookup. + std::set ext_set(lt.extruders.begin(), lt.extruders.end()); + + // 1. Build DAG from mixed group constraints. + // For each group [c0, c1, c2, ...], add edges c0->c1, c1->c2, ... + // Only between components that are both present in lt.extruders. + // Use an edge set to avoid duplicate edges inflating in-degree. + std::map> adj; + std::map in_degree; + std::set> edge_set; + + for (unsigned int ext : lt.extruders) + in_degree[ext] = 0; + + for (const auto &grp : lt.mixed_sub_layer_groups) { + for (size_t i = 0; i + 1 < grp.components_0based.size(); ++i) { + unsigned int a = grp.components_0based[i]; + unsigned int b = grp.components_0based[i + 1]; + if (!ext_set.count(a) || !ext_set.count(b)) + continue; + if (edge_set.insert({a, b}).second) { + adj[a].push_back(b); + in_degree[b] += 1; + } + } + } + + // 2. Record original position (from flush optimizer) as priority. + std::map orig_pos; + for (size_t i = 0; i < lt.extruders.size(); ++i) + orig_pos[lt.extruders[i]] = i; + + // 3. Kahn's topological sort with priority queue (prefer original position). + auto cmp = [&orig_pos](unsigned int lhs, unsigned int rhs) { + return orig_pos[lhs] > orig_pos[rhs]; // min-heap by orig_pos + }; + std::priority_queue, decltype(cmp)> pq(cmp); + + for (unsigned int ext : lt.extruders) { + if (in_degree[ext] == 0) + pq.push(ext); + } + + std::vector ordered; + ordered.reserve(lt.extruders.size()); + while (!pq.empty()) { + unsigned int ext = pq.top(); + pq.pop(); + ordered.push_back(ext); + if (auto it = adj.find(ext); it != adj.end()) { + for (unsigned int next : it->second) { + if (--in_degree[next] == 0) + pq.push(next); + } + } + } + + // Safety: if topological sort didn't produce all elements, keep original order. + if (ordered.size() != lt.extruders.size()) + ordered = lt.extruders; + + // 4. Verify: every mixed group's component order is preserved as subsequence. + for (const auto &grp : lt.mixed_sub_layer_groups) { + size_t prev_pos = 0; + bool valid = true; + for (unsigned int c : grp.components_0based) { + if (!ext_set.count(c)) + continue; + auto it = std::find(ordered.begin() + prev_pos, ordered.end(), c); + if (it == ordered.end()) { valid = false; break; } + prev_pos = (it - ordered.begin()) + 1; + } + assert(valid && "enforce_mixed_component_order: mixed group subsequence violated"); + (void)valid; + } + + lt.extruders = ordered; + } +} + void ToolOrdering::reorder_extruders_for_minimum_flush_volume(bool reorder_first_layer) { const PrintConfig* print_config = m_print_config_ptr; diff --git a/src/libslic3r/GCode/ToolOrdering.hpp b/src/libslic3r/GCode/ToolOrdering.hpp index c77b152fe9..699afa7091 100644 --- a/src/libslic3r/GCode/ToolOrdering.hpp +++ b/src/libslic3r/GCode/ToolOrdering.hpp @@ -5,12 +5,16 @@ #include "../libslic3r.h" +#include +#include #include #include #include "../FilamentGroup.hpp" +#include "../FilamentMixer.hpp" #include "../MultiNozzleUtils.hpp" #include "../ExtrusionEntity.hpp" +#include "../ObjectID.hpp" #include "../PrintConfig.hpp" namespace Slic3r { @@ -172,6 +176,65 @@ public: // Custom G-code (color change, extruder switch, pause) to be performed before this layer starts to print. const CustomGCode::Item *custom_gcode = nullptr; + // 0-based mixed filament slot → 0-based resolved physical filament for this layer. + // Populated by ToolOrdering::resolve_mixed_filaments(). Empty when no mixed filaments. + std::map mixed_filament_resolution; + + unsigned int resolve_mixed(unsigned int filament_0based) const { + auto it = mixed_filament_resolution.find(filament_0based); + return (it != mixed_filament_resolution.end()) ? it->second : filament_0based; + } + + struct MixedSubLayerGroup { + unsigned int mixed_slot_0based; + std::vector components_0based; + std::vector sub_heights; // per-component, sum ≈ layer_height + double layer_height = 0.; // the actual lh used to compute sub_heights + bool is_gradient = false; + int gradient_first_sorted_idx = 0; // index of "first" config component after sorting + + struct ObjectGradient { + size_t total_layers; + size_t current_idx; + double gradient_start; + double gradient_end; + GradientCurve curve; // empty -> linear fallback (start, end); non-empty wins + }; + std::map per_object_gradient; + + // Per-volume gradient: same metadata layout as ObjectGradient but keyed by + // (PrintObject*, ModelVolume id). Populated only when filament_mixed_gradient_per_part is + // enabled for this slot AND the corresponding ModelObject contains >=2 model-part volumes + // using this slot. When non-empty for a given (PrintObject*), GCode emission takes the + // per-volume path for tagged regions; untagged regions (modifier/painted/fuzzy_skin) still + // use per_object_gradient. Both maps are populated in parallel to keep run states correct. + struct VolumeKey { + const PrintObject* obj; + ObjectID volume_id; + bool operator<(const VolumeKey &o) const { + if (obj != o.obj) return std::less{}(obj, o.obj); + return volume_id < o.volume_id; + } + bool operator==(const VolumeKey &o) const { + return obj == o.obj && volume_id == o.volume_id; + } + }; + using VolumeGradient = ObjectGradient; + std::map per_volume_gradient; + }; + std::vector mixed_sub_layer_groups; + + const MixedSubLayerGroup* mixed_group_by_slot(unsigned int slot_id) const { + for (const auto &g : mixed_sub_layer_groups) + if (g.mixed_slot_0based == slot_id) + return &g; + return nullptr; + } + + bool is_mixed_slot(unsigned int slot_id) const { + return mixed_group_by_slot(slot_id) != nullptr; + } + WipingExtrusions& wiping_extrusions() { m_wiping_extrusions.set_layer_tools_ptr(this); return m_wiping_extrusions; @@ -299,6 +362,8 @@ private: void mark_skirt_layers(const PrintConfig &config, coordf_t max_layer_height); void collect_extruder_statistics(bool prime_multi_material); void reorder_extruders_for_minimum_flush_volume(bool reorder_first_layer); + void resolve_mixed_filaments(const PrintConfig &config); + void enforce_mixed_component_order(); // BBS std::vector generate_first_layer_tool_order(const Print& print); @@ -313,6 +378,23 @@ private: std::vector m_all_printing_extruders; const DynamicPrintConfig* m_print_full_config = nullptr; const PrintConfig* m_print_config_ptr = nullptr; + + // Per-object gradient tracking: slot(0-based) -> PrintObject* -> list of layer indices + // where that object uses the slot. Populated by collect_extruders, consumed by resolve_mixed_filaments. + std::map>> m_mixed_object_layers; + + // All layer indices (in m_layer_tools) where each object has any layer. + // Used by gradient run detection to distinguish real gaps (object has a layer + // that doesn't use the slot) from spurious gaps (another object's layer). + std::map> m_object_all_layer_indices; + + // Per-volume gradient tracking: slot(0-based) -> (PrintObject*, ModelVolume id) -> list of + // layer indices where the given volume contributes to the slot. Populated by collect_extruders + // alongside m_mixed_object_layers when per_part gradient is enabled for the slot AND the + // ModelObject has >=2 model-part volumes using the slot. Empty for all other configurations, + // which keeps every legacy per-object code path bit-identical (loops over an empty map are + // no-ops; downstream emission falls through to the per-object branch). + std::map>> m_gradient_volume_layers; const PrintObject* m_print_object_ptr = nullptr; Print* m_print; bool m_sorted = false; diff --git a/src/libslic3r/Layer.cpp b/src/libslic3r/Layer.cpp index b3a145bed0..87ad11bcf8 100644 --- a/src/libslic3r/Layer.cpp +++ b/src/libslic3r/Layer.cpp @@ -210,6 +210,12 @@ void Layer::make_perimeters() if (! (*it)->slices.empty()) { LayerRegion* other_layerm = *it; const PrintRegion &other_region = other_layerm->region(); + // Per-part gradient tags a region with its owning ModelVolume; merging two + // differently-tagged regions would collapse volumes that need independent + // gradient runs. Both tags are invalid unless per-part gradient is on, so + // this is a no-op for every other configuration. + if (this_region.gradient_volume_id() != other_region.gradient_volume_id()) + continue; if (is_perimeter_compatible(*m_object->print(), this_region, other_region)) { other_layerm->perimeters.clear(); diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index c689c7ce78..3617e85991 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1,6 +1,7 @@ #include "Model.hpp" #include "libslic3r.h" #include "BuildVolume.hpp" +#include "TexturePainting.hpp" #include "ClipperUtils.hpp" #include "Exception.hpp" #include "Model.hpp" @@ -104,6 +105,7 @@ Model& Model::assign_copy(const Model &rhs) this->mk_version = rhs.mk_version; this->md_name = rhs.md_name; this->md_value = rhs.md_value; + this->texture_mesh = rhs.texture_mesh; return *this; } @@ -139,6 +141,7 @@ Model& Model::assign_copy(Model &&rhs) this->mk_version = rhs.mk_version; this->md_name = rhs.md_name; this->md_value = rhs.md_value; + this->texture_mesh = std::move(rhs.texture_mesh); this->backup_path = std::move(rhs.backup_path); this->object_backup_id_map = std::move(rhs.object_backup_id_map); this->next_object_backup_id = rhs.next_object_backup_id; @@ -281,8 +284,21 @@ Model Model::read_from_file(const std::string& result = load_stl(input_file.c_str(), &model, nullptr, stlFn,256); else if (boost::algorithm::iends_with(input_file, ".obj")) { ObjInfo obj_info; - result = load_obj(input_file.c_str(), &model, obj_info, message); - if (result){ + ObjParser::MtlData mtl_data; + result = load_obj(input_file.c_str(), &model, obj_info, message, nullptr, &mtl_data); + if (result && obj_info.has_uv_png && !obj_info.uvs.empty() && !model.objects.empty()) { + // Textured OBJ: hand the mesh + materials to the texture-to-color importer instead + // of the flat per-face colour dialog. Replaces Orca's previous "not implemented" + // placeholder for this branch. + auto tex_mesh = std::make_shared(); + std::string obj_dir = boost::filesystem::path(input_file).parent_path().string(); + if (obj_to_textured_mesh(obj_info, + model.objects.back()->volumes[0]->mesh().its, + mtl_data, obj_dir, *tex_mesh)) { + model.texture_mesh = tex_mesh; + } + } + else if (result){ ObjDialogInOut in_out; in_out.model = &model; in_out.lost_material_name = obj_info.lost_material_name; @@ -578,6 +594,7 @@ void Model::clear_objects() this->objects.clear(); object_backup_id_map.clear(); next_object_backup_id = 1; + texture_mesh.reset(); } // BBS: backup, reuse objects @@ -2576,7 +2593,8 @@ void ModelVolume::update_extruder_count(size_t extruder_count) } } -void ModelVolume::update_extruder_count_when_delete_filament(size_t extruder_count, size_t filament_id, int replace_filament_id) +void ModelVolume::update_extruder_count_when_delete_filament(size_t extruder_count, size_t filament_id, int replace_filament_id, + const std::vector &filament_is_mixed) { std::vector used_extruders = get_extruders(); for (int extruder_id : used_extruders) { @@ -2587,8 +2605,13 @@ void ModelVolume::update_extruder_count_when_delete_filament(size_t extruder_cou } // Same stale-assignment cleanup as update_extruder_count, for the filament-delete path. // Ported from BambuStudio (STUDIO-15763). - if (extruder_id() > extruder_count) { - this->config.erase("extruder"); + size_t eid = extruder_id(); + if (eid > extruder_count) { + // A mixed-color slot is virtual and legitimately sits past the physical filament count, + // so an assignment to one is not stale and must survive the delete. + bool is_mixed = !filament_is_mixed.empty() && eid >= 1 && (eid - 1) < filament_is_mixed.size() && filament_is_mixed[eid - 1]; + if (!is_mixed) + this->config.erase("extruder"); } } @@ -3495,6 +3518,15 @@ void FacetsAnnotation::get_facets(const ModelVolume& mv, std::vectorset(selector); +} + void FacetsAnnotation::set_enforcer_block_type_limit(const ModelVolume &mv, EnforcerBlockerType max_type, EnforcerBlockerType to_delete_filament, diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 2d46bc4cdf..6834c7a59b 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -47,6 +47,8 @@ namespace cereal { } namespace Slic3r { + +struct TexturedMesh; enum class ConversionType; class BuildVolume; @@ -740,6 +742,9 @@ public: EnforcerBlockerType max_type, EnforcerBlockerType to_delete_filament = EnforcerBlockerType::NONE, EnforcerBlockerType replace_filament = EnforcerBlockerType::NONE); + // Shift painted filament indices >= threshold by delta. Used when a physical filament is + // inserted ahead of existing slots (mixed-color slots are kept at the end of the list). + void shift_states_above(const ModelVolume &mv, EnforcerBlockerType threshold, int delta); indexed_triangle_set get_facets_strict(const ModelVolume& mv, EnforcerBlockerType type) const; bool has_facets(const ModelVolume& mv, EnforcerBlockerType type) const; bool empty() const { return m_data.triangles_to_split.empty(); } @@ -932,7 +937,8 @@ public: // BBS std::vector get_extruders() const; void update_extruder_count(size_t extruder_count); - void update_extruder_count_when_delete_filament(size_t extruder_count, size_t filament_id, int replace_filament_id = -1); + void update_extruder_count_when_delete_filament(size_t extruder_count, size_t filament_id, int replace_filament_id = -1, + const std::vector &filament_is_mixed = {}); // Split this volume, append the result to the object owning this volume. // Return the number of volumes created from this one. @@ -1549,6 +1555,10 @@ public: std::shared_ptr model_info = nullptr; std::shared_ptr profile_info = nullptr; + // Textured mesh data for texture-to-painting import. Populated by the loader when a mesh + // arrives with usable UVs and a texture map; consumed (and reset) by the import dialog. + std::shared_ptr texture_mesh; + //makerlab information std::string mk_name; std::string mk_version; diff --git a/src/libslic3r/Preset.cpp b/src/libslic3r/Preset.cpp index 1334bd4e7a..0a6491078d 100644 --- a/src/libslic3r/Preset.cpp +++ b/src/libslic3r/Preset.cpp @@ -1183,6 +1183,7 @@ static std::vector s_Preset_print_options{ "flush_into_infill", "flush_into_objects", "flush_into_support", + "enable_mixed_color_sublayer", "tree_support_branch_angle", "tree_support_angle_slow", "tree_support_wall_count", diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 6fcc6e05c1..961ed59d2b 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -7,6 +7,7 @@ #include "PresetCacheFormat.hpp" #include "PrintConfig.hpp" +#include "FilamentMixer.hpp" #include "libslic3r.h" #include "I18N.hpp" #include "Utils.hpp" @@ -71,7 +72,17 @@ static std::vector s_project_options { // whether dynamic per-nozzle filament mapping is active. Persisted with the project and // restored from a saved 3mf; reset to false on load and set true only by live device sync. "has_filament_switcher", - "enable_filament_dynamic_map" + "enable_filament_dynamic_map", + // Mixed-color filament slots. Project-level parallel arrays indexed like filament_colour: + // which slots are virtual mixes, their component filaments, blend ratios and the optional + // Z-gradient description. Kept with the project so a saved 3mf round-trips the mix setup. + "filament_is_mixed", + "filament_mixed_components", + "filament_mixed_sublayer_ratios", + "filament_mixed_gradient", + "filament_mixed_gradient_range", + "filament_mixed_gradient_curve", + "filament_mixed_gradient_per_part" }; //Orca: add custom as default @@ -2704,6 +2715,40 @@ void PresetBundle::load_installed_sla_materials(AppConfig &config) preset.set_visible_from_appconfig(config); } +// Restore the mixed-color filament metadata written by export_selections(). Every array is +// resized to the filament count so a project saved with a different filament count, or one +// predating these keys, still yields well-formed parallel arrays. +static void load_mixed_filament_settings(DynamicPrintConfig &project_config, AppConfig &config, + const std::string &printer_name, size_t n_filaments) +{ + std::vector parts; + auto load_bools = [&](const char *key, const char *opt_key) { + auto &vals = project_config.option(opt_key)->values; + if (config.has_printer_setting(printer_name, key)) { + boost::algorithm::split(parts, config.get_printer_setting(printer_name, key), boost::algorithm::is_any_of(",")); + vals.clear(); + for (const auto &p : parts) vals.push_back(p == "1"); + } + vals.resize(n_filaments, false); + }; + auto load_strings = [&](const char *key, const char *opt_key) { + auto &vals = project_config.option(opt_key)->values; + if (config.has_printer_setting(printer_name, key)) { + boost::algorithm::split(parts, config.get_printer_setting(printer_name, key), boost::algorithm::is_any_of("|")); + vals = parts; + } + vals.resize(n_filaments, std::string{}); + }; + + load_bools("filament_is_mixed", "filament_is_mixed"); + load_strings("filament_mixed_components", "filament_mixed_components"); + load_strings("filament_mixed_sublayer_ratios", "filament_mixed_sublayer_ratios"); + load_bools("filament_mixed_gradient", "filament_mixed_gradient"); + load_strings("filament_mixed_gradient_range", "filament_mixed_gradient_range"); + load_strings("filament_mixed_gradient_curve", "filament_mixed_gradient_curve"); + load_bools("filament_mixed_gradient_per_part", "filament_mixed_gradient_per_part"); +} + void PresetBundle::update_selections(AppConfig &config) { std::string initial_printer_profile_name = printers.get_selected_preset_name(); @@ -2784,6 +2829,7 @@ void PresetBundle::update_selections(AppConfig &config) auto flush_multipliers = matrix | boost::adaptors::transformed(boost::lexical_cast); project_config.option("flush_multiplier")->values = std::vector(flush_multipliers.begin(), flush_multipliers.end()); } + load_mixed_filament_settings(project_config, config, initial_printer_profile_name, filament_presets.size()); // Update visibility of presets based on their compatibility with the active printer. // Always try to select a compatible print and filament preset to the current printer preset, @@ -2934,6 +2980,7 @@ void PresetBundle::load_selections(AppConfig &config, const PresetPreferences& p auto flush_multipliers = matrix | boost::adaptors::transformed(boost::lexical_cast); project_config.option("flush_multiplier")->values = std::vector(flush_multipliers.begin(), flush_multipliers.end()); } + load_mixed_filament_settings(project_config, config, initial_printer_profile_name, filament_presets.size()); // Update visibility of presets based on their compatibility with the active printer. // Always try to select a compatible print and filament preset to the current printer preset, @@ -3068,6 +3115,31 @@ void PresetBundle::export_selections(AppConfig &config) "|"); config.set_printer_setting(printer_name, "flush_multiplier", flush_multiplier_str); + // Mixed-color filament metadata. Bools are joined with ',' and strings with '|' because + // the component/ratio/curve strings themselves contain commas. + auto join_bools = [](const std::vector &vals) { + std::string s; + for (size_t i = 0; i < vals.size(); ++i) { + if (i > 0) s += ","; + s += (vals[i] ? "1" : "0"); + } + return s; + }; + if (auto *opt = project_config.option("filament_is_mixed")) + config.set_printer_setting(printer_name, "filament_is_mixed", join_bools(opt->values)); + if (auto *opt = project_config.option("filament_mixed_components")) + config.set_printer_setting(printer_name, "filament_mixed_components", boost::algorithm::join(opt->values, "|")); + if (auto *opt = project_config.option("filament_mixed_sublayer_ratios")) + config.set_printer_setting(printer_name, "filament_mixed_sublayer_ratios", boost::algorithm::join(opt->values, "|")); + if (auto *opt = project_config.option("filament_mixed_gradient")) + config.set_printer_setting(printer_name, "filament_mixed_gradient", join_bools(opt->values)); + if (auto *opt = project_config.option("filament_mixed_gradient_range")) + config.set_printer_setting(printer_name, "filament_mixed_gradient_range", boost::algorithm::join(opt->values, "|")); + if (auto *opt = project_config.option("filament_mixed_gradient_curve")) + config.set_printer_setting(printer_name, "filament_mixed_gradient_curve", boost::algorithm::join(opt->values, "|")); + if (auto *opt = project_config.option("filament_mixed_gradient_per_part")) + config.set_printer_setting(printer_name, "filament_mixed_gradient_per_part", join_bools(opt->values)); + // BBS //config.set("presets", "sla_print", sla_prints.get_selected_preset_name()); //config.set("presets", "sla_material", sla_materials.get_selected_preset_name()); @@ -3103,6 +3175,24 @@ void PresetBundle::set_num_filaments(unsigned int n, std::vector ne filament_volume_map->values.resize(n, static_cast(NozzleVolumeType::nvtStandard)); ams_multi_color_filment.resize(n); + // Mixed-color metadata is a parallel per-filament array set, so it has to grow and shrink + // with the filament count exactly like filament_colour above. Missing this leaves the + // arrays short and every lookup of a newly created slot reads past the end. + if (auto* opt = project_config.option("filament_is_mixed")) + opt->values.resize(n, false); + if (auto* opt = project_config.option("filament_mixed_components")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_sublayer_ratios")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_gradient")) + opt->values.resize(n, false); + if (auto* opt = project_config.option("filament_mixed_gradient_range")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_gradient_curve")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_gradient_per_part")) + opt->values.resize(n, false); + // BBS set new filament color to new_color if (old_filament_count < n) { if (!new_colors.empty()) { @@ -3143,6 +3233,24 @@ void PresetBundle::set_num_filaments(unsigned int n, std::string new_color) filament_volume_map->values.resize(n, static_cast(NozzleVolumeType::nvtStandard)); ams_multi_color_filment.resize(n); + // Mixed-color metadata is a parallel per-filament array set, so it has to grow and shrink + // with the filament count exactly like filament_colour above. Missing this leaves the + // arrays short and every lookup of a newly created slot reads past the end. + if (auto* opt = project_config.option("filament_is_mixed")) + opt->values.resize(n, false); + if (auto* opt = project_config.option("filament_mixed_components")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_sublayer_ratios")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_gradient")) + opt->values.resize(n, false); + if (auto* opt = project_config.option("filament_mixed_gradient_range")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_gradient_curve")) + opt->values.resize(n, std::string{}); + if (auto* opt = project_config.option("filament_mixed_gradient_per_part")) + opt->values.resize(n, false); + //BBS set new filament color to new_color if (old_filament_count < n) { if (!new_color.empty()) { @@ -3215,9 +3323,53 @@ void PresetBundle::update_num_filaments(unsigned int to_del_flament_id) erase_or_resize(filament_color_type->values); erase_or_resize(ams_multi_color_filment); + // Mixed-color metadata. Component IDs reference other slots by 1-based index, so a deleted + // *physical* filament must be remapped out of every mix before the arrays themselves shrink. + // Deleting a mixed slot needs no remap (nothing references a mixed slot as a component). + { + auto *is_mixed_opt = project_config.option("filament_is_mixed"); + auto *comp_opt = project_config.option("filament_mixed_components"); + if (is_mixed_opt && comp_opt) { + bool del_is_physical = (to_del_flament_id >= is_mixed_opt->values.size() + || !is_mixed_opt->values[to_del_flament_id]); + if (del_is_physical) + remap_mixed_components_on_delete(is_mixed_opt->values, comp_opt->values, + to_del_flament_id + 1); + } + if (is_mixed_opt) + erase_or_resize(is_mixed_opt->values); + if (comp_opt) + erase_or_resize(comp_opt->values); + } + if (auto *opt = project_config.option("filament_mixed_sublayer_ratios")) + erase_or_resize(opt->values); + if (auto *opt = project_config.option("filament_mixed_gradient")) + erase_or_resize(opt->values); + if (auto *opt = project_config.option("filament_mixed_gradient_range")) + erase_or_resize(opt->values); + if (auto *opt = project_config.option("filament_mixed_gradient_curve")) + erase_or_resize(opt->values); + if (auto *opt = project_config.option("filament_mixed_gradient_per_part")) + erase_or_resize(opt->values); + update_multi_material_filament_presets(to_del_flament_id); } +bool PresetBundle::is_mixed_filament(size_t idx) const +{ + auto *opt = project_config.option("filament_is_mixed"); + return opt && idx < opt->values.size() && opt->values[idx]; +} + +std::vector PresetBundle::physical_filament_config_indices() const +{ + std::vector indices; + for (size_t i = 0; i < filament_presets.size(); ++i) + if (!is_mixed_filament(i)) + indices.push_back(i); + return indices; +} + void PresetBundle::get_ams_cobox_infos(AMSComboInfo& combox_info) { diff --git a/src/libslic3r/PresetBundle.hpp b/src/libslic3r/PresetBundle.hpp index 9da8fb4251..7640d1ff8d 100644 --- a/src/libslic3r/PresetBundle.hpp +++ b/src/libslic3r/PresetBundle.hpp @@ -497,6 +497,9 @@ public: // Read out the number of extruders from an active printer preset, // update size and content of filament_presets. void update_multi_material_filament_presets(size_t to_delete_filament_id = size_t(-1)); + // Mixed-color filament slots: virtual slots realized from 2-3 physical filaments. + bool is_mixed_filament(size_t idx) const; + std::vector physical_filament_config_indices() const; void on_extruders_count_changed(int extruder_count); diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 509744abe2..33389d27c6 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -2719,6 +2719,19 @@ void Print::process(long long *time_cost_with_cache, bool use_cache) } auto objectExtruderMap = getObjectExtruderMap(*this); + // Resolve mixed filament virtual slots to physical components so brim + // extruder matching works correctly (mixed slot IDs are not present + // in printExtruders after ToolOrdering::resolve_mixed_filaments). + if (m_config.print_sequence != PrintSequence::ByObject && !tool_ordering.layer_tools().empty()) { + const LayerTools &first_lt = tool_ordering.layer_tools().front(); + for (auto &[obj_id, ext_1based] : objectExtruderMap) { + if (ext_1based == 0) + continue; + auto it = first_lt.mixed_filament_resolution.find(ext_1based - 1); + if (it != first_lt.mixed_filament_resolution.end()) + ext_1based = it->second + 1; + } + } std::vector> objPrintVec; for (const PrintInstance* instance : print_object_instances_ordering) { const ObjectID& print_object_ID = instance->print_object->id(); @@ -3776,6 +3789,14 @@ bool Print::is_dynamic_group_reorder() const const bool enabled = opt && opt->value; if (!enabled || m_config.filament_map_mode != FilamentMapMode::fmmAutoForFlush || m_config.nozzle_diameter.size() <= 1) return false; + + // Dynamic regrouping and mixed-color slots are incompatible: a mixed slot is resolved to + // different physical components per layer, so a group assignment made up-front would be wrong. + const auto &is_mixed = m_config.filament_is_mixed.values; + for (unsigned int filament_id : extruders()) { + if (filament_id < is_mixed.size() && is_mixed[filament_id]) + return false; + } return true; } diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index b38a0ca058..efee489c57 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -117,9 +117,9 @@ class PrintRegion public: PrintRegion() = default; PrintRegion(const PrintRegionConfig &config); - PrintRegion(const PrintRegionConfig &config, const size_t config_hash, int print_object_region_id = -1) : m_config(config), m_config_hash(config_hash), m_print_object_region_id(print_object_region_id) {} + PrintRegion(const PrintRegionConfig &config, const size_t config_hash, int print_object_region_id = -1, ObjectID gradient_volume_id = ObjectID()) : m_config(config), m_config_hash(config_hash), m_print_object_region_id(print_object_region_id), m_gradient_volume_id(gradient_volume_id) {} PrintRegion(PrintRegionConfig &&config); - PrintRegion(PrintRegionConfig &&config, const size_t config_hash, int print_object_region_id = -1) : m_config(std::move(config)), m_config_hash(config_hash), m_print_object_region_id(print_object_region_id) {} + PrintRegion(PrintRegionConfig &&config, const size_t config_hash, int print_object_region_id = -1, ObjectID gradient_volume_id = ObjectID()) : m_config(std::move(config)), m_config_hash(config_hash), m_print_object_region_id(print_object_region_id), m_gradient_volume_id(gradient_volume_id) {} ~PrintRegion() = default; // Methods NOT modifying the PrintRegion's state: @@ -129,6 +129,10 @@ public: // Identifier of this PrintRegion in the list of Print::m_print_regions. int print_region_id() const throw() { return m_print_region_id; } int print_object_region_id() const throw() { return m_print_object_region_id; } + // Volume identity used to differentiate same-config regions when per-part gradient is enabled. + // Default-constructed (invalid) means this region is not tied to a specific volume — preserves + // existing behavior for all paths not using per_part_gradient. + ObjectID gradient_volume_id() const throw() { return m_gradient_volume_id; } // 1-based extruder identifier for this region and role. unsigned int extruder(FlowRole role) const; Flow flow(const PrintObject &object, FlowRole role, double layer_height, bool first_layer = false) const; @@ -158,6 +162,10 @@ private: int m_print_region_id { -1 }; int m_print_object_region_id { -1 }; int m_ref_cnt { 0 }; + // Per-part gradient: when non-invalid, this region belongs exclusively to one ModelVolume, + // letting same-color volumes within a combined ModelObject be tracked separately for gradient + // emission. Default invalid -> region keying behaves exactly as before. + ObjectID m_gradient_volume_id; }; inline bool operator==(const PrintRegion &lhs, const PrintRegion &rhs) { return lhs.config_hash() == rhs.config_hash() && lhs.config() == rhs.config(); } @@ -306,6 +314,11 @@ public: Transform3d trafo_bboxes; std::vector cached_volume_ids; + // Per-part gradient: the slot_per_part_enabled bit vector that produced these regions. + // Print::apply compares it against the current one to detect a change that PrintRegionConfig + // alone would not reveal, and regenerates the regions when it differs. + std::vector last_slot_per_part_enabled; + void ref_cnt_inc() { ++ m_ref_cnt; } void ref_cnt_dec() { if (-- m_ref_cnt == 0) delete this; } void clear() { diff --git a/src/libslic3r/PrintApply.cpp b/src/libslic3r/PrintApply.cpp index e2e9bc737d..bb9da850ca 100644 --- a/src/libslic3r/PrintApply.cpp +++ b/src/libslic3r/PrintApply.cpp @@ -1,6 +1,7 @@ #include "ClipperUtils.hpp" #include "Model.hpp" #include "Print.hpp" +#include "FilamentMixer.hpp" #include #include @@ -886,7 +887,12 @@ bool verify_update_print_object_regions( size_t hash = regions[i]->config_hash(); size_t j = i; for (++ j; j < regions.size() && regions[j]->config_hash() == hash; ++ j) - if (regions[i]->config() == regions[j]->config()) { + // Same config but different gradient_volume_id is intentional (per-part gradient + // splitting) and must NOT be flagged as a merge. When per-part is off all regions + // carry an invalid (default) gradient_volume_id, so the AND condition is always + // true and behavior matches the legacy check. + if (regions[i]->config() == regions[j]->config() + && regions[i]->gradient_volume_id() == regions[j]->gradient_volume_id()) { // Regions were merged. We need to reslice. return false; } @@ -978,7 +984,10 @@ static PrintObjectRegions* generate_print_object_regions( const float xy_contour_compensation, const std::vector &painting_extruders, std::vector &variant_index, - const bool has_painted_fuzzy_skin) + const bool has_painted_fuzzy_skin, + // Per-part gradient: slot_per_part_enabled[s-1] is true when mixed slot s has + // filament_mixed_gradient_per_part on. Empty / all-false preserves legacy behavior. + const std::vector &slot_per_part_enabled = {}) { // Reuse the old object or generate a new one. auto out = print_object_regions_old ? std::unique_ptr(print_object_regions_old) : std::make_unique(); @@ -1013,19 +1022,71 @@ static PrintObjectRegions* generate_print_object_regions( update_volume_bboxes(layer_ranges_regions, out->cached_volume_ids, model_volumes, out->trafo_bboxes, is_mm_painted ? 0.f : std::max(0.f, xy_contour_compensation)); std::vector region_set; - auto get_create_region = [®ion_set, &all_regions](PrintRegionConfig &&config) -> PrintRegion* { + // Look up or create a PrintRegion. The optional volume_tag, when valid (non-zero ObjectID), + // keys the region to one ModelVolume so two volumes with identical settings still get + // separate regions — needed so each part can run its own gradient. A default (invalid) + // tag reproduces the previous lookup exactly. + auto get_create_region = [®ion_set, &all_regions](PrintRegionConfig &&config, ObjectID volume_tag = ObjectID()) -> PrintRegion* { size_t hash = config.hash(); - auto it = Slic3r::lower_bound_by_predicate(region_set.begin(), region_set.end(), [&config, hash](const PrintRegion* l) { - return l->config_hash() < hash || (l->config_hash() == hash && l->config() < config); }); - if (it != region_set.end() && (*it)->config_hash() == hash && (*it)->config() == config) + auto it = Slic3r::lower_bound_by_predicate(region_set.begin(), region_set.end(), [&config, hash, volume_tag](const PrintRegion* l) { + return l->config_hash() < hash || (l->config_hash() == hash && l->config() < config) + || (l->config_hash() == hash && l->config() == config && l->gradient_volume_id() < volume_tag); }); + if (it != region_set.end() && (*it)->config_hash() == hash && (*it)->config() == config + && (*it)->gradient_volume_id() == volume_tag) return *it; // Insert into a sorted array, it has O(n) complexity, but the calling algorithm has an O(n^2*log(n)) complexity anyways. - all_regions.emplace_back(std::make_unique(std::move(config), hash, int(all_regions.size()))); + all_regions.emplace_back(std::make_unique(std::move(config), hash, int(all_regions.size()), volume_tag)); PrintRegion *region = all_regions.back().get(); region_set.emplace(it, region); return region; }; + // Per-part gradient: count how many model-part volumes in this object use each + // per-part-enabled gradient slot. Only slots with at least 2 users get their volumes + // tagged — a single-user slot gains nothing from per-volume splitting and would only + // inflate the region count. Empty slot_per_part_enabled leaves this empty, so + // compute_volume_tag below always returns an invalid tag and nothing changes. + std::vector per_part_volume_users; + if (!slot_per_part_enabled.empty()) { + per_part_volume_users.assign(slot_per_part_enabled.size(), 0); + for (const ModelVolume *mv : model_volumes) { + if (! mv->is_model_part()) + continue; + const DynamicPrintConfig *range_cfg = layer_ranges_regions.empty() ? nullptr : layer_ranges_regions.front().config; + PrintRegionConfig vol_cfg = region_config_from_model_volume(default_region_config, range_cfg, *mv, num_extruders, variant_index); + for (unsigned int s_1based : { (unsigned int)vol_cfg.outer_wall_filament_id.value, + (unsigned int)vol_cfg.inner_wall_filament_id.value, + (unsigned int)vol_cfg.sparse_infill_filament_id.value, + (unsigned int)vol_cfg.internal_solid_filament_id.value, + (unsigned int)vol_cfg.top_surface_filament_id.value, + (unsigned int)vol_cfg.bottom_surface_filament_id.value }) { + if (s_1based >= 1 + && size_t(s_1based - 1) < slot_per_part_enabled.size() + && slot_per_part_enabled[s_1based - 1]) + ++per_part_volume_users[s_1based - 1]; + } + } + } + auto compute_volume_tag = [&](const PrintRegionConfig &cfg, const ModelVolume &mv) -> ObjectID { + if (per_part_volume_users.empty()) + return ObjectID(); + auto qualifies = [&](unsigned int s_1based) { + return s_1based >= 1 + && size_t(s_1based - 1) < slot_per_part_enabled.size() + && slot_per_part_enabled[s_1based - 1] + && per_part_volume_users[s_1based - 1] >= 2; + }; + if (qualifies((unsigned int)cfg.outer_wall_filament_id.value) + || qualifies((unsigned int)cfg.inner_wall_filament_id.value) + || qualifies((unsigned int)cfg.sparse_infill_filament_id.value) + || qualifies((unsigned int)cfg.internal_solid_filament_id.value) + || qualifies((unsigned int)cfg.top_surface_filament_id.value) + || qualifies((unsigned int)cfg.bottom_surface_filament_id.value)) { + return mv.id(); + } + return ObjectID(); + }; + // Chain the regions in the order they are stored in the volumes list. for (int volume_id = 0; volume_id < int(model_volumes.size()); ++ volume_id) { const ModelVolume &volume = *model_volumes[volume_id]; @@ -1034,9 +1095,11 @@ static PrintObjectRegions* generate_print_object_regions( if (const PrintObjectRegions::BoundingBox *bbox = find_volume_extents(layer_range, volume); bbox) { if (volume.is_model_part()) { // Add a model volume, assign an existing region or generate a new one. + PrintRegionConfig vol_cfg = region_config_from_model_volume(default_region_config, layer_range.config, volume, num_extruders, variant_index); + ObjectID volume_tag = compute_volume_tag(vol_cfg, volume); layer_range.volume_regions.push_back({ &volume, -1, - get_create_region(region_config_from_model_volume(default_region_config, layer_range.config, volume, num_extruders, variant_index)), + get_create_region(std::move(vol_cfg), volume_tag), bbox }); } else if (volume.is_negative_volume()) { @@ -1121,6 +1184,12 @@ static PrintObjectRegions* generate_print_object_regions( } } + + // Save the slot_per_part_enabled bit vector that produced these regions, so the guard in + // Print::apply can detect changes on the next call even when PrintRegionConfig did not + // change. Always written — including an empty vector — so the snapshot always reflects + // the exact input used to generate the current regions. + out->last_slot_per_part_enabled = slot_per_part_enabled; return out.release(); } @@ -1141,6 +1210,17 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ std::vector used_filaments = this->extruders(true); std::unordered_set used_filament_set(used_filaments.begin(), used_filaments.end()); + // A mixed slot is virtual: the filaments actually consumed are its components, so add them + // to the used set or they would be treated as unused and stripped from the config. + { + auto* is_mixed_opt = new_full_config.option("filament_is_mixed"); + auto* comp_strs_opt = new_full_config.option("filament_mixed_components"); + if (is_mixed_opt && comp_strs_opt && has_any_mixed_filament(is_mixed_opt->values)) { + auto expanded = expand_mixed_filaments(used_filaments, is_mixed_opt->values, comp_strs_opt->values); + used_filament_set.insert(expanded.begin(), expanded.end()); + } + } + //new_full_config.normalize_fdm(used_filaments); new_full_config.normalize_fdm_1(); t_config_option_keys changed_keys = new_full_config.normalize_fdm_2(objects().size(), used_filaments.size()); @@ -1802,6 +1882,29 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ update_filament_self_index_cache(); } + // Per-part gradient: compute the per-slot enable bit vector once for this Print::apply pass. + // Used by generate_print_object_regions to decide which volumes deserve their own PrintRegion. + std::vector slot_per_part_enabled; + { + const auto &is_mixed_vec = m_config.filament_is_mixed.values; + const auto &grad_vec = m_config.filament_mixed_gradient.values; + const auto &per_part_vec = m_config.filament_mixed_gradient_per_part.values; + const auto &components_vec = m_config.filament_mixed_components.values; + slot_per_part_enabled.assign(is_mixed_vec.size(), false); + for (size_t i = 0; i < is_mixed_vec.size(); ++i) { + if (! is_mixed_vec[i]) + continue; + std::vector comps = parse_mixed_components(i < components_vec.size() ? components_vec[i] : ""); + if (comps.size() != 2) + continue; + if (i >= grad_vec.size() || ! grad_vec[i]) + continue; + if (i >= per_part_vec.size() || ! per_part_vec[i]) + continue; + slot_per_part_enabled[i] = true; + } + } + // All regions now have distinct settings. // Check whether applying the new region config defaults we would get different regions, // update regions or create regions from scratch. @@ -1862,6 +1965,15 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ update_apply_status((*it)->invalidate_state_by_config_options(old_config, new_config, diff_keys)); }, print_variant_index)) { + // Per-part gradient: PrintRegionConfig alone cannot reveal a change in which slots + // have per-part enabled, so compare against the snapshot taken when these regions + // were generated and regenerate on any difference (slot toggled, per-part moved + // between slots, eligibility changed via components / gradient / is_mixed). + if (print_object_regions->last_slot_per_part_enabled != slot_per_part_enabled) { + invalidate(); + model_object_status.print_object_regions_status = ModelObjectStatus::PrintObjectRegionsStatus::PartiallyValid; + print_regions_reshuffled = true; + } // Regions are valid, just keep them. } else { // Regions were reshuffled. @@ -1884,7 +1996,8 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ print_object.is_mm_painted() ? 0.f : float(print_object.config().xy_contour_compensation.value), painting_extruders, print_variant_index, - print_object.is_fuzzy_skin_painted()); + print_object.is_fuzzy_skin_painted(), + slot_per_part_enabled); } for (auto it = it_print_object; it != it_print_object_end; ++it) if ((*it)->m_shared_regions) { diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 8083da954e..43559a3120 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -3263,6 +3263,62 @@ void PrintConfigDef::init_fff_params() def->mode = comAdvanced; def->set_default_value(new ConfigOptionBools { false }); + // Mixed-color filament. A slot flagged here is virtual: it is not loaded into any + // physical extruder, but resolved at slicing time into the physical filaments listed + // in filament_mixed_components, blended either by splitting each layer into + // sub-layers or by alternating whole layers (see enable_mixed_color_sublayer). + def = this->add("filament_is_mixed", coBools); + def->label = L("Is mixed filament"); + def->tooltip = L("Whether this filament slot is a mixed filament composed of multiple physical filaments"); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionBools{false}); + + def = this->add("filament_mixed_components", coStrings); + def->label = L("Mixed filament components"); + def->tooltip = L("Comma-separated 1-based indices of component filaments, e.g. \"1,3\""); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionStrings{""}); + + def = this->add("filament_mixed_sublayer_ratios", coStrings); + def->label = L("Mixed filament sublayer ratios"); + def->tooltip = L("Comma-separated ratio values summing to 1.0, e.g. \"0.7,0.3\""); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionStrings{""}); + + def = this->add("filament_mixed_gradient", coBools); + def->label = L("Mixed filament gradient"); + def->tooltip = L("Enable Z-direction gradient mode for mixed filament sub-layers. " + "When enabled, the sub-layer ratios vary linearly across layers."); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionBools{false}); + + def = this->add("filament_mixed_gradient_range", coStrings); + def->label = L("Mixed filament gradient range"); + def->tooltip = L("Start and end ratios for the first component in gradient mode. " + "Comma-separated pair, e.g. \"0.10,0.90\" means 10% to 90%."); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionStrings{""}); + + def = this->add("filament_mixed_gradient_curve", coStrings); + def->label = L("Mixed filament gradient curve"); + def->tooltip = L("Optional Photoshop-style custom curve mapping Z progress to the first " + "component ratio. Encoded as pipe-separated control points, " + "either \"x,y\" (legacy) or \"x,y,m_in,m_out\" when a tangent override " + "is needed (empty token or \"nan\" means use PCHIP default). " + "x in [0,1]; y is clamped to the configured ratio range, " + "e.g. \"0,0.15|0.5,0.50|1,0.85\". When empty, the linear " + "gradient_range is used instead."); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionStrings{""}); + + def = this->add("filament_mixed_gradient_per_part", coBools); + def->label = L("Mixed filament per-part gradient"); + def->tooltip = L("When gradient mode is enabled, apply the gradient to each part of an " + "assembly independently rather than treating the whole assembly as one " + "Z range."); + def->mode = comDevelop; + def->set_default_value(new ConfigOptionBools{false}); + // defined in bits // 0 means cannot support, 1 means support // 0 bit: can support in left extruder @@ -7402,6 +7458,14 @@ void PrintConfigDef::init_fff_params() def->mode = comAdvanced; def->set_default_value(new ConfigOptionFloats { 1. }); + def = this->add("enable_mixed_color_sublayer", coBool); + def->label = L("Mixed color sublayer"); + def->tooltip = L("Enable mixed color sublayer splitting. When enabled, layers containing mixed color " + "filaments will be split into sub-layers to achieve color mixing effects."); + def->category = L("Quality"); + def->mode = comSimple; + def->set_default_value(new ConfigOptionBool(false)); + def = this->add("enable_prime_tower", coBool); def->label = L("Enable"); def->tooltip = L("The wiping tower can be used to clean up residue on the nozzle and stabilize the chamber pressure inside the nozzle in order to avoid appearance defects when printing objects."); diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 255c8721b9..330151c4d3 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -1538,6 +1538,14 @@ PRINT_CONFIG_CLASS_DEFINE( ((ConfigOptionStrings, filament_colour)) ((ConfigOptionStrings, filament_vendor)) ((ConfigOptionBools, filament_is_support)) + // Mixed-color filament: a virtual slot realized from 2-3 physical filaments. + ((ConfigOptionBools, filament_is_mixed)) + ((ConfigOptionStrings, filament_mixed_components)) + ((ConfigOptionStrings, filament_mixed_sublayer_ratios)) + ((ConfigOptionBools, filament_mixed_gradient)) + ((ConfigOptionStrings, filament_mixed_gradient_range)) + ((ConfigOptionStrings, filament_mixed_gradient_curve)) + ((ConfigOptionBools, filament_mixed_gradient_per_part)) ((ConfigOptionInts, filament_printable)) ((ConfigOptionInts, filament_extruder_compatibility)) ((ConfigOptionFloats, filament_change_length)) @@ -1838,6 +1846,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE( ((ConfigOptionInts, nozzle_temperature_range_low)) ((ConfigOptionInts, nozzle_temperature_range_high)) ((ConfigOptionFloats, wipe_distance)) + ((ConfigOptionBool, enable_mixed_color_sublayer)) ((ConfigOptionBool, enable_prime_tower)) ((ConfigOptionBool, prime_tower_enable_framework)) // BBS: change wipe_tower_x and wipe_tower_y data type to floats to add partplate logic diff --git a/src/libslic3r/TexturePainting.cpp b/src/libslic3r/TexturePainting.cpp new file mode 100644 index 0000000000..187f218863 --- /dev/null +++ b/src/libslic3r/TexturePainting.cpp @@ -0,0 +1,663 @@ +#include "TexturePainting.hpp" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "TextureToColor/TextureToColor.hpp" +#include "TextureToColor/ColorUtils.hpp" + +#include "Model.hpp" +#include "TriangleMesh.hpp" +#include "TriangleSelector.hpp" + +namespace Slic3r { + +static cv::Mat decode_texture_image(const TextureImage& img) { + if (img.data.empty()) + return {}; + + // Raw encoded image data (PNG/JPEG) from glTF loader: width == -1 + if (img.width <= 0 || img.height <= 0) { + std::vector buf(img.data.begin(), img.data.end()); + cv::Mat raw(1, static_cast(buf.size()), CV_8UC1, buf.data()); + cv::Mat decoded = cv::imdecode(raw, cv::IMREAD_COLOR); + return decoded; + } + + int cv_type = (img.channels == 4) ? CV_8UC4 : CV_8UC3; + std::vector pixel_buf(img.data.begin(), img.data.end()); + cv::Mat src(img.height, img.width, cv_type, pixel_buf.data()); + + cv::Mat bgr; + if (img.channels == 4) + cv::cvtColor(src, bgr, cv::COLOR_RGBA2BGR); + else if (img.channels == 3) + cv::cvtColor(src, bgr, cv::COLOR_RGB2BGR); + else + return {}; + + return bgr; +} + +static void build_tex2color_mesh( + const TexturedMesh& textured, + tex2color::TriMesh& mesh, + std::vector>& uv_coords) +{ + const size_t nv = textured.vertices.size(); + const size_t nf = textured.indices.size(); + + mesh.vertices.resize(nv); + for (size_t i = 0; i < nv; ++i) { + mesh.vertices[i] = Vec3f( + textured.vertices[i][0], + textured.vertices[i][1], + textured.vertices[i][2]); + } + + mesh.indices.resize(nf); + for (size_t i = 0; i < nf; ++i) { + mesh.indices[i] = Vec3i32( + textured.indices[i][0], + textured.indices[i][1], + textured.indices[i][2]); + } + + uv_coords.resize(nf); + for (size_t fi = 0; fi < nf; ++fi) { + uv_coords[fi].resize(3); + for (int vi = 0; vi < 3; ++vi) { + if (textured.has_face_uvs()) { + int uv_idx = textured.uv_indices[fi][vi]; + if (uv_idx >= 0 && static_cast(uv_idx) < textured.uv_coords.size()) { + uv_coords[fi][vi] = Vec2f( + textured.uv_coords[uv_idx][0], + textured.uv_coords[uv_idx][1]); + } else { + uv_coords[fi][vi] = Vec2f(0.f, 0.f); + } + } else { + int vtx_idx = textured.indices[fi][vi]; + if (vtx_idx >= 0 && static_cast(vtx_idx) < textured.uvs.size()) { + uv_coords[fi][vi] = Vec2f( + textured.uvs[vtx_idx][0], + textured.uvs[vtx_idx][1]); + } else { + uv_coords[fi][vi] = Vec2f(0.f, 0.f); + } + } + } + } +} + +static void extract_painted_mesh( + const tex2color::TriMesh& color_mesh, + const std::vector>& face_colors, + PaintedMesh& painted) +{ + const size_t nv = color_mesh.vertices.size(); + const size_t nf = color_mesh.indices.size(); + + painted.vertices.resize(nv); + for (size_t i = 0; i < nv; ++i) { + const auto& v = color_mesh.vertices[i]; + painted.vertices[i] = {v.x(), v.y(), v.z()}; + } + + painted.indices.resize(nf); + for (size_t i = 0; i < nf; ++i) { + const auto& f = color_mesh.indices[i]; + painted.indices[i] = {f[0], f[1], f[2]}; + } + + painted.face_colors = face_colors; + + std::set> unique_colors(face_colors.begin(), face_colors.end()); + painted.cluster_colors.assign(unique_colors.begin(), unique_colors.end()); +} + +// Build a vertically-stacked atlas from multiple textures and remap per-face UVs. +// +// Sub-textures are laid out left-aligned (x=0) at successive y offsets, with +// atlas_w taken as the maximum width across all sub-textures. UVs must therefore +// be remapped on BOTH axes so that faces belonging to a sub-texture narrower +// than atlas_w sample inside that sub-texture's region (left side of the atlas) +// instead of the right-side zero-padding. Materials that carry only a baseColor +// (no map_Kd / glTF baseColorTexture) get their own 1x1 swatch at the bottom of +// the atlas so their faces sample the correct flat colour rather than being +// silently aliased onto textures[0]. +static bool build_multi_texture_atlas( + const TexturedMesh& textured, + cv::Mat& out_atlas, + std::vector>& out_uv_coords) +{ + std::vector decoded; + decoded.reserve(textured.textures.size()); + for (const auto& ti : textured.textures) + decoded.push_back(decode_texture_image(ti)); + + const bool has_mapping = !textured.material_texture_map.empty(); + const size_t nf = textured.indices.size(); + + auto resolve_tex_idx = [&](int mat_idx) -> int { + if (!has_mapping || mat_idx < 0 + || static_cast(mat_idx) >= textured.material_texture_map.size()) + return -1; + const int ti = textured.material_texture_map[mat_idx]; + if (ti < 0 || static_cast(ti) >= decoded.size() || decoded[ti].empty()) + return -1; + return ti; + }; + + // Determine atlas width (max width across all textures) and per-texture row offsets. + int atlas_w = 0; + int atlas_h = 0; + std::vector y_offsets(decoded.size(), 0); + int first_usable_tex = -1; + for (size_t i = 0; i < decoded.size(); ++i) { + if (decoded[i].empty()) continue; + if (first_usable_tex < 0) first_usable_tex = static_cast(i); + y_offsets[i] = atlas_h; + atlas_w = std::max(atlas_w, decoded[i].cols); + atlas_h += decoded[i].rows; + } + if (atlas_w == 0 || atlas_h == 0) + return false; + + // Collect materials that have a baseColor but no usable texture so we can + // route their faces to a dedicated 1x1 solid swatch instead of aliasing + // them onto textures[0]. + std::map mat_solid_y; // mat_idx -> y row in atlas + std::map> mat_solid_color; // mat_idx -> baseColor (RGBA) + for (size_t fi = 0; fi < nf; ++fi) { + const int mat_idx = (fi < textured.material_ids.size()) ? textured.material_ids[fi] : -1; + if (mat_idx < 0) continue; + if (resolve_tex_idx(mat_idx) >= 0) continue; + if (static_cast(mat_idx) >= textured.material_colors.size()) continue; + if (mat_solid_y.find(mat_idx) != mat_solid_y.end()) continue; + mat_solid_y[mat_idx] = atlas_h++; + mat_solid_color[mat_idx] = textured.material_colors[mat_idx]; + } + + out_atlas = cv::Mat::zeros(atlas_h, atlas_w, CV_8UC3); + for (size_t i = 0; i < decoded.size(); ++i) { + if (decoded[i].empty()) continue; + cv::Mat roi = out_atlas(cv::Rect(0, y_offsets[i], decoded[i].cols, decoded[i].rows)); + decoded[i].copyTo(roi); + } + for (const auto& kv : mat_solid_color) { + const auto& c = kv.second; + // OpenCV stores BGR; baseColor is RGBA in [0,1]. + out_atlas.at(mat_solid_y[kv.first], 0) = cv::Vec3b( + static_cast(std::clamp(c[2] * 255.f, 0.f, 255.f)), + static_cast(std::clamp(c[1] * 255.f, 0.f, 255.f)), + static_cast(std::clamp(c[0] * 255.f, 0.f, 255.f))); + } + + out_uv_coords.resize(nf); + for (size_t fi = 0; fi < nf; ++fi) { + const int mat_idx = (fi < textured.material_ids.size()) ? textured.material_ids[fi] : -1; + const int tex_idx = resolve_tex_idx(mat_idx); + + // Pick the atlas region this face samples from. + int y_off = 0, x_off = 0, th = atlas_h, tw = atlas_w; + bool use_solid = false; + if (tex_idx >= 0) { + y_off = y_offsets[tex_idx]; + th = decoded[tex_idx].rows; + tw = decoded[tex_idx].cols; + } else if (mat_idx >= 0 && mat_solid_y.count(mat_idx) > 0) { + y_off = mat_solid_y[mat_idx]; + th = 1; + tw = 1; + use_solid = true; + } else if (first_usable_tex >= 0) { + // Last-resort fallback: faces without a material or without any + // baseColor still need somewhere to sample; the first usable + // texture preserves legacy behaviour and, with the per-axis + // remapping below, no longer aliases onto the zero-padded right + // margin even when sub-textures have unequal widths. + y_off = y_offsets[first_usable_tex]; + th = decoded[first_usable_tex].rows; + tw = decoded[first_usable_tex].cols; + } + + out_uv_coords[fi].resize(3); + for (int vi = 0; vi < 3; ++vi) { + float u = 0.f, v = 0.f; + if (textured.has_face_uvs()) { + int uv_idx = textured.uv_indices[fi][vi]; + if (uv_idx >= 0 && static_cast(uv_idx) < textured.uv_coords.size()) { + u = textured.uv_coords[uv_idx][0]; + v = textured.uv_coords[uv_idx][1]; + } + } else { + int vtx_idx = textured.indices[fi][vi]; + if (vtx_idx >= 0 && static_cast(vtx_idx) < textured.uvs.size()) { + u = textured.uvs[vtx_idx][0]; + v = textured.uvs[vtx_idx][1]; + } + } + if (use_solid) { + // Aim at the centre of the 1x1 swatch so bilinear sampling + // (in tex2color) cannot drift into neighbouring rows. + const float u_atlas = (x_off + 0.5f) / static_cast(atlas_w); + const float v_atlas = (y_off + 0.5f) / static_cast(atlas_h); + out_uv_coords[fi][vi] = Vec2f(u_atlas, v_atlas); + } else { + // Wrap to [0,1) on both axes (OBJ tile UVs may step outside + // the unit square), then scale by the sub-texture extents so + // samples land inside its actual region. Without scaling u, + // any sub-texture narrower than atlas_w would have all its + // faces sampled from the right-side zero-padding. + u = u - std::floor(u); + v = v - std::floor(v); + const float u_atlas = (x_off + u * tw) / static_cast(atlas_w); + const float v_atlas = (y_off + v * th) / static_cast(atlas_h); + out_uv_coords[fi][vi] = Vec2f(u_atlas, v_atlas); + } + } + } + return true; +} + +bool texture_to_painting( + const TexturedMesh& textured, + PaintedMesh& painted, + const TexturePaintingSettings& settings, + PaintProgressCallback progress, + PaintCancelCallback cancel) +{ + if (textured.vertices.empty() || textured.indices.empty() || textured.textures.empty()) + return false; + + cv::Mat texture; + tex2color::TriMesh input_mesh; + std::vector> uv_coords; + + const bool multi_tex = textured.textures.size() > 1 && !textured.material_texture_map.empty(); + + if (multi_tex) { + if (!build_multi_texture_atlas(textured, texture, uv_coords)) + return false; + // Build mesh geometry (atlas UVs already computed above) + const size_t nv = textured.vertices.size(); + const size_t nf = textured.indices.size(); + input_mesh.vertices.resize(nv); + for (size_t i = 0; i < nv; ++i) + input_mesh.vertices[i] = Vec3f( + textured.vertices[i][0], textured.vertices[i][1], textured.vertices[i][2]); + input_mesh.indices.resize(nf); + for (size_t i = 0; i < nf; ++i) + input_mesh.indices[i] = Vec3i32( + textured.indices[i][0], textured.indices[i][1], textured.indices[i][2]); + } else { + texture = decode_texture_image(textured.textures[0]); + if (texture.empty()) + return false; + build_tex2color_mesh(textured, input_mesh, uv_coords); + } + + tex2color::TextureToColorSettings algo_settings; + algo_settings.target_colors_num = settings.target_colors_num; + algo_settings.smooth_weight = settings.smooth_weight; + algo_settings.oversampling_iters = settings.oversampling_iters; + switch (settings.mesh_repair_decision) { + case TexturePaintingSettings::MeshRepairDecision::Ask: + algo_settings.mesh_repair_decision = tex2color::MeshRepairDecision::Ask; + break; + case TexturePaintingSettings::MeshRepairDecision::RepairAndImport: + algo_settings.mesh_repair_decision = tex2color::MeshRepairDecision::RepairAndImport; + break; + case TexturePaintingSettings::MeshRepairDecision::ImportWithoutRepair: + default: + algo_settings.mesh_repair_decision = tex2color::MeshRepairDecision::ImportWithoutRepair; + break; + } + + tex2color::AlgoProgressCallback algo_progress = nullptr; + if (progress) { + algo_progress = [&progress](tex2color::AlgoProgress p) { + progress(p.percent, p.message); + }; + } + + tex2color::AlgoCancelCallback algo_cancel = nullptr; + if (cancel) { + algo_cancel = [&cancel]() -> bool { return cancel(); }; + } + + tex2color::TriMesh color_mesh; + std::vector> face_colors; + algo_settings.mesh_repair_decision_required = settings.mesh_repair_decision_required; + algo_settings.mesh_repair_callback = settings.mesh_repair_callback; + + bool ok = tex2color::TextureToColor( + input_mesh, uv_coords, texture, + color_mesh, face_colors, + algo_settings, algo_progress, algo_cancel); + + if (!ok) + return false; + + extract_painted_mesh(color_mesh, face_colors, painted); + return true; +} + +double compute_delta_e( + const std::array& rgb1, + const std::array& rgba2) +{ + return tex2color::color_utils::calc_rgb_color_difference_by_ciede2000( + rgb1, + { + static_cast(rgba2[0] * 255.0f), + static_cast(rgba2[1] * 255.0f), + static_cast(rgba2[2] * 255.0f) + }); +} + +std::vector match_clusters_to_filaments( + const std::vector>& cluster_colors, + const std::vector>& filament_colors, + const std::vector& /*filament_names*/) +{ + std::vector matches(cluster_colors.size()); + + for (size_t ci = 0; ci < cluster_colors.size(); ++ci) { + matches[ci].cluster_index = static_cast(ci); + matches[ci].cluster_color = cluster_colors[ci]; + matches[ci].delta_e = 1e9; + + for (size_t fi = 0; fi < filament_colors.size(); ++fi) { + double de = compute_delta_e(cluster_colors[ci], filament_colors[fi]); + if (de < matches[ci].delta_e) { + matches[ci].delta_e = de; + matches[ci].filament_index = static_cast(fi); + matches[ci].filament_color = filament_colors[fi]; + } + } + } + return matches; +} + +bool apply_painted_mesh_to_volume( + const PaintedMesh& painted, + const std::vector& matches, + ModelVolume& volume) +{ + if (painted.face_colors.empty() || matches.empty()) + return false; + + const auto& cluster_colors = painted.cluster_colors; + std::map, int> color_to_filament; + for (const auto& m : matches) { + if (m.cluster_index >= 0 && m.cluster_index < (int)cluster_colors.size() && m.filament_index >= 0) + color_to_filament[cluster_colors[m.cluster_index]] = m.filament_index; + } + + indexed_triangle_set its; + its.vertices.resize(painted.vertices.size()); + for (size_t i = 0; i < painted.vertices.size(); ++i) { + its.vertices[i] = Vec3f( + painted.vertices[i][0], + painted.vertices[i][1], + painted.vertices[i][2]); + } + its.indices.resize(painted.indices.size()); + for (size_t i = 0; i < painted.indices.size(); ++i) { + its.indices[i] = Vec3i32( + painted.indices[i][0], + painted.indices[i][1], + painted.indices[i][2]); + } + + TriangleMesh new_mesh(std::move(its)); + + // The volume already went through ModelObject::add_volume -> + // center_geometry_after_creation, which translated its mesh by + // -source.mesh_offset (and folded that shift into the volume + // transformation). The painted mesh, however, is derived from the + // raw textured mesh and is therefore expressed in the original + // un-centered coordinate frame. Reuse the exact recorded shift to + // align it -- do NOT compute it from the bounding-box centers of + // the two meshes: tex2color::TextureToColor performs subdivision + // and CGAL polygon-soup repair, so the painted vertex count and + // bbox no longer match the original textured mesh and a bbox- + // center alignment would silently displace the geometry. + // + // If the model has been scaled by Model::convert_from_meters / + // convert_from_imperial_units after load, the painted mesh fed + // here is already in millimetres (Model::convert_* also scales + // texture_mesh in place) while source.mesh_offset was recorded + // before the conversion and therefore still lives in the original + // pre-scaled frame. Bring it into the same frame as the painted + // vertices so the alignment shift below stays correct on the + // textured-import path. This compensation is scoped to this + // function so that other (non-textured) import paths are not + // affected. + Vec3d mesh_offset = volume.source.mesh_offset; + double unit_scale = 1.0; + if (volume.source.is_converted_from_meters) + unit_scale = 1000.0; + else if (volume.source.is_converted_from_inches) + unit_scale = 25.4; + if (unit_scale != 1.0) + mesh_offset *= unit_scale; + + if (!mesh_offset.isApprox(Vec3d::Zero())) + new_mesh.translate(-mesh_offset.cast()); + new_mesh.set_init_shift(mesh_offset); + + // Log bbox drift for diagnostics. Subdivision + CGAL polygon-soup + // repair routinely changes vertex count and bbox, so moderate drift + // is expected and must not block the apply. + if (!new_mesh.empty() && !volume.mesh().empty()) { + const Vec3d new_center = new_mesh.bounding_box().center(); + const Vec3d cur_center = volume.mesh().bounding_box().center(); + const double diag = volume.mesh().bounding_box().size().norm(); + const double drift = (new_center - cur_center).norm(); + if (drift > 0.05 * std::max(1.0, diag)) + BOOST_LOG_TRIVIAL(warning) + << "apply_painted_mesh_to_volume: painted bbox center drifted by " + << drift << " (bbox diag=" << diag + << ", unit_scale=" << unit_scale + << ", from_meters=" << volume.source.is_converted_from_meters + << ", from_inches=" << volume.source.is_converted_from_inches << ")"; + else if (drift > 1e-3 * std::max(1.0, diag)) + BOOST_LOG_TRIVIAL(info) + << "apply_painted_mesh_to_volume: minor bbox drift " + << drift << " (bbox diag=" << diag + << ", unit_scale=" << unit_scale << ")"; + } + + volume.set_mesh(std::move(new_mesh)); + volume.calculate_convex_hull(); + + // Re-center the replaced mesh so its bbox center sits at the origin, + // matching what center_geometry_after_creation did for the original mesh. + // CGAL repair / subdivision may shift the bbox center (drift); without + // re-centering, the volume offset (which was computed for the original + // centered mesh) no longer matches, causing the model to float or clip. + // Pass false to keep source.mesh_offset unchanged. + volume.center_geometry_after_creation(false); + volume.invalidate_convex_hull_2d(); + + // Mesh geometry has been replaced; any per-face annotation indexed + // against the previous triangle set is now stale. mmu_segmentation_facets + // is rewritten below from the new selector; reset the others so future + // import paths that carry support / seam / fuzzy_skin painting cannot + // leak indices from the old mesh into the new one. + volume.supported_facets.reset(); + volume.fuzzy_skin_facets.reset(); + volume.seam_facets.reset(); + + if (ModelObject* obj = volume.get_object()) + obj->invalidate_bounding_box(); + + TriangleSelector selector(volume.mesh()); + for (size_t fi = 0; fi < painted.face_colors.size() && fi < (size_t)volume.mesh().its.indices.size(); ++fi) { + auto it = color_to_filament.find(painted.face_colors[fi]); + if (it != color_to_filament.end()) { + int extruder_idx = it->second; + auto state = static_cast( + static_cast(EnforcerBlockerType::Extruder1) + extruder_idx); + if (state <= EnforcerBlockerType::ExtruderMax) + selector.set_facet(static_cast(fi), state); + } + } + + volume.mmu_segmentation_facets.set(selector); + return true; +} + +bool decode_texture_to_pixels( + const TextureImage& img, + std::vector& out_pixels, + int& out_w, int& out_h) +{ + cv::Mat decoded = decode_texture_image(img); + if (decoded.empty()) + return false; + + // decoded is BGR, CV_8UC3 + out_w = decoded.cols; + out_h = decoded.rows; + size_t nbytes = (size_t)out_w * out_h * 3; + out_pixels.resize(nbytes); + + if (decoded.isContinuous()) { + std::memcpy(out_pixels.data(), decoded.data, nbytes); + } else { + for (int r = 0; r < out_h; ++r) + std::memcpy(out_pixels.data() + r * out_w * 3, decoded.ptr(r), out_w * 3); + } + return true; +} + +// Sample face color from texture using 3 explicit UV values (centroid + bilinear). +static std::array sample_face_from_uvs( + const cv::Mat& tex, + const std::array& uv0, + const std::array& uv1, + const std::array& uv2) +{ + float cu = (uv0[0] + uv1[0] + uv2[0]) / 3.f; + float cv_val = (uv0[1] + uv1[1] + uv2[1]) / 3.f; + + cu = cu - std::floor(cu); + cv_val = cv_val - std::floor(cv_val); + + float fx = cu * (tex.cols - 1); + float fy = cv_val * (tex.rows - 1); + + int x0 = std::clamp(static_cast(fx), 0, tex.cols - 1); + int y0 = std::clamp(static_cast(fy), 0, tex.rows - 1); + int x1 = std::min(x0 + 1, tex.cols - 1); + int y1 = std::min(y0 + 1, tex.rows - 1); + + float wx = fx - x0; + float wy = fy - y0; + + const int ch = tex.channels(); + auto sample = [&](int row, int col) -> std::array { + const uchar* ptr = tex.data + row * tex.step[0] + col * ch; + return {static_cast(ptr[2]), static_cast(ptr[1]), static_cast(ptr[0])}; + }; + + auto c00 = sample(y0, x0); + auto c10 = sample(y0, x1); + auto c01 = sample(y1, x0); + auto c11 = sample(y1, x1); + + std::array color; + for (int i = 0; i < 3; ++i) { + float top = c00[i] * (1.f - wx) + c10[i] * wx; + float bot = c01[i] * (1.f - wx) + c11[i] * wx; + color[i] = static_cast(std::clamp(top * (1.f - wy) + bot * wy, 0.f, 255.f)); + } + return color; +} + +// Legacy overload: look up UVs from per-vertex array by vertex indices. +static std::array sample_face_from_texture( + const cv::Mat& tex, + const std::vector>& uvs, + const std::array& face) +{ + std::array uv0 = {0.f, 0.f}, uv1 = {0.f, 0.f}, uv2 = {0.f, 0.f}; + if (face[0] >= 0 && static_cast(face[0]) < uvs.size()) uv0 = uvs[face[0]]; + if (face[1] >= 0 && static_cast(face[1]) < uvs.size()) uv1 = uvs[face[1]]; + if (face[2] >= 0 && static_cast(face[2]) < uvs.size()) uv2 = uvs[face[2]]; + return sample_face_from_uvs(tex, uv0, uv1, uv2); +} + +bool sample_original_face_colors( + const TexturedMesh& textured, + std::vector>& out_face_colors) +{ + if (textured.indices.empty()) + return false; + + // Decode all textures up front + std::vector decoded_textures; + decoded_textures.reserve(textured.textures.size()); + for (const auto& ti : textured.textures) { + decoded_textures.push_back(decode_texture_image(ti)); + } + + const bool has_mapping = !textured.material_texture_map.empty(); + const size_t nf = textured.indices.size(); + out_face_colors.resize(nf); + + for (size_t fi = 0; fi < nf; ++fi) { + int mat_idx = (fi < textured.material_ids.size()) ? textured.material_ids[fi] : -1; + + int tex_idx = -1; + if (has_mapping && mat_idx >= 0 && static_cast(mat_idx) < textured.material_texture_map.size()) + tex_idx = textured.material_texture_map[mat_idx]; + else if (!decoded_textures.empty()) + tex_idx = 0; // fallback: single-texture model + + if (tex_idx >= 0 && static_cast(tex_idx) < decoded_textures.size() + && !decoded_textures[tex_idx].empty()) { + if (textured.has_face_uvs()) { + const auto& ui = textured.uv_indices[fi]; + auto get_uv = [&](int vi) -> std::array { + int idx = ui[vi]; + if (idx >= 0 && static_cast(idx) < textured.uv_coords.size()) + return textured.uv_coords[idx]; + return {0.f, 0.f}; + }; + out_face_colors[fi] = sample_face_from_uvs( + decoded_textures[tex_idx], get_uv(0), get_uv(1), get_uv(2)); + } else { + out_face_colors[fi] = sample_face_from_texture( + decoded_textures[tex_idx], textured.uvs, textured.indices[fi]); + } + } else if (has_mapping && mat_idx >= 0 + && static_cast(mat_idx) < textured.material_colors.size()) { + // No texture — use baseColorFactor as solid color + const auto& c = textured.material_colors[mat_idx]; + out_face_colors[fi] = { + static_cast(std::clamp(c[0] * 255.f, 0.f, 255.f)), + static_cast(std::clamp(c[1] * 255.f, 0.f, 255.f)), + static_cast(std::clamp(c[2] * 255.f, 0.f, 255.f)) + }; + } else { + out_face_colors[fi] = {192, 192, 192}; // default gray + } + } + return true; +} + +} // namespace Slic3r diff --git a/src/libslic3r/TexturePainting.hpp b/src/libslic3r/TexturePainting.hpp new file mode 100644 index 0000000000..ac98e968c7 --- /dev/null +++ b/src/libslic3r/TexturePainting.hpp @@ -0,0 +1,115 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +struct indexed_triangle_set; + +namespace Slic3r { + +class TriangleMesh; +class ModelVolume; + +struct TextureImage { + int width = 0; + int height = 0; + int channels = 4; + std::vector data; +}; + +struct TexturedMesh { + std::vector> vertices; + std::vector> indices; + std::vector> uvs; + std::vector textures; + std::vector material_ids; + // material index -> index in textures[] (-1 if no texture, use material_colors) + std::vector material_texture_map; + // per-material baseColorFactor (RGBA 0-1), indexed by material index + std::vector> material_colors; + + // Per-face independent UV support (for OBJ where the same vertex can have + // different texture coordinates on different faces). + std::vector> uv_coords; // UV coordinate pool + std::vector> uv_indices; // per-face UV indices into uv_coords + + bool has_face_uvs() const { return !uv_indices.empty() && !uv_coords.empty(); } +}; + +struct PaintedMesh { + std::vector> vertices; + std::vector> indices; + std::vector> face_colors; // per-face RGB [0..255] + std::vector> cluster_colors; +}; + +using PaintProgressCallback = std::function; +using PaintCancelCallback = std::function; +using PaintMeshRepairCallback = std::function progress_callback, + std::function cancel_callback, + std::string* error_message)>; + +struct TexturePaintingSettings { + std::size_t target_colors_num = 4; + double smooth_weight = 0.5; + std::size_t oversampling_iters = 0; + enum class MeshRepairDecision { + Ask, + ImportWithoutRepair, + RepairAndImport + }; + MeshRepairDecision mesh_repair_decision = MeshRepairDecision::ImportWithoutRepair; + bool* mesh_repair_decision_required = nullptr; + PaintMeshRepairCallback mesh_repair_callback; +}; + +struct FilamentMatch { + int cluster_index = -1; + int filament_index = -1; + double delta_e = 0.0; + std::array cluster_color = {0,0,0}; + std::array filament_color = {0,0,0,1}; +}; + +bool texture_to_painting( + const TexturedMesh& textured, + PaintedMesh& painted, + const TexturePaintingSettings& settings = {}, + PaintProgressCallback progress = nullptr, + PaintCancelCallback cancel = nullptr); + +std::vector match_clusters_to_filaments( + const std::vector>& cluster_colors, + const std::vector>& filament_colors, + const std::vector& filament_names); + +double compute_delta_e( + const std::array& rgb1, + const std::array& rgba2); + +bool apply_painted_mesh_to_volume( + const PaintedMesh& painted, + const std::vector& matches, + ModelVolume& volume); + +// Decode a TextureImage (which may contain raw PNG/JPEG bytes) into BGR pixel data. +// On success, populates out_pixels (BGR, 3 bytes/pixel) and sets out_w/out_h. +bool decode_texture_to_pixels( + const TextureImage& img, + std::vector& out_pixels, + int& out_w, int& out_h); + +// Sample per-face colors from the correct texture per material_ids. +// Uses material_texture_map / material_colors for multi-material GLBs. +// Falls back to textures[0] when the mapping is absent. +bool sample_original_face_colors( + const TexturedMesh& textured, + std::vector>& out_face_colors); + +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/Callbacks.hpp b/src/libslic3r/TextureToColor/Callbacks.hpp new file mode 100644 index 0000000000..70084f585d --- /dev/null +++ b/src/libslic3r/TextureToColor/Callbacks.hpp @@ -0,0 +1,15 @@ +#pragma once +#include + +namespace Slic3r { namespace tex2color { + +struct AlgoProgress { + int percent = 0; + const char* message = ""; +}; + +using AlgoProgressCallback = std::function; +using AlgoCancelCallback = std::function; + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/CgalUtils.hpp b/src/libslic3r/TextureToColor/CgalUtils.hpp new file mode 100644 index 0000000000..109d454827 --- /dev/null +++ b/src/libslic3r/TextureToColor/CgalUtils.hpp @@ -0,0 +1,173 @@ +#pragma once +#include "TriMesh.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Slic3r { namespace tex2color { +namespace cgalutils { + +using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; +using CGALMesh = CGAL::Surface_mesh; + +inline CGALMesh trimesh_to_cgal(const TriMesh& mesh) { + CGALMesh cm; + std::vector vmap(mesh.vertices.size()); + for (size_t i = 0; i < mesh.vertices.size(); ++i) + vmap[i] = cm.add_vertex(Kernel::Point_3(mesh.vertices[i].x(), mesh.vertices[i].y(), mesh.vertices[i].z())); + for (const auto& f : mesh.indices) { + cm.add_face(vmap[f[0]], vmap[f[1]], vmap[f[2]]); + } + return cm; +} + +inline TriMesh cgal_to_trimesh(const CGALMesh& cm) { + TriMesh mesh; + std::map vmap; + size_t idx = 0; + for (auto v : cm.vertices()) { + if (!cm.is_valid(v) || cm.is_removed(v)) continue; + auto p = cm.point(v); + mesh.vertices.push_back(Vec3f((float)p.x(), (float)p.y(), (float)p.z())); + vmap[v] = idx++; + } + for (auto f : cm.faces()) { + if (!cm.is_valid(f) || cm.is_removed(f)) continue; + auto h = cm.halfedge(f); + auto v0 = cm.target(h); + auto v1 = cm.target(cm.next(h)); + auto v2 = cm.target(cm.next(cm.next(h))); + mesh.indices.push_back(Vec3i32((int)vmap[v0], (int)vmap[v1], (int)vmap[v2])); + } + return mesh; +} + +inline bool is_mesh_halfedge_compatible(const TriMesh& mesh) { + std::vector> vtx_to_adj_faces(mesh.vertices.size()); + std::size_t edge_id = 0; + std::vector> edge_to_faces; + std::vector> vtx_to_prev_vtxs(mesh.vertices.size()); + std::vector> vtx_to_next_vtxs(mesh.vertices.size()); + std::vector> vtx_vtx_to_edge(mesh.vertices.size()); + + for (std::size_t fid = 0; fid < mesh.indices.size(); ++fid) { + const TriFace& face = mesh.indices[fid]; + if (face[0] == face[1] || face[1] == face[2] || face[2] == face[0]) { + return false; + } + for (std::size_t i = 0; i < 3; ++i) { + if (static_cast(face[i]) >= mesh.vertices.size()) { + return false; + } + vtx_to_adj_faces[face[i]].insert(fid); + + std::size_t prev_vtx = face[(i + 2) % 3]; + std::size_t next_vtx = face[(i + 1) % 3]; + + if (vtx_to_prev_vtxs[face[i]].count(prev_vtx)) { + return false; + } + vtx_to_prev_vtxs[face[i]].insert(prev_vtx); + + if (vtx_to_next_vtxs[face[i]].count(next_vtx)) { + return false; + } + vtx_to_next_vtxs[face[i]].insert(next_vtx); + } + + for (std::size_t i = 0; i < 3; ++i) { + std::size_t va = face[i]; + std::size_t vb = face[(i + 1) % 3]; + if (!vtx_vtx_to_edge[va].count(vb)) { + vtx_vtx_to_edge[va][vb] = edge_id; + vtx_vtx_to_edge[vb][va] = edge_id; + ++edge_id; + edge_to_faces.emplace_back(std::unordered_set()); + } + edge_to_faces[vtx_vtx_to_edge[va][vb]].insert(fid); + } + } + + for (std::size_t vid = 0; vid < mesh.vertices.size(); ++vid) { + if (vtx_to_adj_faces[vid].empty()) { + continue; + } + std::unordered_set visited_faces; + std::queue face_queue; + face_queue.push(*(vtx_to_adj_faces[vid].begin())); + visited_faces.insert(*(vtx_to_adj_faces[vid].begin())); + while (!face_queue.empty()) { + std::size_t fid = face_queue.front(); + face_queue.pop(); + const TriFace& face = mesh.indices[fid]; + for (std::size_t i = 0; i < 3; ++i) { + if (static_cast(face[i]) != vid) { + continue; + } + std::size_t v_next = face[(i + 1) % 3]; + std::size_t v_prev = face[(i + 2) % 3]; + for (std::size_t nbr : {v_next, v_prev}) { + std::size_t eid = vtx_vtx_to_edge[vid][nbr]; + for (std::size_t adj_fid : edge_to_faces[eid]) { + if (!visited_faces.count(adj_fid) && vtx_to_adj_faces[vid].count(adj_fid)) { + visited_faces.insert(adj_fid); + face_queue.push(adj_fid); + } + } + } + break; + } + } + + for (std::size_t fid : vtx_to_adj_faces[vid]) { + if (!visited_faces.count(fid)) { + return false; + } + } + } + + return true; +} + +inline bool convert_trimesh_to_cgal(const TriMesh& mesh, CGALMesh& cgal_mesh) { + cgal_mesh = trimesh_to_cgal(mesh); + return cgal_mesh.number_of_faces() > 0 || mesh.indices.empty(); +} + +inline bool convert_trimesh_to_cgal( + const TriMesh& mesh, const std::vector& vertex_uvs, + CGALMesh& cgal_mesh, std::vector& cgal_vertex_uvs) +{ + cgal_mesh.clear(); + std::vector vmap(mesh.vertices.size()); + cgal_vertex_uvs.clear(); + + for (size_t i = 0; i < mesh.vertices.size(); ++i) { + vmap[i] = cgal_mesh.add_vertex(Kernel::Point_3( + mesh.vertices[i].x(), mesh.vertices[i].y(), mesh.vertices[i].z())); + } + + cgal_vertex_uvs.resize(cgal_mesh.num_vertices()); + for (size_t i = 0; i < mesh.vertices.size(); ++i) { + if (i < vertex_uvs.size()) + cgal_vertex_uvs[vmap[i]] = vertex_uvs[i]; + else + cgal_vertex_uvs[vmap[i]] = Vec2f(0.f, 0.f); + } + + for (const auto& f : mesh.indices) + cgal_mesh.add_face(vmap[f[0]], vmap[f[1]], vmap[f[2]]); + + return true; +} + +} // namespace cgalutils +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/ColorUtils.cpp b/src/libslic3r/TextureToColor/ColorUtils.cpp new file mode 100644 index 0000000000..7127a5d364 --- /dev/null +++ b/src/libslic3r/TextureToColor/ColorUtils.cpp @@ -0,0 +1,1643 @@ +#include "ColorUtils.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CgalUtils.hpp" +#include "libslic3r/AABBTreeIndirect.hpp" +#include + +namespace Slic3r { namespace tex2color { +namespace color_utils { + +// #define DEBUG_FLAG + +#ifndef M_PI +#define M_PI 3.1415926535897932 +#endif + +#ifndef EPSILON +#define EPSILON 1e-6 +#endif + +#ifndef DOUBLE_LIMITS +#define DOUBLE_LIMITS +#define Double_MAX std::numeric_limits::max() +#define Double_MIN -std::numeric_limits::max() +#endif // !DOUBLE_LIMITS + +namespace PMP = CGAL::Polygon_mesh_processing; + +using cgalutils::CGALMesh; +using CGALKernel = cgalutils::Kernel; + +static constexpr double TOPO_SMOOTH_WEIGHT_THRESHOLD = 0.3; + +namespace detail { +template +double average_edge_length_impl(const Mesh& m) { + double total = 0.0; + size_t count = 0; + for (auto e : m.edges()) { + auto h = m.halfedge(e); + auto p0 = m.point(m.source(h)); + auto p1 = m.point(m.target(h)); + total += std::sqrt(CGAL::squared_distance(p0, p1)); + ++count; + } + return count > 0 ? total / count : 1.0; +} +} // namespace detail + +typedef CGAL::Aff_transformation_3 Affine_transformation_3; +typedef boost::graph_traits::halfedge_descriptor halfedge_descriptor; +typedef boost::graph_traits::edge_descriptor edge_descriptor; +typedef boost::graph_traits::vertex_descriptor vertex_descriptor; +typedef CGAL::AABB_face_graph_triangle_primitive Primitive; +typedef CGAL::AABB_traits Traits; +typedef CGAL::AABB_tree Tree; +typedef CGALMesh::template Property_map VNMap; + +static inline ColorDouble convert_rgb_uint_to_rgb_double(const Color& color) { + return ColorDouble{static_cast(color[0]), static_cast(color[1]), static_cast(color[2])}; +} + +static void normalize(CGALKernel::Vector_3& vec) { + double squared_length = vec.squared_length(); + if (squared_length > EPSILON) { + vec /= sqrt(squared_length); + } +} + +static double get_angle_between_vectors(const CGALKernel::Vector_3& v1, const CGALKernel::Vector_3& v2) { + CGALKernel::Vector_3 dir1{v1}, dir2{v2}; + normalize(dir1); + normalize(dir2); + double product_dot = dir1.x() * dir2.x() + dir1.y() * dir2.y() + dir1.z() * dir2.z(); + if (product_dot > 1.0 - EPSILON) { + return 0.0; + } else if (product_dot < -1.0 + EPSILON) { + return 180.0; + } + return std::acos(product_dot) / M_PI * 180.0; +} + +static void calc_face_normals(const CGALMesh& mesh, std::vector& face_normals) { + std::size_t fcnt = mesh.number_of_faces(); + face_normals.resize(fcnt); + for (auto face : mesh.faces()) { + std::vector points; + for (auto vtx : mesh.vertices_around_face(mesh.halfedge(face))) { + points.push_back(mesh.point(vtx)); + } + Eigen::Vector3d pos1(points[0].x(), points[0].y(), points[0].z()); + Eigen::Vector3d pos2(points[1].x(), points[1].y(), points[1].z()); + Eigen::Vector3d pos3(points[2].x(), points[2].y(), points[2].z()); + face_normals[face] = (pos3 - pos2).cross(pos1 - pos2); + face_normals[face].normalize(); + } + return; +} + +static bool check_and_repair_self_intersect(CGALMesh& mesh, bool* is_self_intersect_status = nullptr) { + // true means the mesh is no self intersect now + // false means the mesh is still self intersect + auto is_self_intersect = PMP::does_self_intersect(mesh); + if (is_self_intersect_status != nullptr) { + *is_self_intersect_status = is_self_intersect; + } + if (is_self_intersect) { + bool repair = PMP::experimental::remove_self_intersections(mesh); + if (repair) { + return true; + } else { + return false; + } + } + return true; +} + +static bool save_polylines(const std::string& file_name, const std::vector>& polylines) { + std::vector points; + std::vector> lines; + for (auto& polyline : polylines) { + std::size_t begin_pt_idx = points.size(); + for (auto& pt : polyline) { + points.push_back(pt); + } + for (std::size_t i = 1; i < polyline.size(); ++i) { + lines.emplace_back(begin_pt_idx + i, begin_pt_idx + i + 1); // obj is begin at 1 + } + } + std::ofstream output_file(file_name, std::ios::out); + for (auto& point : points) { + output_file << "v " << point[0] << " " << point[1] << " " << point[2] << "\n"; + } + for (auto& line : lines) { + output_file << "l " << line.first << " " << line.second << "\n"; + } + output_file.close(); + return true; +} + +static bool smooth_region_topo_boundary(CGALMesh& mesh, std::vector& face_labels, std::size_t max_iters = 20) { + // Topological smoothing: reassign face labels + std::size_t iter = 0; + while (iter < max_iters) { + ++iter; + bool flip_flag = false; + for (auto face : mesh.faces()) { + std::size_t same_label_count = 0; + std::unordered_map map_label_to_cnt; + std::size_t max_adj_cnt = 0; + std::size_t max_adj_label = face_labels[face]; + for (auto adj_face : mesh.faces_around_face(mesh.halfedge(face))) { + if (adj_face == CGALMesh::null_face() || !mesh.is_valid(adj_face) || mesh.is_removed(adj_face)) { + continue; + } + if (face_labels[adj_face] == face_labels[face]) { + ++same_label_count; + } else { + ++map_label_to_cnt[face_labels[adj_face]]; + if (map_label_to_cnt[face_labels[adj_face]] > max_adj_cnt) { + max_adj_cnt = map_label_to_cnt[face_labels[adj_face]]; + max_adj_label = face_labels[adj_face]; + } + } + } + if (max_adj_cnt > same_label_count) { + face_labels[face] = max_adj_label; + flip_flag = true; + } + } + + if (!flip_flag) { + break; + } + } + return true; +} + +static bool smooth_region_geom_boundary(CGALMesh& mesh, std::vector& face_labels, const SmoothParameters& smooth_parameters) { + // 1. extract boundary vertices and make polines + std::unordered_map map_vtx_to_degree; + std::unordered_set segment_boundary_edges; + std::unordered_set feature_edges; + std::unordered_set feature_vertices; + constexpr double feature_angle = 45; + for (const auto& edge : mesh.edges()) { + if (!mesh.is_valid(edge) || mesh.is_border(edge)) { + continue; + } + auto source = mesh.source(mesh.halfedge(edge)); + auto target = mesh.target(mesh.halfedge(edge)); + auto face_1 = mesh.face(mesh.halfedge(edge)); + auto face_2 = mesh.face(mesh.opposite(mesh.halfedge(edge))); + auto normal_1 = PMP::compute_face_normal(face_1, mesh); + auto normal_2 = PMP::compute_face_normal(face_2, mesh); + double angle = get_angle_between_vectors(normal_1, normal_2); + if (angle > feature_angle) { + feature_edges.insert(edge); + feature_vertices.insert(source); + feature_vertices.insert(target); + } + + if (face_labels[face_1] == face_labels[face_2]) { + continue; + } + + segment_boundary_edges.insert(edge); + ++map_vtx_to_degree[source]; + ++map_vtx_to_degree[target]; + } + + // 2. smooth each polyline + std::vector> polylines; + std::unordered_set visited_edges; + + std::function&)> trace_polyline = [&](std::vector& polyline) -> void { + if (polyline.empty()) { + return; + } + CGAL::SM_Vertex_index curr_vtx = polyline.back(); + if (map_vtx_to_degree[curr_vtx] != 2) { + return; + } + for (const auto& halfedge : mesh.halfedges_around_target(mesh.halfedge(curr_vtx))) { + CGAL::SM_Edge_index edge = mesh.edge(halfedge); + if (visited_edges.count(edge) || !segment_boundary_edges.count(edge)) { + continue; + } + visited_edges.insert(edge); + CGAL::SM_Vertex_index adj_vtx = mesh.source(halfedge); + if (!map_vtx_to_degree.count(adj_vtx)) { + continue; + } + polyline.push_back(adj_vtx); + return trace_polyline(polyline); + } + }; + + // 2.1. open polyline: from T nodes search other 2-degree nodes + for (auto& [src_vtx, degree] : map_vtx_to_degree) { + if (degree == 2) { + continue; + } + for (auto& src_halfedge : mesh.halfedges_around_target(mesh.halfedge(src_vtx))) { + CGAL::SM_Edge_index src_edge = mesh.edge(src_halfedge); + if (visited_edges.count(src_edge) || !segment_boundary_edges.count(src_edge)) { + continue; + } + visited_edges.insert(src_edge); + CGAL::SM_Vertex_index adj_vtx = mesh.source(src_halfedge); + if (!map_vtx_to_degree.count(adj_vtx)) { + continue; + } + std::vector polyline{src_vtx, adj_vtx}; + trace_polyline(polyline); + polylines.push_back(std::move(polyline)); + } + } + + // 2.2. closed polylines + for (auto edge : segment_boundary_edges) { + if (visited_edges.count(edge)) { + continue; + } + visited_edges.insert(edge); + CGAL::SM_Halfedge_index halfedge = mesh.halfedge(edge); + std::vector polyline{mesh.source(halfedge), mesh.target(halfedge)}; + trace_polyline(polyline); + if (polyline.front() != polyline.back()) { + std::cerr << "[Error]: loop polyline but not closed!!!\n"; + } + polylines.push_back(std::move(polyline)); + } + + // 3. smooth boundary + Tree boundary_tree(mesh.faces().begin(), mesh.faces().end(), mesh); + boundary_tree.accelerate_distance_queries(); + + constexpr std::size_t max_iters = 5; + const double smooth_weight = smooth_parameters.smooth_weight; // Controls smoothing intensity; larger values produce smoother results. Range: 0.1~1.0. + double origin_weight = std::max(1.0 - smooth_weight, 0.0); + for (std::size_t iter = 0; iter < max_iters; ++iter) { + for (const auto& polyline : polylines) { + std::size_t pt_cnt = polyline.size(); + std::vector points(pt_cnt); + for (std::size_t pt_idx = 1; pt_idx + 1 < pt_cnt; ++pt_idx) { + std::vector pts{mesh.point(polyline[pt_idx - 1]), mesh.point(polyline[pt_idx + 1])}; + CGALKernel::Point_3 smooth_pt = CGAL::ORIGIN + ((CGAL::centroid(pts.begin(), pts.end()) - CGAL::ORIGIN) * smooth_weight + + (mesh.point(polyline[pt_idx]) - CGAL::ORIGIN) * origin_weight); + points[pt_idx] = boundary_tree.closest_point(smooth_pt); + } + + if (polyline.front() == polyline.back()) { + if (feature_vertices.count(polyline.front())) { + continue; + } + std::vector pts{mesh.point(polyline[1]), mesh.point(polyline[pt_cnt - 2])}; + CGALKernel::Point_3 smooth_pt = CGAL::ORIGIN + ((CGAL::centroid(pts.begin(), pts.end()) - CGAL::ORIGIN) * smooth_weight + + (mesh.point(polyline.front()) - CGAL::ORIGIN) * origin_weight); + mesh.point(polyline.front()) = boundary_tree.closest_point(smooth_pt); + } + + for (std::size_t pt_idx = 1; pt_idx + 1 < pt_cnt; ++pt_idx) { + if (feature_vertices.count(polyline[pt_idx])) { + continue; + } + mesh.point(polyline[pt_idx]) = points[pt_idx]; + } + } + } + + for (auto& polyline : polylines) { + for (auto& vtx : polyline) { + mesh.point(vtx) = boundary_tree.closest_point(mesh.point(vtx)); + } + } + + return true; +} + +// HSV, XYZ, and LAB are only used internally for computing color differences, so they are declared in this cpp file only. +typedef std::array HSV; +typedef std::array XYZ; // Intermediate space for converting between LAB and RGB +typedef std::array LAB; // CIELAB was designed to match human visual perception; the standard method for perceptual color difference +// Common white points +const XYZ D65_WHITE = {0.95047, 1.0, 1.08883}; + +/** + * @brief Convert an RGB color to the HSV color space. + * + * @param rgb Input RGB color [R, G, B], range 0~255. + * @return HSV output [H, S, V], H in 0~360, S and V in 0~1. + */ +static HSV convert_rgb_to_hsv(const RGB& rgb) { + // Normalize to [0, 1] + double r = rgb[0] / 255.0; + double g = rgb[1] / 255.0; + double b = rgb[2] / 255.0; + + double max = std::max({r, g, b}); + double min = std::min({r, g, b}); + double delta = max - min; + + // Compute hue H + double h = 0; + if (delta == 0) { + h = 0; // Gray; hue is undefined + } else { + if (max == r) { + h = 60.0 * fmod((g - b) / delta, 6.0); + } else if (max == g) { + h = 60.0 * ((b - r) / delta + 2.0); + } else { // max == b + h = 60.0 * ((r - g) / delta + 4.0); + } + if (h < 0) { + h += 360.0; + } + } + + // Compute saturation S + double s = (max == 0) ? 0 : (delta / max); + + // Compute value V + double v = max; + + return {h, s, v}; +} + +/** + * @brief Convert an HSV color to the RGB color space. + * + * @param hsv Input HSV color [H, S, V], H in 0~360, S and V in 0~1. + * @return RGB output [R, G, B], range 0~255. + */ +static RGB convert_hsv_to_rgb(const HSV& hsv) { + double h = hsv[0]; + double s = hsv[1]; + double v = hsv[2]; + + double c = v * s; + double x = c * (1 - std::abs(fmod(h / 60.0, 2.0) - 1)); + double m = v - c; + + double r, g, b; + + if (h < 60) { + r = c; + g = x; + b = 0; + } else if (h < 120) { + r = x; + g = c; + b = 0; + } else if (h < 180) { + r = 0; + g = c; + b = x; + } else if (h < 240) { + r = 0; + g = x; + b = c; + } else if (h < 300) { + r = x; + g = 0; + b = c; + } else { + r = c; + g = 0; + b = x; + } + + return {static_cast((r + m) * 255 + 0.5), static_cast((g + m) * 255 + 0.5), static_cast((b + m) * 255 + 0.5)}; +} + +static XYZ convert_rgb_to_xyz(const RGB& color_rgb) { + ColorDouble rgb{static_cast(color_rgb[0]), static_cast(color_rgb[1]), static_cast(color_rgb[2])}; + auto gammaCorrect = [](double v) -> double { + v = v / 255.0; + if (v > 0.04045) { + return std::pow((v + 0.055) / 1.055, 2.4); + } else { + return v / 12.92; + } + }; + + double r = gammaCorrect(rgb[0]); + double g = gammaCorrect(rgb[1]); + double b = gammaCorrect(rgb[2]); + + // sRGB to XYZ matrix + return {r * 0.4124564 + g * 0.3575761 + b * 0.1804375, r * 0.2126729 + g * 0.7151522 + b * 0.0721750, r * 0.0193339 + g * 0.1191920 + b * 0.9503041}; +} + +// sRGB non-linear channel values [0,1] (double) -> linear light -> XYZ; equivalent to convert_rgb_to_xyz when v=n/255 +static XYZ convert_srgb01_to_xyz(double rs, double gs, double bs) { + auto gamma_correct = [](double v) -> double { + v = std::clamp(v, 0.0, 1.0); + return (v > 0.04045) ? std::pow((v + 0.055) / 1.055, 2.4) : (v / 12.92); + }; + const double r = gamma_correct(rs); + const double g = gamma_correct(gs); + const double b = gamma_correct(bs); + return {r * 0.4124564 + g * 0.3575761 + b * 0.1804375, r * 0.2126729 + g * 0.7151522 + b * 0.0721750, r * 0.0193339 + g * 0.1191920 + b * 0.9503041}; +} + +static LAB convert_xyz_to_lab(const XYZ& xyz) { + auto f = [](double t) -> double { + const double delta = 6.0 / 29.0; + if (t > delta * delta * delta) { + return std::cbrt(t); + } else { + return t / (3.0 * delta * delta) + 4.0 / 29.0; + } + }; + + // D65 white point + double xn = D65_WHITE[0], yn = D65_WHITE[1], zn = D65_WHITE[2]; + + double fx = f(xyz[0] / xn); + double fy = f(xyz[1] / yn); + double fz = f(xyz[2] / zn); + + return {116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz)}; +} + +// ColorDouble here represents sRGB [0,1]; see calc_rgb_color_difference_by_ciede2000_srgb01 header comment +static LAB convert_srgb01_to_lab(const ColorDouble& srgb01) { + return convert_xyz_to_lab(convert_srgb01_to_xyz(srgb01[0], srgb01[1], srgb01[2])); +} + +static LAB convert_rgb_to_lab(const RGB& rgb) { + return convert_xyz_to_lab(convert_rgb_to_xyz(rgb)); +} + +static Color convert_lab_to_rgb(const LAB& lab) { + // Lab → XYZ + const double delta = 6.0 / 29.0; + const double delta2x3 = 3.0 * delta * delta; + + double fy = (lab[0] + 16.0) / 116.0; + double fx = lab[1] / 500.0 + fy; + double fz = fy - lab[2] / 200.0; + + double x = D65_WHITE[0] * (fx > delta ? fx * fx * fx : delta2x3 * (fx - 4.0 / 29.0)); + double y = D65_WHITE[1] * (fy > delta ? fy * fy * fy : delta2x3 * (fy - 4.0 / 29.0)); + double z = D65_WHITE[2] * (fz > delta ? fz * fz * fz : delta2x3 * (fz - 4.0 / 29.0)); + + // XYZ -> linear RGB (sRGB inverse matrix) + double r_lin = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z; + double g_lin = -0.9692660 * x + 1.8760108 * y + 0.0415560 * z; + double b_lin = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z; + + // linear RGB -> sRGB (inverse gamma correction) + auto inverse_gamma = [](double v) -> double { + v = std::max(v, 0.0); + return v <= 0.0031308 ? 12.92 * v : 1.055 * std::pow(v, 1.0 / 2.4) - 0.055; + }; + + auto to_uint8 = [](double v) -> std::size_t { return static_cast(std::clamp(std::round(v * 255.0), 0.0, 255.0)); }; + + return {to_uint8(inverse_gamma(r_lin)), to_uint8(inverse_gamma(g_lin)), to_uint8(inverse_gamma(b_lin))}; +} + +/** + * @brief CIEDE2000 color-difference computation. + * + * Currently the most accurate color-difference formula, recommended by CIE as the industry standard. + * + * @param lab1 LAB values of the first color. + * @param lab2 LAB values of the second color. + * @return Color difference (typically < 1.0 is imperceptible to the human eye). + */ +static double ciede2000(const std::array& lab1, const std::array& lab2) { + // Parameters in the CIE L*C*h* formula + double L1 = lab1[0], a1 = lab1[1], b1 = lab1[2]; + double L2 = lab2[0], a2 = lab2[1], b2 = lab2[2]; + + // Compute C1 and C2 + double C1 = std::sqrt(a1 * a1 + b1 * b1); + double C2 = std::sqrt(a2 * a2 + b2 * b2); + double C_avg = (C1 + C2) / 2.0; + + // G factor (compensates for non-linearity in the mid-low chroma region) + double C7 = C_avg * C_avg * C_avg * C_avg * C_avg * C_avg * C_avg; + double G = 0.5 * (1.0 - std::sqrt(C7 / (C7 + 6103515625.0))); + + // a1' and a2' + double a1_prime = (1.0 + G) * a1; + double a2_prime = (1.0 + G) * a2; + + // C'1 and C'2 + double C1_prime = std::sqrt(a1_prime * a1_prime + b1 * b1); + double C2_prime = std::sqrt(a2_prime * a2_prime + b2 * b2); + double C_prime_avg = (C1_prime + C2_prime) / 2.0; + + // h'1 and h'2 + double h1_prime = std::atan2(b1, a1_prime); + double h2_prime = std::atan2(b2, a2_prime); + if (h1_prime < 0) { + h1_prime += 2 * M_PI; + } + if (h2_prime < 0) { + h2_prime += 2 * M_PI; + } + + // Compute dh' + double dh_prime; + if (std::abs(h1_prime - h2_prime) <= M_PI) { + dh_prime = h2_prime - h1_prime; + } else if (h2_prime <= h1_prime) { + dh_prime = h2_prime - h1_prime + 2 * M_PI; + } else { + dh_prime = h2_prime - h1_prime - 2 * M_PI; + } + + // Compute dH' + double dH_prime = 2.0 * std::sqrt(C1_prime * C2_prime) * std::sin(dh_prime / 2.0); + + // Compute dL' + double dL_prime = L2 - L1; + + // Compute dC' + double dC_prime = C2_prime - C1_prime; + + // Compute h_prime_avg + double h_prime_avg; + if (std::abs(h1_prime - h2_prime) > M_PI) { + h_prime_avg = (h1_prime + h2_prime + 2 * M_PI) / 2.0; + } else { + h_prime_avg = (h1_prime + h2_prime) / 2.0; + } + + // Compute T + double T = 1.0 - 0.17 * std::cos(h_prime_avg - M_PI / 6.0) + 0.24 * std::cos(2.0 * h_prime_avg) + 0.32 * std::cos(3.0 * h_prime_avg + M_PI / 30.0) - + 0.20 * std::cos(4.0 * h_prime_avg - 3.0 * M_PI / 6.0); + + // Compute rotation term R_T = -R_C * sin(2*delta_theta), where delta_theta = 30 * exp(-((h_bar'-275)/25)^2) + // h_prime_avg is in radians; convert to degrees for delta_theta; 2*delta_theta = 60 * exp(...), convert back to radians for sin + double h_prime_avg_deg = h_prime_avg * 180.0 / M_PI; + double C_prime_avg_7 = std::pow(C_prime_avg, 7); + double R = -2.0 * std::sqrt(C_prime_avg_7 / (C_prime_avg_7 + 6103515625.0)) * + std::sin((60.0 * M_PI / 180.0) * std::exp(-std::pow((h_prime_avg_deg - 275.0) / 25.0, 2))); + + // Compute SL, SC, SH + double L_prime_avg = (L1 + L2) / 2.0; + double SL = 1.0 + 0.015 * std::pow(L_prime_avg - 50.0, 2) / std::sqrt(20 + std::pow(L_prime_avg - 50.0, 2)); + double SC = 1.0 + 0.045 * C_prime_avg; + double SH = 1.0 + 0.015 * C_prime_avg * T; + + // Final color difference + double deltaE = std::sqrt(std::pow(dL_prime / SL, 2) + std::pow(dC_prime / SC, 2) + std::pow(dH_prime / SH, 2) + R * (dC_prime / SC) * (dH_prime / SH)); + + return deltaE; +} + +double calc_rgb_color_difference_by_ciede2000(const RGB& rgb1, const RGB& rgb2) { + auto lab1 = convert_rgb_to_lab(rgb1); + auto lab2 = convert_rgb_to_lab(rgb2); + return ciede2000(lab1, lab2); +} + +double calc_rgb_color_difference_by_ciede2000_srgb01(const ColorDouble& rgb1, const ColorDouble& rgb2) { + const LAB lab1 = convert_srgb01_to_lab(rgb1); + const LAB lab2 = convert_srgb01_to_lab(rgb2); + return ciede2000(lab1, lab2); +} + +// Working-space distance function type: inputs are two colors in the same space (RGB-double or Lab) +using WorkingDistFunc = double (*)(const ColorDouble&, const ColorDouble&); + +// Farthest Point Sampling (FPS) initialization algorithm. +// The first center is the point nearest to the global centroid; subsequent centers are the points farthest from the existing set. +static std::vector farthest_point_sampling_init(const std::vector& working_colors, std::size_t k, WorkingDistFunc dist_func) { + std::vector centers(k); + + // 1. Compute the global centroid and pick the nearest point as the first center + ColorDouble centroid = {0.0, 0.0, 0.0}; + for (const auto& c : working_colors) { + centroid[0] += c[0]; + centroid[1] += c[1]; + centroid[2] += c[2]; + } + const auto n = static_cast(working_colors.size()); + centroid[0] /= n; + centroid[1] /= n; + centroid[2] /= n; + + double best_dist = std::numeric_limits::max(); + std::size_t first_idx = 0; + for (std::size_t i = 0; i < working_colors.size(); ++i) { + double d = dist_func(working_colors[i], centroid); + if (d < best_dist) { + best_dist = d; + first_idx = i; + } + } + centers[0] = working_colors[first_idx]; + + // Minimum distance from each point to the already-selected center set + std::vector min_distances(working_colors.size(), std::numeric_limits::max()); + + // 2. Greedily select the remaining K-1 centers: pick the point with the largest min_distance each time + for (std::size_t i = 1; i < k; ++i) { + const ColorDouble& last_center = centers[i - 1]; + + // Update each point's minimum distance with the newly added center + double farthest_dist = -1.0; + std::size_t farthest_idx = 0; + for (std::size_t c_idx = 0; c_idx < working_colors.size(); ++c_idx) { + double d = dist_func(working_colors[c_idx], last_center); + if (d < min_distances[c_idx]) { + min_distances[c_idx] = d; + } + if (min_distances[c_idx] > farthest_dist) { + farthest_dist = min_distances[c_idx]; + farthest_idx = c_idx; + } + } + + centers[i] = working_colors[farthest_idx]; + } + + return centers; +} + +bool remesh_mesh(TriMesh& bbs_mesh, std::vector& face_labels, double target_edge_length_ratio) { + if (face_labels.size() != bbs_mesh.indices.size()) { + BOOST_LOG_TRIVIAL(warning) << "Input mesh face count does not match label count"; + return false; + } + + // Back up face labels for recovery after remeshing. + std::vector face_labels_of_original_mesh(face_labels); + + CGALMesh cgal_mesh; + cgalutils::convert_trimesh_to_cgal(bbs_mesh, cgal_mesh); + + // AABBTreeIndirect references vertices/faces externally, so snapshot the + // pre-remesh geometry by moving it out of bbs_mesh (which is overwritten + // below with the post-remesh mesh). std::move on std::vector is O(1). + TriVertices old_vertices = std::move(bbs_mesh.vertices); + TriFaces old_indices = std::move(bbs_mesh.indices); + auto original_mesh_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(old_vertices, old_indices); + + std::unordered_set feature_edges; + std::unordered_set feature_vertices; + CGALMesh::Property_map constrained_edges = + cgal_mesh.add_property_map("constrained_edges", false).first; + CGALMesh::Property_map constrained_vertices = + cgal_mesh.add_property_map("constrained_vertices", false).first; + + // An edge is considered a geometric feature edge if its dihedral angle is less than 135 degrees (loose threshold) + constexpr double feature_angle = 135; + + auto is_feature_edge = [&](CGAL::SM_Edge_index edge) -> bool { + if (cgal_mesh.is_border(edge)) { + return true; + } + auto halfedge_1 = cgal_mesh.halfedge(edge); + auto halfedge_2 = cgal_mesh.opposite(halfedge_1); + auto face_1 = cgal_mesh.face(halfedge_1); + auto face_2 = cgal_mesh.face(halfedge_2); + if (face_labels[face_1] != face_labels[face_2]) { + // Boundary between different color regions; treated as a feature edge + return true; + } + // TODO: CGAL remeshing tends to crash when too many constrained edges are added; needs handling + //auto normal_1 = PMP::compute_face_normal(face_1, cgal_mesh); + //auto normal_2 = PMP::compute_face_normal(face_2, cgal_mesh); + //double angle = 180 - get_angle_between_vectors(normal_1, normal_2); + //BOOST_LOG_TRIVIAL(debug) << "end.\n"; + //return angle > feature_angle; + return false; + }; + + for (auto edge : cgal_mesh.edges()) { + if (is_feature_edge(edge)) { + feature_edges.insert(edge); + feature_vertices.insert(cgal_mesh.source(cgal_mesh.halfedge(edge))); + feature_vertices.insert(cgal_mesh.target(cgal_mesh.halfedge(edge))); + constrained_edges[edge] = true; + constrained_vertices[cgal_mesh.source(cgal_mesh.halfedge(edge))] = true; + constrained_vertices[cgal_mesh.target(cgal_mesh.halfedge(edge))] = true; + } + } + +#ifdef DEBUG_FLAG + BOOST_LOG_TRIVIAL(debug) << "remesh_mesh: feature_edges.size() = " << feature_edges.size() << ".\n"; + + std::vector> polylines; + for (auto edge : feature_edges) { + auto src_vtx = cgal_mesh.source(cgal_mesh.halfedge(edge)); + auto trg_vtx = cgal_mesh.target(cgal_mesh.halfedge(edge)); + polylines.push_back({cgal_mesh.point(src_vtx), cgal_mesh.point(trg_vtx)}); + } + save_polylines("ColorUtils_remesh_feature_lines.obj", polylines); +#endif // DEBUG_FLAG + + std::size_t iters = 5; +#ifdef DEBUG_FLAG + BOOST_LOG_TRIVIAL(debug) << "PMP::isotropic_remeshing start...\n"; +#endif // DEBUG_FLAG + // TODO: CGAL remeshing preserves geometric boundaries but does not maintain face labels well; colors need to be recomputed + PMP::isotropic_remeshing(cgal_mesh.faces(), target_edge_length_ratio * detail::average_edge_length_impl(cgal_mesh), cgal_mesh, + CGAL::parameters::number_of_iterations(iters) + .protect_constraints(true) + .edge_is_constrained_map(constrained_edges) + .vertex_is_constrained_map(constrained_vertices) + .collapse_constraints(true)); +#ifdef DEBUG_FLAG + BOOST_LOG_TRIVIAL(debug) << "PMP::isotropic_remeshing finshed...\n"; +#endif // DEBUG_FLAG + if (PMP::does_self_intersect(cgal_mesh)) { + PMP::experimental::remove_self_intersections(cgal_mesh); + } + + bbs_mesh.clear(); + + std::unordered_map map_cgal_vtx_to_bbs_vtx; + TriVertices bbs_vertices; + TriFaces bbs_faces; + face_labels.clear(); + face_labels.reserve(cgal_mesh.number_of_faces()); + + for (const auto& cgal_vtx : cgal_mesh.vertices()) { + if (!cgal_mesh.is_valid(cgal_vtx) || cgal_mesh.is_removed(cgal_vtx) || cgal_mesh.is_isolated(cgal_vtx)) { + continue; + } + if (!map_cgal_vtx_to_bbs_vtx.count(cgal_vtx)) { + map_cgal_vtx_to_bbs_vtx[cgal_vtx] = bbs_vertices.size(); + const auto& cgal_point = cgal_mesh.point(cgal_vtx); + bbs_vertices.emplace_back(TriVertex(cgal_point.x(), cgal_point.y(), cgal_point.z())); + } + } + + for (const auto& cgal_face : cgal_mesh.faces()) { + if (!cgal_mesh.is_valid(cgal_face) || cgal_mesh.is_removed(cgal_face)) { + continue; + } + TriFace bbs_face; + std::size_t face_vid = 0; + for (auto cgal_vtx : cgal_mesh.vertices_around_face(cgal_mesh.halfedge(cgal_face))) { + do { if (!(map_cgal_vtx_to_bbs_vtx.count(cgal_vtx))) { BOOST_LOG_TRIVIAL(warning) << "CGAL mesh contains a face with an invalid vertex"; return false; } } while(0); + bbs_face[face_vid] = map_cgal_vtx_to_bbs_vtx[cgal_vtx]; + ++face_vid; + } + bbs_faces.push_back(std::move(bbs_face)); + } + + bbs_mesh = TriMesh(bbs_faces, bbs_vertices); + + face_labels.resize(bbs_mesh.indices.size()); + tbb::parallel_for(tbb::blocked_range(0, bbs_mesh.indices.size()), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + auto& face = bbs_mesh.indices[fid]; + Vec3f face_centroid = (bbs_mesh.vertices[face[0]] + bbs_mesh.vertices[face[1]] + bbs_mesh.vertices[face[2]]) / 3.0; + size_t hit_idx = 0; + Vec3f closest; + AABBTreeIndirect::squared_distance_to_indexed_triangle_set( + old_vertices, old_indices, original_mesh_tree, face_centroid, hit_idx, closest); + face_labels[fid] = face_labels_of_original_mesh[hit_idx]; + } + }); + + return true; +} + +bool is_closed(const TriMesh& bbs_mesh) { + CGALMesh cgal_mesh; + cgalutils::convert_trimesh_to_cgal(bbs_mesh, cgal_mesh); + +#ifdef DEBUG_FLAG + std::size_t border_edges_count = 0; + std::size_t edges_count = 0; + for (auto edge : cgal_mesh.edges()) { + if (cgal_mesh.is_border(edge)) { + ++border_edges_count; + } + ++edges_count; + } + + BOOST_LOG_TRIVIAL(debug) << "border edges count = " << border_edges_count << "\n"; + BOOST_LOG_TRIVIAL(debug) << "edges count = " << edges_count << "\n"; + + std::size_t border_faces_count = 0; + std::size_t faces_count = 0; + for (auto face : cgal_mesh.faces()) { + for (auto halfedge : cgal_mesh.halfedges_around_face(cgal_mesh.halfedge(face))) { + auto edge = cgal_mesh.edge(halfedge); + if (cgal_mesh.is_border(edge)) { + ++border_faces_count; + break; + } + } + ++faces_count; + } + + BOOST_LOG_TRIVIAL(debug) << "border faces count = " << border_faces_count << "\n"; + BOOST_LOG_TRIVIAL(debug) << "faces count = " << faces_count << "\n"; + + BOOST_LOG_TRIVIAL(debug) << "vertices count = " << cgal_mesh.number_of_vertices() << "\n"; + std::size_t num_of_components = 0; + std::unordered_set visited_faces; + for (auto src_face : cgal_mesh.faces()) { + if (visited_faces.count(src_face)) { + continue; + } + ++num_of_components; + std::queue que; + que.push(src_face); + visited_faces.insert(src_face); + while (!que.empty()) { + auto curr_face = que.front(); + que.pop(); + for (auto adj_face : cgal_mesh.faces_around_face(cgal_mesh.halfedge(curr_face))) { + if (!cgal_mesh.is_valid(adj_face) || cgal_mesh.is_removed(adj_face) || visited_faces.count(adj_face)) { + continue; + } + que.push(adj_face); + visited_faces.insert(adj_face); + } + } + } + BOOST_LOG_TRIVIAL(debug) << "components count = " << num_of_components << "\n"; +#endif // DEBUG_FLAG + + return CGAL::is_closed(cgal_mesh); +} + +static bool smooth_region_labels(TriMesh& tri_mesh, std::vector& face_labels, const SmoothParameters& smooth_parameters) { + CGALMesh mesh; + cgalutils::convert_trimesh_to_cgal(tri_mesh, mesh); + + if (mesh.number_of_faces() != face_labels.size()) { + BOOST_LOG_TRIVIAL(warning) << "Face count does not match label count"; + return false; + } + + if (smooth_parameters.smooth_weight >= TOPO_SMOOTH_WEIGHT_THRESHOLD) { + // Topological smoothing: reassign face labels. + smooth_region_topo_boundary(mesh, face_labels); + } + + if (smooth_parameters.smooth_weight > EPSILON) { + // Geometric smoothing: smooth polylines and project back onto the original mesh. + smooth_region_geom_boundary(mesh, face_labels, smooth_parameters); + } + + tri_mesh = cgalutils::cgal_to_trimesh(mesh); + + return true; +} + +bool smooth_region(TriMesh& tri_mesh, std::vector>& face_colors, const SmoothParameters& smooth_parameters) { + // Convert colors to labels + std::size_t label_next = 0; + std::vector face_labels; + face_labels.reserve(face_colors.size()); + std::map, std::size_t> map_color_to_label; + std::unordered_map> map_label_to_color; + for (auto& color : face_colors) { + if (!map_color_to_label.count(color)) { + map_color_to_label[color] = label_next; + map_label_to_color[label_next] = color; + ++label_next; + } + face_labels.push_back(map_color_to_label[color]); + } + + if (!smooth_region_labels(tri_mesh, face_labels, smooth_parameters)) + return false; + + // Convert labels back to colors + for (std::size_t fid = 0; fid < face_labels.size(); ++fid) { + face_colors[fid] = map_label_to_color[face_labels[fid]]; + } + + return true; +} + +bool smooth_region(TriMesh& tri_mesh, std::vector& face_labels, const SmoothParameters& smooth_parameters) { + return smooth_region_labels(tri_mesh, face_labels, smooth_parameters); +} + +// Compute the squared Euclidean distance between two colors (RGB vectors) +double calc_rgb_color_difference_by_squared_rgb_double(const ColorDouble& c1, const ColorDouble& c2) { + double dr = c1[0] - c2[0]; + double dg = c1[1] - c2[1]; + double db = c1[2] - c2[2]; + return dr * dr + dg * dg + db * db; +} + +// Compute the squared Euclidean distance between two colors (RGB vectors) +double calc_rgb_color_difference_by_squared_rgb(const RGB& c1, const RGB& c2) { + auto c1d = convert_rgb_uint_to_rgb_double(c1); + auto c2d = convert_rgb_uint_to_rgb_double(c2); + return calc_rgb_color_difference_by_squared_rgb_double(c1d, c2d); +} + +// K-Means core: run FPS initialization + assign/update iterations in working space, return cluster centers +static std::vector kmeans_core(const std::vector& working_colors, std::size_t k, std::size_t max_iter, WorkingDistFunc dist_func, + const std::function& cancel_cb = nullptr) { + std::vector centers = farthest_point_sampling_init(working_colors, k, dist_func); + std::vector assignments(working_colors.size()); + + for (std::size_t iter = 0; iter < max_iter; ++iter) { + if (cancel_cb && cancel_cb()) return centers; + std::atomic changed(false); + + tbb::parallel_for(tbb::blocked_range(0, working_colors.size()), [&](const tbb::blocked_range& range) { + for (std::size_t i = range.begin(); i < range.end(); ++i) { + double min_dist = std::numeric_limits::max(); + std::size_t best_cluster = 0; + + for (std::size_t j = 0; j < k; ++j) { + double d = dist_func(working_colors[i], centers[j]); + if (d < min_dist) { + min_dist = d; + best_cluster = j; + } + } + + if (assignments[i] != best_cluster) { + changed.store(true, std::memory_order_relaxed); + assignments[i] = best_cluster; + } + } + }); + + if (!changed.load()) { + break; + } + + std::vector new_centers(k, {0.0, 0.0, 0.0}); + std::vector counts(k, 0); + + for (std::size_t i = 0; i < working_colors.size(); ++i) { + std::size_t cluster_id = assignments[i]; + ++counts[cluster_id]; + new_centers[cluster_id][0] += working_colors[i][0]; + new_centers[cluster_id][1] += working_colors[i][1]; + new_centers[cluster_id][2] += working_colors[i][2]; + } + + for (std::size_t i = 0; i < k; ++i) { + if (counts[i] == 0) { + double max_min_dist = -1.0; + std::size_t best_idx = 0; + for (std::size_t p = 0; p < working_colors.size(); ++p) { + double nearest = std::numeric_limits::max(); + for (std::size_t c = 0; c < k; ++c) { + if (c == i || counts[c] == 0) { + continue; + } + double d = dist_func(working_colors[p], centers[c]); + if (d < nearest) { + nearest = d; + } + } + if (nearest > max_min_dist) { + max_min_dist = nearest; + best_idx = p; + } + } + centers[i] = working_colors[best_idx]; + } else { + centers[i][0] = new_centers[i][0] / counts[i]; + centers[i][1] = new_centers[i][1] / counts[i]; + centers[i][2] = new_centers[i][2] / counts[i]; + } + } + } + + return centers; +} + +// K-Means clustering algorithm +std::vector cluster_k_means(const std::vector& colors, const ClusterParameters& cluster_parameters) { + std::size_t k = cluster_parameters.cluster_k; + std::size_t max_iter = cluster_parameters.max_iter; + + if (k == 0 || colors.empty()) { + return {}; + } + + const bool use_lab = (cluster_parameters.color_difference_method != ColorDifferenceMethod::RGB); + WorkingDistFunc working_dist_func = use_lab ? ciede2000 : calc_rgb_color_difference_by_squared_rgb_double; + + // ========================================== + // 1. Preprocessing: deduplicate + pre-convert to working space + // ========================================== + std::vector unique_colors = colors; + std::sort(unique_colors.begin(), unique_colors.end()); + auto last = std::unique(unique_colors.begin(), unique_colors.end()); + unique_colors.erase(last, unique_colors.end()); + +#ifdef DEBUG_FLAG + BOOST_LOG_TRIVIAL(debug) << "Input colors: " << colors.size() << ", Unique colors: " << unique_colors.size(); +#endif // DEBUG_FLAG + + if (unique_colors.size() < k) { + BOOST_LOG_TRIVIAL(warning) << "Unique color count (" << unique_colors.size() << ") is less than target K (" << k << "). Adjusting K."; + k = unique_colors.size(); + if (k == 0) { + return {}; + } + return unique_colors; + } + + std::vector working_colors(unique_colors.size()); + for (std::size_t i = 0; i < unique_colors.size(); ++i) { + if (use_lab) { + working_colors[i] = convert_rgb_to_lab(unique_colors[i]); + } else { + working_colors[i] = {static_cast(unique_colors[i][0]), static_cast(unique_colors[i][1]), static_cast(unique_colors[i][2])}; + } + } + + // ========================================== + // 2. K-Means clustering + // ========================================== + auto centers = kmeans_core(working_colors, k, max_iter, working_dist_func, cluster_parameters.cancel_callback); + + // ========================================== + // 3. Output: convert from working space back to RGB + // ========================================== + std::vector result(k); + for (std::size_t i = 0; i < k; ++i) { + if (use_lab) { + result[i] = convert_lab_to_rgb(centers[i]); + } else { + result[i] = {static_cast(std::round(centers[i][0])), static_cast(std::round(centers[i][1])), + static_cast(std::round(centers[i][2]))}; + } + } + + return result; +} + +std::vector cluster_to_specified_colors(const std::vector& colors, const std::vector& specified_colors) { + std::vector cluster_colors = colors; + std::vector specified_double_colors; + specified_double_colors.reserve(specified_colors.size()); + for (auto& color : specified_colors) { + specified_double_colors.push_back(ColorDouble{static_cast(color[0]), static_cast(color[1]), static_cast(color[2])}); + } + + tbb::parallel_for(tbb::blocked_range(0, cluster_colors.size()), [&](const tbb::blocked_range& range) { + for (std::size_t i = range.begin(); i < range.end(); ++i) { + ColorDouble p_color{static_cast(cluster_colors[i][0]), static_cast(cluster_colors[i][1]), + static_cast(cluster_colors[i][2])}; + + double min_dist = std::numeric_limits::max(); + std::size_t best_cluster = 0; + + for (std::size_t j = 0; j < specified_double_colors.size(); ++j) { + double d = calc_rgb_color_difference_by_squared_rgb_double(p_color, specified_double_colors[j]); + if (d < min_dist) { + min_dist = d; + best_cluster = j; + } + } + + cluster_colors[i] = specified_colors[best_cluster]; + } + }); + + return cluster_colors; +} + +// Color PCA struct +struct ColorPCA { + std::size_t color_idx; // Index of the original color in ColorList + double pca_value; // Projection value onto the first principal component + + ColorPCA(std::size_t c_idx, double pca_val) + : color_idx(c_idx), + pca_value(pca_val) {} + + // Comparison operator (ascending order) + bool operator<(const ColorPCA& other) const { return pca_value < other.pca_value; } + + // Equality check + bool operator==(const ColorPCA& other) const { return color_idx == other.color_idx; } +}; + +[[maybe_unused]] static std::vector sort_colors_by_pca(const std::vector& colors) { + const std::size_t n = colors.size(); + + if (n == 0) { + return {}; + } + + if (n == 1) { + return {{0, 0.0}}; + } + + // Step 1: Data preprocessing - normalize to [0, 1] + Eigen::MatrixXd data(n, 3); + + for (std::size_t i = 0; i < n; ++i) { + data(i, 0) = static_cast(colors[i][0]) / 255.0; // R + data(i, 1) = static_cast(colors[i][1]) / 255.0; // G + data(i, 2) = static_cast(colors[i][2]) / 255.0; // B + } + + // Step 2: Compute mean and center the data + Eigen::RowVector3d mean = data.colwise().mean(); + Eigen::MatrixXd centered = data.rowwise() - mean; + + // Step 3: Compute covariance matrix (3x3) + Eigen::Matrix3d cov = (centered.adjoint() * centered) / static_cast(n - 1); + + // Step 4: Eigenvalue decomposition + Eigen::SelfAdjointEigenSolver solver(cov); + + if (solver.info() != Eigen::Success) { +#ifdef DEBUG_FLAG + std::cerr << "PCA: Eigenvalue decomposition failed" << std::endl; +#endif // DEBUG_FLAG + // Fallback: return an approximate result sorted by luminance. + // Luminance is a key perceptual feature; convert RGB to grayscale (L = 0.299R + 0.587G + 0.114B) and sort in ascending order. + std::vector result; + result.reserve(n); + for (std::size_t i = 0; i < n; ++i) { + double luminance = 0.299 * colors[i][0] + 0.587 * colors[i][1] + 0.114 * colors[i][2]; + result.push_back({i, luminance}); + } + std::sort(result.begin(), result.end()); + return result; + } + + // Get eigenvalues and eigenvectors (sorted by eigenvalue in descending order) + Eigen::Vector3d eigenvalues = solver.eigenvalues(); + Eigen::Matrix3d eigenvectors = solver.eigenvectors(); + + // Step 5: Find the eigenvector corresponding to the largest eigenvalue (first principal component) + Eigen::MatrixXd::Index max_eigenvalue_idx; + eigenvalues.maxCoeff(&max_eigenvalue_idx); + + Eigen::Vector3d first_principal_component = eigenvectors.col(max_eigenvalue_idx); + + // Step 6: Project centered data onto the first principal component + Eigen::VectorXd projections = centered * first_principal_component; + + // Step 7: Build result and sort + std::vector result; + result.reserve(n); + + for (std::size_t i = 0; i < n; ++i) { + result.push_back({i, projections(i)}); + } + + std::sort(result.begin(), result.end()); + + return result; +} + +std::vector cluster_adaptive(const std::vector& colors, const ClusterParameters& cluster_parameters) { + if (colors.empty()) { + return {}; + } + + const double max_color_distance = cluster_parameters.max_color_distance; + const std::size_t max_iter = cluster_parameters.max_iter; + const bool use_lab = (cluster_parameters.color_difference_method != ColorDifferenceMethod::RGB); + WorkingDistFunc working_dist_func = use_lab ? ciede2000 : calc_rgb_color_difference_by_squared_rgb_double; + + // ========================================== + // 1. Deduplicate + convert to working space + // ========================================== + std::vector unique_colors = colors; + std::sort(unique_colors.begin(), unique_colors.end()); + auto last = std::unique(unique_colors.begin(), unique_colors.end()); + unique_colors.erase(last, unique_colors.end()); + + BOOST_LOG_TRIVIAL(debug) << "cluster_adaptive: colors=" << colors.size() + << " unique=" << unique_colors.size() + << " max_color_distance=" << max_color_distance; + + if (unique_colors.size() <= 1) { + return unique_colors; + } + + std::vector working_colors(unique_colors.size()); + for (std::size_t i = 0; i < unique_colors.size(); ++i) { + if (use_lab) { + working_colors[i] = convert_rgb_to_lab(unique_colors[i]); + } else { + working_colors[i] = {static_cast(unique_colors[i][0]), static_cast(unique_colors[i][1]), static_cast(unique_colors[i][2])}; + } + } + + // ========================================== + // 2. Binary search k: find the smallest k where P99 radius <= max_color_distance + // ========================================== + const std::size_t max_k = cluster_parameters.max_cluster_k; + std::size_t lo = 1; + std::size_t hi = std::min(max_k, unique_colors.size()); + std::size_t best_k = 0; + std::vector best_centers; + + constexpr double kRadiusPercentile = 0.99; + + auto calc_max_radius = [&](const std::vector& centers) -> double { + std::vector distances(working_colors.size()); + tbb::parallel_for(tbb::blocked_range(0, working_colors.size()), [&](const tbb::blocked_range& range) { + for (std::size_t i = range.begin(); i < range.end(); ++i) { + double min_dist = std::numeric_limits::max(); + for (const auto& center : centers) { + double d = working_dist_func(working_colors[i], center); + if (d < min_dist) { + min_dist = d; + } + } + distances[i] = min_dist; + } + }); + if (distances.empty()) { + return 0.0; + } + std::size_t idx = std::min(static_cast(distances.size() * kRadiusPercentile), distances.size() - 1); + std::nth_element(distances.begin(), distances.begin() + idx, distances.end()); + return distances[idx]; + }; + + const auto& cancel_cb = cluster_parameters.cancel_callback; + + while (lo <= hi) { + if (cancel_cb && cancel_cb()) return {}; + std::size_t mid = lo + (hi - lo) / 2; + auto centers = kmeans_core(working_colors, mid, max_iter, working_dist_func, cancel_cb); + if (cancel_cb && cancel_cb()) return {}; + double max_radius = calc_max_radius(centers); + + BOOST_LOG_TRIVIAL(debug) << "cluster_adaptive binary search: k=" << mid << " max_radius=" << max_radius; + + if (max_radius <= max_color_distance) { + best_k = mid; + best_centers = std::move(centers); + hi = mid - 1; + } else { + lo = mid + 1; + } + } + + if (best_k == 0) { + best_k = std::min(max_k, unique_colors.size()); + best_centers = kmeans_core(working_colors, best_k, max_iter, working_dist_func, cancel_cb); + BOOST_LOG_TRIVIAL(warning) << "cluster_adaptive: binary search found no k satisfying max_radius<=" + << max_color_distance << ", fallback to k=" << best_k; + } + + BOOST_LOG_TRIVIAL(debug) << "cluster_adaptive: best_k=" << best_k; + + // ========================================== + // 3. Convert centers back to RGB + // ========================================== + std::vector result(best_k); + for (std::size_t i = 0; i < best_k; ++i) { + if (use_lab) { + result[i] = convert_lab_to_rgb(best_centers[i]); + } else { + result[i] = {static_cast(std::round(best_centers[i][0])), static_cast(std::round(best_centers[i][1])), + static_cast(std::round(best_centers[i][2]))}; + } + } + + return result; +} + +static std::vector> get_connected_face_groups(const CGALMesh& mesh) { + std::vector> face_groups; + std::unordered_set visited_faces; + for (auto src_face : mesh.faces()) { + if (visited_faces.count(src_face)) { + continue; + } + std::vector face_group; + std::queue que; + que.push(src_face); + visited_faces.insert(src_face); + while (!que.empty()) { + auto curr_face = que.front(); + que.pop(); + face_group.push_back(curr_face); + for (auto adj_face : mesh.faces_around_face(mesh.halfedge(curr_face))) { + if (!mesh.is_valid(adj_face) || mesh.is_removed(adj_face) || visited_faces.count(adj_face)) { + continue; + } + que.push(adj_face); + visited_faces.insert(adj_face); + } + } + face_groups.push_back(std::move(face_group)); + } + return face_groups; +} + +bool get_components(const TriMesh& bbs_mesh, const std::vector& bbs_vertex_uvs, std::vector& component_meshes, + std::vector>& component_vertex_uvs) { + component_meshes.clear(); + component_vertex_uvs.clear(); + + if (bbs_mesh.vertices.size() != bbs_vertex_uvs.size()) { + BOOST_LOG_TRIVIAL(warning) << "Input mesh vertex count does not match texture coordinate count"; + return false; + } + + CGALMesh cgal_mesh; + std::vector cgal_vertex_uvs; + if (!cgalutils::convert_trimesh_to_cgal(bbs_mesh, bbs_vertex_uvs, cgal_mesh, cgal_vertex_uvs)) { + BOOST_LOG_TRIVIAL(warning) << "Mesh conversion failed"; + return false; + } + + auto face_groups = get_connected_face_groups(cgal_mesh); + + for (const auto& faces : face_groups) { + std::unordered_map map_cgal_vtx_to_bbs_vtx; + TriVertices bbs_vertices; + TriFaces bbs_faces; + std::vector bbs_vertex_uvs; + + for (auto cgal_face : faces) { + TriFace bbs_face; + std::size_t face_vid = 0; + for (auto cgal_vtx : cgal_mesh.vertices_around_face(cgal_mesh.halfedge(cgal_face))) { + if (!map_cgal_vtx_to_bbs_vtx.count(cgal_vtx)) { + map_cgal_vtx_to_bbs_vtx[cgal_vtx] = bbs_vertices.size(); + const auto& cgal_point = cgal_mesh.point(cgal_vtx); + bbs_vertices.push_back(TriVertex(cgal_point.x(), cgal_point.y(), cgal_point.z())); + bbs_vertex_uvs.push_back(cgal_vertex_uvs[cgal_vtx]); + } + bbs_face[face_vid] = map_cgal_vtx_to_bbs_vtx[cgal_vtx]; + ++face_vid; + } + bbs_faces.push_back(std::move(bbs_face)); + } + + component_meshes.push_back(TriMesh(bbs_faces, bbs_vertices)); + component_vertex_uvs.push_back(std::move(bbs_vertex_uvs)); + } + + return true; +} + +bool calc_nearest_color_id(const std::vector& colors, const RGB& color, std::size_t& nearest_color_id) { + if (colors.empty()) { + BOOST_LOG_TRIVIAL(warning) << "No color list provided"; + return false; + } + double min_dist = std::numeric_limits::max(); + nearest_color_id = 0; + for (std::size_t i = 0; i < colors.size(); ++i) { + double dist = calc_rgb_color_difference_by_ciede2000(colors[i], color); + if (dist < min_dist) { + min_dist = dist; + nearest_color_id = i; + } + } + return true; +} + +bool mesh_cluster(const TriMesh& bbs_mesh, const std::vector& cluster_centers, std::vector& map_face_to_rgb, + std::vector& map_face_to_cluster_id) { + if (cluster_centers.empty()) { + BOOST_LOG_TRIVIAL(warning) << "No cluster centers provided"; + return false; + } + + CGALMesh mesh; + cgalutils::convert_trimesh_to_cgal(bbs_mesh, mesh); + if (mesh.number_of_faces() != bbs_mesh.indices.size()) { + BOOST_LOG_TRIVIAL(warning) << "CGAL mesh face count does not match BBS mesh face count"; + return false; + } + if (mesh.number_of_faces() != map_face_to_rgb.size()) { + BOOST_LOG_TRIVIAL(warning) << "CGAL mesh face count does not match RGB count"; + return false; + } + + if (cluster_centers.size() == 1) { + std::fill(map_face_to_rgb.begin(), map_face_to_rgb.end(), cluster_centers[0]); +#ifdef DEBUG_FLAG + BOOST_LOG_TRIVIAL(debug) << "input cluster centers' size is 1, so we set all face RGB as same with it and return.\n"; +#endif + return true; + } + + std::vector map_face_to_area(mesh.number_of_faces()); + tbb::parallel_for(tbb::blocked_range(0, mesh.number_of_faces()), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + CGAL::SM_Face_index face(fid); + map_face_to_area[fid] = std::max(PMP::face_area(face, mesh), EPSILON); + } + }); + + // Step 1: Identify faces that definitely belong to a cluster center. + // A face is definitively assigned when dist1 * absolute_difference_times < dist2 (nearest vs. second-nearest center). + constexpr double absolute_difference_times = 1.5; + // dE <= 1.0: imperceptible to the human eye, high-precision color matching + // dE <= 2.0: slight difference, noticeable by experts; printing / image processing standard + // dE <= 3.0: noticeable by ordinary observers; general quality control + constexpr double difference_epsilon = 3.0; + constexpr std::size_t invalid_cluster_id = std::numeric_limits::max(); + map_face_to_cluster_id.resize(mesh.number_of_faces(), invalid_cluster_id); + std::vector>> map_face_to_dists(mesh.number_of_faces(), + std::vector>(cluster_centers.size())); + tbb::parallel_for(tbb::blocked_range(0, mesh.number_of_faces()), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + CGAL::SM_Face_index face(fid); + std::vector>& dist_and_cid_vec = map_face_to_dists[fid]; + for (std::size_t cluster_id = 0; cluster_id < cluster_centers.size(); ++cluster_id) { + double dist = calc_rgb_color_difference_by_ciede2000(map_face_to_rgb[fid], cluster_centers[cluster_id]); + //dist_and_cid_vec.emplace_back(dist, cluster_id); + dist_and_cid_vec[cluster_id] = std::pair{dist, cluster_id}; + } + std::sort(dist_and_cid_vec.begin(), dist_and_cid_vec.end()); + if (dist_and_cid_vec[0].first < difference_epsilon || dist_and_cid_vec[0].first * absolute_difference_times < dist_and_cid_vec[1].first) { + map_face_to_cluster_id[fid] = dist_and_cid_vec[0].second; + } + } + }); + + std::unordered_set unclusted_fids; + for (std::size_t fid = 0; fid < mesh.number_of_faces(); ++fid) { + if (map_face_to_cluster_id[fid] == invalid_cluster_id) { + unclusted_fids.insert(fid); + } + } + + auto convert_unclustered_to_clustered = [&](const std::unordered_set& iter_clustered_fids) -> bool { + if (iter_clustered_fids.empty()) { + return false; + } + for (auto& fid : iter_clustered_fids) { + unclusted_fids.erase(fid); + } + return true; + }; + + // Step 2: Flood. Use faces computed in the previous step as seeds and propagate outward. + while (!unclusted_fids.empty()) { + bool changed = false; + std::unordered_set iter_clustered_fids; + // If an uncolored face has an adjacent color whose count exceeds the sum of all other colors, assign that color + for (auto fid : unclusted_fids) { + CGAL::SM_Face_index face(fid); + std::size_t count = 0; + std::unordered_map map_cluster_id_to_count; + for (auto adj_face : mesh.faces_around_face(mesh.halfedge(face))) { + if (!mesh.is_valid(adj_face) || mesh.is_removed(adj_face)) { + continue; + } + ++count; + ++map_cluster_id_to_count[map_face_to_cluster_id[adj_face]]; + } + for (auto& [cluster_id, cnt] : map_cluster_id_to_count) { + if (cluster_id != invalid_cluster_id && cnt * 2 > count) { + map_face_to_cluster_id[fid] = cluster_id; + iter_clustered_fids.insert(fid); + break; + } + } + } + changed = convert_unclustered_to_clustered(iter_clustered_fids) || changed; + iter_clustered_fids.clear(); + + // If a face's nearest cluster center (by color distance) happens to have an adjacent face, assign that color too + for (auto fid : unclusted_fids) { + CGAL::SM_Face_index face(fid); + std::unordered_set adj_cluster_ids; + for (auto adj_face : mesh.faces_around_face(mesh.halfedge(face))) { + if (!mesh.is_valid(adj_face) || mesh.is_removed(adj_face)) { + continue; + } + if (map_face_to_cluster_id[adj_face] == invalid_cluster_id) { + continue; + } + adj_cluster_ids.insert(map_face_to_cluster_id[adj_face]); + } + if (adj_cluster_ids.count(map_face_to_dists[fid].front().second)) { + map_face_to_cluster_id[fid] = map_face_to_dists[fid].front().second; + iter_clustered_fids.insert(fid); + } + } + changed = convert_unclustered_to_clustered(iter_clustered_fids) || changed; + iter_clustered_fids.clear(); + + // If no face colors were modified in this iteration, stop + if (!changed) { + break; + } + } + + // Step 3: Handle remaining unclustered faces (run after the above operations complete) + constexpr bool use_average_color = true; + for (auto src_fid : std::vector(unclusted_fids.begin(), unclusted_fids.end())) { + if (!unclusted_fids.count(src_fid)) { + continue; + } + // Compute connected unclustered faces + std::queue que; + std::unordered_set connected_unclustered_faces; + que.push(src_fid); + connected_unclustered_faces.insert(src_fid); + double sum_r = 0, sum_g = 0, sum_b = 0; + double sum_area = 0.0; + std::unordered_map map_cluster_id_to_adj_area; + while (!que.empty()) { + auto curr_fid = que.front(); + que.pop(); + sum_r += map_face_to_area[curr_fid] * map_face_to_rgb[curr_fid][0]; + sum_g += map_face_to_area[curr_fid] * map_face_to_rgb[curr_fid][1]; + sum_b += map_face_to_area[curr_fid] * map_face_to_rgb[curr_fid][2]; + sum_area += map_face_to_area[curr_fid]; + CGAL::SM_Face_index curr_face(curr_fid); + for (auto adj_face : mesh.faces_around_face(mesh.halfedge(curr_face))) { + if (!mesh.is_valid(adj_face) || mesh.is_removed(adj_face)) { // Invalid face + continue; + } + if (map_face_to_cluster_id[adj_face] != invalid_cluster_id) { + map_cluster_id_to_adj_area[map_face_to_cluster_id[adj_face]] += map_face_to_area[adj_face]; + } else { + if (!connected_unclustered_faces.count(adj_face)) { // Already clustered or already recorded + que.push(adj_face); + connected_unclustered_faces.insert(adj_face); + } + } + } + } + std::size_t matched_cluster_id = invalid_cluster_id; + if (use_average_color) { + // Use average color + RGB average_color{static_cast(sum_r / sum_area), static_cast(sum_g / sum_area), + static_cast(sum_b / sum_area)}; + double min_dist = std::numeric_limits::max(); + for (auto& [cluster_id, area] : map_cluster_id_to_adj_area) { + double dist = calc_rgb_color_difference_by_ciede2000(average_color, cluster_centers[cluster_id]); + if (dist < min_dist) { + min_dist = dist; + matched_cluster_id = cluster_id; + } + } + } else { + // Use adjacent area + double adj_max_area = 0.0; + for (auto& [cluster_id, area] : map_cluster_id_to_adj_area) { + if (area > adj_max_area) { + adj_max_area = area; + matched_cluster_id = cluster_id; + } + } + } + for (auto fid : connected_unclustered_faces) { + map_face_to_cluster_id[fid] = matched_cluster_id; + unclusted_fids.erase(fid); + } + } + + // Convert cluster center IDs to colors + for (std::size_t fid = 0; fid < mesh.number_of_faces(); ++fid) { + if (map_face_to_cluster_id[fid] == invalid_cluster_id) { + map_face_to_rgb[fid] = cluster_centers[0]; + map_face_to_cluster_id[fid] = 0; // Prevent out-of-bounds errors when using cluster_id later + } else { + map_face_to_rgb[fid] = cluster_centers[map_face_to_cluster_id[fid]]; + } + } + + return true; +} + +} // namespace color_utils + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/ColorUtils.hpp b/src/libslic3r/TextureToColor/ColorUtils.hpp new file mode 100644 index 0000000000..05849109f4 --- /dev/null +++ b/src/libslic3r/TextureToColor/ColorUtils.hpp @@ -0,0 +1,207 @@ +#pragma once + +#include "Callbacks.hpp" +#include "TriMesh.hpp" + +namespace Slic3r { namespace tex2color { + +namespace color_utils { +struct ClusterParameters; + +typedef std::array Color; // RGB: [R, G, B] 0~255 +typedef std::vector ColorList; +typedef std::array ColorDouble; +typedef std::array RGB; + +// Function pointer type that points to a specific color-difference function based on the chosen method. +using DistanceFunction = double (*)(const Color&, const Color&); + +// Color space used for computing color differences. +enum struct ColorDifferenceMethod : std::size_t { + RGB = 0, // Simplest and fastest + Lab = 1 // Most perceptually accurate +}; + +struct ClusterParameters { + ColorDifferenceMethod color_difference_method = ColorDifferenceMethod::Lab; // Method for measuring color difference; Lab is the most accurate + + double max_color_distance = 25; // Max intra-cluster radius (CIEDE2000 dE) for adaptive clustering; ignored by the fixed-K algorithm + + std::size_t cluster_k = 10; // Target number of cluster centers; ignored by the adaptive algorithm + + std::size_t max_cluster_k = 32; // Max cluster count upper bound for adaptive algorithm + + std::size_t max_iter = 50; // Maximum number of iterations + + std::function cancel_callback; // Optional cancellation check; returns true when the caller requests abort +}; + +struct SmoothParameters { + double smooth_weight = 0.5; // Controls smoothing intensity; larger values produce smoother results. Range: [0.0, 1.0] +}; + +/** + * @brief Compute the squared Euclidean distance between two RGB colors. + * + * @param[in] rgb1 First RGB color [R, G, B], range 0~255. + * @param[in] rgb2 Second RGB color [R, G, B], range 0~255. + * @return Squared Euclidean distance: (R1-R2)^2 + (G1-G2)^2 + (B1-B2)^2. + */ +double calc_rgb_color_difference_by_squared_rgb(const RGB& rgb1, const RGB& rgb2); + +/** + * @brief Compute the squared Euclidean distance between two RGB colors (double precision). + * + * @param[in] c1 First RGB color [R, G, B], as double. + * @param[in] c2 Second RGB color [R, G, B], as double. + * @return Squared Euclidean distance: (R1-R2)^2 + (G1-G2)^2 + (B1-B2)^2. + */ +double calc_rgb_color_difference_by_squared_rgb_double(const ColorDouble& c1, const ColorDouble& c2); + +/** + * @brief Compute the CIEDE2000 color difference between two RGB colors. + * + * Currently the most accurate color-difference formula, recommended by CIE as the industry standard. + * - dE <= 1.0: imperceptible to the human eye, high-precision color matching. + * - dE <= 2.0: slight difference, noticeable by experts; printing / image processing standard. + * - dE <= 3.0: noticeable by ordinary observers; general quality control. + * + * @param[in] rgb1 First RGB color [R, G, B], range 0~255. + * @param[in] rgb2 Second RGB color [R, G, B], range 0~255. + * @return CIEDE2000 color difference; smaller values indicate more similar colors. + */ +double calc_rgb_color_difference_by_ciede2000(const RGB& rgb1, const RGB& rgb2); + +/** + * @brief Compute the CIEDE2000 color difference between two sRGB colors (double precision, non-linear channels in [0,1]). + * + * Uses the same XYZ/Lab/dE00 pipeline as calc_rgb_color_difference_by_ciede2000 but without uint8 + * quantization or the intermediate x255 conversion; suitable for bisection, color blending, and other + * iterative scenarios. Note: ColorDouble here represents [R,G,B] in [0,1], which differs from the + * 0~255 scale used by other interfaces in this file. Callers should follow the naming convention. + * + * @param[in] rgb1 rgb2 sRGB non-linear channel values, recommended range [0,1]. + */ +double calc_rgb_color_difference_by_ciede2000_srgb01(const ColorDouble& rgb1, const ColorDouble& rgb2); + +/** + * @brief K-Means clustering algorithm that minimizes the sum of squared errors. + * + * Uses K-Means++ initialization to iteratively find the optimal cluster centers. + * + * @param[in] colors Input color list. + * @param[in] cluster_parameters Clustering parameters including cluster count, max iterations, color-difference method, etc. + * @return List of cluster-center colors whose size equals cluster_parameters.cluster_k. + */ +std::vector cluster_k_means(const std::vector& colors, const ClusterParameters& cluster_parameters); + +/** + * @brief Adaptive K-Means clustering that determines an appropriate number of clusters under a max color-distance constraint. + * + * Automatically finds the optimal cluster count via binary search so that max_color_distance is satisfied. + * + * @param[in] colors Input color list. + * @param[in] cluster_parameters Clustering parameters; cluster_k is ignored and determined automatically. + * @return List of cluster-center colors whose count is determined by the algorithm based on max_color_distance. + */ +std::vector cluster_adaptive(const std::vector& colors, const ClusterParameters& cluster_parameters); + +/** + * @brief Cluster a color list to a set of specified cluster centers. + * + * For each input color, find the nearest specified cluster center and replace it. + * + * @param[in] colors Input color list. + * @param[in] specified_colors Specified cluster-center colors. + * @return Clustered color list where each color is replaced by its nearest center. + */ +std::vector cluster_to_specified_colors(const std::vector& colors, const std::vector& specified_colors); + +/** + * @brief Remesh the mesh while preserving color boundaries. + * + * Performs isotropic remeshing while protecting color boundaries. Edges whose two adjacent + * faces have different colors are marked as feature edges and will not be modified. + * + * @param[in,out] mesh Input mesh; modified in-place after remeshing. + * @param[in,out] face_labels Face color labels; updated to match the new mesh. + * @param[in] target_edge_length_ratio Ratio of target average edge length to input average edge length; >1 simplifies, <1 refines. + * @return true on success, false on failure. + */ +bool remesh_mesh(TriMesh& mesh, std::vector& face_labels, double target_edge_length_ratio); + +/** + * @brief Check whether the mesh is closed (watertight). + * + * A mesh is closed if it has no boundary edges, i.e. every edge is shared by exactly two faces. + * + * @param[in] tri_mesh Input mesh. + * @return true if the mesh is closed, false if it has boundary edges. + */ +bool is_closed(const TriMesh& tri_mesh); + +/** + * @brief Smooth region boundaries (RGB color labels). + * + * Applies topological smoothing (label reassignment) and geometric smoothing (boundary vertex relocation). + * + * @param[in,out] tri_mesh Input mesh; modified in-place after smoothing. + * @param[in,out] face_labels Face color labels (RGB format); updated after smoothing. + * @param[in] smooth_parameters Smoothing control parameters. + * @return true on success, false on failure. + */ +bool smooth_region(TriMesh& tri_mesh, std::vector>& face_labels, const SmoothParameters& smooth_parameters = SmoothParameters()); + +/** + * @brief Smooth region boundaries (integer labels). + * + * Applies topological smoothing (label reassignment) and geometric smoothing (boundary vertex relocation). + * + * @param[in,out] tri_mesh Input mesh; modified in-place after smoothing. + * @param[in,out] face_labels Integer face labels; updated after smoothing. + * @param[in] smooth_parameters Smoothing control parameters. + * @return true on success, false on failure. + */ +bool smooth_region(TriMesh& tri_mesh, std::vector& face_labels, const SmoothParameters& smooth_parameters = SmoothParameters()); + +/** + * @brief Split the mesh into connected components. + * + * Based on face connectivity, the mesh is split into independent components, each forming a + * standalone mesh. Texture coordinates for each component are preserved. + * + * @param[in] mesh Input mesh. + * @param[in] vertex_uvs Vertex texture coordinates. + * @param[out] component_meshes Output list of component meshes. + * @param[out] component_vertex_uvs Output list of texture coordinates per component. + * @return true on success, false on failure. + */ +bool get_components(const TriMesh& mesh, const std::vector& vertex_uvs, std::vector& component_meshes, + std::vector>& component_vertex_uvs); + +/** + * @brief Find the ID of the nearest color in a color list to a given color. + * + * @param[in] colors Color list. + * @param[in] color Target color. + * @param[out] nearest_color_id ID of the nearest color found. + * @return true on success, false on failure. + */ +bool calc_nearest_color_id(const std::vector& colors, const RGB& color, std::size_t& nearest_color_id); + +/** + * @brief Cluster mesh face colors based on given cluster centers. + * + * @param[in] mesh Input mesh. + * @param[in] cluster_centers Cluster-center RGB colors. + * @param[in, out] map_face_to_rgb RGB color per face; updated to the nearest cluster center after clustering. + * @param[out] map_face_to_cluster_id Cluster-center ID per face; updated to the nearest cluster center ID. + * @return true on success, false on failure. + */ +bool mesh_cluster(const TriMesh& mesh, const std::vector& cluster_centers, std::vector& map_face_to_rgb, + std::vector& map_face_to_cluster_id); + +} // namespace color_utils + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/Repair.hpp b/src/libslic3r/TextureToColor/Repair.hpp new file mode 100644 index 0000000000..44dc30c144 --- /dev/null +++ b/src/libslic3r/TextureToColor/Repair.hpp @@ -0,0 +1,252 @@ +#pragma once +#include "TriMesh.hpp" +#include "CgalUtils.hpp" +#include "Callbacks.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Slic3r { namespace tex2color { + +namespace PMP = CGAL::Polygon_mesh_processing; + +// Default upper bound on the number of half-edges in any single boundary cycle +// that CloseBoundariesAndRepairManifoldness will attempt to triangulate. The +// cost of triangulate_hole grows non-linearly with cycle length, so this caps +// the worst-case per-hole work rather than the aggregate boundary size: a mesh +// with many small holes is still fully repaired, while a mesh containing one +// pathologically large hole skips triangulation entirely. +inline constexpr std::size_t MAX_REPAIRABLE_MESH_HOLE_EDGES = 500; + +// Default upper bound on the aggregate number of boundary half-edges in the +// mesh (summed across every boundary cycle). When the total boundary length is +// excessive, even if each individual cycle is short, triangulating all of them +// usually indicates a severely fragmented input (e.g. heavily damaged scans) +// and rarely yields a usable result, so we skip hole closing entirely. +inline constexpr std::size_t MAX_REPAIRABLE_MESH_BOUNDARY_EDGES = 5000; + +struct RepairSetting +{ + // Skip triangulating a boundary cycle whose half-edge count exceeds this. + std::size_t max_hole_edges = MAX_REPAIRABLE_MESH_HOLE_EDGES; + // Skip hole closing entirely when the total boundary half-edge count + // (summed across all cycles) exceeds this. + std::size_t max_boundary_edges = MAX_REPAIRABLE_MESH_BOUNDARY_EDGES; +}; + +struct BoundaryEdgeStats +{ + std::size_t total_boundary_edges = 0; + std::size_t max_cycle_edges = 0; + std::size_t cycle_count = 0; +}; + +// Read-only inspection of the mesh's boundary cycles. Caller is responsible for +// any pre-processing (e.g. stitch_borders) needed for the count to be meaningful. +inline BoundaryEdgeStats ComputeBoundaryEdgeStats(const cgalutils::CGALMesh& cgal_mesh) +{ + using CGALMesh = cgalutils::CGALMesh; + using HalfedgeDescriptor = boost::graph_traits::halfedge_descriptor; + + std::vector border_cycles; + PMP::extract_boundary_cycles(cgal_mesh, std::back_inserter(border_cycles)); + + BoundaryEdgeStats stats; + stats.cycle_count = border_cycles.size(); + for (const HalfedgeDescriptor h0 : border_cycles) { + std::size_t len = 0; + HalfedgeDescriptor h = h0; + do { + ++len; + h = next(h, cgal_mesh); + } while (h != h0); + stats.max_cycle_edges = std::max(stats.max_cycle_edges, len); + stats.total_boundary_edges += len; + } + return stats; +} + +// Unconditionally close every boundary cycle of the mesh and repair non-manifold +// vertices. The caller (e.g. RepairMesh) is expected to gate this call based on +// boundary statistics; entering this function always triggers triangulation. +inline void CloseBoundariesAndRepairManifoldness(cgalutils::CGALMesh& cgal_mesh) +{ + using CGALMesh = cgalutils::CGALMesh; + using HalfedgeDescriptor = boost::graph_traits::halfedge_descriptor; + using FaceDescriptor = boost::graph_traits::face_descriptor; + + PMP::stitch_borders(cgal_mesh); + PMP::duplicate_non_manifold_vertices(cgal_mesh); + + std::vector border_cycles; + PMP::extract_boundary_cycles(cgal_mesh, std::back_inserter(border_cycles)); + + for (const HalfedgeDescriptor h : border_cycles) { + std::vector patch_faces; + PMP::triangulate_hole(cgal_mesh, h, std::back_inserter(patch_faces)); + } + + PMP::remove_degenerate_faces(cgal_mesh); + PMP::duplicate_non_manifold_vertices(cgal_mesh); +} + +inline bool RepairMesh(const TriMesh& mesh, + std::shared_ptr& out_mesh, + AlgoProgressCallback progress_callback = nullptr, + AlgoCancelCallback cancel_callback = nullptr, + const RepairSetting& setting = RepairSetting{}) +{ + using Clock = std::chrono::steady_clock; + auto elapsed_ms = [](Clock::time_point t0) { + return std::chrono::duration_cast(Clock::now() - t0).count(); + }; + + const Clock::time_point t_total = Clock::now(); + + // Convert TriMesh to polygon soup (point container + triangle index container) + std::vector soup_points; + std::vector> soup_triangles; + + soup_points.reserve(mesh.vertices.size()); + for (const TriVertex& v : mesh.vertices) { + soup_points.emplace_back(v.x(), v.y(), v.z()); + } + + soup_triangles.reserve(mesh.indices.size()); + for (const TriFace& f : mesh.indices) { + soup_triangles.push_back({static_cast(f[0]), + static_cast(f[1]), + static_cast(f[2])}); + } + + if (progress_callback) { + progress_callback({30, "Repairing polygon soup"}); + } + if (cancel_callback && cancel_callback()) { + return false; + } + + { + const auto t0 = Clock::now(); + PMP::repair_polygon_soup(soup_points, soup_triangles); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=repair_polygon_soup took=" + << elapsed_ms(t0) << " ms"; + } + + if (progress_callback) { + progress_callback({50, "Orienting polygon soup"}); + } + if (cancel_callback && cancel_callback()) { + return false; + } + + { + const auto t0 = Clock::now(); + PMP::orient_polygon_soup(soup_points, soup_triangles); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=orient_polygon_soup took=" + << elapsed_ms(t0) << " ms"; + } + + if (progress_callback) { + progress_callback({70, "Converting to CGAL mesh"}); + } + if (cancel_callback && cancel_callback()) { + return false; + } + + cgalutils::CGALMesh cgal_mesh; + { + const auto t0 = Clock::now(); + PMP::polygon_soup_to_polygon_mesh(soup_points, soup_triangles, cgal_mesh); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=polygon_soup_to_polygon_mesh took=" + << elapsed_ms(t0) << " ms"; + } + + { + const auto t0 = Clock::now(); + PMP::remove_degenerate_faces(cgal_mesh); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=remove_degenerate_faces took=" + << elapsed_ms(t0) << " ms"; + } + + if (progress_callback) { + progress_callback({80, "Closing mesh boundaries"}); + } + if (cancel_callback && cancel_callback()) { + return false; + } + + // Stitch borders and duplicate non-manifold vertices first so that the + // boundary statistics below reflect the post-stitch topology; otherwise + // boundaries that would close on stitching inflate the counts and may + // cause the gate to skip hole filling unnecessarily. + BoundaryEdgeStats stats; + { + const auto t0 = Clock::now(); + PMP::stitch_borders(cgal_mesh); + PMP::duplicate_non_manifold_vertices(cgal_mesh); + stats = ComputeBoundaryEdgeStats(cgal_mesh); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=boundary_stats took=" + << elapsed_ms(t0) << " ms" + << " total_boundary_edges=" << stats.total_boundary_edges + << " max_cycle_edges=" << stats.max_cycle_edges + << " cycle_count=" << stats.cycle_count; + } + + const bool can_repair_holes = + stats.total_boundary_edges <= setting.max_boundary_edges && + stats.max_cycle_edges <= setting.max_hole_edges; + + if (can_repair_holes) { + const auto t0 = Clock::now(); + CloseBoundariesAndRepairManifoldness(cgal_mesh); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=close_boundaries took=" + << elapsed_ms(t0) << " ms"; + } else { + BOOST_LOG_TRIVIAL(info) + << "TextureToColor: RepairMesh skip hole closing" + << ", total_boundary_edges=" << stats.total_boundary_edges + << " (limit=" << setting.max_boundary_edges << ")" + << ", max_cycle_edges=" << stats.max_cycle_edges + << " (limit=" << setting.max_hole_edges << ")" + << ", cycle_count=" << stats.cycle_count; + } + + if (progress_callback) { + progress_callback({85, "Converting from CGAL mesh"}); + } + if (cancel_callback && cancel_callback()) { + return false; + } + + std::shared_ptr out; + { + const auto t0 = Clock::now(); + out = std::make_shared(cgalutils::cgal_to_trimesh(cgal_mesh)); + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh stage=cgal_to_trimesh took=" + << elapsed_ms(t0) << " ms"; + } + + out_mesh = std::move(out); + if (progress_callback) { + progress_callback({100, "Done"}); + } + + BOOST_LOG_TRIVIAL(info) << "TextureToColor: RepairMesh total=" << elapsed_ms(t_total) << " ms"; + + return true; +} + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/TextureToColor.cpp b/src/libslic3r/TextureToColor/TextureToColor.cpp new file mode 100644 index 0000000000..e3afc63cd9 --- /dev/null +++ b/src/libslic3r/TextureToColor/TextureToColor.cpp @@ -0,0 +1,789 @@ +#include "TextureToColor.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "CgalUtils.hpp" +#include "ColorUtils.hpp" +#include "libslic3r/TriangleMesh.hpp" +#include +#include +#include "Repair.hpp" +#include "libslic3r/AABBTreeIndirect.hpp" +#include + +namespace Slic3r { namespace tex2color { + +using namespace color_utils; + +// #define OUTPUT_TEST_RESULT + +static void SaveToOFF(const std::string& path, const TriMesh& mesh, const std::vector& face_colors) +{ + std::filesystem::create_directories(std::filesystem::path(path).parent_path()); + std::ofstream ofs(path); + if (!ofs.is_open()) { + BOOST_LOG_TRIVIAL(warning) << "SaveToOFF: failed to open " << path; + return; + } + + const auto& vertices = mesh.vertices; + const auto& faces = mesh.indices; + + ofs << "OFF\n"; + ofs << vertices.size() << " " << faces.size() << " 0\n"; + + for (const auto& v : vertices) { + ofs << v.x() << " " << v.y() << " " << v.z() << "\n"; + } + + for (std::size_t i = 0; i < faces.size(); ++i) { + const auto& f = faces[i]; + ofs << "3 " << f[0] << " " << f[1] << " " << f[2]; + if (i < face_colors.size()) { + ofs << " " << face_colors[i][0] / 255.0 + << " " << face_colors[i][1] / 255.0 + << " " << face_colors[i][2] / 255.0 + << " 1.0"; + } + ofs << "\n"; + } +} + +static std::vector count_cluster_label_usage(const std::vector& face_labels, std::size_t cluster_count) +{ + std::vector usage(cluster_count, 0); + for (std::size_t label : face_labels) { + if (label < cluster_count) { + ++usage[label]; + } + } + return usage; +} + +static bool discard_unused_cluster_centers(std::vector& cluster_centers, std::vector& face_labels, const char* stage_name) +{ + if (cluster_centers.empty()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cannot discard unused cluster centers at " << stage_name + << ", no cluster center is available."; + return false; + } + + const std::vector usage = count_cluster_label_usage(face_labels, cluster_centers.size()); + std::vector label_remap(cluster_centers.size(), std::numeric_limits::max()); + std::vector used_cluster_centers; + used_cluster_centers.reserve(cluster_centers.size()); + + for (std::size_t cluster_id = 0; cluster_id < cluster_centers.size(); ++cluster_id) { + if (usage[cluster_id] == 0) { + continue; + } + label_remap[cluster_id] = used_cluster_centers.size(); + used_cluster_centers.push_back(cluster_centers[cluster_id]); + } + + if (used_cluster_centers.size() == cluster_centers.size()) { + return true; + } + if (used_cluster_centers.empty()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cannot discard unused cluster centers at " << stage_name + << ", no face uses any valid cluster center."; + return false; + } + + for (std::size_t& label : face_labels) { + if (label >= label_remap.size() || label_remap[label] == std::numeric_limits::max()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cannot remap cluster label " << label + << " at " << stage_name << "."; + return false; + } + label = label_remap[label]; + } + + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: discarded " << (cluster_centers.size() - used_cluster_centers.size()) + << " unused adaptive cluster centers at " << stage_name << "."; + cluster_centers = std::move(used_cluster_centers); + return true; +} + +static bool ensure_all_cluster_centers_used(const std::vector& source_face_colors, const std::vector& cluster_centers, + std::vector& face_labels, const char* stage_name) +{ + if (source_face_colors.size() != face_labels.size()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cannot preserve cluster colors at " << stage_name + << ", face color count does not match label count."; + return false; + } + if (cluster_centers.empty()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cannot preserve cluster colors at " << stage_name + << ", no cluster center is available."; + return false; + } + if (cluster_centers.size() > face_labels.size()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cannot use all cluster centers at " << stage_name + << ", centers=" << cluster_centers.size() << " faces=" << face_labels.size() << "."; + return false; + } + + std::vector usage = count_cluster_label_usage(face_labels, cluster_centers.size()); + std::size_t missing_count = 0; + for (std::size_t cluster_id = 0; cluster_id < usage.size(); ++cluster_id) { + if (usage[cluster_id] != 0) { + continue; + } + ++missing_count; + + double best_cost = std::numeric_limits::max(); + std::size_t best_face_id = std::numeric_limits::max(); + std::size_t best_old_cluster_id = std::numeric_limits::max(); + + for (std::size_t fid = 0; fid < face_labels.size(); ++fid) { + const std::size_t old_cluster_id = face_labels[fid]; + if (old_cluster_id >= cluster_centers.size() || usage[old_cluster_id] <= 1) { + continue; + } + + const double old_dist = calc_rgb_color_difference_by_ciede2000(source_face_colors[fid], cluster_centers[old_cluster_id]); + const double new_dist = calc_rgb_color_difference_by_ciede2000(source_face_colors[fid], cluster_centers[cluster_id]); + const double cost = new_dist - old_dist; + if (cost < best_cost) { + best_cost = cost; + best_face_id = fid; + best_old_cluster_id = old_cluster_id; + } + } + + if (best_face_id == std::numeric_limits::max()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: failed to assign a seed face for unused cluster " << cluster_id + << " at " << stage_name << "."; + continue; + } + + face_labels[best_face_id] = cluster_id; + --usage[best_old_cluster_id]; + ++usage[cluster_id]; + } + + if (missing_count > 0) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: reassigned seed faces for " << missing_count + << " unused cluster centers at " << stage_name << "."; + } + + for (std::size_t count : usage) { + if (count == 0) { + return false; + } + } + return true; +} + +// Bilinear interpolation texture sampling; sub-pixel precision avoids nearest-neighbor aliasing +static RGB get_pixel_color(float u, float v, const cv::Mat& texture) { + u = u - std::floor(u); + v = v - std::floor(v); + + // glTF UV convention: (0,0) = top-left, v increases downward + float fx = u * (texture.cols - 1); + float fy = v * (texture.rows - 1); + + int x0 = std::clamp(static_cast(fx), 0, texture.cols - 1); + int y0 = std::clamp(static_cast(fy), 0, texture.rows - 1); + int x1 = std::min(x0 + 1, texture.cols - 1); + int y1 = std::min(y0 + 1, texture.rows - 1); + + float wx = fx - x0; + float wy = fy - y0; + + const int ch = texture.channels(); + auto sample = [&](int row, int col) -> std::array { + const uchar* ptr = texture.data + row * texture.step[0] + col * ch; + return {static_cast(ptr[2]), static_cast(ptr[1]), static_cast(ptr[0])}; + }; + + auto c00 = sample(y0, x0); + auto c10 = sample(y0, x1); + auto c01 = sample(y1, x0); + auto c11 = sample(y1, x1); + + // Bilinear blend: lerp(lerp(c00,c10,wx), lerp(c01,c11,wx), wy) + RGB color; + for (int i = 0; i < 3; ++i) { + float top = c00[i] * (1.0f - wx) + c10[i] * wx; + float bot = c01[i] * (1.0f - wx) + c11[i] * wx; + color[i] = static_cast(std::clamp(top * (1.0f - wy) + bot * wy, 0.0f, 255.0f)); + } + return color; +} + +// 7-point triangular Gaussian quadrature barycentric coordinates and weights (precision sufficient for capturing texture detail within faces) +static constexpr std::array, 7> GAUSS_TRI_BARY = {{ + {1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f}, + {0.059715871f, 0.470142064f, 0.470142064f}, + {0.470142064f, 0.059715871f, 0.470142064f}, + {0.470142064f, 0.470142064f, 0.059715871f}, + {0.797426985f, 0.101286507f, 0.101286507f}, + {0.101286507f, 0.797426985f, 0.101286507f}, + {0.101286507f, 0.101286507f, 0.797426985f}, +}}; +static constexpr std::array GAUSS_TRI_WEIGHT = {0.225f, 0.132394152f, 0.132394152f, 0.132394152f, 0.125939181f, 0.125939181f, 0.125939181f}; +static_assert( + []() constexpr { + float sum = 0.0f; + for (auto w : GAUSS_TRI_WEIGHT) { + sum += w; + } + return sum > 0.999f && sum < 1.001f; + }(), + "Sum of Gaussian quadrature weights must be 1.0"); + +// Multi-point Gaussian quadrature sampling on a single face; returns weighted average color. +// GAUSS_TRI_WEIGHT sums to 1.0 (Hammer quadrature formula), no normalization needed. +static RGB sample_face_color(const std::array& uvs, const cv::Mat& texture) { + float r = 0.0f, g = 0.0f, b = 0.0f; + for (int k = 0; k < 7; ++k) { + float u = GAUSS_TRI_BARY[k][0] * uvs[0].x() + GAUSS_TRI_BARY[k][1] * uvs[1].x() + GAUSS_TRI_BARY[k][2] * uvs[2].x(); + float v = GAUSS_TRI_BARY[k][0] * uvs[0].y() + GAUSS_TRI_BARY[k][1] * uvs[1].y() + GAUSS_TRI_BARY[k][2] * uvs[2].y(); + RGB c = get_pixel_color(u, v, texture); + float w = GAUSS_TRI_WEIGHT[k]; + r += w * c[0]; + g += w * c[1]; + b += w * c[2]; + } + return RGB{static_cast(std::clamp(r, 0.0f, 255.0f)), static_cast(std::clamp(g, 0.0f, 255.0f)), + static_cast(std::clamp(b, 0.0f, 255.0f))}; +} + +// Use array instead of vector for UV storage to avoid per-face heap allocations at million-face scale +using FaceUVArray = std::array; + +static bool linear_subdivision(TriMesh& mesh, std::vector& uv_coords, const std::function& sub_progress = nullptr) { + const auto& original_vertices = mesh.vertices; + const auto& original_faces = mesh.indices; + TriVertices sub_vertices = mesh.vertices; + sub_vertices.reserve(original_vertices.size() + original_faces.size() * 3); + TriFaces sub_faces; + std::vector sub_uv_coords; + + // Single-level flat map with edge key encoding replaces nested unordered_map; + // merges two vertex indices into a single uint64_t to reduce hash lookups and indirection. + if (original_vertices.size() >= (1ULL << 32)) [[unlikely]] { + BOOST_LOG_TRIVIAL(warning) << "[boundary] " << __FUNCTION__ << " vertex_count=" << original_vertices.size() << " exceeds 32-bit edge_key encoding range, skipping subdivision"; + return false; + } + auto edge_key = [](std::size_t a, std::size_t b) -> uint64_t { + return a < b ? ((static_cast(a) << 32) | b) : ((static_cast(b) << 32) | a); + }; + std::unordered_map map_edge_to_sub_vtx; + map_edge_to_sub_vtx.reserve(original_faces.size() * 3 / 2); + + for (const auto& face : original_faces) { + for (std::size_t i = 0; i < 3; ++i) { + std::size_t vtx_1 = face[i]; + std::size_t vtx_2 = face[(i + 1) % 3]; + uint64_t key = edge_key(vtx_1, vtx_2); + if (map_edge_to_sub_vtx.count(key) > 0) { + continue; + } + TriVertex edge_vtx = (original_vertices[vtx_1] + original_vertices[vtx_2]) * 0.5; + map_edge_to_sub_vtx[key] = sub_vertices.size(); + sub_vertices.push_back(edge_vtx); + } + } + if (sub_progress) { + sub_progress(50); + } + + // Subdivide faces and their UVs: each original face splits into 4 sub-faces (parallel writes, no contention) + const std::size_t N = original_faces.size(); + sub_faces.resize(N * 4); + sub_uv_coords.resize(N * 4); + std::atomic has_missing_edge{false}; + + tbb::parallel_for(tbb::blocked_range(0, N), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + const std::size_t base = fid * 4; + const auto& face = original_faces[fid]; + std::size_t vtx_0 = face[0]; + std::size_t vtx_1 = face[1]; + std::size_t vtx_2 = face[2]; + + auto it01 = map_edge_to_sub_vtx.find(edge_key(vtx_0, vtx_1)); + auto it12 = map_edge_to_sub_vtx.find(edge_key(vtx_1, vtx_2)); + auto it20 = map_edge_to_sub_vtx.find(edge_key(vtx_2, vtx_0)); + if (it01 == map_edge_to_sub_vtx.end() || it12 == map_edge_to_sub_vtx.end() || it20 == map_edge_to_sub_vtx.end()) [[unlikely]] { + has_missing_edge.store(true, std::memory_order_relaxed); + Vec3i32 degen(vtx_0, vtx_0, vtx_0); + FaceUVArray degen_uv = {uv_coords[fid][0], uv_coords[fid][0], uv_coords[fid][0]}; + for (int k = 0; k < 4; ++k) { + sub_faces[base + k] = degen; + sub_uv_coords[base + k] = degen_uv; + } + continue; + } + std::size_t e01 = it01->second; + std::size_t e12 = it12->second; + std::size_t e20 = it20->second; + + const Vec2f& uv0 = uv_coords[fid][0]; + const Vec2f& uv1 = uv_coords[fid][1]; + const Vec2f& uv2 = uv_coords[fid][2]; + Vec2f uv_e01 = (uv0 + uv1) * 0.5f; + Vec2f uv_e12 = (uv1 + uv2) * 0.5f; + Vec2f uv_e20 = (uv2 + uv0) * 0.5f; + + sub_faces[base + 0] = Vec3i32(vtx_0, e01, e20); + sub_uv_coords[base + 0] = {uv0, uv_e01, uv_e20}; + + sub_faces[base + 1] = Vec3i32(e01, vtx_1, e12); + sub_uv_coords[base + 1] = {uv_e01, uv1, uv_e12}; + + sub_faces[base + 2] = Vec3i32(e01, e12, e20); + sub_uv_coords[base + 2] = {uv_e01, uv_e12, uv_e20}; + + sub_faces[base + 3] = Vec3i32(e20, e12, vtx_2); + sub_uv_coords[base + 3] = {uv_e20, uv_e12, uv2}; + } + }); + // Remove degenerate triangles (three identical vertices) to avoid impacting downstream SDF / Remesh steps + if (has_missing_edge.load(std::memory_order_relaxed)) { + std::size_t write_idx = 0; + for (std::size_t i = 0; i < sub_faces.size(); ++i) { + if (sub_faces[i][0] == sub_faces[i][1] && sub_faces[i][1] == sub_faces[i][2]) { + continue; + } + if (write_idx != i) { + sub_faces[write_idx] = sub_faces[i]; + sub_uv_coords[write_idx] = sub_uv_coords[i]; + } + ++write_idx; + } + BOOST_LOG_TRIVIAL(warning) << "[warning] linear_subdivision has missing edge vertex, removed " << (sub_faces.size() - write_idx) << " degenerate triangles"; + sub_faces.resize(write_idx); + sub_uv_coords.resize(write_idx); + } + + if (sub_progress) { + sub_progress(100); + } + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: input faces count = " << mesh.indices.size() << "."; + mesh = TriMesh(sub_faces, sub_vertices); + uv_coords = std::move(sub_uv_coords); + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: output faces count = " << mesh.indices.size() << "."; + return true; +} + +bool TextureToColor(const TriMesh& texture_mesh, const std::vector>& texture_mesh_uv_coords, const cv::Mat& texture, TriMesh& color_mesh, + std::vector>& face_colors, const TextureToColorSettings& settings, AlgoProgressCallback progress_callback, + AlgoCancelCallback cancel_callback) { + auto report = [&](int pct, const char* msg) { + if (progress_callback) { + progress_callback({pct, msg}); + } + }; + auto sub_report = [&](int sub_pct, int range_start, int range_end, const char* msg) { + int pct = range_start + sub_pct * (range_end - range_start) / 100; + report(pct, msg); + }; + auto cancelled = [&]() -> bool { + if (cancel_callback && cancel_callback()) { + BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " cancelled"; + return true; + } + return false; + }; + + color_mesh.clear(); + face_colors.clear(); + + report(0, "Initializing"); + if (cancelled()) { + return false; + } + + if (texture_mesh.indices.size() == 0) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: texture mesh has no faces."; + return false; + } + if (texture.empty()) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: texture is empty."; + return false; + } + if (texture.channels() < 3) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: texture must have at least 3 channels, got " << texture.channels(); + return false; + } + if (texture_mesh_uv_coords.size() != texture_mesh.indices.size()) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: uv_coords size is not equal to texture mesh faces size."; + return false; + } + for (std::size_t fid = 0; fid < texture_mesh.indices.size(); ++fid) { + if (texture_mesh_uv_coords[fid].size() != 3) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: uv_coords of single face size is not equal to 3."; + return false; + } + } + color_mesh = texture_mesh; + + using Clock = std::chrono::high_resolution_clock; + const auto t_total_start = Clock::now(); + auto t_step = t_total_start; + auto lap = [&](const char* step_name) { + auto now = Clock::now(); + double ms = std::chrono::duration(now - t_step).count(); + BOOST_LOG_TRIVIAL(debug) << "[timing] " << step_name << ": " << ms << "ms" + << " faces=" << color_mesh.facets_count(); + t_step = now; + }; + + report(5, "Oversampling"); + if (cancelled()) { + return false; + } + + // Step 1: Oversampling (subdivision while propagating UVs) + // Convert external vector> to internal vector> to eliminate inner-level heap allocations + std::vector color_mesh_uv_coords(texture_mesh_uv_coords.size()); + for (std::size_t i = 0; i < texture_mesh_uv_coords.size(); ++i) { + color_mesh_uv_coords[i] = {texture_mesh_uv_coords[i][0], texture_mesh_uv_coords[i][1], texture_mesh_uv_coords[i][2]}; + } + { + // Estimate total iterations and map each iteration's sub-progress to the [5, 25] range + size_t estimated_iters = 0; + if (settings.oversampling_iters > 0) { + estimated_iters = settings.oversampling_iters; + } else { + size_t fc = color_mesh.facets_count(); + while (fc < settings.oversampling_min_face_count) { + fc *= 4; + ++estimated_iters; + } + if (estimated_iters == 0) { + estimated_iters = 1; + } + } + + auto make_iter_progress = [&](size_t iter) { + return [&, iter, estimated_iters](int pct) { + int iter_start = static_cast(iter * 100 / estimated_iters); + int iter_end = static_cast((iter + 1) * 100 / estimated_iters); + int sub_pct = iter_start + pct * (iter_end - iter_start) / 100; + sub_report(sub_pct, 5, 25, "Oversampling"); + }; + }; + + if (settings.oversampling_iters > 0) { + for (size_t i = 0; i < settings.oversampling_iters && color_mesh.facets_count() * 4.0 < settings.oversampling_max_face_count; ++i) { + if (cancelled()) return false; + linear_subdivision(color_mesh, color_mesh_uv_coords, make_iter_progress(i)); + } + } else { + size_t iter = 0; + while (color_mesh.facets_count() < settings.oversampling_min_face_count) { + if (cancelled()) return false; + linear_subdivision(color_mesh, color_mesh_uv_coords, make_iter_progress(iter++)); + } + } + } + + lap("Oversampling"); + + face_colors.resize(color_mesh.indices.size()); + + report(25, "Computing face colors"); + if (cancelled()) { + return false; + } + + // Step 2: Compute each face's color (7-point Gaussian quadrature + bilinear interpolation sampling) + { + std::atomic done_faces{0}; + std::atomic cancel_requested{false}; + const size_t total_faces = color_mesh.indices.size(); + const size_t report_interval = std::max(total_faces / 20, 1); + tbb::parallel_for(tbb::blocked_range(0, total_faces), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + if (cancel_requested.load(std::memory_order_relaxed)) return; + face_colors[fid] = sample_face_color(color_mesh_uv_coords[fid], texture); + size_t cnt = done_faces.fetch_add(1, std::memory_order_relaxed) + 1; + if (cnt % report_interval == 0) { + if (cancelled()) { cancel_requested.store(true, std::memory_order_relaxed); return; } + sub_report(static_cast(cnt * 100 / total_faces), 25, 40, "Computing face colors"); + } + } + }); + if (cancel_requested.load() || cancelled()) return false; + } + lap("Computing face colors"); +#ifdef OUTPUT_TEST_RESULT + SaveToOFF("texture_to_color_0_initialize.off", color_mesh, face_colors); +#endif + + report(40, "Repairing mesh"); + if (cancelled()) { + return false; + } + + // Sub-stage timing helper for the "Repairing mesh" outer lap. Logs each + // sub-phase under a [timing][Repairing mesh] prefix so that regressions in + // mesh inspection, RepairMesh, AABB resampling, etc. can be attributed + // to a specific sub-stage without changing the outer lap structure. + auto sub_lap = [&](const char* sub_name, Clock::time_point t0) { + double ms = std::chrono::duration(Clock::now() - t0).count(); + BOOST_LOG_TRIVIAL(debug) << "[timing][Repairing mesh] " << sub_name << ": " << ms << "ms"; + }; + + // Step 3: Repair mesh + // Many textured models have non-manifold, non-closed, or other issues that need to be fixed beforehand + auto resample_repaired_mesh = [&](TriMesh&& repaired_mesh) -> bool { + // AABBTreeIndirect references vertices/faces externally, so snapshot the + // pre-repair geometry by moving them out of color_mesh before it gets + // overwritten with the repaired mesh below. std::move on std::vector is + // O(1) (pointer adoption), no element copy. + const auto t_aabb = Clock::now(); + TriVertices old_vertices = std::move(color_mesh.vertices); + TriFaces old_indices = std::move(color_mesh.indices); + auto before_repair_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(old_vertices, old_indices); + sub_lap("resample.aabb_build", t_aabb); + + color_mesh = std::move(repaired_mesh); + + const auto t_is_closed = Clock::now(); + if (is_closed(color_mesh)) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: repaired mesh is closed."; + } else { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: repaired mesh is open."; + } + sub_lap("resample.is_closed", t_is_closed); + + // New faces after repair inherit old face colors via centroid nearest-neighbor lookup. + // Since the mesh barely changes after repair, resampling via centroid nearest-neighbor is sufficient. + const auto t_resample = Clock::now(); + std::vector new_face_colors(color_mesh.facets_count()); + tbb::parallel_for(tbb::blocked_range(0, color_mesh.facets_count()), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + const auto& face = color_mesh.indices[fid]; + Vec3f center = (color_mesh.vertices[face[0]] + color_mesh.vertices[face[1]] + color_mesh.vertices[face[2]]) / 3.0f; + size_t hit_idx = 0; + Vec3f closest; + AABBTreeIndirect::squared_distance_to_indexed_triangle_set( + old_vertices, old_indices, before_repair_tree, center, hit_idx, closest); + new_face_colors[fid] = face_colors[hit_idx]; + } + }); + face_colors = std::move(new_face_colors); + sub_lap("resample.parallel_nearest", t_resample); + return true; + }; + + auto repair_and_resample_mesh = [&]() -> bool { + std::shared_ptr repaired_mesh; + const auto t_repair = Clock::now(); + bool success = RepairMesh(color_mesh, repaired_mesh); + sub_lap("RepairMesh", t_repair); + if (success == false) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: repair mesh failed."; + return false; + } + if (cancelled()) return false; + return resample_repaired_mesh(std::move(*repaired_mesh)); + }; + + { + const auto t_stats = Clock::now(); + TriangleMesh stats_mesh(static_cast(color_mesh)); + const auto& stats = stats_mesh.stats(); + sub_lap("stats_check", t_stats); + // Orca's TriangleMeshStats defines manifold() as open_edges == 0 and does not track + // non-manifold edges/vertices separately, so BBS's "!manifold() || has_open_edges()" + // collapses to this single test and the extra counters drop out of the log. + if (!stats.manifold()) { + BOOST_LOG_TRIVIAL(info) << "TextureToColor: mesh has non-manifold geometry or open boundaries, open_edges=" + << stats.open_edges; + if (settings.mesh_repair_decision == MeshRepairDecision::Ask) { + if (settings.mesh_repair_decision_required) + *settings.mesh_repair_decision_required = true; + return false; + } + if (settings.mesh_repair_decision == MeshRepairDecision::RepairAndImport) { + indexed_triangle_set repaired_its; + std::string repair_error; + const auto t_win3d = Clock::now(); + bool repaired = settings.mesh_repair_callback && settings.mesh_repair_callback(static_cast(color_mesh), repaired_its, + [&](const char* message, unsigned percent) { + sub_report(static_cast(percent), 40, 60, message ? message : "Repairing mesh"); + }, + [&]() { return cancelled(); }, &repair_error); + sub_lap("windows_3d_repair", t_win3d); + if (repaired) { + if (cancelled()) return false; + BOOST_LOG_TRIVIAL(info) << "TextureToColor: Windows 3D mesh repair finished."; + if (!resample_repaired_mesh(TriMesh(std::move(repaired_its)))) + return false; + } else { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: Windows 3D mesh repair failed: " << repair_error; + } + } else { + BOOST_LOG_TRIVIAL(info) << "TextureToColor: importing mesh without Windows 3D repair."; + } + } + } + + const auto t_halfedge = Clock::now(); + const bool halfedge_ok = cgalutils::is_mesh_halfedge_compatible(color_mesh); + sub_lap("is_mesh_halfedge_compatible", t_halfedge); + if (!halfedge_ok && repair_and_resample_mesh() == false) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: repair and resample mesh failed."; + return false; + } + lap("Repairing mesh"); +#ifdef OUTPUT_TEST_RESULT + SaveToOFF("texture_to_color_1_repair.off", color_mesh, face_colors); +#endif + + report(65, "Color clustering"); + if (cancelled()) { + return false; + } + + // Step 5: Color clustering + std::vector cluster_centers; + std::vector clustered_face_colors = face_colors; + std::vector clustered_face_labels(face_colors.size()); + const bool adaptive_cluster = settings.target_colors_num == 0; + + // Compute cluster centers + if (adaptive_cluster) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: use cluster adaptive method."; + ClusterParameters para; + para.max_color_distance = settings.max_color_distance; + para.max_cluster_k = settings.max_cluster_k; + para.cancel_callback = cancel_callback ? [&]() { return cancel_callback(); } : std::function{}; + cluster_centers = cluster_adaptive(face_colors, para); + if (cancelled()) return false; + } else { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: use cluster k-means method."; + ClusterParameters para; + para.cluster_k = settings.target_colors_num; + para.cancel_callback = cancel_callback ? [&]() { return cancel_callback(); } : std::function{}; + cluster_centers = cluster_k_means(face_colors, para); + if (cancelled()) return false; + } + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: the k is " << cluster_centers.size() << "."; + if (cluster_centers.empty()) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: no cluster center generated."; + return false; + } + const std::set unique_cluster_centers(cluster_centers.begin(), cluster_centers.end()); + if (unique_cluster_centers.size() != cluster_centers.size()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: cluster centers contain duplicated RGB values, unique exported colors may be fewer than centers."; + } + + report(70, "Assigning cluster labels"); + if (cancelled()) { + return false; + } + + // Assign each face's color to the nearest cluster center + constexpr bool use_simple_cluster = true; // Complex algorithm is still being optimized; use simple assignment for now + if (use_simple_cluster) { + std::atomic done_cluster{0}; + std::atomic cancel_requested{false}; + const size_t total_cluster = color_mesh.indices.size(); + const size_t cluster_interval = std::max(total_cluster / 20, 1); + tbb::parallel_for(tbb::blocked_range(0, total_cluster), [&](const tbb::blocked_range& range) { + for (std::size_t fid = range.begin(); fid < range.end(); ++fid) { + if (cancel_requested.load(std::memory_order_relaxed)) return; + auto& face_color = face_colors[fid]; + auto nearest_color_id = std::numeric_limits::max(); + bool success = calc_nearest_color_id(cluster_centers, face_color, nearest_color_id); + if (success == false) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: calc nearest color id failed."; + continue; + } + clustered_face_labels[fid] = nearest_color_id; + clustered_face_colors[fid] = cluster_centers[nearest_color_id]; + size_t cnt = done_cluster.fetch_add(1, std::memory_order_relaxed) + 1; + if (cnt % cluster_interval == 0) { + if (cancelled()) { cancel_requested.store(true, std::memory_order_relaxed); return; } + sub_report(static_cast(cnt * 100 / total_cluster), 70, 85, "Assigning cluster labels"); + } + } + }); + if (cancel_requested.load() || cancelled()) return false; + } else { + bool success = mesh_cluster(color_mesh, cluster_centers, clustered_face_colors, clustered_face_labels); + if (success == false) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: mesh cluster failed."; + return false; + } + } + if (adaptive_cluster) { + if (!discard_unused_cluster_centers(cluster_centers, clustered_face_labels, "cluster assignment")) { + return false; + } + } else { + ensure_all_cluster_centers_used(face_colors, cluster_centers, clustered_face_labels, "cluster assignment"); + } + lap("Color clustering & labeling"); +#ifdef OUTPUT_TEST_RESULT + for (std::size_t i = 0; i < clustered_face_colors.size(); ++i) { + clustered_face_colors[i] = cluster_centers[clustered_face_labels[i]]; + } + SaveToOFF("texture_to_color_3_cluster.off", color_mesh, clustered_face_colors); +#endif + + report(85, "Smoothing colors"); + if (cancelled()) { + return false; + } + + // Step 6: Post-process colors + SmoothParameters smooth_parameters; + smooth_parameters.smooth_weight = settings.smooth_weight; + if (!smooth_region(color_mesh, clustered_face_labels, smooth_parameters)) { + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: smooth region failed."; + return false; + } + BOOST_LOG_TRIVIAL(debug) << "TextureToColor: smooth region success."; + if (adaptive_cluster) { + if (!discard_unused_cluster_centers(cluster_centers, clustered_face_labels, "color smoothing")) { + return false; + } + } else { + ensure_all_cluster_centers_used(face_colors, cluster_centers, clustered_face_labels, "color smoothing"); + } + report(95, "Updating face colors"); + if (cancelled()) { + return false; + } + for (std::size_t i = 0; i < clustered_face_colors.size(); ++i) { + clustered_face_colors[i] = cluster_centers[clustered_face_labels[i]]; + } + const std::set unique_exported_colors(clustered_face_colors.begin(), clustered_face_colors.end()); + if (unique_exported_colors.size() < cluster_centers.size()) { + BOOST_LOG_TRIVIAL(warning) << "TextureToColor: final exported unique colors (" << unique_exported_colors.size() + << ") are fewer than cluster centers (" << cluster_centers.size() + << "), likely due to duplicate centers or unsatisfied seed assignment."; + } +#ifdef OUTPUT_TEST_RESULT + SaveToOFF("texture_to_color_4_smooth.off", color_mesh, clustered_face_colors); +#endif + + face_colors = std::move(clustered_face_colors); + lap("Smoothing colors"); + double total_ms = std::chrono::duration(Clock::now() - t_total_start).count(); + BOOST_LOG_TRIVIAL(debug) << "[timing] TextureToColor total: " << total_ms << "ms" + << " faces=" << color_mesh.facets_count(); + report(100, "Completed"); + return true; +} + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/TextureToColor.hpp b/src/libslic3r/TextureToColor/TextureToColor.hpp new file mode 100644 index 0000000000..f18cce5759 --- /dev/null +++ b/src/libslic3r/TextureToColor/TextureToColor.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include "Callbacks.hpp" +#include "TriMesh.hpp" +#include "opencv2/core.hpp" +#include +#include + +namespace Slic3r { namespace tex2color { + +enum class MeshRepairDecision { + Ask, + ImportWithoutRepair, + RepairAndImport +}; + +using MeshRepairCallback = std::function progress_callback, + std::function cancel_callback, + std::string* error_message)>; + +struct TextureToColorSettings { + std::size_t target_colors_num = 4; // 目标颜色数量, 为0时, 自适应计算; 否则计算指定数目的颜色聚类 + + double smooth_weight = 0.5; // 光顺权重, 范围[0, 1], 0表示不进行光顺, 1表示完全光顺 + + // 当超采样迭代次数大于0时, 进行指定迭代次数的超采样; 否则, 自适应超采样 + std::size_t oversampling_iters = 0; // 超采样迭代次数 + std::size_t oversampling_min_face_count = 10000; // 自适应采样: 当face_count小于oversampling_min_face_count时, 进行超采样 + std::size_t oversampling_max_face_count = 1000000; // 无论输入参数如何, 超采样后的面片数不能超过oversampling_max_face_count + + double max_color_distance = 25.0; // 自适应聚类允许的最大簇内半径(CIEDE2000 ΔE) + std::size_t max_cluster_k = 32; // 自适应聚类的最大颜色数量上限 + + MeshRepairDecision mesh_repair_decision = MeshRepairDecision::ImportWithoutRepair; + + // Set by TextureToColor when Ask is selected and mesh repair needs user confirmation. + bool* mesh_repair_decision_required = nullptr; + + MeshRepairCallback mesh_repair_callback; +}; + +/** + * @brief 将纹理贴图转换为网格面片颜色, 并通过聚类和光顺生成可用于多色打印的着色网格 + * + * 基于纹理网格的UV坐标对纹理图像进行采样, 计算每个面片的颜色, + * 然后对颜色进行聚类(K-Means或自适应)和区域光顺, 最终输出带颜色信息的网格 + * + * @param[in] texture_mesh 带有UV坐标的输入三角网格 + * @param[in] uv_coords 每个面片的UV坐标, 大小等于面片数, 每个面片有三个UV坐标 + * @param[in] texture 纹理图像 + * @param[out] color_mesh 输出的着色网格 + * @param[out] face_colors 输出的着色网格的面片颜色, 大小等于面片数, 颜色值为[R, G, B], 范围0~255 + * @param[in] settings 算法参数, 包括目标颜色数量、光顺权重等 + * @param[in] progress_callback 进度回调函数 + * @param[in] cancel_callback 取消回调函数 + * @return 成功返回true, 输入数据无效(空网格、无UV、空纹理等)返回false + */ +bool TextureToColor(const TriMesh& texture_mesh, const std::vector>& uv_coords, const cv::Mat& texture, TriMesh& color_mesh, + std::vector>& face_colors, const TextureToColorSettings& settings = TextureToColorSettings(), + AlgoProgressCallback progress_callback = nullptr, AlgoCancelCallback cancel_callback = nullptr); + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TextureToColor/TriMesh.hpp b/src/libslic3r/TextureToColor/TriMesh.hpp new file mode 100644 index 0000000000..d557ae1d2e --- /dev/null +++ b/src/libslic3r/TextureToColor/TriMesh.hpp @@ -0,0 +1,28 @@ +#pragma once +#include +#include "Point.hpp" + +namespace Slic3r { namespace tex2color { + +using TriVertex = stl_vertex; +using TriVertices = std::vector; +using TriFace = stl_triangle_vertex_indices; +using TriFaces = std::vector; + +struct TriMesh : ::indexed_triangle_set { + TriMesh() = default; + TriMesh(const TriMesh&) = default; + TriMesh& operator=(const TriMesh&) = default; + TriMesh(TriMesh&&) = default; + TriMesh& operator=(TriMesh&&) = default; + TriMesh(const ::indexed_triangle_set& d) : ::indexed_triangle_set(d) {} + TriMesh(::indexed_triangle_set&& d) : ::indexed_triangle_set(std::move(d)) {} + TriMesh(std::vector indices_, + std::vector vertices_) + : ::indexed_triangle_set(std::move(indices_), std::move(vertices_)) {} + + std::size_t facets_count() const { return indices.size(); } +}; + +} // namespace tex2color +} // namespace Slic3r diff --git a/src/libslic3r/TriangleSelector.cpp b/src/libslic3r/TriangleSelector.cpp index 3b032cc57e..12f314a799 100644 --- a/src/libslic3r/TriangleSelector.cpp +++ b/src/libslic3r/TriangleSelector.cpp @@ -1983,6 +1983,20 @@ void TriangleSelector::seed_fill_unselect_all_triangles() triangle.unselect_by_seed_fill(); } +void TriangleSelector::shift_states_above(EnforcerBlockerType threshold, int delta) +{ + for (Triangle &triangle : m_triangles) { + if (triangle.is_split() || !triangle.valid()) + continue; + EnforcerBlockerType s = triangle.get_state(); + if (s >= threshold && s != EnforcerBlockerType::NONE) { + int new_val = (int)s + delta; + if (new_val >= 0) + triangle.set_state(EnforcerBlockerType(new_val)); + } + } +} + void TriangleSelector::seed_fill_apply_on_triangles(EnforcerBlockerType new_state) { for (Triangle &triangle : m_triangles) diff --git a/src/libslic3r/TriangleSelector.hpp b/src/libslic3r/TriangleSelector.hpp index 11517f5c6c..41d189cdd1 100644 --- a/src/libslic3r/TriangleSelector.hpp +++ b/src/libslic3r/TriangleSelector.hpp @@ -369,6 +369,9 @@ public: // For all triangles, remove the flag indicating that the triangle was selected by seed fill. void seed_fill_unselect_all_triangles(); + // Shift all triangle states >= threshold by delta (used when inserting filaments) + void shift_states_above(EnforcerBlockerType threshold, int delta); + // For all triangles selected by seed fill, set new EnforcerBlockerType and remove flag indicating that triangle was selected by seed fill. // The operation may merge split triangles if they are being assigned the same color. void seed_fill_apply_on_triangles(EnforcerBlockerType new_state); diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index 9174b044ec..b014b44bb2 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -353,6 +353,16 @@ set(SLIC3R_GUI_SOURCES GUI/Monitor.hpp GUI/MonitorPage.cpp GUI/MonitorPage.hpp + GUI/MixedFilamentDialog.cpp + GUI/MixedFilamentDialog.hpp + GUI/GradientCurveEditor.cpp + GUI/GradientCurveEditor.hpp + GUI/ColorDecomposeDialog.cpp + GUI/ColorDecomposeDialog.hpp + GUI/ColorDecomposeSupport.cpp + GUI/ColorDecomposeSupport.hpp + GUI/TextureImportDialog.cpp + GUI/TextureImportDialog.hpp GUI/Mouse3DController.cpp GUI/Mouse3DController.hpp GUI/MsgDialog.cpp diff --git a/src/slic3r/GUI/ColorDecomposeDialog.cpp b/src/slic3r/GUI/ColorDecomposeDialog.cpp new file mode 100644 index 0000000000..3877d71e10 --- /dev/null +++ b/src/slic3r/GUI/ColorDecomposeDialog.cpp @@ -0,0 +1,943 @@ +#include "ColorDecomposeDialog.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "wx/graphics.h" + +#include "I18N.hpp" +#include "GUI.hpp" +#include "GUI_App.hpp" +#include "format.hpp" +#include "Widgets/ComboBox.hpp" +#include "Widgets/DropDown.hpp" +#include "Widgets/Button.hpp" +#include "Widgets/CheckBox.hpp" +#include "Widgets/Label.hpp" +#include "wxExtensions.hpp" +#include "ColorDecomposeSupport.hpp" +#include "libslic3r/ColorDecomposeRecipe.hpp" + +namespace Slic3r { +namespace GUI { + +static const wxColour COLOR_BRAND("#00AE42"); +static const wxColour COLOR_BORDER_NORMAL("#EEEEEE"); +static const wxColour COLOR_BG_CARD("#F8F8F8"); +static const wxColour COLOR_LABEL_GREY("#ACACAC"); +static const wxColour COLOR_TEXT_DARK("#262E30"); +static const wxColour COLOR_DIVIDER("#EEEEEE"); + +// Standard CMYW base colors +static const wxColour CMYW_CYAN(0, 255, 255); +static const wxColour CMYW_MAGENTA(255, 0, 255); +static const wxColour CMYW_YELLOW(255, 255, 0); +static const wxColour CMYW_WHITE(255, 255, 255); + +// Standard RYBW base colors +static const wxColour RYBW_RED(255, 0, 0); +static const wxColour RYBW_YELLOW(255, 255, 0); +static const wxColour RYBW_BLUE(0, 0, 255); +static const wxColour RYBW_WHITE(255, 255, 255); + +static size_t mode_index(DecomposeMode mode) +{ + return static_cast(mode); +} + +static ColorDecomposeRgb wx_colour_to_recipe_rgb(const wxColour& color) +{ + return { + static_cast(color.Red()), + static_cast(color.Green()), + static_cast(color.Blue()) + }; +} + +static wxColour hex_to_wx_colour(const std::string& hex, const wxColour& fallback) +{ + wxColour color(hex); + return color.IsOk() ? color : fallback; +} + +static bool same_rgb(const wxColour& lhs, const wxColour& rhs) +{ + return lhs.Red() == rhs.Red() && lhs.Green() == rhs.Green() && lhs.Blue() == rhs.Blue(); +} + +static DecomposeBaseColor standard_base_color_from_key(const std::string& key) +{ + if (key == "Cyan") return DecomposeBaseColor::Cyan; + if (key == "Magenta") return DecomposeBaseColor::Magenta; + if (key == "Yellow") return DecomposeBaseColor::Yellow; + if (key == "White") return DecomposeBaseColor::White; + if (key == "Red") return DecomposeBaseColor::Red; + if (key == "Green") return DecomposeBaseColor::Green; + if (key == "Blue") return DecomposeBaseColor::Blue; + return DecomposeBaseColor::None; +} + +static wxColour pure_color_for_base(DecomposeBaseColor base) +{ + switch (base) { + case DecomposeBaseColor::Cyan: return CMYW_CYAN; + case DecomposeBaseColor::Magenta: return CMYW_MAGENTA; + case DecomposeBaseColor::Yellow: return CMYW_YELLOW; + case DecomposeBaseColor::White: return CMYW_WHITE; + case DecomposeBaseColor::Red: return RYBW_RED; + case DecomposeBaseColor::Blue: return RYBW_BLUE; + default: return *wxBLACK; + } +} + +static DecomposeBaseColor standard_base_color_for(DecomposeMode mode, const wxColour& color) +{ + if (mode == DecomposeMode::CMYW) { + if (same_rgb(color, CMYW_CYAN)) return DecomposeBaseColor::Cyan; + if (same_rgb(color, CMYW_MAGENTA)) return DecomposeBaseColor::Magenta; + if (same_rgb(color, CMYW_YELLOW)) return DecomposeBaseColor::Yellow; + if (same_rgb(color, CMYW_WHITE)) return DecomposeBaseColor::White; + } else if (mode == DecomposeMode::RYBW) { + if (same_rgb(color, RYBW_RED)) return DecomposeBaseColor::Red; + if (same_rgb(color, RYBW_YELLOW)) return DecomposeBaseColor::Yellow; + if (same_rgb(color, RYBW_BLUE)) return DecomposeBaseColor::Blue; + if (same_rgb(color, RYBW_WHITE)) return DecomposeBaseColor::White; + } + return DecomposeBaseColor::None; +} + +static ColorDecomposeResult to_dialog_result(const ColorDecomposeRecipeResult& recipe, + const wxColour& fallback) +{ + ColorDecomposeResult result; + result.mode = recipe.mode; + result.matched_color = hex_to_wx_colour(recipe.matched_color_hex, fallback); + for (const auto& comp_recipe : recipe.components) { + DecomposeComponent comp; + comp.colour = hex_to_wx_colour(comp_recipe.color_hex, fallback); + comp.ratio = comp_recipe.ratio; + comp.filament_index = static_cast(comp_recipe.filament_index); + comp.base_color = standard_base_color_from_key(comp_recipe.base_color); + if (comp.base_color == DecomposeBaseColor::None) + comp.base_color = standard_base_color_for(recipe.mode, comp.colour); + result.components.push_back(comp); + } + return result; +} + +static wxPanel* create_h_divider(wxWindow* parent, int fixed_width = -1) +{ + const int h = parent->FromDIP(1); + int w = fixed_width > 0 ? fixed_width : -1; + auto* panel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(w, h)); + panel->SetMinSize(wxSize(w, h)); + if (fixed_width > 0) + panel->SetMaxSize(wxSize(fixed_width, h)); + panel->SetBackgroundColour(StateColor::darkModeColorFor(COLOR_DIVIDER)); + return panel; +} + +static wxStaticText* create_mode_group_label(wxWindow* parent, const wxString& text) +{ + auto* label = new wxStaticText(parent, wxID_ANY, text); + label->SetFont(Label::Body_11); + label->SetForegroundColour(StateColor::darkModeColorFor(COLOR_LABEL_GREY)); + return label; +} + +static void match_parent_bg(wxWindow* w, const wxColour& bg) +{ + w->SetBackgroundColour(bg); +} + +static bool material_type_matches(const std::string& a, const std::string& b) +{ + if (a.empty() || b.empty()) + return false; + return a == b || a == b + " Basic" || b == a + " Basic"; +} + + +ColorDecomposeDialog::ColorDecomposeDialog(wxWindow* parent, + int filament_idx, + const wxColour& target_color, + const std::vector& physical_colors, + const std::vector& filament_names, + const std::vector& filament_types, + size_t current_filament_count, + size_t max_filament_count, + std::vector physical_config_indices) + : DPIDialog(parent, wxID_ANY, _L("Decompose Color"), wxDefaultPosition, + wxDefaultSize, wxCAPTION | wxCLOSE_BOX) + , m_filament_idx(filament_idx) + , m_target_color(target_color) + , m_physical_colors(physical_colors) + , m_filament_names(filament_names) + , m_filament_types(filament_types) + , m_current_filament_count(current_filament_count) + , m_max_filament_count(max_filament_count) + , m_physical_config_indices(std::move(physical_config_indices)) +{ + for (const auto& t : m_filament_types) { + if (std::find(m_project_types.begin(), m_project_types.end(), t) == m_project_types.end()) + m_project_types.push_back(t); + } + + if (m_filament_idx >= 0 && static_cast(m_filament_idx) < m_filament_types.size()) + m_preferred_type = m_filament_types[m_filament_idx]; + else if (!m_project_types.empty()) + m_preferred_type = m_project_types.front(); + + build_ui(); + wxGetApp().UpdateDlgDarkUI(this); + // Restore target swatch after dark mode color remapping + if (m_target_swatch) + m_target_swatch->SetBackgroundColour(m_target_color); + + update_card_visibility(); + Fit(); + compute_decomposition(); + update_matched_color_display(); + update_ok_button_state(); +} + +void ColorDecomposeDialog::on_dpi_changed(const wxRect& suggested_rect) +{ + (void)suggested_rect; + Fit(); + Refresh(); +} + +void ColorDecomposeDialog::build_ui() +{ + SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + + auto* main_sizer = new wxBoxSizer(wxVERTICAL); + + const int selector_side_margin = FromDIP(26); + const int selector_top_gap = FromDIP(22); + const int content_side_margin = FromDIP(30); + const int target_section_top_gap = FromDIP(18); + + main_sizer->AddSpacer(selector_top_gap); + main_sizer->Add(create_filament_selector(), 0, wxEXPAND | wxLEFT | wxRIGHT, selector_side_margin); + main_sizer->AddSpacer(target_section_top_gap); + main_sizer->Add(create_target_color_section(), 0, wxEXPAND | wxLEFT | wxRIGHT, content_side_margin); + main_sizer->AddSpacer(FromDIP(16)); + main_sizer->Add(create_h_divider(this), 0, wxEXPAND | wxLEFT | wxRIGHT, content_side_margin); + main_sizer->AddSpacer(FromDIP(16)); + main_sizer->Add(create_mode_selection_section(), 0, wxEXPAND | wxLEFT | wxRIGHT, content_side_margin); + main_sizer->AddSpacer(FromDIP(16)); + main_sizer->Add(create_button_panel(), 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, content_side_margin); + + SetSizer(main_sizer); + SetMinSize(wxSize(FromDIP(477), FromDIP(380))); + Fit(); + CenterOnParent(); +} + +wxBoxSizer* ColorDecomposeDialog::create_filament_selector() +{ + auto* sizer = new wxBoxSizer(wxHORIZONTAL); + + m_type_combo = new ComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, + wxSize(-1, FromDIP(36)), 0, nullptr, wxCB_READONLY); + m_type_combo->SetFont(Label::Body_13); + + m_combo_item_types.clear(); + int default_sel = -1; + + // --- Group 1: Project filament list (deduplicated by type) --- + m_type_combo->Append(_L("Project Filament List"), wxNullBitmap, DD_ITEM_STYLE_SPLIT_ITEM | DD_ITEM_STYLE_DISABLED); + m_combo_item_types.push_back(std::string()); + + std::set seen_types; + for (size_t i = 0; i < m_filament_names.size(); ++i) { + const std::string& type = (i < m_filament_types.size()) ? m_filament_types[i] : "PLA"; + if (!seen_types.insert(type).second) + continue; + int idx = m_type_combo->Append(wxString::FromUTF8(m_filament_names[i])); + m_combo_item_types.push_back(type); + if (type == m_preferred_type && default_sel < 0) + default_sel = idx; + } + + // --- Group 2: Standard mode material recommendations --- + static const char* kStandardTypes[] = { + kDecomposePlaBasicType + }; + + m_type_combo->Append(_L("Standard Mode Recommendations"), wxNullBitmap, DD_ITEM_STYLE_SPLIT_ITEM | DD_ITEM_STYLE_DISABLED); + m_combo_item_types.push_back(std::string()); + + for (size_t s = 0; s < sizeof(kStandardTypes) / sizeof(kStandardTypes[0]); ++s) { + // Always show standard recommendations, even if the same type already + // appears in the project filament list above. + const std::string label = std::string(kDecomposeBambuPresetPrefix) + kStandardTypes[s]; + int idx = m_type_combo->Append(wxString::FromUTF8(label)); + m_combo_item_types.push_back(kStandardTypes[s]); + if (kStandardTypes[s] == m_preferred_type && default_sel < 0) + default_sel = idx; + } + + if (default_sel < 0) { + for (int i = 0; i < static_cast(m_combo_item_types.size()); ++i) { + if (!m_combo_item_types[i].empty()) { + default_sel = i; + break; + } + } + } + + if (default_sel >= 0) { + m_type_combo->SetSelection(default_sel); + if (!m_combo_item_types[default_sel].empty()) + m_preferred_type = m_combo_item_types[default_sel]; + } + + m_type_combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& evt) { + evt.StopPropagation(); + int sel = m_type_combo->GetSelection(); + if (sel >= 0 && static_cast(sel) < m_combo_item_types.size() + && !m_combo_item_types[sel].empty()) { + m_preferred_type = m_combo_item_types[sel]; + } + update_card_visibility(); + compute_decomposition(); + update_matched_color_display(); + update_ok_button_state(); + }); + + sizer->Add(m_type_combo, 1, wxEXPAND); + return sizer; +} + +static wxPanel* create_color_swatch(wxWindow* parent, const wxColour& color, int size) +{ + auto* panel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(size, size)); + panel->SetBackgroundColour(color); + panel->SetMinSize(wxSize(size, size)); + return panel; +} + +wxBoxSizer* ColorDecomposeDialog::create_target_color_section() +{ + auto* sizer = new wxBoxSizer(wxHORIZONTAL); + + auto* label = new wxStaticText(this, wxID_ANY, _L("Target Color")); + label->SetFont(Label::Head_14); + label->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + sizer->Add(label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(19)); + + m_target_swatch = create_color_swatch(this, m_target_color, FromDIP(28)); + sizer->Add(m_target_swatch, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(12)); + + m_target_rgb_text = new wxStaticText(this, wxID_ANY, + wxString::Format("RGB: %d, %d, %d", m_target_color.Red(), m_target_color.Green(), m_target_color.Blue())); + m_target_rgb_text->SetFont(Label::Body_13); + m_target_rgb_text->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + sizer->Add(m_target_rgb_text, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(12)); + + auto* arrow_text = new wxStaticText(this, wxID_ANY, wxString::FromUTF8("\xe2\x86\x92")); + arrow_text->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + sizer->Add(arrow_text, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(12)); + + m_matched_swatch = create_color_swatch(this, m_target_color, FromDIP(28)); + sizer->Add(m_matched_swatch, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(12)); + + m_matched_rgb_text = new wxStaticText(this, wxID_ANY, + wxString::Format("RGB: %d, %d, %d", m_target_color.Red(), m_target_color.Green(), m_target_color.Blue())); + m_matched_rgb_text->SetFont(Label::Head_13); + m_matched_rgb_text->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + sizer->Add(m_matched_rgb_text, 0, wxALIGN_CENTER_VERTICAL); + + return sizer; +} + +wxPanel* ColorDecomposeDialog::create_mode_card(wxWindow* parent, DecomposeMode mode, + const wxString& title) +{ + const int pad = FromDIP(12); + + auto* card = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE); + card->SetBackgroundStyle(wxBG_STYLE_PAINT); + + auto* card_sizer = new wxBoxSizer(wxVERTICAL); + + auto* title_sizer = new wxBoxSizer(wxHORIZONTAL); + auto* title_label = new wxStaticText(card, wxID_ANY, title); + title_label->SetFont(Label::Body_14); + title_label->SetForegroundColour(StateColor::darkModeColorFor(wxColour("#909090"))); + match_parent_bg(title_label, StateColor::darkModeColorFor(COLOR_BG_CARD)); + title_sizer->Add(title_label, 1, wxALIGN_CENTER_VERTICAL); + + auto* chk = new ::CheckBox(card); + chk->SetValue(mode == m_selected_mode); + match_parent_bg(chk, StateColor::darkModeColorFor(COLOR_BG_CARD)); + switch (mode) { + case DecomposeMode::MaterialList: m_chk_material_list = chk; break; + case DecomposeMode::CMYW: m_chk_cmyw = chk; break; + case DecomposeMode::RYBW: m_chk_rybw = chk; break; + } + chk->Bind(wxEVT_TOGGLEBUTTON, [this, mode](wxCommandEvent& e) { + select_mode(mode); + e.Skip(); // let CheckBox::update() re-sync its bitmap to GetValue() + }); + title_sizer->Add(chk, 0, wxALIGN_CENTER_VERTICAL); + + card_sizer->Add(title_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, pad); + + card_sizer->Add(create_h_divider(card), 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, FromDIP(8)); + + auto* colors_sizer = new wxBoxSizer(wxHORIZONTAL); + card_sizer->Add(colors_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM, pad); + + auto& controls = m_mode_cards[mode_index(mode)]; + controls.card = card; + controls.components_sizer = colors_sizer; + + card->SetSizer(card_sizer); + card->SetMinSize(wxSize(FromDIP(128), FromDIP(111))); + card->SetMaxSize(wxSize(FromDIP(128), FromDIP(111))); + + card->Bind(wxEVT_PAINT, [this, card, mode](wxPaintEvent&) { + wxBufferedPaintDC dc(card); + wxSize sz = card->GetClientSize(); + dc.SetBackground(wxBrush(StateColor::darkModeColorFor(*wxWHITE))); + dc.Clear(); + + bool selected = (m_selected_mode == mode); + wxColour border_col = selected + ? StateColor::darkModeColorFor(COLOR_BRAND) + : StateColor::darkModeColorFor(COLOR_BORDER_NORMAL); + const int border_width = FromDIP(selected ? 2 : 1); + const double inset = border_width / 2.0; + std::unique_ptr gc(wxGraphicsContext::Create(dc)); + if (gc) { + gc->SetPen(wxPen(border_col, border_width)); + gc->SetBrush(wxBrush(StateColor::darkModeColorFor(COLOR_BG_CARD))); + gc->DrawRoundedRectangle(inset, inset, sz.x - 2 * inset, sz.y - 2 * inset, FromDIP(8)); + } else { + const int fallback_inset = (border_width + 1) / 2; + dc.SetPen(wxPen(border_col, border_width)); + dc.SetBrush(wxBrush(StateColor::darkModeColorFor(COLOR_BG_CARD))); + dc.DrawRoundedRectangle(fallback_inset, fallback_inset, sz.x - 2 * fallback_inset, sz.y - 2 * fallback_inset, FromDIP(8)); + } + }); + + std::function bind_click; + bind_click = [this, mode, chk, &bind_click](wxWindow* w) { + if (w == chk || dynamic_cast<::CheckBox*>(w)) + return; + w->Bind(wxEVT_LEFT_UP, [this, mode](wxMouseEvent&) { + select_mode(mode); + }); + w->SetCursor(wxCursor(wxCURSOR_HAND)); + for (auto* child : w->GetChildren()) + bind_click(child); + }; + bind_click(card); + + return card; +} + +wxBoxSizer* ColorDecomposeDialog::create_mode_selection_section() +{ + auto* sizer = new wxBoxSizer(wxVERTICAL); + + auto* section_label = new wxStaticText(this, wxID_ANY, _L("Select Color Decomposition")); + section_label->SetFont(Label::Head_14); + section_label->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + sizer->Add(section_label, 0, wxBOTTOM, FromDIP(4)); + + auto* modes_sizer = new wxBoxSizer(wxHORIZONTAL); + + // --- Arbitrary mode column (wrapped in a panel so the whole column hides together) --- + m_arb_column_panel = new wxPanel(this, wxID_ANY); + m_arb_column_panel->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + auto* arb_col = new wxBoxSizer(wxVERTICAL); + { + auto* arb_header_sizer = new wxBoxSizer(wxHORIZONTAL); + arb_header_sizer->Add(create_mode_group_label(m_arb_column_panel, _L("Arbitrary Mode")), + 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(5)); + arb_header_sizer->Add(create_h_divider(m_arb_column_panel, FromDIP(88)), 0, wxALIGN_CENTER_VERTICAL); + arb_col->Add(arb_header_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(8)); + + m_card_material_list = create_mode_card(m_arb_column_panel, DecomposeMode::MaterialList, + _L("Material List")); + arb_col->Add(m_card_material_list, 0, wxEXPAND); + } + m_arb_column_panel->SetSizer(arb_col); + modes_sizer->Add(m_arb_column_panel, 0, wxEXPAND | wxRIGHT, FromDIP(16)); + + // --- Standard mode column --- + auto* std_col = new wxBoxSizer(wxVERTICAL); + { + auto* std_header_sizer = new wxBoxSizer(wxHORIZONTAL); + std_header_sizer->Add(create_mode_group_label(this, _L("Standard Mode")), + 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(5)); + std_header_sizer->Add(create_h_divider(this), 1, wxALIGN_CENTER_VERTICAL); + std_col->Add(std_header_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(8)); + + auto* cards_sizer = new wxBoxSizer(wxHORIZONTAL); + + m_card_cmyw = create_mode_card(this, DecomposeMode::CMYW, "CMYW"); + cards_sizer->Add(m_card_cmyw, 0, wxRIGHT, FromDIP(12)); + + m_card_rybw = create_mode_card(this, DecomposeMode::RYBW, "RYBW"); + cards_sizer->Add(m_card_rybw, 0); + + std_col->Add(cards_sizer, 0, wxEXPAND); + } + modes_sizer->Add(std_col, 0, wxEXPAND); + + sizer->Add(modes_sizer, 0, wxEXPAND); + + m_no_card_hint = new wxStaticText(this, wxID_ANY, + _L("At least two filaments of the same material type are required for decomposition")); + m_no_card_hint->SetFont(Label::Body_13); + m_no_card_hint->SetForegroundColour(StateColor::darkModeColorFor(wxColour("#909090"))); + m_no_card_hint->Wrap(FromDIP(400)); + m_no_card_hint->Hide(); + sizer->Add(m_no_card_hint, 0, wxTOP, FromDIP(8)); + + m_limit_warning_panel = new wxPanel(this, wxID_ANY); + m_limit_warning_panel->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + auto* warning_sizer = new wxBoxSizer(wxHORIZONTAL); + auto* warn_bmp = new wxStaticBitmap(m_limit_warning_panel, wxID_ANY, + create_scaled_bitmap("obj_warning", m_limit_warning_panel, 16), + wxDefaultPosition, wxSize(FromDIP(16), FromDIP(16))); + m_limit_warning_text = new wxStaticText(m_limit_warning_panel, wxID_ANY, wxEmptyString); + m_limit_warning_text->SetFont(Label::Body_13); + m_limit_warning_text->SetForegroundColour(StateColor::darkModeColorFor(wxColour("#D32F2F"))); + m_limit_warning_text->Wrap(FromDIP(400)); + warning_sizer->Add(warn_bmp, 0, wxALIGN_TOP | wxRIGHT, FromDIP(6)); + warning_sizer->Add(m_limit_warning_text, 1, wxEXPAND); + m_limit_warning_panel->SetSizer(warning_sizer); + m_limit_warning_panel->Hide(); + sizer->Add(m_limit_warning_panel, 0, wxEXPAND | wxTOP, FromDIP(8)); + + return sizer; +} + +wxBoxSizer* ColorDecomposeDialog::create_button_panel() +{ + auto* sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->AddStretchSpacer(); + + m_btn_cancel = new Button(this, _L("Cancel")); + m_btn_cancel->SetBackgroundColor(StateColor::darkModeColorFor(*wxWHITE)); + m_btn_cancel->SetBorderColor(StateColor::darkModeColorFor(wxColour("#CECECE"))); + m_btn_cancel->SetTextColor(StateColor::darkModeColorFor(wxColour("#262E30"))); + m_btn_cancel->SetMinSize(wxSize(FromDIP(55), FromDIP(24))); + m_btn_cancel->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { EndModal(wxID_CANCEL); }); + + m_btn_ok = new Button(this, _L("OK")); + m_btn_ok->SetBackgroundColor(StateColor( + std::make_pair(wxColour("#C2C2C2"), (int) StateColor::Disabled), + std::make_pair(wxColour("#00AE42"), (int) StateColor::Normal))); + m_btn_ok->SetBorderColor(StateColor( + std::make_pair(wxColour("#C2C2C2"), (int) StateColor::Disabled), + std::make_pair(wxColour("#00AE42"), (int) StateColor::Normal))); + m_btn_ok->SetTextColor(StateColor( + std::make_pair(*wxWHITE, (int) StateColor::Disabled), + std::make_pair(*wxWHITE, (int) StateColor::Normal))); + m_btn_ok->SetMinSize(wxSize(FromDIP(55), FromDIP(24))); + m_btn_ok->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { + EndModal(wxID_OK); + }); + + sizer->Add(m_btn_cancel, 0, wxRIGHT, FromDIP(12)); + sizer->Add(m_btn_ok, 0); + + return sizer; +} + +void ColorDecomposeDialog::select_mode(DecomposeMode mode) +{ + m_selected_mode = mode; + m_result = m_mode_results[mode_index(mode)]; + update_card_styles(); + update_matched_color_display(); + update_ok_button_state(); +} + +void ColorDecomposeDialog::update_card_styles() +{ + if (m_card_material_list) m_card_material_list->Refresh(); + if (m_card_cmyw) m_card_cmyw->Refresh(); + if (m_card_rybw) m_card_rybw->Refresh(); + + if (m_chk_material_list) + m_chk_material_list->SetValue(m_selected_mode == DecomposeMode::MaterialList); + if (m_chk_cmyw) + m_chk_cmyw->SetValue(m_selected_mode == DecomposeMode::CMYW); + if (m_chk_rybw) + m_chk_rybw->SetValue(m_selected_mode == DecomposeMode::RYBW); +} + +void ColorDecomposeDialog::update_card_visibility() +{ + // Count physical filaments of the same type (excluding the source filament) + int same_type_count = 0; + for (size_t i = 0; i < m_filament_types.size(); ++i) { + if (static_cast(i) == m_filament_idx) + continue; + if (material_type_matches(m_filament_types[i], m_preferred_type)) + ++same_type_count; + } + + bool show_arb = (same_type_count >= 2); + bool show_cmyw = (m_preferred_type == kDecomposePlaBasicType); + bool show_rybw = (m_preferred_type == kDecomposePlaBasicType); + + if (m_arb_column_panel) m_arb_column_panel->Show(show_arb); + if (m_card_material_list) m_card_material_list->Show(show_arb); + if (m_card_cmyw) m_card_cmyw->Show(show_cmyw); + if (m_card_rybw) m_card_rybw->Show(show_rybw); + + bool any_visible = show_arb || show_cmyw || show_rybw; + if (m_no_card_hint) + m_no_card_hint->Show(!any_visible); + + // Auto-select a visible mode when current selection becomes hidden + if (any_visible) { + bool cur_visible = false; + if (m_selected_mode == DecomposeMode::MaterialList && show_arb) cur_visible = true; + if (m_selected_mode == DecomposeMode::CMYW && show_cmyw) cur_visible = true; + if (m_selected_mode == DecomposeMode::RYBW && show_rybw) cur_visible = true; + if (!cur_visible) { + if (show_arb) select_mode(DecomposeMode::MaterialList); + else if (show_cmyw) select_mode(DecomposeMode::CMYW); + else select_mode(DecomposeMode::RYBW); + } + } + + Layout(); + update_ok_button_state(); +} + +void ColorDecomposeDialog::update_filament_limit_warning() +{ + if (!m_limit_warning_panel || !m_limit_warning_text) + return; + + size_t missing_new = 0; + if (m_missing_calculator) { + missing_new = m_missing_calculator(m_result); + } else { + const size_t source_physical_idx = m_filament_idx >= 0 ? static_cast(m_filament_idx) : size_t(-1); + const std::vector* indices = + m_physical_config_indices.empty() ? nullptr : &m_physical_config_indices; + missing_new = count_decompose_new_physical_filaments( + m_result, m_physical_colors, m_filament_types, source_physical_idx, indices); + } + // A result with fewer than 2 components (e.g. target color is already a + // standard base color shown as "100%") creates no mixed filament and no new + // physical filament, so it can never exceed the limit. + const bool creates_mixed = m_result.components.size() >= 2; + // +1 for the mixed filament slot that will be created after decomposition. + const size_t needed = m_current_filament_count + missing_new + 1; + const bool blocked = creates_mixed && needed > m_max_filament_count; + + const bool was_shown = m_limit_warning_panel->IsShown(); + + if (!blocked) { + if (was_shown) { + m_limit_warning_panel->Hide(); + Layout(); + Fit(); + CenterOnParent(); + } + return; + } + + wxString mode_name; + switch (m_selected_mode) { + case DecomposeMode::CMYW: mode_name = "CMYW"; break; + case DecomposeMode::RYBW: mode_name = "RYBW"; break; + case DecomposeMode::MaterialList: mode_name = _L("Material List"); break; + } + + const wxString warning_text = format_wxstr( + _L("The material list supports at most %1% colors. After %2% decomposition, the material count would exceed %1%. Please delete unused filaments on the main screen before decomposing."), + m_max_filament_count, mode_name); + + // Show first so the panel is laid out and the text control gets its real + // width, then wrap to that width so the paragraph fills the content area. + m_limit_warning_panel->Show(); + Layout(); + const int avail = m_limit_warning_text->GetClientSize().x; + m_limit_warning_text->SetLabel(warning_text); + if (avail > FromDIP(50)) + m_limit_warning_text->Wrap(avail); + + Layout(); + // Only resize/recenter when the warning panel actually toggled from hidden + // to shown. While already visible, switching modes must not re-Fit/recenter + // the dialog, which would make it jump on every card switch. + if (!was_shown) { + Fit(); + CenterOnParent(); + } +} + +void ColorDecomposeDialog::set_missing_physical_calculator(std::function fn) +{ + m_missing_calculator = std::move(fn); + update_ok_button_state(); +} + +void ColorDecomposeDialog::update_ok_button_state() +{ + if (!m_btn_ok) return; + update_filament_limit_warning(); + bool any_card_visible = (m_card_material_list && m_card_material_list->IsShown()) + || (m_card_cmyw && m_card_cmyw->IsShown()) + || (m_card_rybw && m_card_rybw->IsShown()); + const bool blocked = m_limit_warning_panel && m_limit_warning_panel->IsShown(); + m_btn_ok->Enable(any_card_visible && !blocked); + Layout(); +} + +void ColorDecomposeDialog::update_mode_card_content(DecomposeMode mode) +{ + auto& controls = m_mode_cards[mode_index(mode)]; + auto* sizer = controls.components_sizer; + auto* card = controls.card; + if (!sizer || !card) + return; + + sizer->Clear(true); + const auto& components = m_mode_results[mode_index(mode)].components; + const size_t count = components.size(); + if (count == 0) { + card->Layout(); + card->Refresh(); + return; + } + + const int swatch_sz = FromDIP(24); + const int plus_gap = FromDIP(24); + const wxFont& ratio_font = Label::Body_13; + auto bind_select = [this, mode](wxWindow* w) { + w->Bind(wxEVT_LEFT_UP, [this, mode](wxMouseEvent&) { + select_mode(mode); + }); + w->SetCursor(wxCursor(wxCURSOR_HAND)); + }; + + for (size_t i = 0; i < count; ++i) { + auto* col = new wxBoxSizer(wxVERTICAL); + auto* swatch = create_color_swatch(card, components[i].colour, swatch_sz); + bind_select(swatch); + col->Add(swatch, 0, wxALIGN_CENTER_HORIZONTAL); + auto* ratio_text = new wxStaticText(card, wxID_ANY, wxString::Format("%d%%", components[i].ratio)); + ratio_text->SetFont(ratio_font); + ratio_text->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + match_parent_bg(ratio_text, StateColor::darkModeColorFor(COLOR_BG_CARD)); + bind_select(ratio_text); + col->Add(ratio_text, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, FromDIP(4)); + sizer->Add(col, 0, wxALIGN_TOP); + + if (i + 1 < count) { + sizer->AddStretchSpacer(); + auto* plus_panel = new wxPanel(card, wxID_ANY, wxDefaultPosition, wxSize(plus_gap, swatch_sz)); + plus_panel->SetMinSize(wxSize(plus_gap, swatch_sz)); + plus_panel->SetMaxSize(wxSize(plus_gap, swatch_sz)); + plus_panel->SetBackgroundColour(StateColor::darkModeColorFor(COLOR_BG_CARD)); + auto* plus_sizer = new wxBoxSizer(wxVERTICAL); + auto* plus_label = new wxStaticText(plus_panel, wxID_ANY, "+"); + plus_label->SetFont(Label::Body_13); + plus_label->SetForegroundColour(StateColor::darkModeColorFor(COLOR_TEXT_DARK)); + match_parent_bg(plus_label, StateColor::darkModeColorFor(COLOR_BG_CARD)); + bind_select(plus_panel); + bind_select(plus_label); + plus_sizer->AddStretchSpacer(); + plus_sizer->Add(plus_label, 0, wxALIGN_CENTER_HORIZONTAL); + plus_sizer->AddStretchSpacer(); + plus_panel->SetSizer(plus_sizer); + sizer->Add(plus_panel, 0, wxALIGN_TOP); + sizer->AddStretchSpacer(); + } + } + + const int card_width = FromDIP(128 + (count > 2 ? static_cast(count - 2) * 31 : 0)); + card->SetMinSize(wxSize(card_width, FromDIP(111))); + card->SetMaxSize(wxSize(card_width, FromDIP(111))); + + card->Layout(); + card->Refresh(); +} + +void ColorDecomposeDialog::update_mode_card_contents() +{ + update_mode_card_content(DecomposeMode::MaterialList); + update_mode_card_content(DecomposeMode::CMYW); + update_mode_card_content(DecomposeMode::RYBW); + Layout(); + Fit(); +} + +void ColorDecomposeDialog::update_matched_color_display() +{ + if (!m_result.matched_color.IsOk()) + m_result.matched_color = m_target_color; + + if (m_matched_swatch) { + m_matched_swatch->SetBackgroundColour(m_result.matched_color); + m_matched_swatch->Refresh(); + } + if (m_matched_rgb_text) { + m_matched_rgb_text->SetLabel(wxString::Format("RGB: %d, %d, %d", + m_result.matched_color.Red(), m_result.matched_color.Green(), m_result.matched_color.Blue())); + } +} + +bool ColorDecomposeDialog::try_build_single_base_result(DecomposeMode mode, ColorDecomposeResult& out) const +{ + // Gate by preferred type, matching card visibility: CMYW and RYBW only for PLA Basic. + if (mode == DecomposeMode::CMYW || mode == DecomposeMode::RYBW) { + if (m_preferred_type != kDecomposePlaBasicType) + return false; + } else { + return false; + } + + static const DecomposeBaseColor cmyw_bases[] = { + DecomposeBaseColor::Cyan, DecomposeBaseColor::Magenta, + DecomposeBaseColor::Yellow, DecomposeBaseColor::White + }; + static const DecomposeBaseColor rybw_bases[] = { + DecomposeBaseColor::Red, DecomposeBaseColor::Yellow, + DecomposeBaseColor::Blue, DecomposeBaseColor::White + }; + const DecomposeBaseColor* bases = (mode == DecomposeMode::CMYW) ? cmyw_bases : rybw_bases; + const size_t base_count = (mode == DecomposeMode::CMYW) + ? sizeof(cmyw_bases) / sizeof(cmyw_bases[0]) + : sizeof(rybw_bases) / sizeof(rybw_bases[0]); + + const std::string target_hex = decompose_normalize_color_hex( + m_target_color.GetAsString(wxC2S_HTML_SYNTAX).ToStdString()); + + for (size_t i = 0; i < base_count; ++i) { + const DecomposeBaseColor base = bases[i]; + DecomposeOfficialComponent official = + lookup_decompose_official_component(m_preferred_type, base, pure_color_for_base(base)); + if (decompose_normalize_color_hex(official.color_hex) != target_hex) + continue; + + out = ColorDecomposeResult{}; + out.mode = mode; + out.matched_color = hex_to_wx_colour(official.color_hex, m_target_color); + DecomposeComponent comp; + comp.colour = out.matched_color; + comp.ratio = 100; + comp.filament_index = -1; + comp.base_color = base; + out.components.push_back(comp); + return true; + } + return false; +} + +void ColorDecomposeDialog::compute_decomposition() +{ + auto fallback_result = [this](DecomposeMode mode, const std::vector& components) { + ColorDecomposeResult result; + result.mode = mode; + result.components = components; + int total = 0; + double r = 0.0, g = 0.0, b = 0.0; + for (const auto& comp : result.components) + total += comp.ratio; + if (total <= 0) + total = 100; + for (const auto& comp : result.components) { + const double w = static_cast(comp.ratio) / total; + r += comp.colour.Red() * w; + g += comp.colour.Green() * w; + b += comp.colour.Blue() * w; + } + result.matched_color = result.components.empty() + ? m_target_color + : wxColour(static_cast(std::clamp(r, 0.0, 255.0)), + static_cast(std::clamp(g, 0.0, 255.0)), + static_cast(std::clamp(b, 0.0, 255.0))); + return result; + }; + + std::vector physical_filaments; + physical_filaments.reserve(m_physical_colors.size()); + for (size_t i = 0; i < m_physical_colors.size(); ++i) { + if (m_filament_idx >= 0 && i == static_cast(m_filament_idx)) + continue; + ColorDecomposePhysicalFilament filament; + filament.color_hex = m_physical_colors[i]; + filament.name = i < m_filament_names.size() ? m_filament_names[i] : ""; + filament.type = i < m_filament_types.size() ? m_filament_types[i] : ""; + filament.filament_index = static_cast(i + 1); + physical_filaments.push_back(std::move(filament)); + } + + const ColorDecomposeRgb target_rgb = wx_colour_to_recipe_rgb(m_target_color); + + auto material_recipe = recommend_from_physical_filaments(target_rgb, physical_filaments, m_preferred_type); + if (material_recipe.valid) { + m_mode_results[mode_index(DecomposeMode::MaterialList)] = + to_dialog_result(material_recipe, m_target_color); + } else { + std::vector components; + for (size_t i = 0; i < std::min(2, physical_filaments.size()); ++i) { + DecomposeComponent comp; + comp.colour = wxColour(physical_filaments[i].color_hex); + comp.ratio = 50; + comp.filament_index = static_cast(physical_filaments[i].filament_index); + components.push_back(comp); + } + if (components.empty()) { + components.push_back({m_target_color, 100, -1}); + } else if (components.size() == 1) { + components.front().ratio = 100; + } + m_mode_results[mode_index(DecomposeMode::MaterialList)] = + fallback_result(DecomposeMode::MaterialList, components); + } + + ColorDecomposeResult single_base; + if (try_build_single_base_result(DecomposeMode::CMYW, single_base)) { + m_mode_results[mode_index(DecomposeMode::CMYW)] = single_base; + } else { + auto cmyw_recipe = lookup_standard_recipe(target_rgb, ColorDecomposeRecipeMode::CMYW, m_preferred_type); + m_mode_results[mode_index(DecomposeMode::CMYW)] = cmyw_recipe.valid + ? to_dialog_result(cmyw_recipe, m_target_color) + : fallback_result(DecomposeMode::CMYW, { + {CMYW_YELLOW, 50, -1, DecomposeBaseColor::Yellow}, + {CMYW_CYAN, 50, -1, DecomposeBaseColor::Cyan} + }); + } + + if (try_build_single_base_result(DecomposeMode::RYBW, single_base)) { + m_mode_results[mode_index(DecomposeMode::RYBW)] = single_base; + } else { + auto rybw_recipe = lookup_standard_recipe(target_rgb, ColorDecomposeRecipeMode::RYBW, m_preferred_type); + m_mode_results[mode_index(DecomposeMode::RYBW)] = rybw_recipe.valid + ? to_dialog_result(rybw_recipe, m_target_color) + : fallback_result(DecomposeMode::RYBW, { + {RYBW_YELLOW, 50, -1, DecomposeBaseColor::Yellow}, + {RYBW_BLUE, 50, -1, DecomposeBaseColor::Blue} + }); + } + + m_result = m_mode_results[mode_index(m_selected_mode)]; + update_mode_card_contents(); + update_ok_button_state(); +} + +} // namespace GUI +} // namespace Slic3r diff --git a/src/slic3r/GUI/ColorDecomposeDialog.hpp b/src/slic3r/GUI/ColorDecomposeDialog.hpp new file mode 100644 index 0000000000..419dfe8cea --- /dev/null +++ b/src/slic3r/GUI/ColorDecomposeDialog.hpp @@ -0,0 +1,152 @@ +#ifndef slic3r_ColorDecomposeDialog_hpp_ +#define slic3r_ColorDecomposeDialog_hpp_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "GUI_Utils.hpp" +#include "libslic3r/ColorDecomposeRecipe.hpp" + +class Button; +class CheckBox; +class ComboBox; + +namespace Slic3r { +namespace GUI { + +using DecomposeMode = ColorDecomposeRecipeMode; + +enum class DecomposeBaseColor { + None, + Cyan, + Magenta, + Yellow, + White, + Red, + Green, + Blue +}; + +struct DecomposeComponent { + wxColour colour; + int ratio{50}; // percentage + int filament_index{-1}; // 1-based physical filament index, -1 if standard base color + DecomposeBaseColor base_color{DecomposeBaseColor::None}; +}; + +struct ColorDecomposeResult { + DecomposeMode mode{DecomposeMode::MaterialList}; + wxColour matched_color; + std::vector components; +}; + +class ColorDecomposeDialog : public DPIDialog +{ +public: + ColorDecomposeDialog(wxWindow* parent, + int filament_idx, + const wxColour& target_color, + const std::vector& physical_colors, + const std::vector& filament_names, + const std::vector& filament_types, + size_t current_filament_count = 0, + size_t max_filament_count = 32, + std::vector physical_config_indices = {}); + + ColorDecomposeResult get_result() const { return m_result; } + + // Override the "new physical filaments" count used by the filament-limit + // warning. The Texture import path supplies its own calculator so the + // pre-check shares the exact reuse rule as its write-back (existing + + // virtual physical filaments), instead of the project-config based default + // that cannot see not-yet-committed virtual base colors. + void set_missing_physical_calculator(std::function fn); + +protected: + void on_dpi_changed(const wxRect& suggested_rect) override; + +private: + void build_ui(); + wxBoxSizer* create_filament_selector(); + wxBoxSizer* create_target_color_section(); + wxBoxSizer* create_mode_selection_section(); + wxPanel* create_mode_card(wxWindow* parent, DecomposeMode mode, const wxString& title); + wxBoxSizer* create_button_panel(); + + void select_mode(DecomposeMode mode); + void update_card_styles(); + void update_card_visibility(); + void update_mode_card_content(DecomposeMode mode); + void update_mode_card_contents(); + void update_matched_color_display(); + void update_ok_button_state(); + void update_filament_limit_warning(); + + void compute_decomposition(); + + // When the target color is exactly one of the standard base colors for the + // preferred type, the standard card should show that base at 100% instead of + // a mix. PLA Basic covers CMYW and RYBW. + bool try_build_single_base_result(DecomposeMode mode, ColorDecomposeResult& out) const; + + struct ModeCardControls { + wxPanel* card{nullptr}; + wxBoxSizer* components_sizer{nullptr}; + }; + + ColorDecomposeResult m_result; + std::array m_mode_results; + std::array m_mode_cards; + int m_filament_idx{-1}; + wxColour m_target_color; + std::vector m_physical_colors; + std::vector m_filament_names; + std::vector m_filament_types; + std::vector m_project_types; + std::string m_preferred_type; + // Dropdown selectable item index -> material type string + std::vector m_combo_item_types; + size_t m_current_filament_count{0}; + size_t m_max_filament_count{32}; + std::vector m_physical_config_indices; + std::function m_missing_calculator; + + // UI controls + ComboBox* m_type_combo{nullptr}; + wxPanel* m_target_swatch{nullptr}; + wxStaticText* m_target_rgb_text{nullptr}; + wxPanel* m_matched_swatch{nullptr}; + wxStaticText* m_matched_rgb_text{nullptr}; + + // Mode cards + wxPanel* m_card_material_list{nullptr}; + wxPanel* m_card_cmyw{nullptr}; + wxPanel* m_card_rybw{nullptr}; + wxPanel* m_arb_column_panel{nullptr}; + CheckBox* m_chk_material_list{nullptr}; + CheckBox* m_chk_cmyw{nullptr}; + CheckBox* m_chk_rybw{nullptr}; + DecomposeMode m_selected_mode{DecomposeMode::MaterialList}; + + // Hint shown when no mode card is visible + wxStaticText* m_no_card_hint{nullptr}; + + // Warning shown when decomposition would exceed filament limit + wxPanel* m_limit_warning_panel{nullptr}; + wxStaticText* m_limit_warning_text{nullptr}; + + Button* m_btn_ok{nullptr}; + Button* m_btn_cancel{nullptr}; +}; + +} // namespace GUI +} // namespace Slic3r + +#endif // slic3r_ColorDecomposeDialog_hpp_ diff --git a/src/slic3r/GUI/ColorDecomposeSupport.cpp b/src/slic3r/GUI/ColorDecomposeSupport.cpp new file mode 100644 index 0000000000..e8fb4c9082 --- /dev/null +++ b/src/slic3r/GUI/ColorDecomposeSupport.cpp @@ -0,0 +1,386 @@ +#include "ColorDecomposeSupport.hpp" +#include "MixedFilamentDialog.hpp" +#include "GUI_App.hpp" +#include "MsgDialog.hpp" +#include "I18N.hpp" +#include "libslic3r/Preset.hpp" +#include "libslic3r/PresetBundle.hpp" +#include "libslic3r/Utils.hpp" + +#include "nlohmann/json.hpp" + +#include +#include +#include + +using json = nlohmann::json; + +namespace Slic3r { namespace GUI { + +std::string decompose_normalize_color_hex(std::string color) +{ + if (color.size() >= 7) + color = color.substr(0, 7); + std::transform(color.begin(), color.end(), color.begin(), [](unsigned char c) { + return static_cast(std::toupper(c)); + }); + return color; +} + +const char* decompose_base_color_en(DecomposeBaseColor color) +{ + switch (color) { + case DecomposeBaseColor::Cyan: return "Cyan"; + case DecomposeBaseColor::Magenta: return "Magenta"; + case DecomposeBaseColor::Yellow: return "Yellow"; + case DecomposeBaseColor::White: return "White"; + case DecomposeBaseColor::Red: return "Red"; + case DecomposeBaseColor::Green: return "Green"; + case DecomposeBaseColor::Blue: return "Blue"; + default: return ""; + } +} + +wxString decompose_base_color_display(DecomposeBaseColor color) +{ + switch (color) { + case DecomposeBaseColor::Cyan: return _L("Cyan"); + case DecomposeBaseColor::Magenta: return _L("Magenta"); + case DecomposeBaseColor::Yellow: return _L("Yellow"); + case DecomposeBaseColor::White: return _L("White"); + case DecomposeBaseColor::Red: return _L("Red"); + case DecomposeBaseColor::Green: return _L("Green"); + case DecomposeBaseColor::Blue: return _L("Blue"); + default: return wxString(); + } +} + +std::string decompose_basic_type_from_source(size_t source_config_idx, + size_t source_physical_idx, + const std::vector& physical_types) +{ + auto& project_config = wxGetApp().preset_bundle->project_config; + if (auto* filament_id_opt = project_config.option("filament_id")) { + if (source_config_idx < filament_id_opt->values.size()) { + const std::string& filament_id = filament_id_opt->values[source_config_idx]; + if (filament_id == kDecomposePetgFilamentId) + return kDecomposePetgBasicType; + if (filament_id == kDecomposePlaFilamentId) + return kDecomposePlaBasicType; + } + } + + if (source_physical_idx < physical_types.size()) { + const std::string& type = physical_types[source_physical_idx]; + if (type == kDecomposePetgShortType || type == kDecomposePetgBasicType) + return kDecomposePetgBasicType; + if (type == kDecomposePlaShortType || type == kDecomposePlaBasicType) + return kDecomposePlaBasicType; + } + return kDecomposePlaBasicType; +} + +std::string decompose_basic_filament_id(const std::string& basic_type) +{ + if (basic_type == kDecomposePetgBasicType) + return kDecomposePetgFilamentId; + return kDecomposePlaFilamentId; +} + +void set_created_standard_component_metadata(size_t config_idx, const DecomposeOfficialComponent& component) +{ + auto& project_config = wxGetApp().preset_bundle->project_config; + if (!component.filament_id.empty()) { + if (auto* filament_id_opt = project_config.option("filament_id")) { + while (filament_id_opt->values.size() <= config_idx) + filament_id_opt->values.push_back(""); + filament_id_opt->values[config_idx] = component.filament_id; + } + } + + const std::string type = component.filament_id == kDecomposePetgFilamentId ? kDecomposePetgShortType : + component.filament_id == kDecomposePlaFilamentId ? kDecomposePlaShortType : ""; + if (!type.empty()) { + if (auto* type_opt = project_config.option("filament_type")) { + while (type_opt->values.size() <= config_idx) + type_opt->values.push_back(""); + type_opt->values[config_idx] = type; + } + } +} + +DecomposeOfficialComponent lookup_decompose_official_component( + const std::string& basic_type, + DecomposeBaseColor base_color, + const wxColour& fallback) +{ + DecomposeOfficialComponent result; + result.base_color = base_color; + result.color_hex = decompose_normalize_color_hex(fallback.GetAsString(wxC2S_HTML_SYNTAX).ToStdString()); + result.filament_id = decompose_basic_filament_id(basic_type); + + const char* color_name = decompose_base_color_en(base_color); + if (color_name[0] == '\0') + return result; + + // Some materials name a standard base color differently in the color-code + // table. PETG Basic's RYBW blue base is "Reflex Blue" (deep blue, B00, + // #001489), not "Blue". Match by an ordered list of exact English names so + // "Navy Blue" (B01, #0086D6) is never picked up by mistake. + std::vector candidate_names; + candidate_names.emplace_back(color_name); + if (base_color == DecomposeBaseColor::Blue && basic_type == kDecomposePetgBasicType) + candidate_names.emplace_back("Reflex Blue"); + + std::ifstream ifs(resources_dir() + "/profiles/BBL/filament/filaments_color_codes.json"); + if (!ifs) + return result; + + json root = json::parse(ifs, nullptr, false); + if (root.is_discarded() || !root.contains("data") || !root["data"].is_array()) + return result; + + for (const std::string& candidate : candidate_names) { + for (const auto& item : root["data"]) { + if (!item.is_object() || item.value("fila_type", "") != basic_type) + continue; + if (!item.contains("fila_color_name")) + continue; + const auto& names = item["fila_color_name"]; + if (!names.is_object() || names.value("en", "") != candidate) + continue; + if (item.contains("fila_color") && item["fila_color"].is_array() && !item["fila_color"].empty()) + result.color_hex = decompose_normalize_color_hex(item["fila_color"][0].get()); + result.filament_id = item.value("fila_id", result.filament_id); + return result; + } + } + return result; +} + +std::string find_decompose_standard_preset_name(size_t source_config_idx, const std::string& basic_type) +{ + const PresetBundle& preset_bundle = *wxGetApp().preset_bundle; + if (source_config_idx < preset_bundle.filament_presets.size()) { + const std::string& source_name = preset_bundle.filament_presets[source_config_idx]; + if (source_name.find(std::string(kDecomposeBambuPresetPrefix) + basic_type) != std::string::npos) + return source_name; + } + + const std::string prefix = std::string(kDecomposeBambuPresetPrefix) + basic_type + " @BBL "; + for (const std::string& preset_name : preset_bundle.filament_presets) { + if (preset_name.find(prefix) == 0) + return preset_name; + } + + return {}; +} + +std::string official_basic_type_from_preset_name(const std::string& preset_name) +{ + if (preset_name.find(std::string(kDecomposeBambuPresetPrefix) + kDecomposePlaBasicType) != std::string::npos) + return kDecomposePlaBasicType; + if (preset_name.find(std::string(kDecomposeBambuPresetPrefix) + kDecomposePetgBasicType) != std::string::npos) + return kDecomposePetgBasicType; + return {}; +} + +std::string filament_type_for_color_decompose(Preset* preset) +{ + if (!preset) + return kDecomposePlaShortType; + + std::string display_type; + std::string ft = preset->config.get_filament_type(display_type); + const std::string basic = official_basic_type_from_preset_name(preset->name); + if (!basic.empty()) + ft = basic; + if (ft.empty()) + ft = kDecomposePlaShortType; + return ft; +} + +int find_existing_decompose_component( + const DecomposeOfficialComponent& component, + const std::vector& physical_colors, + const std::vector& physical_config_indices, + size_t source_config_idx) +{ + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* filament_id_opt = project_config.option("filament_id"); + auto* type_opt = project_config.option("filament_type"); + const PresetBundle& preset_bundle = *wxGetApp().preset_bundle; + const size_t num_physical = physical_colors.size(); + const std::string expected_basic_type = component.filament_id == kDecomposePetgFilamentId ? kDecomposePetgBasicType : + component.filament_id == kDecomposePlaFilamentId ? kDecomposePlaBasicType : ""; + const std::string expected_short_type = expected_basic_type == kDecomposePetgBasicType ? kDecomposePetgShortType : + expected_basic_type == kDecomposePlaBasicType ? kDecomposePlaShortType : ""; + const std::string expected_preset_part = expected_basic_type.empty() ? "" : std::string(kDecomposeBambuPresetPrefix) + expected_basic_type; + for (size_t i = 0; i < num_physical && i < physical_config_indices.size(); ++i) { + const size_t config_idx = physical_config_indices[i]; + const std::string slot_color = decompose_normalize_color_hex(physical_colors[i]); + const std::string slot_filament_id = (filament_id_opt && config_idx < filament_id_opt->values.size()) ? filament_id_opt->values[config_idx] : ""; + const std::string slot_type = (type_opt && config_idx < type_opt->values.size()) ? type_opt->values[config_idx] : ""; + const std::string preset_name = config_idx < preset_bundle.filament_presets.size() ? preset_bundle.filament_presets[config_idx] : ""; + if (config_idx == source_config_idx) { + continue; + } + if (slot_color != component.color_hex) { + continue; + } + + if (!component.filament_id.empty() && slot_filament_id == component.filament_id) { + return static_cast(config_idx + 1); + } + + if (!expected_basic_type.empty() && (slot_type == expected_basic_type || slot_type == expected_short_type)) { + return static_cast(config_idx + 1); + } + + if (!expected_preset_part.empty() && preset_name.find(expected_preset_part) != std::string::npos) { + return static_cast(config_idx + 1); + } + + const bool has_material_hint = !slot_filament_id.empty() || !slot_type.empty() || !preset_name.empty(); + if (!expected_basic_type.empty() && has_material_hint) + continue; + + return static_cast(config_idx + 1); + } + return -1; +} + +bool prepare_decompose_mixed_result( + const ColorDecomposeResult& result, + size_t source_config_idx, + size_t source_physical_idx, + const std::vector& physical_colors, + const std::vector& physical_types, + const std::vector& physical_config_indices, + MixedFilamentResult& out_result, + std::vector& missing) +{ + out_result = {}; + missing.clear(); + if (result.components.size() < 2) { + return false; + } + + const bool standard_mode = result.mode == DecomposeMode::CMYW || result.mode == DecomposeMode::RYBW; + std::string basic_type; + std::string preset_name; + if (standard_mode) { + basic_type = decompose_basic_type_from_source(source_config_idx, source_physical_idx, physical_types); + preset_name = find_decompose_standard_preset_name(source_config_idx, basic_type); + } + + for (size_t i = 0; i < result.components.size(); ++i) { + const DecomposeComponent& comp = result.components[i]; + out_result.ratios.push_back(comp.ratio); + if (!standard_mode) { + if (comp.filament_index <= 0) { + return false; + } + const size_t physical_idx = static_cast(comp.filament_index - 1); + if (physical_idx >= physical_config_indices.size()) { + return false; + } + out_result.components.push_back(static_cast(physical_config_indices[physical_idx] + 1)); + continue; + } + + if (comp.base_color == DecomposeBaseColor::None) { + return false; + } + DecomposeOfficialComponent official_component = + lookup_decompose_official_component(basic_type, comp.base_color, comp.colour); + int existing_idx = find_existing_decompose_component(official_component, physical_colors, + physical_config_indices, source_config_idx); + if (existing_idx > 0) { + out_result.components.push_back(static_cast(existing_idx)); + continue; + } + + DecomposeMissingComponent missing_comp; + missing_comp.component_idx = out_result.components.size(); + missing_comp.official_component = official_component; + missing_comp.preset_name = preset_name; + missing_comp.display_name = decompose_base_color_display(comp.base_color) + + wxString::FromUTF8(" ") + wxString::FromUTF8(basic_type); + missing.push_back(std::move(missing_comp)); + out_result.components.push_back(0); + } + + const bool ok = out_result.components.size() == out_result.ratios.size() && out_result.components.size() >= 2; + return ok; +} + +size_t count_decompose_new_physical_filaments( + const ColorDecomposeResult& result, + const std::vector& physical_colors, + const std::vector& physical_types, + size_t source_physical_idx, + const std::vector* physical_config_indices) +{ + if (result.mode != DecomposeMode::CMYW && result.mode != DecomposeMode::RYBW) + return 0; + + std::vector fallback_indices; + const std::vector* indices = physical_config_indices; + if (!indices) { + fallback_indices.resize(physical_colors.size()); + for (size_t i = 0; i < fallback_indices.size(); ++i) + fallback_indices[i] = i; + indices = &fallback_indices; + } + + size_t source_config_idx = size_t(-1); + if (source_physical_idx < indices->size()) + source_config_idx = (*indices)[source_physical_idx]; + + const std::string basic_type = + decompose_basic_type_from_source(source_config_idx, source_physical_idx, physical_types); + + size_t missing_count = 0; + for (const DecomposeComponent& comp : result.components) { + if (comp.base_color == DecomposeBaseColor::None) + continue; + DecomposeOfficialComponent official_component = + lookup_decompose_official_component(basic_type, comp.base_color, comp.colour); + int existing_idx = find_existing_decompose_component(official_component, physical_colors, + *indices, source_config_idx); + if (existing_idx <= 0) + ++missing_count; + } + return missing_count; +} + +bool confirm_create_decompose_missing_components(wxWindow* parent, const std::vector& missing) +{ + if (missing.empty()) + return true; + + static const char* config_key = "not_show_color_decompose_missing_component_tip"; + if (wxGetApp().app_config->get(config_key) == "1") { + return true; + } + + wxString missing_text; + for (size_t i = 0; i < missing.size(); ++i) { + if (i > 0) + missing_text += _L(", "); + missing_text += missing[i].display_name; + } + + wxString message = _L("The current filament list does not contain ") + missing_text + + _L(". A project filament required by the mixed filament will be created automatically after decomposition."); + + MessageDialog dlg(parent, message, _L("Tip"), wxOK | wxCANCEL | wxICON_INFORMATION); + dlg.show_dsa_button(); + int res = dlg.ShowModal(); + if (res == wxID_OK && dlg.get_checkbox_state()) + wxGetApp().app_config->set(config_key, "1"); + return res == wxID_OK; +} + +}} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/ColorDecomposeSupport.hpp b/src/slic3r/GUI/ColorDecomposeSupport.hpp new file mode 100644 index 0000000000..a982c51303 --- /dev/null +++ b/src/slic3r/GUI/ColorDecomposeSupport.hpp @@ -0,0 +1,104 @@ +#ifndef slic3r_GUI_ColorDecomposeSupport_hpp_ +#define slic3r_GUI_ColorDecomposeSupport_hpp_ + +#include +#include +#include +#include +#include "ColorDecomposeDialog.hpp" + +class wxWindow; + +namespace Slic3r { +class Preset; +namespace GUI { + +// ---- Constants ---- + +inline constexpr const char* kDecomposePlaBasicType = "PLA Basic"; +inline constexpr const char* kDecomposePetgBasicType = "PETG Basic"; +inline constexpr const char* kDecomposePlaShortType = "PLA"; +inline constexpr const char* kDecomposePetgShortType = "PETG"; +inline constexpr const char* kDecomposePlaFilamentId = "GFA00"; +inline constexpr const char* kDecomposePetgFilamentId = "GFG00"; +inline constexpr const char* kDecomposeBambuPresetPrefix = "Bambu "; + +// ---- Types ---- + +struct DecomposeOfficialComponent { + DecomposeBaseColor base_color{DecomposeBaseColor::None}; + std::string color_hex; + std::string filament_id; +}; + +struct DecomposeMissingComponent { + size_t component_idx{0}; + DecomposeOfficialComponent official_component; + std::string preset_name; + wxString display_name; +}; + +struct MixedFilamentResult; + +// ---- Functions ---- + +std::string decompose_normalize_color_hex(std::string color); + +const char* decompose_base_color_en(DecomposeBaseColor color); + +wxString decompose_base_color_display(DecomposeBaseColor color); + +std::string decompose_basic_type_from_source(size_t source_config_idx, + size_t source_physical_idx, + const std::vector& physical_types); + +std::string decompose_basic_filament_id(const std::string& basic_type); + +void set_created_standard_component_metadata(size_t config_idx, const DecomposeOfficialComponent& component); + +DecomposeOfficialComponent lookup_decompose_official_component( + const std::string& basic_type, + DecomposeBaseColor base_color, + const wxColour& fallback); + +std::string find_decompose_standard_preset_name(size_t source_config_idx, const std::string& basic_type); + +// Returns "PLA Basic" / "PETG Basic" when preset_name names an official Bambu +// basic filament, else an empty string. +std::string official_basic_type_from_preset_name(const std::string& preset_name); + +// Resolve display type for color-decompose: official Bambu Basic overrides +// get_filament_type when preset name matches; empty/missing -> "PLA". +std::string filament_type_for_color_decompose(Preset* preset); + +int find_existing_decompose_component( + const DecomposeOfficialComponent& component, + const std::vector& physical_colors, + const std::vector& physical_config_indices, + size_t source_config_idx); + +bool prepare_decompose_mixed_result( + const ColorDecomposeResult& result, + size_t source_config_idx, + size_t source_physical_idx, + const std::vector& physical_colors, + const std::vector& physical_types, + const std::vector& physical_config_indices, + MixedFilamentResult& out_result, + std::vector& missing); + +// For standard modes: how many base colors are not reusable from physical list. +// MaterialList returns 0. When physical_config_indices is null, indices are 0..n-1. +size_t count_decompose_new_physical_filaments( + const ColorDecomposeResult& result, + const std::vector& physical_colors, + const std::vector& physical_types, + size_t source_physical_idx, + const std::vector* physical_config_indices); + +bool confirm_create_decompose_missing_components(wxWindow* parent, + const std::vector& missing); + +}} // namespace Slic3r::GUI + +#endif // slic3r_GUI_ColorDecomposeSupport_hpp_ diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index de94bb6b4b..519e75d9d2 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -577,17 +577,30 @@ void ConfigManipulation::update_print_fff_config(DynamicPrintConfig* config, con } // BBS - static const char* keys[] = { "support_filament", "support_interface_filament"}; + // A per-role filament override must name a real, physical filament. Out-of-range values are + // stale; a mixed-color slot is virtual and cannot be driven directly by a role override, so + // both are reset to 0 ("inherit the object's filament"). The object's own extruder assignment + // is what legitimately carries a mixed slot. Orca splits BBS's wall/solid_infill roles into + // six keys, so all of them are checked here. + static const char* keys[] = { "support_filament", "support_interface_filament", + "outer_wall_filament_id", "inner_wall_filament_id", + "sparse_infill_filament_id", "internal_solid_filament_id", + "top_surface_filament_id", "bottom_surface_filament_id" }; for (int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) { std::string key = std::string(keys[i]); auto* opt = dynamic_cast(config->option(key, false)); if (opt != nullptr) { - if (opt->getInt() > filament_cnt) { + int val = opt->getInt(); + bool out_of_range = val > filament_cnt; + bool is_mixed = (val > 0 && val <= filament_cnt && + wxGetApp().preset_bundle->is_mixed_filament(val - 1)); + if (out_of_range || is_mixed) { DynamicPrintConfig new_conf = *config; - const DynamicPrintConfig *conf_temp = wxGetApp().plater()->config(); int new_value = 0; - if (conf_temp != nullptr && conf_temp->has(key)) { - new_value = conf_temp->opt_int(key); + if (out_of_range) { + const DynamicPrintConfig *conf_temp = wxGetApp().plater()->config(); + if (conf_temp != nullptr && conf_temp->has(key)) + new_value = conf_temp->opt_int(key); } new_conf.set_key_value(key, new ConfigOptionInt(new_value)); apply(config, &new_conf); @@ -595,6 +608,37 @@ void ConfigManipulation::update_print_fff_config(DynamicPrintConfig* config, con } } + // Sub-layer splitting divides each layer by the mix ratio; an adaptive layer profile makes + // those sub-layer heights vary per layer, which degrades the blend. Warn once per enable. + { + static bool s_mixed_sublayer_warned = false; + bool sublayer_on = config->opt_bool("enable_mixed_color_sublayer"); + if (sublayer_on && !s_mixed_sublayer_warned && + wxGetApp().app_config->get("no_warn_mixed_sublayer_variable_layer") != "1") { + bool has_variable_layer = false; + for (const auto* obj : wxGetApp().model().objects) { + if (obj->layer_height_profile.get().size() > 4) { + has_variable_layer = true; + break; + } + } + if (has_variable_layer) { + MessageDialog dialog(m_msg_dlg_parent, + _L("Using variable layer height together with mixed color sublayer may result in poor color mixing quality."), + "", wxICON_WARNING | wxOK); + dialog.show_dsa_button(); + is_msg_dlg_already_exist = true; + dialog.ShowModal(); + is_msg_dlg_already_exist = false; + if (dialog.get_checkbox_state()) + wxGetApp().app_config->set("no_warn_mixed_sublayer_variable_layer", "1"); + s_mixed_sublayer_warned = true; + } + } + if (!sublayer_on) + s_mixed_sublayer_warned = false; + } + if (config->opt_enum("seam_slope_type") != SeamScarfType::None && config->get_abs_value("seam_slope_start_height") >= layer_height) { const wxString msg_text = _(L("seam_slope_start_height need to be smaller than layer_height.\nReset to 0.")); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index fa6902de8d..967e02a907 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -155,6 +155,11 @@ std::string& get_filament_mixture_warning_text(){ return filament_mixture_warning_text; } +std::string& get_single_extruder_mixed_filament_warning_text(){ + static std::string single_extruder_mixed_filament_warning_text; + return single_extruder_mixed_filament_warning_text; +} + static std::string format_number(float value) { @@ -2984,6 +2989,9 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re bool mix_pla_and_petg = cur_plate->check_mixture_of_pla_and_petg(full_config_temp); _set_warning_notification(EWarning::MixUsePLAAndPETG, !mix_pla_and_petg); + bool single_extruder_mixed_risk = cur_plate->check_single_extruder_mixed_filament_risk(full_config_temp, get_single_extruder_mixed_filament_warning_text()); + _set_warning_notification(EWarning::SingleExtruderMixedFilament, single_extruder_mixed_risk); + bool filament_nozzle_compatible = cur_plate->check_compatible_of_nozzle_and_filament(full_config_temp, wxGetApp().preset_bundle->filament_presets, get_nozzle_filament_incompatible_text()); _set_warning_notification(EWarning::NozzleFilamentIncompatible, !filament_nozzle_compatible); @@ -3010,6 +3018,7 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re _set_warning_notification(EWarning::TPUPrintableError, false); _set_warning_notification(EWarning::FilamentPrintableError, false); _set_warning_notification(EWarning::MixUsePLAAndPETG, false); + _set_warning_notification(EWarning::SingleExtruderMixedFilament, false); _set_warning_notification(EWarning::PrimeTowerOutside, false); _set_warning_notification(EWarning::MultiExtruderPrintableError,false); _set_warning_notification(EWarning::MultiExtruderHeightOutside,false); @@ -10570,6 +10579,9 @@ void GLCanvas3D::_set_warning_notification(EWarning warning, bool state) case EWarning::MixUsePLAAndPETG: text = _u8L("PLA and PETG filaments detected in the mixture. Adjust parameters according to the Wiki to ensure print quality."); break; + case EWarning::SingleExtruderMixedFilament: + text = get_single_extruder_mixed_filament_warning_text(); + break; case EWarning::PrimeTowerOutside: text = _u8L("The prime tower extends beyond the plate boundary."); break; diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 17497edf16..84dbd5d652 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -391,6 +391,7 @@ class GLCanvas3D PrimeTowerOutside, NozzleFilamentIncompatible, MixtureFilamentIncompatible, + SingleExtruderMixedFilament, FlushingVolumeZero }; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp index 761228550f..0ee0230ea9 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp @@ -731,6 +731,10 @@ void GLGizmoMmuSegmentation::init_model_triangle_selectors() continue; int extruder_idx = (mv->extruder_id() > 0) ? mv->extruder_id() - 1 : 0; + // A volume may be assigned to a mixed-color slot, whose index can sit past the + // physical colour list; fall back to the first colour rather than reading OOB. + if (extruder_idx >= (int)m_extruders_colors.size()) + extruder_idx = 0; std::vector ebt_colors; ebt_colors.push_back(m_extruders_colors[size_t(extruder_idx)]); ebt_colors.insert(ebt_colors.end(), m_extruders_colors.begin(), m_extruders_colors.end()); @@ -753,6 +757,9 @@ void GLGizmoMmuSegmentation::update_triangle_selectors_colors() TriangleSelectorPatch* selector = dynamic_cast(m_triangle_selectors[i].get()); int extruder_idx = m_volumes_extruder_idxs[i]; int extruder_color_idx = std::max(0, extruder_idx - 1); + // As above: a mixed-color slot can index past the physical colour list. + if (extruder_color_idx >= (int)m_extruders_colors.size()) + extruder_color_idx = 0; std::vector ebt_colors; ebt_colors.push_back(m_extruders_colors[extruder_color_idx]); ebt_colors.insert(ebt_colors.end(), m_extruders_colors.begin(), m_extruders_colors.end()); diff --git a/src/slic3r/GUI/GradientCurveEditor.cpp b/src/slic3r/GUI/GradientCurveEditor.cpp new file mode 100644 index 0000000000..b3d3436465 --- /dev/null +++ b/src/slic3r/GUI/GradientCurveEditor.cpp @@ -0,0 +1,644 @@ +#include "GradientCurveEditor.hpp" +#include "GUI_App.hpp" +#include "GuiColor.hpp" +#include "I18N.hpp" +#include "Widgets/StateColor.hpp" + +#include +#include +#include + +#include +#include +#include +#include + +namespace Slic3r { +namespace GUI { + +wxDEFINE_EVENT(wxEVT_GRADIENT_CURVE_CHANGED, wxCommandEvent); + +namespace { +// Layout (Figma "Property 1=Default", 214.06 x 179.63 px reference). +// Plot rect occupies the upper-left region; right + bottom margins host axis arrows / labels. +constexpr double kPlotLeftRatio = 0.0316; +constexpr double kPlotRightRatio = 0.6766; +constexpr double kPlotTopRatio = 0.1529; +constexpr double kPlotBottomRatio = 0.8474; +constexpr int kGridDivisions = 9; // 10 grid lines including the outer borders. + +// Hit / stroke (DIP). +constexpr int kHitRadius = 6; +constexpr int kCurveHitRadius = 5; +constexpr int kPointRadius = 4; // anchor outer radius (DIP) +constexpr int kStrokeUnselected = 2; +constexpr int kStrokeSelected = 4; +constexpr int kStrokeAxis = 2; // axis line width (px, no DPI scaling - matches kGridColor pen and 2DBed convention) +constexpr int kAxisArrowHalf = 5; // half-base of the axis arrow triangle (DIP) +constexpr int kAxisArrowLen = 10; // length of the axis arrow triangle (DIP) + +// Light-mode design tokens from Figma. Resolved through StateColor::darkModeColorFor() +// at paint time so the editor follows the app theme (#EEEEEE -> #4C4C55, #6B6B6B -> +// #818183, #262E30 -> #EFEFF0, *wxWHITE -> #2D2D31). Don't read these directly in paint; +// always go through the resolved locals declared at the top of on_paint(). +const wxColour kGridColor (238, 238, 238); // #EEEEEE grey 300 +const wxColour kAxisColor (107, 107, 107); // #6B6B6B grey 700 +const wxColour kLabelMuted (107, 107, 107); // #6B6B6B grey 700 +const wxColour kLabelStrong ( 38, 46, 48); // #262E30 grey 900 + +// LAB (DeltaE76) threshold for "curve color is too close to the background". Below this +// we paint a subtle axis-color outline so the curve doesn't visually vanish; above this +// we draw the curve plain. ~15 is "perceptible but still close", looser than the strict +// 5.0 used by FlushPredict::is_similar_color but loose enough that a pastel pink on white +// or a charcoal on #2B2B2B still triggers an outline. +constexpr float kBgSimilarThreshold = 15.0f; +constexpr int kOutlineExtraDip = 2; +} // namespace + +GradientCurveEditor::GradientCurveEditor(wxWindow* parent, + const wxColour& color_low, + const wxColour& color_high) + : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE) + , m_color_low(color_low) + , m_color_high(color_high) +{ + SetBackgroundStyle(wxBG_STYLE_PAINT); + SetBackgroundColour(wxGetApp().get_window_default_clr()); + // Wide enough so the X-axis "Material Ratio" label fits past the arrow tip without overlap. + // 260 (was 240): adds room for the "Material Ratio" label that gets shifted right by the + // longer axis arrow; the hosting MixedFilamentDialog grows to 470 DIP to accommodate. + SetMinSize(FromDIP(wxSize(260, 200))); + + reset_to_linear(0.10, 0.90); + + Bind(wxEVT_PAINT, &GradientCurveEditor::on_paint, this); + Bind(wxEVT_LEFT_DOWN, &GradientCurveEditor::on_left_down, this); + Bind(wxEVT_LEFT_UP, &GradientCurveEditor::on_left_up, this); + Bind(wxEVT_RIGHT_DOWN, &GradientCurveEditor::on_right_down, this); + Bind(wxEVT_MOTION, &GradientCurveEditor::on_motion, this); + Bind(wxEVT_LEAVE_WINDOW,&GradientCurveEditor::on_leave, this); + Bind(wxEVT_SIZE, &GradientCurveEditor::on_size, this); + Bind(wxEVT_MOUSE_CAPTURE_LOST, [this](wxMouseCaptureLostEvent&) { + m_drag_mode = DragMode::None; + m_drag_idx = -1; + m_dragged_moved = false; + }); +} + +void GradientCurveEditor::set_points(const PointList& pts) +{ + m_points = pts; + normalize_points(); + Refresh(); +} + +void GradientCurveEditor::set_colors(const wxColour& color_low, const wxColour& color_high) +{ + m_color_low = color_low; + m_color_high = color_high; + Refresh(); +} + +void GradientCurveEditor::set_selected_curve(int curve_idx) +{ + const int new_sel = (curve_idx == 0) ? 0 : 1; + if (m_selected_curve == new_sel) return; + m_selected_curve = new_sel; + Refresh(); +} + +void GradientCurveEditor::reset_to_linear(double y0, double y1) +{ + auto clamp_y = [](double v) { + return std::max(kGradientMinRatio, std::min(kGradientMaxRatio, v)); + }; + m_points.clear(); + GradientAnchor a0; a0.x = 0.0; a0.y = clamp_y(y0); + GradientAnchor a1; a1.x = 1.0; a1.y = clamp_y(y1); + m_points.push_back(a0); + m_points.push_back(a1); + m_selected_curve = 0; + Refresh(); + emit_changed(); +} + +void GradientCurveEditor::reverse() +{ + // Mirror y around 0.5. Tangents are slopes dy/dx so they flip sign to keep the + // local shape consistent across the mirror; NaN tangents remain "use PCHIP default". + for (auto& p : m_points) { + p.y = 1.0 - p.y; + if (std::isfinite(p.m_in)) p.m_in = -p.m_in; + if (std::isfinite(p.m_out)) p.m_out = -p.m_out; + } + Refresh(); + emit_changed(); +} + +void GradientCurveEditor::normalize_points() +{ + if (m_points.empty()) { + GradientAnchor a0; a0.x = 0.0; a0.y = kGradientMinRatio; + GradientAnchor a1; a1.x = 1.0; a1.y = kGradientMaxRatio; + m_points.push_back(a0); + m_points.push_back(a1); + return; + } + + for (auto& p : m_points) { + p.x = std::max(0.0, std::min(1.0, p.x)); + p.y = std::max(kGradientMinRatio, std::min(kGradientMaxRatio, p.y)); + } + std::sort(m_points.begin(), m_points.end(), + [](const GradientAnchor& a, const GradientAnchor& b) { + return a.x < b.x; + }); + + if (m_points.size() < 2) { + GradientAnchor tail; tail.x = 1.0; tail.y = m_points.front().y; + m_points.push_back(tail); + } + + m_points.front().x = 0.0; + m_points.back().x = 1.0; +} + +void GradientCurveEditor::emit_changed() +{ + wxCommandEvent evt(wxEVT_GRADIENT_CURVE_CHANGED, GetId()); + evt.SetEventObject(this); + ProcessWindowEvent(evt); +} + +wxRect GradientCurveEditor::plot_rect() const +{ + const wxSize sz = GetClientSize(); + const int x = static_cast(std::lround(sz.x * kPlotLeftRatio)); + const int y = static_cast(std::lround(sz.y * kPlotTopRatio)); + const int x2 = static_cast(std::lround(sz.x * kPlotRightRatio)); + const int y2 = static_cast(std::lround(sz.y * kPlotBottomRatio)); + // Force square 1:1 so X/Y axes share the same scale and grid cells stay square. Anchor at + // the top-left so the "100%" labels on the bottom/right still align with the plot edges. + const int side = std::max(1, std::min(x2 - x, y2 - y)); + return wxRect(x, y, side, side); +} + +wxPoint GradientCurveEditor::data_to_px(double x, double y) const +{ + const wxRect r = plot_rect(); + const int px = r.x + static_cast(std::lround(x * r.width)); + // y axis is inverted: y=1 should sit at the top. + const int py = r.y + static_cast(std::lround((1.0 - y) * r.height)); + return wxPoint(px, py); +} + +void GradientCurveEditor::px_to_data(int px, int py, double& x, double& y) const +{ + const wxRect r = plot_rect(); + const double w = std::max(1, r.width); + const double h = std::max(1, r.height); + x = std::max(0.0, std::min(1.0, (px - r.x) / w)); + y = std::max(0.0, std::min(1.0, 1.0 - (py - r.y) / h)); +} + +double GradientCurveEditor::sample_curve_y(double x) const +{ + GradientCurve gc; + gc.points = m_points; + return sample_gradient_curve(gc, x); +} + +int GradientCurveEditor::hit_test(int px, int py) const +{ + const int tol = FromDIP(kHitRadius); + int best_idx = -1; + int best_d2 = tol * tol; + for (size_t i = 0; i < m_points.size(); ++i) { + // Anchor visual y is curve-specific: component 1's anchor sits at (x, 1 - stored_y). + const double vy = to_visual_y(m_selected_curve, m_points[i].y); + const wxPoint p = data_to_px(m_points[i].x, vy); + const int dx = px - p.x; + const int dy = py - p.y; + const int d2 = dx * dx + dy * dy; + if (d2 <= best_d2) { + best_idx = static_cast(i); + best_d2 = d2; + } + } + return best_idx; +} + +int GradientCurveEditor::hit_test_curve(int px, int py, int* seg_out) const +{ + if (seg_out) *seg_out = -1; + if (m_points.size() < 2) return -1; + const int tol = FromDIP(kCurveHitRadius); + const int tol2 = tol * tol; + + auto dist2_to_seg = [&](int ax, int ay, int bx, int by) -> int { + const double dx = bx - ax; + const double dy = by - ay; + const double l2 = dx * dx + dy * dy; + if (l2 == 0.0) { + const double ddx = px - ax; + const double ddy = py - ay; + return static_cast(ddx * ddx + ddy * ddy); + } + double t = ((px - ax) * dx + (py - ay) * dy) / l2; + t = std::max(0.0, std::min(1.0, t)); + const double ex = ax + t * dx; + const double ey = ay + t * dy; + const double ddx = px - ex; + const double ddy = py - ey; + return static_cast(ddx * ddx + ddy * ddy); + }; + + // Hit-test against the same dense Hermite polyline that on_paint draws, so the + // clickable line follows the visual curve exactly (no offset on the bent parts). + // When a hit is found, also report the index of the left anchor of the data-space + // segment that covers cursor x; needed by the segment-bend interaction. + const wxRect rc = plot_rect(); + const int samples = std::max(128, rc.width * 2); + auto seg_for_x = [&](double cursor_x) -> int { + for (size_t i = 1; i < m_points.size(); ++i) { + if (cursor_x <= m_points[i].x) + return static_cast(i - 1); + } + return static_cast(m_points.size() - 2); + }; + + auto curve_hit = [&](int curve_idx) -> bool { + wxPoint prev; + for (int s = 0; s <= samples; ++s) { + const double x = double(s) / samples; + const double y0 = sample_curve_y(x); + const double vy = to_visual_y(curve_idx, y0); + const wxPoint cur = data_to_px(x, vy); + if (s > 0 && dist2_to_seg(prev.x, prev.y, cur.x, cur.y) <= tol2) + return true; + prev = cur; + } + return false; + }; + + // Prefer the selected curve so overlapping segments don't unintentionally steal focus. + if (curve_hit(m_selected_curve)) { + if (seg_out) { + double nx = 0, dummy = 0; + px_to_data(px, py, nx, dummy); + *seg_out = seg_for_x(nx); + } + return m_selected_curve; + } + const int other = 1 - m_selected_curve; + if (curve_hit(other)) { + if (seg_out) { + double nx = 0, dummy = 0; + px_to_data(px, py, nx, dummy); + *seg_out = seg_for_x(nx); + } + return other; + } + return -1; +} + +void GradientCurveEditor::on_paint(wxPaintEvent& /*evt*/) +{ + // Resolve theme colors every paint so dark-mode toggles (no re-construction) take + // effect without an explicit listener. Window bg is read from GUI_App, not + // GetBackgroundColour(), since the latter is snapshotted at construction time. + const wxColour bg = wxGetApp().get_window_default_clr(); + const wxColour grid_color = StateColor::darkModeColorFor(kGridColor); + const wxColour axis_color = StateColor::darkModeColorFor(kAxisColor); + const wxColour label_muted = StateColor::darkModeColorFor(kLabelMuted); + const wxColour label_strong = StateColor::darkModeColorFor(kLabelStrong); + const wxColour point_fill = StateColor::darkModeColorFor(*wxWHITE); + + wxAutoBufferedPaintDC raw_dc(this); + raw_dc.SetBackground(wxBrush(bg)); + raw_dc.Clear(); + + // Render through wxGCDC so curves, arrows and anchor circles get anti-aliased; the buffered + // DC is the actual back buffer that gets blitted to the window. + wxGCDC dc(raw_dc); + + const wxRect rc = plot_rect(); + if (rc.width <= 0 || rc.height <= 0) + return; + + // 10x10 light grid (10 lines including outer borders, 9 equal divisions). + dc.SetPen(wxPen(grid_color, 1)); + for (int i = 0; i <= kGridDivisions; ++i) { + const int x = rc.x + rc.width * i / kGridDivisions; + const int y = rc.y + rc.height * i / kGridDivisions; + dc.DrawLine(x, rc.y, x, rc.y + rc.height); + dc.DrawLine(rc.x, y, rc.x + rc.width, y); + } + + // Set the label font first so text width measurements drive arrow / label placement. + wxFont label_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); + label_font.SetPointSize(std::max(7, label_font.GetPointSize() - 1)); + dc.SetFont(label_font); + + const wxString axis_y_title = _L("Material Ratio"); + const wxString axis_x_title = _L("Model Height"); + const wxString pct_text = wxT("100%"); + const wxSize x_title_sz = dc.GetTextExtent(axis_x_title); + const wxSize y_title_sz = dc.GetTextExtent(axis_y_title); + + wxFont strong_font = label_font; + strong_font.SetWeight(wxFONTWEIGHT_SEMIBOLD); + dc.SetFont(strong_font); + const wxSize pct_text_sz = dc.GetTextExtent(pct_text); + dc.SetFont(label_font); + + // Axes (grey 700) with filled triangle arrows. Y-axis extends above the plot top to the + // canvas top edge; X-axis extends past the plot right toward the canvas right edge. + const int arrow_half = FromDIP(kAxisArrowHalf); + const int arrow_len = FromDIP(kAxisArrowLen); + const wxSize sz = GetClientSize(); + dc.SetPen(wxPen(axis_color, kStrokeAxis)); + dc.SetBrush(wxBrush(axis_color)); + + // Y-axis: vertical line at plot_left, from arrow tip near canvas top down to plot bottom. + const int y_axis_x = rc.x; + const int y_title_pct_gap = FromDIP(1); + const int y_title_bottom_pad = FromDIP(2); + const int y_title_y = std::max(0, rc.y - y_title_sz.y - y_title_pct_gap - pct_text_sz.y - y_title_bottom_pad); + const int y_arrow_tip_y = y_title_y; + const int y_arrow_ty = y_arrow_tip_y + arrow_len; + dc.DrawLine(y_axis_x, y_arrow_ty, y_axis_x, rc.y + rc.height); + { + wxPoint tri[3] = { + wxPoint(y_axis_x, y_arrow_tip_y), + wxPoint(y_axis_x - arrow_half, y_arrow_ty), + wxPoint(y_axis_x + arrow_half, y_arrow_ty), + }; + dc.DrawPolygon(3, tri); + } + + // X-axis arrow tip: stays just past the plot ideally, but is clamped so the trailing + // "Material Ratio" label still fits inside the canvas without overlapping the arrow. + const int x_axis_y = rc.y + rc.height; + const int x_label_gap = FromDIP(4); + const int x_edge_pad = FromDIP(6); + const int x_arrow_ideal = rc.x + rc.width + FromDIP(10); + const int x_arrow_max = sz.x - x_title_sz.x - x_label_gap - x_edge_pad - arrow_len; + const int x_arrow_tx = std::max(rc.x + rc.width + arrow_len, + std::min(x_arrow_ideal, x_arrow_max)); + const int x_arrow_tip_x = x_arrow_tx + arrow_len; + const int x_title_x = x_arrow_tip_x + x_label_gap; + dc.DrawLine(rc.x, x_axis_y, x_arrow_tx, x_axis_y); + { + wxPoint tri[3] = { + wxPoint(x_arrow_tip_x, x_axis_y), + wxPoint(x_arrow_tx, x_axis_y - arrow_half), + wxPoint(x_arrow_tx, x_axis_y + arrow_half), + }; + dc.DrawPolygon(3, tri); + } + + // Labels. + // "Model Height" and "100%" share the same left x; the gap is larger than the + // axis-arrow half-base so the text never visually touches the Y-axis arrow. + const int label_left_x = y_axis_x + FromDIP(10); + dc.SetTextForeground(label_muted); + dc.DrawText(axis_y_title, label_left_x, y_title_y); + + dc.SetFont(strong_font); + dc.SetTextForeground(label_strong); + dc.DrawText(pct_text, label_left_x, y_title_y + y_title_sz.y + y_title_pct_gap); + + // Bottom-right "100%" sits under the right end of the plot; "Material Ratio" follows the + // X-axis arrow tip (placement was already clamped above to leave room). + dc.DrawText(pct_text, rc.x + rc.width - pct_text_sz.x, x_axis_y); + dc.SetFont(label_font); + dc.SetTextForeground(label_muted); + dc.DrawText(axis_x_title, x_title_x, x_axis_y - x_title_sz.y / 2); + + if (m_points.size() < 2) + return; + + auto color_for_curve = [&](int curve_idx) -> wxColour { + wxColour c = (curve_idx == 0) ? m_color_low : m_color_high; + // Transparent filaments (alpha == 0, e.g. #FFFFFF00) would be invisible. + // Lift alpha so the curve stays visible while still hinting at transparency. + if (c.Alpha() == 0) + c.Set(c.Red(), c.Green(), c.Blue(), 150); + return c; + }; + + auto build_polyline = [&](int curve_idx) -> std::vector { + const int samples = std::max(128, rc.width * 2); + std::vector poly; + poly.reserve(samples + 1); + for (int s = 0; s <= samples; ++s) { + const double x = double(s) / samples; + const double y0 = sample_curve_y(x); + const double vy = to_visual_y(curve_idx, y0); + poly.push_back(data_to_px(x, vy)); + } + return poly; + }; + + auto draw_polyline = [&](const std::vector& poly, const wxColour& col, int stroke_dip) { + dc.SetPen(wxPen(col, FromDIP(stroke_dip))); + dc.DrawLines(static_cast(poly.size()), poly.data()); + }; + + // Outline only when the curve color is perceptually close to the background; otherwise + // the plain filament color reads fine and the extra stroke would look heavy. + // Outline tone is intentionally softer than axis_color so it disambiguates the curve + // from the bg without competing with the structural axis/grid: light mode uses a pale + // grey, dark mode uses a slightly-above-bg grey (gDarkColors has no entry for these). + const wxColour outline_color = wxGetApp().dark_mode() + ? wxColour(90, 90, 94) // > bg #2B2B2B, < axis #818183 + : wxColour(200, 200, 200); // > grid #EEEEEE, < axis #6B6B6B + auto needs_outline = [&](const wxColour& c) { + return calc_color_distance(c, bg) < kBgSimilarThreshold; + }; + + auto draw_one = [&](int curve_idx, int stroke_dip) { + const auto poly = build_polyline(curve_idx); + const wxColour col = color_for_curve(curve_idx); + if (needs_outline(col)) + draw_polyline(poly, outline_color, stroke_dip + kOutlineExtraDip); + draw_polyline(poly, col, stroke_dip); + }; + + // Draw unselected first so the selected curve sits on top. + const int other = 1 - m_selected_curve; + draw_one(other, kStrokeUnselected); + draw_one(m_selected_curve, kStrokeSelected); + + // Control points (selected curve only): hollow circle with axis-color border, theme-aware fill. + const int r = FromDIP(kPointRadius); + dc.SetPen(wxPen(axis_color, 1)); + dc.SetBrush(wxBrush(point_fill)); + for (size_t i = 0; i < m_points.size(); ++i) { + const double vy = to_visual_y(m_selected_curve, m_points[i].y); + const wxPoint p = data_to_px(m_points[i].x, vy); + dc.DrawCircle(p.x, p.y, r); + } +} + +void GradientCurveEditor::on_left_down(wxMouseEvent& evt) +{ + const wxPoint pos = evt.GetPosition(); + m_dragged_moved = false; + + // 1) Anchor on the selected curve takes precedence over everything else. + // Dragging an anchor resets its tangent overrides so the surrounding curve + // returns to PCHIP-default shape (matches user expectation that pulling an + // anchor "straightens out" the local mess). + const int idx = hit_test(pos.x, pos.y); + if (idx >= 0) { + m_drag_mode = DragMode::Anchor; + m_drag_idx = idx; + // Only emit a change event when clearing the tangents actually mutates + // the curve. A plain click on an already-default anchor must not trigger + // re-slicing through the changed-event listener. + const bool had_tangent = std::isfinite(m_points[idx].m_in) + || std::isfinite(m_points[idx].m_out); + m_points[idx].m_in = std::numeric_limits::quiet_NaN(); + m_points[idx].m_out = std::numeric_limits::quiet_NaN(); + if (!HasCapture()) + CaptureMouse(); + Refresh(); + if (had_tangent) + emit_changed(); + return; + } + + // 2) Line-body hit. Determine which curve and which segment. + int seg = -1; + const int curve_hit = hit_test_curve(pos.x, pos.y, &seg); + if (curve_hit < 0) { + m_drag_mode = DragMode::None; + evt.Skip(); + return; + } + + // 3) Non-selected curve hit -> switch selection only, no drag arming. + if (curve_hit != m_selected_curve) { + m_selected_curve = curve_hit; + m_drag_mode = DragMode::None; + Refresh(); + evt.Skip(); + return; + } + + // 4) Selected curve line body hit -> insert a new anchor at cursor x (snapped + // to the current smooth curve so the initial click is visually invisible) + // and immediately enter Anchor drag mode. PS Curves style: the drag-bend + // interaction has no separate "bend without anchor" mode; pressing and + // dragging on the line is equivalent to clicking to add then dragging the + // fresh anchor. Trades the previous (failed) "no anchor on drag" promise + // for genuine cursor tracking, since a single cubic between two existing + // anchors mathematically cannot put its peak under an off-center cursor. + double nx = 0, dummy = 0; + px_to_data(pos.x, pos.y, nx, dummy); + if (nx <= 0.0 || nx >= 1.0 || seg < 0) { + m_drag_mode = DragMode::None; + evt.Skip(); + return; + } + GradientAnchor a; + a.x = nx; + a.y = sample_curve_y(nx); + const size_t insert_idx = static_cast(seg) + 1; + m_points.insert(m_points.begin() + insert_idx, a); + + m_drag_mode = DragMode::Anchor; + m_drag_idx = static_cast(insert_idx); + if (!HasCapture()) + CaptureMouse(); + Refresh(); + emit_changed(); +} + +void GradientCurveEditor::on_left_up(wxMouseEvent& evt) +{ + if (HasCapture()) + ReleaseMouse(); + + // Anchor mode (either an existing anchor or one freshly inserted by on_left_down) + // already fired emit_changed on mouse_down; only fire again here if the user + // actually dragged so the slicer doesn't re-run on a pure click. + if (m_drag_mode == DragMode::Anchor && m_dragged_moved) + emit_changed(); + + m_drag_mode = DragMode::None; + m_drag_idx = -1; + m_dragged_moved = false; + (void)evt; +} + +void GradientCurveEditor::on_right_down(wxMouseEvent& evt) +{ + const wxPoint pos = evt.GetPosition(); + const int idx = hit_test(pos.x, pos.y); + if (idx > 0 && static_cast(idx) + 1 < m_points.size()) { + // Interior anchor on the selected curve -> delete it. Endpoints stay locked. + m_points.erase(m_points.begin() + idx); + Refresh(); + emit_changed(); + return; + } + // Right-click on the non-selected curve switches selection (never deletes). + const int curve_hit = hit_test_curve(pos.x, pos.y); + if (curve_hit >= 0 && curve_hit != m_selected_curve) { + m_selected_curve = curve_hit; + Refresh(); + return; + } + evt.Skip(); +} + +void GradientCurveEditor::on_motion(wxMouseEvent& evt) +{ + if (!evt.LeftIsDown() || m_drag_mode != DragMode::Anchor) { + evt.Skip(); + return; + } + if (static_cast(m_drag_idx) >= m_points.size()) + return; + + const wxPoint pos = evt.GetPosition(); + double nx = 0, vy = 0; + px_to_data(pos.x, pos.y, nx, vy); + + auto& p = m_points[m_drag_idx]; + const bool is_first = (m_drag_idx == 0); + const bool is_last = (static_cast(m_drag_idx) + 1 == m_points.size()); + + // Endpoints stay locked at x=0 / x=1; interior anchors clamp into + // (left_neighbor.x, right_neighbor.x) so they can't cross or coincide. + if (!is_first && !is_last) { + const double xl = m_points[m_drag_idx - 1].x; + const double xr = m_points[m_drag_idx + 1].x; + const double eps = 1e-4; + nx = std::max(xl + eps, std::min(xr - eps, nx)); + p.x = nx; + } + // y is constrained to the reserved blend band so neither component ever + // reaches 0% / 100%, matching the sampler's clamp. + p.y = std::max(kGradientMinRatio, + std::min(kGradientMaxRatio, to_stored_y(m_selected_curve, vy))); + m_dragged_moved = true; + Refresh(); +} + +void GradientCurveEditor::on_leave(wxMouseEvent& evt) +{ + evt.Skip(); +} + +void GradientCurveEditor::on_size(wxSizeEvent& evt) +{ + Refresh(); + evt.Skip(); +} + +} // namespace GUI +} // namespace Slic3r diff --git a/src/slic3r/GUI/GradientCurveEditor.hpp b/src/slic3r/GUI/GradientCurveEditor.hpp new file mode 100644 index 0000000000..8412db3df2 --- /dev/null +++ b/src/slic3r/GUI/GradientCurveEditor.hpp @@ -0,0 +1,115 @@ +#ifndef slic3r_GradientCurveEditor_hpp_ +#define slic3r_GradientCurveEditor_hpp_ + +#include +#include +#include +#include +#include + +#include "libslic3r/FilamentMixer.hpp" + +namespace Slic3r { +namespace GUI { + +// Photoshop-style curve editor for "Z progress -> first-component ratio" mapping. +// Curve evaluation uses cubic Hermite with PCHIP defaults plus optional per-anchor +// tangent overrides (m_in / m_out, NaN = use PCHIP default). The same evaluator +// (FilamentMixer::sample_gradient_curve) is shared with the slicing backend so what +// the editor renders matches the G-code output 1:1. +// +// Interaction model (PS Curves style): +// - Click or press-and-drag on the line body inserts a new anchor at the cursor x +// (snapped to the current smooth curve, NaN tangents) and starts dragging it. +// A pure click leaves an anchor sitting exactly on the previous curve shape; a +// drag moves the new anchor freely so the bump follows the cursor 1:1. +// - Dragging an existing anchor moves (x, y) and clears its m_in / m_out so the +// local curve returns to the PCHIP default shape around it. +// - Right-click on an interior anchor deletes it; endpoints stay locked. +class GradientCurveEditor : public wxPanel +{ +public: + using PointList = std::vector; + + GradientCurveEditor(wxWindow* parent, + const wxColour& color_low = wxColour(217, 217, 217), + const wxColour& color_high = wxColour(217, 217, 217)); + + // Replace the entire point list. The widget enforces x in [0,1], y in [0,1], + // sorts by x, and clamps the first / last x to 0 / 1. Tangent overrides are + // preserved as-is (NaN entries continue to use PCHIP defaults). + void set_points(const PointList& pts); + const PointList& get_points() const { return m_points; } + + void set_colors(const wxColour& color_low, const wxColour& color_high); + + // Which curve currently responds to drag / add / delete and is drawn with the thick stroke. + // 0 = first component (color_low), 1 = second component (color_high). Storage layer is + // unaffected: m_points always represents component 0's ratio. + void set_selected_curve(int curve_idx); + int get_selected_curve() const { return m_selected_curve; } + + // Reset to a two-point linear curve from y0 at t=0 to y1 at t=1. + // Clears all tangent overrides. + void reset_to_linear(double y0, double y1); + // Flip the curve top to bottom (all y -> 1 - y; tangents negated to mirror shape). + void reverse(); + +private: + enum class DragMode { + None, // nothing armed + Anchor, // dragging an anchor (either existing or just inserted from a line hit) + }; + + void normalize_points(); + void emit_changed(); + + void on_paint(wxPaintEvent& evt); + void on_left_down(wxMouseEvent& evt); + void on_left_up(wxMouseEvent& evt); + void on_right_down(wxMouseEvent& evt); + void on_motion(wxMouseEvent& evt); + void on_leave(wxMouseEvent& evt); + void on_size(wxSizeEvent& evt); + + // Coordinate mapping between data (x, y in [0,1]) and pixels in plot area. + wxRect plot_rect() const; + wxPoint data_to_px(double x, double y) const; + void px_to_data(int px, int py, double& x, double& y) const; + // Anchor hit test for the currently-selected curve (uses translated visual y). + int hit_test(int px, int py) const; // returns point index or -1 + // Line-body hit test across both curves. Returns 0/1 for which curve was hit, -1 if none. + // Prefers the selected curve when both are within threshold. seg_out (when non-null) + // receives the left-anchor index of the segment that was hit on the returned curve; + // on_left_down uses it to know where in m_points to insert a freshly-added anchor. + int hit_test_curve(int px, int py, int* seg_out = nullptr) const; + + // Sample the curve in stored space (component 0) at x. + double sample_curve_y(double x) const; + + // Symmetric translation between visual y (what the user sees / clicks) and stored y + // (component 0's ratio in m_points). + static double to_stored_y(int curve_idx, double visual_y) { + return (curve_idx == 0) ? visual_y : (1.0 - visual_y); + } + static double to_visual_y(int curve_idx, double stored_y) { + return (curve_idx == 0) ? stored_y : (1.0 - stored_y); + } + + PointList m_points; + wxColour m_color_low; + wxColour m_color_high; + + int m_selected_curve = 0; + DragMode m_drag_mode = DragMode::None; + int m_drag_idx = -1; // valid when m_drag_mode == Anchor + bool m_dragged_moved = false; +}; + +// Custom event raised when the curve is edited (drag / add / remove / reset / reverse). +wxDECLARE_EVENT(wxEVT_GRADIENT_CURVE_CHANGED, wxCommandEvent); + +} // namespace GUI +} // namespace Slic3r + +#endif // slic3r_GradientCurveEditor_hpp_ diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 3b1cf1dffd..5f7d69244e 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -2330,6 +2330,11 @@ bool MainFrame::get_enable_slice_status() } } + // A mixed filament whose components were deleted, or whose components disagree in type, + // cannot be resolved at slicing time. Block the slice until the user fixes it. + if (enable && m_plater->sidebar().has_broken_mixed_filament()) + enable = false; + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": m_slice_select %1%, enable= %2% ")%m_slice_select %enable; return enable; } diff --git a/src/slic3r/GUI/MixedFilamentDialog.cpp b/src/slic3r/GUI/MixedFilamentDialog.cpp new file mode 100644 index 0000000000..241f051905 --- /dev/null +++ b/src/slic3r/GUI/MixedFilamentDialog.cpp @@ -0,0 +1,1983 @@ +#include "MixedFilamentDialog.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libslic3r/Utils.hpp" +#include "libslic3r/FilamentMixer.hpp" +#include "I18N.hpp" +#include "GUI.hpp" +#include "GUI_App.hpp" +#include "GradientCurveEditor.hpp" +#include "wxExtensions.hpp" +#include "Tab.hpp" +#include "libslic3r/Preset.hpp" +#include "Widgets/Button.hpp" +#include "Widgets/CheckBox.hpp" +#include "Widgets/ComboBox.hpp" +#include "Widgets/DropDown.hpp" +#include "Widgets/Label.hpp" + +namespace Slic3r { +namespace GUI { + +static constexpr int MAX_COMPONENTS = 3; +static constexpr int MIN_COMPONENT_RATIO = 10; + +// Lightweight self-painting label used for both dual-color and triple-color +// ratio percentage display. Hover shows a rounded-rect background; click +// fires wxEVT_LEFT_DOWN which the owning dialog binds to start_ratio_editor. +class RatioLabelPanel : public wxPanel +{ +public: + RatioLabelPanel(wxWindow* parent) + : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE) + { + SetBackgroundStyle(wxBG_STYLE_PAINT); + SetCursor(wxCursor(wxCURSOR_HAND)); + SetToolTip(_L("Click to edit ratio")); + SetFont(::Label::Body_10); + + Bind(wxEVT_ENTER_WINDOW, [this](wxMouseEvent& e) { m_hovered = true; Refresh(); e.Skip(); }); + Bind(wxEVT_LEAVE_WINDOW, [this](wxMouseEvent& e) { m_hovered = false; Refresh(); e.Skip(); }); + Bind(wxEVT_PAINT, &RatioLabelPanel::on_paint, this); + } + + void SetLabel(const wxString& text) override + { + if (m_text == text) return; + m_text = text; + update_best_size(); + Refresh(); + } + wxString GetLabel() const override { return m_text; } + +private: + void update_best_size() + { + wxClientDC dc(this); + dc.SetFont(GetFont()); + wxSize ts = dc.GetTextExtent(m_text); + int pad_x = FromDIP(4), pad_y = FromDIP(3); + SetMinSize(wxSize(ts.GetWidth() + pad_x * 2, ts.GetHeight() + pad_y * 2)); + InvalidateBestSize(); + } + + void on_paint(wxPaintEvent&) + { + wxBufferedPaintDC dc(this); + wxSize sz = GetClientSize(); + + wxColour parent_bg = GetParent() ? GetParent()->GetBackgroundColour() + : StateColor::darkModeColorFor(*wxWHITE); + dc.SetBrush(wxBrush(parent_bg)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + if (m_hovered) { + dc.SetBrush(wxBrush(StateColor::darkModeColorFor(wxColour("#F8F8F8")))); + dc.SetPen(wxPen(StateColor::darkModeColorFor(wxColour("#CECECE")), 1)); + dc.DrawRoundedRectangle(0, 0, sz.GetWidth(), sz.GetHeight(), FromDIP(3)); + } + + dc.SetFont(GetFont()); + dc.SetTextForeground(m_hovered ? wxColour("#00AE42") + : StateColor::darkModeColorFor(wxColour("#262E30"))); + wxSize ts = dc.GetTextExtent(m_text); + int x = (sz.GetWidth() - ts.GetWidth()) / 2; + int y = (sz.GetHeight() - ts.GetHeight()) / 2; + dc.DrawText(m_text, x, y); + } + + wxString m_text; + bool m_hovered{false}; +}; + +static wxColour blend_colors(const wxColour& a, const wxColour& b, double ratio_a) +{ + unsigned char r, g, bl; + Slic3r::filament_mixer_lerp(a.Red(), a.Green(), a.Blue(), + b.Red(), b.Green(), b.Blue(), + static_cast(1.0 - ratio_a), + &r, &g, &bl); + return wxColour(r, g, bl); +} + +static wxColour blend_n_colors(const std::vector& cols, const std::vector& weights) +{ + std::vector hex_colors; + std::vector int_weights; + for (size_t i = 0; i < cols.size() && i < weights.size(); ++i) { + hex_colors.push_back(cols[i].GetAsString(wxC2S_HTML_SYNTAX).ToStdString()); + // Scale double weights (e.g. 0.5) to int (5000) for blend_color_multi; + // only relative magnitude matters. + int_weights.push_back(static_cast(std::lround(weights[i] * 10000))); + } + std::string hex = Slic3r::blend_color_multi(hex_colors, int_weights); + return wxColour(hex); +} + +// ---- Constructors ---- + +MixedFilamentDialog::MixedFilamentDialog(wxWindow* parent, + const std::vector& physical_colors, + const std::vector& physical_names, + const std::vector& physical_types) + : DPIDialog(parent, wxID_ANY, _L("Add Mixed Filament"), wxDefaultPosition, + wxDefaultSize, wxCAPTION | wxCLOSE_BOX) + , m_edit_mode(false) + , m_physical_colors(physical_colors) + , m_physical_names(physical_names) + , m_physical_types(physical_types) +{ + m_result.components = {1, (physical_colors.size() >= 2) ? 2u : 1u}; + m_result.ratios = {50, 50}; + build_ui(); + wxGetApp().UpdateDlgDarkUI(this); + + wxImage img; + if (img.LoadFile(from_u8(Slic3r::var("mixed_filament_preview_twocolor.png")), wxBITMAP_TYPE_PNG)) + m_preview_bmp_two = wxBitmap(img); + if (img.LoadFile(from_u8(Slic3r::var("mixed_filament_preview_threecolor.png")), wxBITMAP_TYPE_PNG)) + m_preview_bmp_three = wxBitmap(img); +} + +MixedFilamentDialog::MixedFilamentDialog(wxWindow* parent, + const MixedFilamentResult& existing, + const std::vector& physical_colors, + const std::vector& physical_names, + const std::vector& physical_types) + : DPIDialog(parent, wxID_ANY, _L("Edit Mixed Filament"), wxDefaultPosition, + wxDefaultSize, wxCAPTION | wxCLOSE_BOX) + , m_result(existing) + , m_edit_mode(true) + , m_physical_colors(physical_colors) + , m_physical_names(physical_names) + , m_physical_types(physical_types) +{ + if (m_result.components.size() < 2) { + m_result.components = {1, (physical_colors.size() >= 2) ? 2u : 1u}; + m_result.ratios = {50, 50}; + } + if (m_result.ratios.size() >= 3) { + int sum = 0; + for (int r : m_result.ratios) sum += r; + if (sum > 0) { + m_tri_wx = (double)m_result.ratios[0] / sum; + m_tri_wy = (double)m_result.ratios[1] / sum; + m_tri_wz = (double)m_result.ratios[2] / sum; + } + } + build_ui(); + wxGetApp().UpdateDlgDarkUI(this); + + wxImage img; + if (img.LoadFile(from_u8(Slic3r::var("mixed_filament_preview_twocolor.png")), wxBITMAP_TYPE_PNG)) + m_preview_bmp_two = wxBitmap(img); + if (img.LoadFile(from_u8(Slic3r::var("mixed_filament_preview_threecolor.png")), wxBITMAP_TYPE_PNG)) + m_preview_bmp_three = wxBitmap(img); +} + +void MixedFilamentDialog::on_dpi_changed(const wxRect&) +{ + int h = (num_components() >= 3) ? FromDIP(680) : FromDIP(580); + SetSize(FromDIP(439), h); + Refresh(); +} + +wxColour MixedFilamentDialog::comp_colour(size_t i) const +{ + unsigned int c = comp(i); + if (c >= 1 && c <= m_physical_colors.size()) + return wxColour(m_physical_colors[c - 1]); + return wxColour("#D9D9D9"); +} + +static wxBitmap make_alpha_bitmap(int w, int h, + const std::function& draw_fn) +{ + wxBitmap bmp(w, h); + wxMemoryDC memdc; +#ifdef __WXOSX__ + bmp.UseAlpha(); + memdc.SelectObject(bmp); +#else + { + wxImage img(w, h); + img.InitAlpha(); + memset(img.GetAlpha(), 0, w * h); + bmp = wxBitmap(std::move(img)); + } + memdc.SelectObject(bmp); +#endif + { +#ifdef __WXMSW__ + wxGCDC dc(memdc); +#else + wxDC& dc = memdc; +#endif + draw_fn(dc); + } + memdc.SelectObject(wxNullBitmap); + return bmp; +} + +wxBitmap MixedFilamentDialog::make_swatch_bitmap(size_t idx) +{ + int swatch_sz = FromDIP(20); + int pad_left = FromDIP(2); + int pad_right = FromDIP(6); + int bmp_w = pad_left + swatch_sz + pad_right; + int bmp_h = swatch_sz; + + // Reuse the sidebar clr_picker swatch (get_extruder_color_icon) so the + // checkerboard (transparent.svg tiling), border and label style match the + // sidebar exactly, instead of a self-drawn rounded rect / programmatic grid. + std::string color_hex = "#D9D9D9"; + if (idx < m_physical_colors.size()) + color_hex = m_physical_colors[idx]; + std::string label = std::to_string(idx + 1); + + wxBitmap* icon = get_extruder_color_icon(color_hex, label, swatch_sz, swatch_sz); + + return make_alpha_bitmap(bmp_w, bmp_h, [&](wxDC& dc) { + if (icon && icon->IsOk()) + dc.DrawBitmap(*icon, pad_left, 0); + }); +} + +void MixedFilamentDialog::reset_manual_ratio_state() +{ + m_ratio_manual_order.clear(); + if (m_ratio_editor_panel) + m_ratio_editor_panel->Hide(); + // Restore any label hidden by an in-flight editor so it can never be left + // permanently invisible if the editor is dismissed without a commit. + if (m_ratio_editor_anchor) { + m_ratio_editor_anchor->Show(); + m_ratio_editor_anchor = nullptr; + } +} + +void MixedFilamentDialog::refresh_ratio_labels() +{ + if (m_label_ratio_a) + m_label_ratio_a->SetLabel(wxString::Format(wxT("%d%%"), ratio(0))); + if (m_label_ratio_b) + m_label_ratio_b->SetLabel(wxString::Format(wxT("%d%%"), ratio(1))); + if (m_ratio_sizer) + m_ratio_sizer->Layout(); + if (m_triangle_panel) + m_triangle_panel->Refresh(); +} + +void MixedFilamentDialog::sync_triangle_weights_from_ratios() +{ + if (m_result.ratios.size() < 3) + return; + + int sum = 0; + for (int r : m_result.ratios) + sum += r; + if (sum <= 0) + return; + + m_tri_wx = (double)m_result.ratios[0] / sum; + m_tri_wy = (double)m_result.ratios[1] / sum; + m_tri_wz = (double)m_result.ratios[2] / sum; +} + +void MixedFilamentDialog::apply_manual_ratio(size_t idx, int value) +{ + const size_t n = num_components(); + if (idx >= n) + return; + if (m_result.ratios.size() != n) + m_result.ratios.assign(n, n > 0 ? 100 / (int)n : 0); + bool manual_stale = false; + for (size_t o : m_ratio_manual_order) { + if (o >= n) { manual_stale = true; break; } + } + if (manual_stale) + reset_manual_ratio_state(); + + int max_value = (int)(100 - (n - 1) * MIN_COMPONENT_RATIO); + value = std::clamp(value, MIN_COMPONENT_RATIO, std::max(MIN_COMPONENT_RATIO, max_value)); + + if (n == 2) { + if (idx == 0) { + m_result.ratios[0] = value; + m_result.ratios[1] = 100 - value; + } else { + m_result.ratios[1] = value; + m_result.ratios[0] = 100 - value; + } + m_result.ratios[0] = std::clamp(m_result.ratios[0], MIN_COMPONENT_RATIO, 100 - MIN_COMPONENT_RATIO); + m_result.ratios[1] = 100 - m_result.ratios[0]; + } else if (n >= 3) { + m_result.ratios[idx] = value; + int remaining = 100 - value; + + std::vector others; + int others_sum = 0; + for (size_t i = 0; i < n; ++i) { + if (i == idx) continue; + others.push_back(i); + others_sum += m_result.ratios[i]; + } + + if (!others.empty()) { + if (others_sum > 0) { + int assigned = 0; + for (size_t k = 0; k < others.size(); ++k) { + int nv = (int)((double)remaining * m_result.ratios[others[k]] / others_sum + 0.5); + nv = std::max(nv, MIN_COMPONENT_RATIO); + m_result.ratios[others[k]] = nv; + assigned += nv; + } + while (assigned != remaining) { + if (assigned > remaining) { + int pick = -1; + for (size_t k = 0; k < others.size(); ++k) + if (m_result.ratios[others[k]] > MIN_COMPONENT_RATIO + && (pick < 0 || m_result.ratios[others[k]] > m_result.ratios[others[pick]])) + pick = (int)k; + if (pick < 0) break; + --m_result.ratios[others[pick]]; --assigned; + } else { + int pick = 0; + for (size_t k = 1; k < others.size(); ++k) + if (m_result.ratios[others[k]] > m_result.ratios[others[pick]]) + pick = (int)k; + ++m_result.ratios[others[pick]]; ++assigned; + } + } + } else { + int base = remaining / (int)others.size(); + for (size_t k = 0; k < others.size(); ++k) + m_result.ratios[others[k]] = base; + m_result.ratios[others.back()] += remaining - base * (int)others.size(); + } + } + } + + refresh_ratio_labels(); + sync_triangle_weights_from_ratios(); + update_preview(); +} + +void MixedFilamentDialog::apply_dragged_triangle_ratio(int r0, int r1, int r2) +{ + if (m_result.ratios.size() < 3) + return; + + int ratios[3] = { + std::clamp(r0, MIN_COMPONENT_RATIO, 100), + std::clamp(r1, MIN_COMPONENT_RATIO, 100), + std::clamp(r2, MIN_COMPONENT_RATIO, 100) + }; + + int sum = ratios[0] + ratios[1] + ratios[2]; + while (sum > 100) { + int idx = 0; + for (int i = 1; i < 3; ++i) { + if (ratios[i] > ratios[idx]) + idx = i; + } + if (ratios[idx] <= MIN_COMPONENT_RATIO) + break; + --ratios[idx]; + --sum; + } + while (sum < 100) { + int idx = 0; + for (int i = 1; i < 3; ++i) { + if (ratios[i] < ratios[idx]) + idx = i; + } + ++ratios[idx]; + ++sum; + } + + m_result.ratios[0] = ratios[0]; + m_result.ratios[1] = ratios[1]; + m_result.ratios[2] = ratios[2]; + sync_triangle_weights_from_ratios(); + reset_manual_ratio_state(); + update_preview(); +} + +void MixedFilamentDialog::start_ratio_editor(size_t idx, wxWindow* anchor, const wxRect& anchor_rect) +{ + if (!anchor || idx >= m_result.ratios.size()) + return; + if (m_ratio_editor_panel && m_ratio_editor_panel->IsShown()) + commit_ratio_editor(true); + + if (!m_ratio_editor_panel) { + wxColour bg = StateColor::darkModeColorFor(wxColour("#F8F8F8")); + wxColour fg = StateColor::darkModeColorFor(wxColour("#262E30")); + + m_ratio_editor_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, + wxDefaultSize, wxBORDER_SIMPLE); + m_ratio_editor_panel->SetBackgroundColour(bg); + + auto* hsizer = new wxBoxSizer(wxHORIZONTAL); + + m_ratio_editor = new wxTextCtrl(m_ratio_editor_panel, wxID_ANY, wxEmptyString, + wxDefaultPosition, wxDefaultSize, + wxTE_PROCESS_ENTER | wxTE_RIGHT | wxBORDER_NONE); + m_ratio_editor->SetFont(::Label::Body_10); + m_ratio_editor->SetMaxLength(3); + m_ratio_editor->SetBackgroundColour(bg); + m_ratio_editor->SetForegroundColour(fg); + // Default wxTextCtrl best width (~140px) is too wide for the sizer to + // shrink, which would push the "%" suffix out of the panel. Cap the + // editor's min width to the digits only (ratios are always two digits). + { + wxClientDC mdc(m_ratio_editor); + mdc.SetFont(::Label::Body_10); + int digits_w = mdc.GetTextExtent(wxT("88")).GetWidth(); + m_ratio_editor->SetMinSize(wxSize(digits_w + FromDIP(2), -1)); + } + + auto* pct_label = new wxStaticText(m_ratio_editor_panel, wxID_ANY, wxT("%")); + pct_label->SetFont(::Label::Body_10); + pct_label->SetForegroundColour(fg); + pct_label->SetBackgroundColour(bg); + pct_label->SetMinSize(pct_label->GetBestSize()); + + hsizer->Add(m_ratio_editor, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(2)); + hsizer->Add(pct_label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(2)); + m_ratio_editor_panel->SetSizer(hsizer); + m_ratio_editor_panel->Hide(); + + m_ratio_editor->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent&) { commit_ratio_editor(true); }); + m_ratio_editor->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { + commit_ratio_editor(true); + e.Skip(); + }); + m_ratio_editor->Bind(wxEVT_CHAR_HOOK, [this](wxKeyEvent& e) { + if (e.GetKeyCode() == WXK_ESCAPE) + commit_ratio_editor(false); + else + e.Skip(); + }); + } + + m_ratio_editor_idx = idx; + + // Keep the editor in the same window hierarchy as the clicked label so the + // z-order is reliable and the editor fully covers the anchor (dual-color + // labels live on the dialog, triple-color labels live on the triangle + // panel). + wxWindow* target_parent = anchor->GetParent(); + if (target_parent && m_ratio_editor_panel->GetParent() != target_parent) + m_ratio_editor_panel->Reparent(target_parent); + + // Hide the label being edited to avoid its (hover-state) text leaking out + // next to the editor; restored on commit. + m_ratio_editor_anchor = anchor; + anchor->Hide(); + + wxPoint pos = anchor->GetPosition() + anchor_rect.GetTopLeft(); + // Match the editor to the label (hover box) size so the inline editor and + // the hover state look identical. A small floor keeps the "%" suffix from + // being squeezed out on very narrow labels. + wxSize size = anchor->GetSize(); + size.SetWidth(std::max(size.GetWidth(), FromDIP(30))); + size.SetHeight(std::max(size.GetHeight(), FromDIP(18))); + m_ratio_editor_panel->SetSize(wxRect(pos, size)); + m_ratio_editor_panel->Layout(); + m_ratio_editor->SetValue(wxString::Format(wxT("%d"), ratio(idx))); + m_ratio_editor_panel->Show(); + m_ratio_editor_panel->Raise(); + m_ratio_editor->SetFocus(); + m_ratio_editor->SelectAll(); + m_ratio_editor_panel->Refresh(); + Update(); +} + +void MixedFilamentDialog::commit_ratio_editor(bool apply) +{ + if (!m_ratio_editor_panel || !m_ratio_editor_panel->IsShown() || m_ratio_editor_committing) + return; + + m_ratio_editor_committing = true; + + // Restore the hidden anchor before applying the ratio, so any sizer layout + // triggered by refresh_ratio_labels() accounts for the visible label. + m_ratio_editor_panel->Hide(); + if (m_ratio_editor_anchor) { + m_ratio_editor_anchor->Show(); + m_ratio_editor_anchor = nullptr; + } + + if (apply) { + wxString value = m_ratio_editor->GetValue(); + value.Trim(true); + value.Trim(false); + if (value.EndsWith(wxT("%"))) + value.RemoveLast(); + + long parsed = 0; + if (value.ToLong(&parsed)) + apply_manual_ratio(m_ratio_editor_idx, (int)parsed); + else + refresh_ratio_labels(); + } + + m_ratio_editor_committing = false; +} + +void MixedFilamentDialog::commit_ratio_editor_from_background(wxMouseEvent& e) +{ + if (m_ratio_editor_panel && m_ratio_editor_panel->IsShown()) { + wxPoint mouse_in_panel = m_ratio_editor_panel->ScreenToClient(wxGetMousePosition()); + if (!m_ratio_editor_panel->GetClientRect().Contains(mouse_in_panel)) + commit_ratio_editor(true); + } + e.Skip(); +} + +// ---- UI Construction ---- + +void MixedFilamentDialog::build_ui() +{ + const wxColour mc_bg = StateColor::darkModeColorFor(*wxWHITE); + const wxColour mc_bg_sub = StateColor::darkModeColorFor(wxColour("#F8F8F8")); + const wxColour mc_border = StateColor::darkModeColorFor(wxColour("#CECECE")); + const wxColour mc_text = StateColor::darkModeColorFor(wxColour("#262E30")); + const wxColour mc_dim_text = StateColor::darkModeColorFor(wxColour("#ACACAC")); + + SetBackgroundColour(mc_bg); + Bind(wxEVT_LEFT_DOWN, &MixedFilamentDialog::commit_ratio_editor_from_background, this); + SetSize(FromDIP(439), FromDIP(580)); + + auto* main_sizer = new wxBoxSizer(wxVERTICAL); + + auto* top_sizer = new wxBoxSizer(wxHORIZONTAL); + top_sizer->Add(create_preview_panel(), 0, wxALL, FromDIP(20)); + + m_right_sizer = new wxBoxSizer(wxVERTICAL); + m_right_sizer->Add(create_material_selection(), 0, wxEXPAND); + m_right_sizer->Add(create_gradient_section(), 0, wxEXPAND | wxTOP, FromDIP(7)); + + m_ratio_sizer = create_ratio_slider(); + m_right_sizer->Add(m_ratio_sizer, 0, wxEXPAND | wxTOP, FromDIP(7)); + + m_triangle_sizer = create_triangle_picker(); + m_right_sizer->Add(m_triangle_sizer, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, FromDIP(7)); + + top_sizer->Add(m_right_sizer, 1, wxTOP | wxRIGHT | wxBOTTOM, FromDIP(20)); + main_sizer->Add(top_sizer, 0, wxEXPAND); + + main_sizer->Add(create_recommendation_grid(), 1, wxEXPAND | wxLEFT | wxRIGHT, FromDIP(25)); + + // Warning panel: red bordered box with exclamation icon + text + m_warning_sizer = new wxBoxSizer(wxVERTICAL); + m_warning_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(-1, FromDIP(48))); + m_warning_panel->SetMinSize(wxSize(-1, FromDIP(48))); + m_warning_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + m_warning_panel->Bind(wxEVT_PAINT, &MixedFilamentDialog::paint_warning_panel, this); + m_warning_sizer->Add(m_warning_panel, 0, wxEXPAND); + main_sizer->Add(m_warning_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, FromDIP(25)); + m_warning_panel->Hide(); + + main_sizer->Add(create_button_panel(), 0, wxALIGN_RIGHT | wxALL, FromDIP(20)); + + SetSizer(main_sizer); + + rebuild_all_combos(); + update_component_count_ui(); + update_preview(); + update_ok_button_state(); + + Layout(); + CentreOnParent(); +} + +wxBoxSizer* MixedFilamentDialog::create_preview_panel() +{ + auto* sizer = new wxBoxSizer(wxVERTICAL); + + m_preview_canvas = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(FromDIP(129), FromDIP(129))); + m_preview_canvas->SetBackgroundStyle(wxBG_STYLE_PAINT); + + m_preview_canvas->Bind(wxEVT_PAINT, [this](wxPaintEvent&) { + wxBufferedPaintDC dc(m_preview_canvas); + wxSize sz = m_preview_canvas->GetClientSize(); + size_t n = num_components(); + + dc.SetBrush(wxBrush(StateColor::darkModeColorFor(*wxWHITE))); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + if (n == 0) return; + + int swatch_sz = FromDIP(80); + int x0 = (sz.GetWidth() - swatch_sz) / 2; + int y0 = (sz.GetHeight() - swatch_sz) / 2; + double radius = FromDIP(6); + + if (m_result.gradient_enabled && n == 2) { + Slic3r::GradientCurve curve; + if (!m_result.gradient_curve.empty()) { + curve.points = m_result.gradient_curve; + } else { + double yStart = (m_result.gradient_direction == 0) ? kGradientMaxRatio : kGradientMinRatio; + double yEnd = (m_result.gradient_direction == 0) ? kGradientMinRatio : kGradientMaxRatio; + curve.points = {{0.0, yStart, NAN, NAN}, {1.0, yEnd, NAN, NAN}}; + } + + wxColour colA = comp_colour(0); + wxColour colB = comp_colour(1); + const int bands = std::max(80, swatch_sz); + double band_h = static_cast(swatch_sz) / bands; + dc.SetPen(*wxTRANSPARENT_PEN); + for (int b = 0; b < bands; ++b) { + double t = 1.0 - (b + 0.5) / bands; + double r1 = Slic3r::sample_gradient_curve(curve, t); + double r2 = 1.0 - r1; + wxColour band_col = blend_n_colors({colA, colB}, {r1, r2}); + dc.SetBrush(wxBrush(band_col)); + int by = y0 + static_cast(b * band_h); + int bh = static_cast((b + 1) * band_h) - static_cast(b * band_h) + 1; + dc.DrawRectangle(x0, by, swatch_sz, bh); + } + + // Mask corners: overdraw a thick background-colored rounded rect frame + // so the inner edge forms the desired rounded corners. + // Known limitation: this assumes the panel background equals + // darkModeColorFor(white). wxGraphicsContext::Clip(path) is not + // available in our wxWidgets build (only Clip(wxRegion) exists). + int r = static_cast(radius); + wxColour bg = StateColor::darkModeColorFor(*wxWHITE); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.SetPen(wxPen(bg, r * 2)); + dc.DrawRoundedRectangle(x0 - r, y0 - r, swatch_sz + r * 2, swatch_sz + r * 2, radius * 2); + } else { + std::vector cols; + std::vector weights; + for (size_t i = 0; i < n; ++i) { + cols.push_back(comp_colour(i)); + weights.push_back(ratio(i) / 100.0); + } + wxColour mixed = blend_n_colors(cols, weights); + dc.SetBrush(wxBrush(mixed)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRoundedRectangle(x0, y0, swatch_sz, swatch_sz, radius); + } + }); + + sizer->Add(m_preview_canvas, 0, wxALIGN_CENTER); + + auto* label = new wxStaticText(this, wxID_ANY, _L("Effect Preview")); + label->SetForegroundColour(wxColour("#909090")); + label->SetFont(::Label::Body_13); + sizer->Add(label, 0, wxALIGN_CENTER | wxTOP, FromDIP(4)); + + return sizer; +} + +wxBoxSizer* MixedFilamentDialog::create_material_selection() +{ + auto* sizer = new wxBoxSizer(wxVERTICAL); + + // Summary panel — draws N components dynamically + m_summary_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(FromDIP(234), FromDIP(40))); + m_summary_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + m_summary_panel->Bind(wxEVT_PAINT, [this](wxPaintEvent&) { + wxBufferedPaintDC dc(m_summary_panel); + wxSize sz = m_summary_panel->GetClientSize(); + + wxColour sum_bg = StateColor::darkModeColorFor(wxColour("#F8F8F8")); + wxColour sum_text = StateColor::darkModeColorFor(wxColour("#262E30")); + dc.SetBrush(wxBrush(sum_bg)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + int swatch_sz = FromDIP(20); + int y_center = (sz.GetHeight() - swatch_sz) / 2; + int x = FromDIP(13); + + dc.SetFont(::Label::Body_13); + + auto draw_summary_swatch = [&](size_t comp_idx) { + unsigned int c = comp(comp_idx); + std::string color_hex = "#D9D9D9"; + if (c >= 1 && c <= m_physical_colors.size()) + color_hex = m_physical_colors[c - 1]; + std::string label = std::to_string(c); + wxBitmap* icon = get_extruder_color_icon(color_hex, label, swatch_sz, swatch_sz); + if (icon && icon->IsOk()) + dc.DrawBitmap(*icon, x, y_center); + x += swatch_sz + FromDIP(4); + }; + + if (m_result.gradient_enabled && num_components() == 2) { + size_t idx_a = (m_result.gradient_direction == 0) ? 0 : 1; + size_t idx_b = 1 - idx_a; + draw_summary_swatch(idx_a); + + dc.SetTextForeground(sum_text); + wxString arrow = wxT("\u2192"); + wxSize arrow_sz = dc.GetTextExtent(arrow); + dc.DrawText(arrow, x, y_center + (swatch_sz - arrow_sz.GetHeight()) / 2); + x += arrow_sz.GetWidth() + FromDIP(4); + + draw_summary_swatch(idx_b); + } else { + for (size_t i = 0; i < num_components(); ++i) { + if (i > 0) { + dc.SetTextForeground(sum_text); + wxString plus = wxT("+"); + wxSize plus_sz = dc.GetTextExtent(plus); + dc.DrawText(plus, x, y_center + (swatch_sz - plus_sz.GetHeight()) / 2); + x += plus_sz.GetWidth() + FromDIP(4); + } + draw_summary_swatch(i); + + dc.SetTextForeground(sum_text); + wxString pct = wxString::Format(wxT("%d%%"), ratio(i)); + wxSize pct_sz = dc.GetTextExtent(pct); + dc.DrawText(pct, x, y_center + (swatch_sz - pct_sz.GetHeight()) / 2); + x += pct_sz.GetWidth() + FromDIP(4); + } + } + }); + sizer->Add(m_summary_panel, 0, wxEXPAND); + + auto* sel_label = new wxStaticText(this, wxID_ANY, _L("Select Mixed Materials")); + sel_label->SetForegroundColour(wxColour("#909090")); + sel_label->SetFont(::Label::Body_12); + sizer->Add(sel_label, 0, wxTOP, FromDIP(6)); + + m_material_rows_sizer = new wxBoxSizer(wxVERTICAL); + + m_combo_filaments.clear(); + m_combo_to_physical.clear(); + for (size_t i = 0; i < m_result.components.size(); ++i) { + auto* row = new wxBoxSizer(wxHORIZONTAL); + wxString lbl_text = wxString::Format(_L("Filament %d"), (int)(i + 1)); + auto* lbl = new wxStaticText(this, wxID_ANY, lbl_text); + lbl->SetFont(::Label::Body_12); + row->Add(lbl, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(8)); + + auto* combo = new ComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, + wxSize(FromDIP(166), FromDIP(24)), 0, nullptr, wxCB_READONLY); + combo->SetKeepDropArrow(true); + combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent&) { on_filament_changed(); }); + row->Add(combo, 1, wxALIGN_CENTER_VERTICAL); + + m_combo_filaments.push_back(combo); + m_combo_to_physical.push_back({}); + m_material_rows_sizer->Add(row, 0, wxEXPAND | wxTOP, FromDIP(9)); + } + + sizer->Add(m_material_rows_sizer, 0, wxEXPAND); + + auto* btn_sizer = new wxBoxSizer(wxHORIZONTAL); + + m_btn_add_material = new Button(this, _L("+ Add Material")); + m_btn_add_material->SetBackgroundColor(wxColour("#F8F8F8")); + m_btn_add_material->SetBorderColor(wxColour("#EEEEEE")); + m_btn_add_material->SetTextColor(wxColour("#262E30")); + m_btn_add_material->SetMinSize(wxSize(-1, FromDIP(24))); + m_btn_add_material->SetCursor(wxCursor(wxCURSOR_HAND)); + m_btn_add_material->EnableTooltipEvenDisabled(); + m_btn_add_material->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { on_add_material(); }); + btn_sizer->Add(m_btn_add_material, 1, wxRIGHT, FromDIP(6)); + + m_btn_remove_material = new Button(this, _L("- Delete Material")); + m_btn_remove_material->SetBackgroundColor(wxColour("#F8F8F8")); + m_btn_remove_material->SetBorderColor(wxColour("#EEEEEE")); + m_btn_remove_material->SetTextColor(wxColour("#262E30")); + m_btn_remove_material->SetMinSize(wxSize(-1, FromDIP(24))); + m_btn_remove_material->SetCursor(wxCursor(wxCURSOR_HAND)); + m_btn_remove_material->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { on_remove_material(); }); + m_btn_remove_material->Hide(); + btn_sizer->Add(m_btn_remove_material, 1, 0, 0); + + sizer->Add(btn_sizer, 0, wxEXPAND | wxTOP, FromDIP(9)); + + return sizer; +} + +wxBoxSizer* MixedFilamentDialog::create_ratio_slider() +{ + auto* sizer = new wxBoxSizer(wxVERTICAL); + + auto* ratio_label = new wxStaticText(this, wxID_ANY, _L("Ratio")); + ratio_label->SetForegroundColour(wxColour("#909090")); + ratio_label->SetFont(::Label::Body_12); + sizer->Add(ratio_label, 0, wxBOTTOM, FromDIP(4)); + + m_ratio_bar = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(-1, FromDIP(27))); + m_ratio_bar->SetMinSize(wxSize(-1, FromDIP(27))); + m_ratio_bar->SetBackgroundStyle(wxBG_STYLE_PAINT); + + m_ratio_bar->Bind(wxEVT_PAINT, [this](wxPaintEvent&) { + wxBufferedPaintDC dc(m_ratio_bar); + wxSize sz = m_ratio_bar->GetClientSize(); + + wxColour col_a = comp_colour(0), col_b = comp_colour(1); + + for (int x = 0; x < sz.GetWidth(); ++x) { + double t = (double)x / sz.GetWidth(); + wxColour c = blend_colors(col_a, col_b, 1.0 - t); + dc.SetPen(wxPen(c)); + dc.DrawLine(x, 0, x, sz.GetHeight()); + } + + int div_x = (int)(ratio(1) / 100.0 * sz.GetWidth()); + dc.SetPen(wxPen(StateColor::darkModeColorFor(wxColour(80, 80, 80)), FromDIP(4))); + dc.DrawLine(div_x, 0, div_x, sz.GetHeight()); + dc.SetPen(wxPen(*wxWHITE, FromDIP(2))); + dc.DrawLine(div_x, 0, div_x, sz.GetHeight()); + }); + + m_ratio_bar->Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent& e) { + if (m_ratio_editor_panel && m_ratio_editor_panel->IsShown()) + commit_ratio_editor(true); + m_dragging = true; + m_ratio_bar->CaptureMouse(); + int new_ratio = 100 - (int)(e.GetX() * 100.0 / m_ratio_bar->GetClientSize().GetWidth() + 0.5); + on_ratio_changed(std::max(MIN_COMPONENT_RATIO, std::min(100 - MIN_COMPONENT_RATIO, new_ratio))); + }); + + m_ratio_bar->Bind(wxEVT_MOTION, [this](wxMouseEvent& e) { + if (!m_dragging) return; + int new_ratio = 100 - (int)(e.GetX() * 100.0 / m_ratio_bar->GetClientSize().GetWidth() + 0.5); + on_ratio_changed(std::max(MIN_COMPONENT_RATIO, std::min(100 - MIN_COMPONENT_RATIO, new_ratio))); + }); + + m_ratio_bar->Bind(wxEVT_LEFT_UP, [this](wxMouseEvent&) { + if (m_dragging) { + m_dragging = false; + if (m_ratio_bar->HasCapture()) + m_ratio_bar->ReleaseMouse(); + } + }); + + sizer->Add(m_ratio_bar, 0, wxEXPAND); + + auto* pct_sizer = new wxBoxSizer(wxHORIZONTAL); + m_label_ratio_a = new RatioLabelPanel(this); + m_label_ratio_a->SetLabel(wxString::Format(wxT("%d%%"), ratio(0))); + m_label_ratio_b = new RatioLabelPanel(this); + m_label_ratio_b->SetLabel(wxString::Format(wxT("%d%%"), ratio(1))); + auto bind_ratio_click = [this](RatioLabelPanel* label, size_t idx) { + label->Bind(wxEVT_LEFT_DOWN, [this, label, idx](wxMouseEvent&) { + wxRect rect(wxPoint(0, 0), label->GetClientSize()); + start_ratio_editor(idx, label, rect); + }); + }; + bind_ratio_click(m_label_ratio_a, 0); + bind_ratio_click(m_label_ratio_b, 1); + pct_sizer->Add(m_label_ratio_a, 0); + pct_sizer->AddStretchSpacer(1); + pct_sizer->Add(m_label_ratio_b, 0); + sizer->Add(pct_sizer, 0, wxEXPAND | wxTOP, FromDIP(2)); + + return sizer; +} + +// ---- Triangle (ternary) ratio picker ---- + +// Barycentric coordinate utilities +struct TriPoint { double x, y; }; + +static double tri_signed_area2(TriPoint a, TriPoint b, TriPoint c) +{ + return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); +} + +static bool tri_contains(TriPoint p, TriPoint v0, TriPoint v1, TriPoint v2) +{ + double total = tri_signed_area2(v0, v1, v2); + if (std::abs(total) < 1e-9) return false; + double s0 = tri_signed_area2(p, v1, v2) / total; + double s1 = tri_signed_area2(v0, p, v2) / total; + double s2 = 1.0 - s0 - s1; + return s0 >= -0.001 && s1 >= -0.001 && s2 >= -0.001; +} + +static void tri_barycentric(TriPoint p, TriPoint v0, TriPoint v1, TriPoint v2, + double& w0, double& w1, double& w2) +{ + double total = std::abs(tri_signed_area2(v0, v1, v2)); + if (total < 1e-9) { w0 = w1 = w2 = 1.0 / 3.0; return; } + w0 = std::abs(tri_signed_area2(p, v1, v2)) / total; + w1 = std::abs(tri_signed_area2(v0, p, v2)) / total; + w2 = 1.0 - w0 - w1; + w0 = std::clamp(w0, 0.0, 1.0); + w1 = std::clamp(w1, 0.0, 1.0); + w2 = std::clamp(w2, 0.0, 1.0); + double s = w0 + w1 + w2; + if (s > 0) { w0 /= s; w1 /= s; w2 /= s; } +} + +static TriPoint tri_clamp(TriPoint p, TriPoint v0, TriPoint v1, TriPoint v2) +{ + double w0, w1, w2; + tri_barycentric(p, v0, v1, v2, w0, w1, w2); + w0 = std::clamp(w0, 0.0, 1.0); + w1 = std::clamp(w1, 0.0, 1.0); + w2 = std::clamp(w2, 0.0, 1.0); + double s = w0 + w1 + w2; + if (s > 0) { w0 /= s; w1 /= s; w2 /= s; } + return {w0 * v0.x + w1 * v1.x + w2 * v2.x, + w0 * v0.y + w1 * v1.y + w2 * v2.y}; +} + +wxBoxSizer* MixedFilamentDialog::create_triangle_picker() +{ + auto* sizer = new wxBoxSizer(wxVERTICAL); + + int panel_w = FromDIP(160); + int panel_h = FromDIP(160); + m_triangle_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(panel_w, panel_h)); + m_triangle_panel->SetMinSize(wxSize(panel_w, panel_h)); + m_triangle_panel->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + m_triangle_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + + auto get_vertices = [this]() -> std::tuple { + wxSize sz = m_triangle_panel->GetClientSize(); + double pw = sz.GetWidth(), ph = sz.GetHeight(); + double margin = FromDIP(20); + double avail = std::min(pw, ph) - 2 * margin; + double side = avail; + double tri_h = side * std::sqrt(3.0) / 2.0; + double cx = pw / 2.0; + double top_y = (ph - tri_h) / 2.0; + double bot_y = top_y + tri_h; + TriPoint v0 = {cx, top_y}; // top + TriPoint v1 = {cx - side / 2.0, bot_y}; // bottom-left + TriPoint v2 = {cx + side / 2.0, bot_y}; // bottom-right + return {v0, v1, v2}; + }; + + m_triangle_panel->Bind(wxEVT_PAINT, [this, get_vertices](wxPaintEvent&) { + wxBufferedPaintDC dc(m_triangle_panel); + wxSize sz = m_triangle_panel->GetClientSize(); + auto [v0, v1, v2] = get_vertices(); + + wxColour tri_bg = StateColor::darkModeColorFor(*wxWHITE); + dc.SetBrush(wxBrush(tri_bg)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + wxColour c0 = comp_colour(0), c1 = comp_colour(1), c2 = comp_colour(2); + + const bool cache_valid = m_tri_cache_bmp.IsOk() && + m_tri_cache_size == sz && + m_tri_cache_c0 == c0 && m_tri_cache_c1 == c1 && m_tri_cache_c2 == c2; + + if (!cache_valid) { + int min_y = (int)std::min({v0.y, v1.y, v2.y}); + int max_y = (int)std::max({v0.y, v1.y, v2.y}); + int min_x = (int)std::min({v0.x, v1.x, v2.x}); + int max_x = (int)std::max({v0.x, v1.x, v2.x}); + + m_tri_cache_bmp = wxBitmap(sz.GetWidth(), sz.GetHeight(), 24); + wxMemoryDC mdc(m_tri_cache_bmp); + mdc.SetBrush(wxBrush(tri_bg)); + mdc.SetPen(*wxTRANSPARENT_PEN); + mdc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + for (int py = min_y; py <= max_y; ++py) { + for (int px = min_x; px <= max_x; ++px) { + TriPoint p = {(double)px, (double)py}; + if (!tri_contains(p, v0, v1, v2)) continue; + double w0, w1, w2; + tri_barycentric(p, v0, v1, v2, w0, w1, w2); + unsigned char mr, mg, mb; + if (w0 + w1 > 1e-6) { + float t01 = static_cast(w1 / (w0 + w1)); + Slic3r::filament_mixer_lerp(c0.Red(), c0.Green(), c0.Blue(), + c1.Red(), c1.Green(), c1.Blue(), + t01, &mr, &mg, &mb); + float t2 = static_cast(w2); + Slic3r::filament_mixer_lerp(mr, mg, mb, + c2.Red(), c2.Green(), c2.Blue(), + t2, &mr, &mg, &mb); + } else { + mr = c2.Red(); mg = c2.Green(); mb = c2.Blue(); + } + mdc.SetPen(wxPen(wxColour(mr, mg, mb))); + mdc.DrawPoint(px, py); + } + } + + mdc.SetPen(wxPen(StateColor::darkModeColorFor(wxColour("#CECECE")), 1)); + mdc.SetBrush(*wxTRANSPARENT_BRUSH); + wxPoint pts[3] = {{(int)v0.x, (int)v0.y}, {(int)v1.x, (int)v1.y}, {(int)v2.x, (int)v2.y}}; + mdc.DrawPolygon(3, pts); + + mdc.SelectObject(wxNullBitmap); + m_tri_cache_c0 = c0; m_tri_cache_c1 = c1; m_tri_cache_c2 = c2; + m_tri_cache_size = sz; + } + + dc.DrawBitmap(m_tri_cache_bmp, 0, 0); + + // Drag handle (always redrawn on top of cached bitmap) + double hx = m_tri_wx * v0.x + m_tri_wy * v1.x + m_tri_wz * v2.x; + double hy = m_tri_wx * v0.y + m_tri_wy * v1.y + m_tri_wz * v2.y; + int handle_r = FromDIP(5); + dc.SetBrush(*wxWHITE_BRUSH); + dc.SetPen(wxPen(wxColour("#262E30"), FromDIP(2))); + dc.DrawCircle((int)hx, (int)hy, handle_r); + + if (m_result.ratios.size() >= 3) { + dc.SetFont(::Label::Body_10); + wxSize ts0 = dc.GetTextExtent(wxString::Format(wxT("%d%%"), m_result.ratios[0])); + int top_label_y = std::max(0, (int)(v0.y - ts0.GetHeight() - FromDIP(4))); + + dc.SetFont(::Label::Body_12); + dc.SetTextForeground(wxColour("#909090")); + dc.DrawText(_L("Ratio"), FromDIP(2), top_label_y); + + // Position the real RatioLabelPanel children + for (int i = 0; i < 3 && i < (int)m_triangle_ratio_labels.size(); ++i) { + if (!m_triangle_ratio_labels[i]) continue; + m_triangle_ratio_labels[i]->SetLabel( + wxString::Format(wxT("%d%%"), m_result.ratios[i])); + wxSize lsz = m_triangle_ratio_labels[i]->GetMinSize(); + int lx = 0, ly = 0; + if (i == 0) { + lx = (int)(v0.x - lsz.GetWidth() / 2); + ly = top_label_y; + } else if (i == 1) { + lx = (int)(v1.x - lsz.GetWidth() / 2); + ly = (int)(v1.y + FromDIP(3)); + } else { + lx = (int)(v2.x - lsz.GetWidth() / 2); + ly = (int)(v2.y + FromDIP(3)); + } + m_triangle_ratio_labels[i]->SetSize(lx, ly, lsz.GetWidth(), lsz.GetHeight()); + } + } + }); + + auto handle_mouse = [this, get_vertices](wxMouseEvent& e, bool is_down) { + auto [v0, v1, v2] = get_vertices(); + TriPoint p = {(double)e.GetX(), (double)e.GetY()}; + + if (is_down) { + // Only start dragging when the press lands inside the triangle; + // clicks outside the triangle must not change the mix ratio. + if (!tri_contains(p, v0, v1, v2)) + return; + m_dragging = true; + m_triangle_panel->CaptureMouse(); + } + + if (!m_dragging) return; + + TriPoint clamped = tri_clamp(p, v0, v1, v2); + tri_barycentric(clamped, v0, v1, v2, m_tri_wx, m_tri_wy, m_tri_wz); + + int r0 = (int)(m_tri_wx * 100 + 0.5); + int r1 = (int)(m_tri_wy * 100 + 0.5); + int r2 = 100 - r0 - r1; + r0 = std::clamp(r0, 0, 100); + r1 = std::clamp(r1, 0, 100); + r2 = std::clamp(r2, 0, 100); + + apply_dragged_triangle_ratio(r0, r1, r2); + }; + + // Create 3 RatioLabelPanel children on the triangle panel + m_triangle_ratio_labels.fill(nullptr); + for (int i = 0; i < 3; ++i) { + auto* lbl = new RatioLabelPanel(m_triangle_panel); + lbl->SetLabel(wxString::Format(wxT("%d%%"), + (i < (int)m_result.ratios.size()) ? m_result.ratios[i] : 33)); + size_t idx = (size_t)i; + lbl->Bind(wxEVT_LEFT_DOWN, [this, lbl, idx](wxMouseEvent&) { + wxRect rect(wxPoint(0, 0), lbl->GetClientSize()); + start_ratio_editor(idx, lbl, rect); + }); + m_triangle_ratio_labels[i] = lbl; + } + + m_triangle_panel->Bind(wxEVT_LEFT_DOWN, [this, handle_mouse](wxMouseEvent& e) { + if (m_ratio_editor_panel && m_ratio_editor_panel->IsShown()) + commit_ratio_editor(true); + handle_mouse(e, true); + }); + m_triangle_panel->Bind(wxEVT_MOTION, [this, handle_mouse](wxMouseEvent& e) { + if (m_dragging) + handle_mouse(e, false); + }); + m_triangle_panel->Bind(wxEVT_LEFT_UP, [this](wxMouseEvent&) { + if (m_dragging) { + m_dragging = false; + if (m_triangle_panel->HasCapture()) + m_triangle_panel->ReleaseMouse(); + } + }); + + sizer->Add(m_triangle_panel, 0); + + return sizer; +} + +wxBoxSizer* MixedFilamentDialog::create_gradient_section() +{ + m_gradient_sizer = new wxBoxSizer(wxHORIZONTAL); + + m_chk_gradient = new ::CheckBox(this); + m_chk_gradient->SetValue(m_result.gradient_enabled); + m_chk_gradient->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent& e) { e.Skip(); on_gradient_toggled(); }); + m_gradient_sizer->Add(m_chk_gradient, 0, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, FromDIP(4)); + + m_label_gradient = new wxStaticText(this, wxID_ANY, _L("Gradient Effect")); + m_label_gradient->SetFont(::Label::Body_13); + m_gradient_sizer->Add(m_label_gradient, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(8)); + + m_combo_gradient_dir = new ComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, + wxSize(FromDIP(152), FromDIP(24)), 0, nullptr, wxCB_READONLY); + m_combo_gradient_dir->SetKeepDropArrow(true); + update_gradient_direction_items(); + m_combo_gradient_dir->SetSelection(m_result.gradient_direction); + m_combo_gradient_dir->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent&) { on_gradient_direction_changed(); }); + m_combo_gradient_dir->Show(m_result.gradient_enabled); + + m_gradient_sizer->Add(m_combo_gradient_dir, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(8)); + + auto* outer = new wxBoxSizer(wxVERTICAL); + outer->Add(m_gradient_sizer, 0, wxEXPAND); + + // Custom curve editor: visible only when gradient is on and exactly 2 components are mixed. + m_curve_sizer = new wxBoxSizer(wxVERTICAL); + m_curve_editor = new GradientCurveEditor(this, comp_colour(0), comp_colour(1)); + if (!m_result.gradient_curve.empty()) + m_curve_editor->set_points(m_result.gradient_curve); + else + m_curve_editor->reset_to_linear((m_result.gradient_direction == 0) ? 0.9 : 0.1, + (m_result.gradient_direction == 0) ? 0.1 : 0.9); + m_curve_editor->Bind(wxEVT_GRADIENT_CURVE_CHANGED, + [this](wxCommandEvent&) { on_gradient_curve_changed(); }); + m_curve_sizer->Add(m_curve_editor, 0, wxEXPAND | wxTOP, FromDIP(4)); + + outer->Add(m_curve_sizer, 0, wxEXPAND | wxTOP, FromDIP(6)); + const bool curve_visible = m_result.gradient_enabled && num_components() == 2; + m_curve_sizer->ShowItems(curve_visible); + + // Per-part gradient toggle sits BELOW the curve editor. + m_per_part_gradient_sizer = new wxBoxSizer(wxHORIZONTAL); + + m_chk_per_part_gradient = new ::CheckBox(this); + m_chk_per_part_gradient->SetValue(m_result.per_part_gradient); + m_chk_per_part_gradient->Bind(wxEVT_TOGGLEBUTTON, + [this](wxCommandEvent& e) { e.Skip(); on_per_part_gradient_toggled(); }); + m_per_part_gradient_sizer->Add(m_chk_per_part_gradient, 0, + wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM, FromDIP(4)); + + m_label_per_part_gradient = new wxStaticText(this, wxID_ANY, _L("Enable per-part gradient effect")); + m_label_per_part_gradient->SetFont(::Label::Body_13); + m_per_part_gradient_sizer->Add(m_label_per_part_gradient, 0, + wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(8)); + + outer->Add(m_per_part_gradient_sizer, 0, wxEXPAND | wxTOP, FromDIP(2)); + m_per_part_gradient_sizer->ShowItems(m_result.gradient_enabled); + + return outer; +} + +wxBoxSizer* MixedFilamentDialog::create_recommendation_grid() +{ + auto* outer = new wxBoxSizer(wxVERTICAL); + + auto* title_sizer = new wxBoxSizer(wxHORIZONTAL); + auto* rec_label = new wxStaticText(this, wxID_ANY, _L("Mixing Recommendations")); + rec_label->SetForegroundColour(StateColor::darkModeColorFor(wxColour("#ACACAC"))); + rec_label->SetFont(::Label::Body_10); + title_sizer->Add(rec_label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(4)); + + auto* rec_line = new wxPanel(this, wxID_ANY); + rec_line->SetMinSize(wxSize(-1, 1)); + rec_line->SetBackgroundColour(StateColor::darkModeColorFor(wxColour("#DFDFDF"))); + title_sizer->Add(rec_line, 1, wxALIGN_CENTER_VERTICAL); + + outer->Add(title_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(4)); + + m_recommendation_scroll = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, FromDIP(116))); + m_recommendation_scroll->SetScrollRate(0, 5); + m_recommendation_scroll->SetBackgroundColour(StateColor::darkModeColorFor(wxColour("#F8F8F8"))); + + m_recommendation_grid = new wxWrapSizer(wxHORIZONTAL, wxWRAPSIZER_DEFAULT_FLAGS); + auto* scroll_inner_sizer = new wxBoxSizer(wxVERTICAL); + scroll_inner_sizer->Add(m_recommendation_grid, 1, wxEXPAND | wxLEFT | wxTOP, FromDIP(8)); + m_recommendation_scroll->SetSizer(scroll_inner_sizer); + + rebuild_recommendation_items(); + + outer->Add(m_recommendation_scroll, 1, wxEXPAND | wxTOP, FromDIP(4)); + return outer; +} + +void MixedFilamentDialog::rebuild_recommendation_items() +{ + if (!m_recommendation_scroll || !m_recommendation_grid) + return; + + static constexpr int MAX_RECOMMENDATIONS = 100; + + m_recommendation_scroll->Freeze(); + m_recommendation_grid->Clear(true); + + size_t n = m_physical_colors.size(); + int count = 0; + + // Group physical filaments by type (only same-type combos are recommended) + std::map> type_groups; + for (size_t i = 0; i < n; ++i) { + std::string t = (i < m_physical_types.size()) ? m_physical_types[i] : "PLA"; + // Skip support filaments (type ends with "-S") + if (t.size() >= 2 && t.compare(t.size() - 2, 2, "-S") == 0) + continue; + type_groups[t].push_back(i); + } + + if (num_components() >= 3) { + // Three-color: C(g,3) x 3 variants per same-type group + for (auto& [type, indices] : type_groups) { + if (count >= MAX_RECOMMENDATIONS) break; + size_t g = indices.size(); + for (size_t ai = 0; ai < g && count < MAX_RECOMMENDATIONS; ++ai) { + for (size_t bi = ai + 1; bi < g && count < MAX_RECOMMENDATIONS; ++bi) { + for (size_t ci = bi + 1; ci < g && count < MAX_RECOMMENDATIONS; ++ci) { + size_t idx[3] = {indices[ai], indices[bi], indices[ci]}; + // 3 variants: each filament takes the 50% role in turn + for (int dominant = 0; dominant < 3 && count < MAX_RECOMMENDATIONS; ++dominant) { + size_t i0 = idx[(dominant + 1) % 3]; // 25% + size_t i1 = idx[(dominant + 2) % 3]; // 25% + size_t i2 = idx[dominant]; // 50% + + wxColour ca(m_physical_colors[i0]); + wxColour cb(m_physical_colors[i1]); + wxColour cc(m_physical_colors[i2]); + wxColour mixed = blend_n_colors({ca, cb, cc}, {0.25, 0.25, 0.50}); + + auto* item = new wxPanel(m_recommendation_scroll, wxID_ANY, + wxDefaultPosition, wxSize(FromDIP(20), FromDIP(20))); + item->SetBackgroundColour(mixed); + item->SetCursor(wxCursor(wxCURSOR_HAND)); + + unsigned int ca_1 = (unsigned int)(i0 + 1); + unsigned int cb_1 = (unsigned int)(i1 + 1); + unsigned int cc_1 = (unsigned int)(i2 + 1); + item->Bind(wxEVT_LEFT_UP, [this, ca_1, cb_1, cc_1](wxMouseEvent&) { + on_recommendation_clicked_triple(ca_1, cb_1, cc_1); + }); + item->SetToolTip(wxString::Format(wxT("%s + %s + %s"), + wxString::FromUTF8(m_physical_names[i0]), + wxString::FromUTF8(m_physical_names[i1]), + wxString::FromUTF8(m_physical_names[i2]))); + + m_recommendation_grid->Add(item, 0, wxRIGHT | wxBOTTOM, FromDIP(6)); + ++count; + } + } + } + } + } + } else { + // Two-color: C(g,2) per same-type group + for (auto& [type, indices] : type_groups) { + if (count >= MAX_RECOMMENDATIONS) break; + size_t g = indices.size(); + for (size_t ai = 0; ai < g && count < MAX_RECOMMENDATIONS; ++ai) { + for (size_t bi = ai + 1; bi < g && count < MAX_RECOMMENDATIONS; ++bi) { + size_t i = indices[ai]; + size_t j = indices[bi]; + + wxColour ca(m_physical_colors[i]); + wxColour cb(m_physical_colors[j]); + wxColour mixed = blend_colors(ca, cb, 0.5); + + auto* item = new wxPanel(m_recommendation_scroll, wxID_ANY, + wxDefaultPosition, wxSize(FromDIP(20), FromDIP(20))); + item->SetBackgroundColour(mixed); + item->SetCursor(wxCursor(wxCURSOR_HAND)); + + unsigned int comp_a = (unsigned int)(i + 1); + unsigned int comp_b = (unsigned int)(j + 1); + item->Bind(wxEVT_LEFT_UP, [this, comp_a, comp_b](wxMouseEvent&) { + on_recommendation_clicked(comp_a, comp_b); + }); + item->SetToolTip(wxString::Format(wxT("%s + %s"), + wxString::FromUTF8(m_physical_names[i]), + wxString::FromUTF8(m_physical_names[j]))); + + m_recommendation_grid->Add(item, 0, wxRIGHT | wxBOTTOM, FromDIP(6)); + ++count; + } + } + } + } + + m_recommendation_scroll->SetScrollbars(0, FromDIP(20), 0, 1); + m_recommendation_scroll->FitInside(); + m_recommendation_scroll->Layout(); + m_recommendation_scroll->Thaw(); +} + +wxBoxSizer* MixedFilamentDialog::create_button_panel() +{ + auto* sizer = new wxBoxSizer(wxHORIZONTAL); + + m_btn_cancel = new Button(this, _L("Cancel")); + m_btn_cancel->SetBackgroundColor(*wxWHITE); + m_btn_cancel->SetBorderColor(wxColour("#CECECE")); + m_btn_cancel->SetTextColor(wxColour("#262E30")); + m_btn_cancel->SetMinSize(wxSize(FromDIP(55), FromDIP(24))); + m_btn_cancel->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { EndModal(wxID_CANCEL); }); + + m_btn_ok = new Button(this, _L("OK")); + m_btn_ok->SetBackgroundColor(wxColour("#00AE42")); + m_btn_ok->SetBorderColor(wxColour("#00AE42")); + m_btn_ok->SetTextColor(*wxWHITE); + m_btn_ok->SetMinSize(wxSize(FromDIP(55), FromDIP(24))); + m_btn_ok->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { EndModal(wxID_OK); }); + + sizer->Add(m_btn_cancel, 0, wxRIGHT, FromDIP(12)); + sizer->Add(m_btn_ok, 0); + + return sizer; +} + +void MixedFilamentDialog::rebuild_all_combos() +{ + m_combo_to_physical.resize(m_combo_filaments.size()); + + for (size_t i = 0; i < m_combo_filaments.size(); ++i) { + std::set others_selected; + std::set others_types; + for (size_t k = 0; k < m_result.components.size(); ++k) { + if (k == i) continue; + unsigned int phys = m_result.components[k]; + others_selected.insert(phys); + if (phys >= 1 && phys <= m_physical_types.size()) + others_types.insert(m_physical_types[phys - 1]); + } + + auto* combo = m_combo_filaments[i]; + combo->Clear(); + m_combo_to_physical[i].clear(); + + int restore_sel = -1; + unsigned int cur_phys = (i < m_result.components.size()) ? m_result.components[i] : 0; + + if (cur_phys == 0) { + combo->Append(_L("-- Select --")); + m_combo_to_physical[i].push_back(0); + restore_sel = 0; + } + + for (size_t j = 0; j < m_physical_names.size(); ++j) { + unsigned int phys_1based = (unsigned int)(j + 1); + + if (others_selected.count(phys_1based)) + continue; + + int style = 0; + if (!others_types.empty() && !m_physical_types.empty()) { + std::string this_type = (j < m_physical_types.size()) ? m_physical_types[j] : "PLA"; + if (others_types.find(this_type) == others_types.end()) + style = DD_ITEM_STYLE_DIMMED; + } + + int idx = combo->Append(wxString::FromUTF8(m_physical_names[j]), make_swatch_bitmap(j), style); + m_combo_to_physical[i].push_back(phys_1based); + + if (phys_1based == cur_phys) + restore_sel = idx; + } + + if (restore_sel >= 0) + combo->SetSelection(restore_sel); + else if (combo->GetCount() > 0) + combo->SetSelection(0); + } +} + +void MixedFilamentDialog::refresh_curve_editor_colors() +{ + if (m_curve_editor) + m_curve_editor->set_colors(comp_colour(0), comp_colour(1)); +} + +// ---- Event Handlers ---- + +void MixedFilamentDialog::on_filament_changed() +{ + for (size_t i = 0; i < m_combo_filaments.size() && i < m_result.components.size(); ++i) { + int sel = m_combo_filaments[i]->GetSelection(); + if (sel >= 0 && i < m_combo_to_physical.size() && sel < (int)m_combo_to_physical[i].size()) + m_result.components[i] = m_combo_to_physical[i][sel]; + } + + refresh_curve_editor_colors(); + rebuild_all_combos(); + update_gradient_direction_items(); + update_preview(); + update_ok_button_state(); +} + +void MixedFilamentDialog::on_ratio_changed(int new_ratio_a) +{ + if (m_result.ratios.size() < 2) return; + m_result.ratios[0] = new_ratio_a; + m_result.ratios[1] = 100 - new_ratio_a; + + reset_manual_ratio_state(); + refresh_ratio_labels(); + + update_preview(); +} + +void MixedFilamentDialog::on_gradient_toggled() +{ + bool checked = m_chk_gradient->GetValue(); + + if (checked) { + auto& print_config = wxGetApp().preset_bundle->prints.get_edited_preset().config; + if (!print_config.opt_bool("enable_mixed_color_sublayer")) { + wxMessageDialog dlg(this, + _L("Gradient effect requires 'Mixed color sublayer' to be enabled. Enable it now?"), + _L("Mixed Color Sublayer"), + wxYES_NO | wxICON_QUESTION); + if (dlg.ShowModal() == wxID_YES) { + DynamicPrintConfig new_conf; + new_conf.set_key_value("enable_mixed_color_sublayer", new ConfigOptionBool(true)); + wxGetApp().get_tab(Preset::TYPE_PRINT)->load_config(new_conf); + } else { + m_chk_gradient->SetValue(false); + return; + } + } + } + + m_result.gradient_enabled = m_chk_gradient->GetValue(); + + if (m_ratio_sizer) + m_ratio_sizer->ShowItems(!m_result.gradient_enabled && num_components() == 2); + if (m_combo_gradient_dir) + m_combo_gradient_dir->Show(m_result.gradient_enabled); + if (m_per_part_gradient_sizer) + m_per_part_gradient_sizer->ShowItems(m_result.gradient_enabled); + if (m_curve_sizer) + m_curve_sizer->ShowItems(m_result.gradient_enabled && num_components() == 2); + if (!m_result.gradient_enabled) { + m_result.per_part_gradient = false; + if (m_chk_per_part_gradient) m_chk_per_part_gradient->SetValue(false); + } + + // Toggling the curve editor changes the right column height (and width when + // turning gradient on), so the dialog must follow or the recommendation list + // gets squeezed off-screen. Same trick as 2-color -> 3-color switching. + const wxSize new_size = compute_dialog_size(); + if (GetSize() != new_size) { + const wxRect old_rect = GetRect(); + const wxPoint center(old_rect.x + old_rect.width / 2, + old_rect.y + old_rect.height / 2); + SetSize(new_size); + SetPosition(wxPoint(center.x - new_size.x / 2, + center.y - new_size.y / 2)); + } + + Layout(); + Refresh(); +} + +void MixedFilamentDialog::on_gradient_direction_changed() +{ + if (!m_combo_gradient_dir) return; + m_result.gradient_direction = m_combo_gradient_dir->GetSelection(); + + // Mirror the user's custom curve around y=0.5 instead of resetting it, so + // shape work (added anchors, bent segments) survives a direction toggle. + // reverse() flips y and tangent signs consistently; default two-point + // linear curves end up matching the new direction exactly (0.9->0.1 <-> 0.1->0.9). + if (m_curve_editor) { + m_curve_editor->reverse(); + m_result.gradient_curve = m_curve_editor->get_points(); + } + update_preview(); +} + +void MixedFilamentDialog::on_gradient_curve_changed() +{ + if (m_curve_editor) + m_result.gradient_curve = m_curve_editor->get_points(); + update_preview(); +} + +void MixedFilamentDialog::on_per_part_gradient_toggled() +{ + if (m_chk_per_part_gradient) + m_result.per_part_gradient = m_chk_per_part_gradient->GetValue(); +} + +void MixedFilamentDialog::on_add_material() +{ + size_t n = num_components(); + if (n >= (size_t)MAX_COMPONENTS) return; + + unsigned int new_comp = 0; + for (size_t j = 0; j < m_physical_names.size(); ++j) { + unsigned int candidate = (unsigned int)(j + 1); + bool used = false; + for (auto c : m_result.components) + if (c == candidate) { used = true; break; } + if (!used) { new_comp = candidate; break; } + } + if (new_comp == 0) return; + m_result.components.push_back(new_comp); + + int each = 100 / (int)(n + 1); + m_result.ratios.clear(); + int assigned = 0; + for (size_t i = 0; i < n; ++i) { + m_result.ratios.push_back(each); + assigned += each; + } + m_result.ratios.push_back(100 - assigned); + + if (m_result.ratios.size() >= 3) { + int sum = 0; + for (int r : m_result.ratios) sum += r; + if (sum > 0) { + m_tri_wx = (double)m_result.ratios[0] / sum; + m_tri_wy = (double)m_result.ratios[1] / sum; + m_tri_wz = (double)m_result.ratios[2] / sum; + } + } + reset_manual_ratio_state(); + + auto* row = new wxBoxSizer(wxHORIZONTAL); + wxString lbl_text = wxString::Format(_L("Filament %d"), (int)(n + 1)); + auto* lbl = new wxStaticText(this, wxID_ANY, lbl_text); + lbl->SetFont(::Label::Body_12); + row->Add(lbl, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(8)); + + auto* combo = new ComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, + wxSize(FromDIP(166), FromDIP(24)), 0, nullptr, wxCB_READONLY); + combo->SetKeepDropArrow(true); + combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent&) { on_filament_changed(); }); + row->Add(combo, 1, wxALIGN_CENTER_VERTICAL); + + m_combo_filaments.push_back(combo); + m_combo_to_physical.push_back({}); + m_material_rows_sizer->Add(row, 0, wxEXPAND | wxTOP, FromDIP(9)); + + rebuild_all_combos(); + refresh_curve_editor_colors(); + update_component_count_ui(); + update_preview(); + update_ok_button_state(); + rebuild_recommendation_items(); + + Layout(); + Refresh(); +} + +void MixedFilamentDialog::on_remove_material() +{ + if (num_components() <= 2) + return; + + m_result.components.resize(2); + m_result.ratios = {50, 50}; + m_tri_wx = 0.5; + m_tri_wy = 0.5; + m_tri_wz = 0.0; + + reset_manual_ratio_state(); + refresh_ratio_labels(); + + if (m_material_rows_sizer && m_material_rows_sizer->GetItemCount() > 2) { + auto* sizer_item = m_material_rows_sizer->GetItem(m_material_rows_sizer->GetItemCount() - 1); + if (sizer_item && sizer_item->GetSizer()) + sizer_item->GetSizer()->Clear(true); + m_material_rows_sizer->Remove(m_material_rows_sizer->GetItemCount() - 1); + } + + if (m_combo_filaments.size() > 2) + m_combo_filaments.pop_back(); + if (m_combo_to_physical.size() > 2) + m_combo_to_physical.pop_back(); + + rebuild_all_combos(); + refresh_curve_editor_colors(); + update_component_count_ui(); + update_preview(); + update_ok_button_state(); + rebuild_recommendation_items(); + Layout(); + Refresh(); +} + +void MixedFilamentDialog::on_recommendation_clicked(unsigned int comp_a, unsigned int comp_b) +{ + while (m_material_rows_sizer->GetItemCount() > 2) { + auto* sizer_item = m_material_rows_sizer->GetItem(m_material_rows_sizer->GetItemCount() - 1); + if (sizer_item && sizer_item->GetSizer()) + sizer_item->GetSizer()->Clear(true); + m_material_rows_sizer->Remove(m_material_rows_sizer->GetItemCount() - 1); + } + + while (m_combo_filaments.size() > 2) + m_combo_filaments.pop_back(); + while (m_combo_to_physical.size() > 2) + m_combo_to_physical.pop_back(); + + m_result.components = {comp_a, comp_b}; + m_result.ratios = {50, 50}; + + reset_manual_ratio_state(); + refresh_ratio_labels(); + + rebuild_all_combos(); + refresh_curve_editor_colors(); + update_gradient_direction_items(); + update_component_count_ui(); + update_preview(); + update_ok_button_state(); + Layout(); + Refresh(); +} + +void MixedFilamentDialog::on_recommendation_clicked_triple(unsigned int a, unsigned int b, unsigned int c) +{ + // Ensure we have exactly 3 combo rows + if (num_components() < 3) { + // Need to add a 3rd combo row + while (m_combo_filaments.size() < 3) { + size_t idx = m_combo_filaments.size(); + auto* row = new wxBoxSizer(wxHORIZONTAL); + wxString lbl_text = wxString::Format(_L("Filament %d"), (int)(idx + 1)); + auto* lbl = new wxStaticText(this, wxID_ANY, lbl_text); + lbl->SetFont(::Label::Body_12); + row->Add(lbl, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(8)); + + auto* combo = new ComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, + wxSize(FromDIP(166), FromDIP(24)), 0, nullptr, wxCB_READONLY); + combo->SetKeepDropArrow(true); + combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent&) { on_filament_changed(); }); + row->Add(combo, 1, wxALIGN_CENTER_VERTICAL); + + m_combo_filaments.push_back(combo); + m_combo_to_physical.push_back({}); + m_material_rows_sizer->Add(row, 0, wxEXPAND | wxTOP, FromDIP(9)); + } + } else if (num_components() > 3) { + while (m_material_rows_sizer->GetItemCount() > 3) { + auto* sizer_item = m_material_rows_sizer->GetItem(m_material_rows_sizer->GetItemCount() - 1); + if (sizer_item && sizer_item->GetSizer()) + sizer_item->GetSizer()->Clear(true); + m_material_rows_sizer->Remove(m_material_rows_sizer->GetItemCount() - 1); + } + while (m_combo_filaments.size() > 3) + m_combo_filaments.pop_back(); + while (m_combo_to_physical.size() > 3) + m_combo_to_physical.pop_back(); + } + + m_result.components = {a, b, c}; + m_result.ratios = {25, 25, 50}; + m_tri_wx = 0.25; + m_tri_wy = 0.25; + m_tri_wz = 0.50; + reset_manual_ratio_state(); + + rebuild_all_combos(); + refresh_curve_editor_colors(); + update_gradient_direction_items(); + update_component_count_ui(); + update_preview(); + update_ok_button_state(); + Layout(); + Refresh(); +} + +void MixedFilamentDialog::update_preview() +{ + if (m_preview_canvas) m_preview_canvas->Refresh(); + if (m_summary_panel) m_summary_panel->Refresh(); + if (m_ratio_bar) m_ratio_bar->Refresh(); + if (m_triangle_panel) m_triangle_panel->Refresh(); +} + +void MixedFilamentDialog::paint_warning_panel(wxPaintEvent&) +{ + wxBufferedPaintDC dc(m_warning_panel); + wxSize sz = m_warning_panel->GetClientSize(); + + dc.SetBrush(wxBrush(StateColor::darkModeColorFor(*wxWHITE))); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + dc.SetBrush(wxBrush(wxColour(255, 245, 245))); + dc.SetPen(wxPen(wxColour("#E84C4C"), 1)); + dc.DrawRoundedRectangle(0, 0, sz.GetWidth(), sz.GetHeight(), FromDIP(4)); + + int x = FromDIP(10); + int cy = sz.GetHeight() / 2; + + int icon_r = FromDIP(7); + dc.SetBrush(wxBrush(wxColour("#E84C4C"))); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawCircle(x + icon_r, cy, icon_r); + dc.SetFont(::Label::Body_10); + dc.SetTextForeground(*wxWHITE); + wxSize ex = dc.GetTextExtent(wxT("!")); + dc.DrawText(wxT("!"), x + icon_r - ex.GetWidth() / 2, cy - ex.GetHeight() / 2); + x += icon_r * 2 + FromDIP(6); + + if (m_type_mismatch_msg.empty()) return; + + dc.SetFont(::Label::Body_12); + dc.SetTextForeground(wxColour("#E84C4C")); + wxString msg = m_type_mismatch_msg; + int avail_w = sz.GetWidth() - x - FromDIP(10); + wxSize ts = dc.GetTextExtent(msg); + if (ts.GetWidth() <= avail_w) { + dc.DrawText(msg, x, cy - ts.GetHeight() / 2); + } else { + wxArrayString lines; + wxString cur_line; + wxArrayString words; + wxStringTokenizer tkz(msg, wxT(" "), wxTOKEN_RET_EMPTY_ALL); + while (tkz.HasMoreTokens()) words.Add(tkz.GetNextToken()); + if (words.empty()) words.Add(msg); + for (size_t w = 0; w < words.size(); ++w) { + wxString test = cur_line.empty() ? words[w] : cur_line + wxT(" ") + words[w]; + if (dc.GetTextExtent(test).GetWidth() > avail_w && !cur_line.empty()) { + lines.Add(cur_line); + cur_line = words[w]; + } else { + cur_line = test; + } + } + if (!cur_line.empty()) lines.Add(cur_line); + if (lines.empty()) lines.Add(msg); + int line_h = dc.GetTextExtent(wxT("Mg")).GetHeight(); + int total_h = (int)lines.size() * line_h; + int y0 = (sz.GetHeight() - total_h) / 2; + for (size_t l = 0; l < lines.size(); ++l) + dc.DrawText(lines[l], x, y0 + (int)l * line_h); + } +} + +void MixedFilamentDialog::update_ok_button_state() +{ + if (!m_btn_ok) return; + + bool has_type_mismatch = false; + if (!m_physical_types.empty() && m_result.components.size() >= 2) { + std::map> type_groups; + for (size_t i = 0; i < m_result.components.size(); ++i) { + unsigned int phys = m_result.components[i]; + if (phys < 1 || phys > m_physical_types.size()) continue; + type_groups[m_physical_types[phys - 1]].push_back(phys); + } + has_type_mismatch = type_groups.size() > 1; + if (has_type_mismatch) { + wxString parts; + for (auto it = type_groups.begin(); it != type_groups.end(); ++it) { + if (!parts.empty()) + parts += _L(" and "); + wxString slots; + for (size_t j = 0; j < it->second.size(); ++j) { + if (!slots.empty()) slots += ", "; + slots += std::to_string(it->second[j]); + } + parts += wxString::Format(_L("Slot %s (%s)"), slots, wxString::FromUTF8(it->first)); + } + m_type_mismatch_msg = parts + " " + _L("cannot be mixed. Please select the same filament type."); + } + } + + bool has_unselected = false; + for (unsigned int c : m_result.components) { + if (c == 0) { has_unselected = true; break; } + } + + bool can_confirm = !has_type_mismatch && !has_unselected; + m_btn_ok->Enable(can_confirm); + if (has_unselected) { + m_btn_ok->SetBackgroundColor(wxColour("#CECECE")); + m_btn_ok->SetBorderColor(wxColour("#CECECE")); + m_btn_ok->SetToolTip(_L("Please select a filament for all components")); + } else if (has_type_mismatch) { + m_btn_ok->SetBackgroundColor(wxColour("#CECECE")); + m_btn_ok->SetBorderColor(wxColour("#CECECE")); + m_btn_ok->SetToolTip(_L("Cannot mix different filament types")); + } else { + m_btn_ok->SetBackgroundColor(wxColour("#00AE42")); + m_btn_ok->SetBorderColor(wxColour("#00AE42")); + m_btn_ok->SetToolTip(wxEmptyString); + } + + if (m_warning_panel) { + m_warning_panel->Show(has_type_mismatch); + Layout(); + } +} + +void MixedFilamentDialog::update_gradient_direction_items() +{ + if (!m_combo_gradient_dir) return; + + int prev_sel = m_combo_gradient_dir->GetSelection(); + m_combo_gradient_dir->Clear(); + + if (num_components() < 2) return; + + auto make_direction_bitmap = [this](size_t idx_from, size_t idx_to) -> wxBitmap { + int swatch_sz = FromDIP(20); + int arrow_w = FromDIP(16); + int gap = FromDIP(4); + int bmp_w = swatch_sz + gap + arrow_w + gap + swatch_sz; + int bmp_h = swatch_sz; + + wxColour dir_text = StateColor::darkModeColorFor(wxColour("#262E30")); + + return make_alpha_bitmap(bmp_w, bmp_h, [&](wxDC& dc) { + dc.SetFont(::Label::Body_13); + + auto draw_swatch = [&](int x, size_t idx) { + std::string color_hex = "#D9D9D9"; + if (idx < m_physical_colors.size()) + color_hex = m_physical_colors[idx]; + std::string label = std::to_string(idx + 1); + wxBitmap* icon = get_extruder_color_icon(color_hex, label, swatch_sz, swatch_sz); + if (icon && icon->IsOk()) + dc.DrawBitmap(*icon, x, 0); + }; + + int x = 0; + draw_swatch(x, idx_from); + x += swatch_sz + gap; + + dc.SetTextForeground(dir_text); + wxString arrow = wxT("\u2192"); + wxSize arrow_sz = dc.GetTextExtent(arrow); + dc.DrawText(arrow, x + (arrow_w - arrow_sz.GetWidth()) / 2, + (bmp_h - arrow_sz.GetHeight()) / 2); + x += arrow_w + gap; + + draw_swatch(x, idx_to); + }); + }; + + size_t idx_a = (comp(0) >= 1) ? comp(0) - 1 : 0; + size_t idx_b = (comp(1) >= 1) ? comp(1) - 1 : 1; + + m_combo_gradient_dir->Append(wxT(" "), make_direction_bitmap(idx_a, idx_b)); + m_combo_gradient_dir->Append(wxT(" "), make_direction_bitmap(idx_b, idx_a)); + + if (prev_sel >= 0 && prev_sel < (int)m_combo_gradient_dir->GetCount()) + m_combo_gradient_dir->SetSelection(prev_sel); + else + m_combo_gradient_dir->SetSelection(0); +} + +wxSize MixedFilamentDialog::compute_dialog_size() const +{ + const bool is_three = (num_components() >= 3); + const bool curve_visible = !is_three && m_result.gradient_enabled; + + int w = FromDIP(439); + int h = FromDIP(580); + if (is_three) { + h = FromDIP(680); + } else if (curve_visible) { + // Wider so the gradient editor can show "Material Ratio" intact; + // +40 over the 3-color height to fit the curve editor while keeping the + // recommendation list visible (it can still scroll if needed). + w = FromDIP(470); + h = FromDIP(720); + } + return wxSize(w, h); +} + +void MixedFilamentDialog::update_component_count_ui() +{ + bool is_two = (num_components() == 2); + bool is_three = (num_components() >= 3); + + // Toggle ratio slider vs triangle picker + if (m_ratio_sizer) + m_ratio_sizer->ShowItems(is_two && !m_result.gradient_enabled); + if (m_triangle_sizer) + m_triangle_sizer->ShowItems(is_three); + + // 3-color: hide gradient entirely, force off + if (m_gradient_sizer) { + bool show_gradient = is_two; + m_chk_gradient->Show(show_gradient); + if (m_label_gradient) m_label_gradient->Show(show_gradient); + m_combo_gradient_dir->Show(show_gradient && m_result.gradient_enabled); + if (m_per_part_gradient_sizer) + m_per_part_gradient_sizer->ShowItems(show_gradient && m_result.gradient_enabled); + if (m_curve_sizer) + m_curve_sizer->ShowItems(show_gradient && m_result.gradient_enabled); + } + if (is_three) { + m_result.gradient_enabled = false; + if (m_chk_gradient) m_chk_gradient->SetValue(false); + m_result.per_part_gradient = false; + if (m_chk_per_part_gradient) m_chk_per_part_gradient->SetValue(false); + } + + if (m_btn_add_material) { + bool can_add = (num_components() < (size_t)MAX_COMPONENTS && m_physical_colors.size() > num_components()); + m_btn_add_material->Enable(can_add); + if (can_add) { + m_btn_add_material->SetTextColor(wxColour("#262E30")); + m_btn_add_material->SetBorderColor(wxColour("#EEEEEE")); + m_btn_add_material->SetToolTip(wxEmptyString); + } else { + m_btn_add_material->SetTextColor(wxColour("#CECECE")); + m_btn_add_material->SetBorderColor(wxColour("#EEEEEE")); + m_btn_add_material->SetToolTip(is_three ? _L("Maximum 3 materials for mixing") : _L("Maximum number of components reached")); + } + } + + if (m_btn_remove_material) { + m_btn_remove_material->Show(is_three); + m_btn_remove_material->Enable(is_three); + m_btn_remove_material->SetToolTip(is_three ? _L("Remove the third material") : wxString()); + } + + const wxSize new_size = compute_dialog_size(); + const wxRect old_rect = GetRect(); + const wxPoint center(old_rect.x + old_rect.width / 2, + old_rect.y + old_rect.height / 2); + SetSize(new_size); + SetPosition(wxPoint(center.x - new_size.x / 2, + center.y - new_size.y / 2)); + Layout(); +} + +} // namespace GUI +} // namespace Slic3r diff --git a/src/slic3r/GUI/MixedFilamentDialog.hpp b/src/slic3r/GUI/MixedFilamentDialog.hpp new file mode 100644 index 0000000000..a1deaa2022 --- /dev/null +++ b/src/slic3r/GUI/MixedFilamentDialog.hpp @@ -0,0 +1,176 @@ +#ifndef slic3r_MixedFilamentDialog_hpp_ +#define slic3r_MixedFilamentDialog_hpp_ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "GUI_Utils.hpp" +#include "libslic3r/FilamentMixer.hpp" + +class Button; +class CheckBox; +class ComboBox; +class wxMouseEvent; +class wxScrolledWindow; +class wxTextCtrl; +class wxWrapSizer; + +namespace Slic3r { +namespace GUI { + +class GradientCurveEditor; +class RatioLabelPanel; + +struct MixedFilamentResult { + std::vector components; // 1-based physical filament indices + std::vector ratios; // percentages, sum = 100 + bool gradient_enabled = false; + int gradient_direction = 0; // 0 = A→B, 1 = B→A (only for 2-color) + bool per_part_gradient = false; // valid only when gradient_enabled == true + // Optional Photoshop-style custom curve overriding the linear A→B gradient. + // Empty -> use linear (gradient_direction). Non-empty -> cubic Hermite over [0,1]^2 + // with optional per-anchor tangent overrides (see GradientAnchor). + std::vector gradient_curve; +}; + +class MixedFilamentDialog : public DPIDialog +{ +public: + MixedFilamentDialog(wxWindow* parent, + const std::vector& physical_colors, + const std::vector& physical_names, + const std::vector& physical_types = {}); + + MixedFilamentDialog(wxWindow* parent, + const MixedFilamentResult& existing, + const std::vector& physical_colors, + const std::vector& physical_names, + const std::vector& physical_types = {}); + + MixedFilamentResult get_result() const { return m_result; } + +protected: + void on_dpi_changed(const wxRect& suggested_rect) override; + +private: + void build_ui(); + wxBoxSizer* create_preview_panel(); + wxBoxSizer* create_material_selection(); + wxBoxSizer* create_ratio_slider(); + wxBoxSizer* create_triangle_picker(); + wxBoxSizer* create_gradient_section(); + wxBoxSizer* create_recommendation_grid(); + wxBoxSizer* create_button_panel(); + + void on_filament_changed(); + void on_ratio_changed(int new_ratio_a); + void on_gradient_toggled(); + void on_gradient_direction_changed(); + void on_gradient_curve_changed(); + void on_per_part_gradient_toggled(); + void on_add_material(); + void on_remove_material(); + void on_recommendation_clicked(unsigned int comp_a, unsigned int comp_b); + void on_recommendation_clicked_triple(unsigned int a, unsigned int b, unsigned int c); + void apply_manual_ratio(size_t idx, int value); + void apply_dragged_triangle_ratio(int r0, int r1, int r2); + void reset_manual_ratio_state(); + void refresh_ratio_labels(); + void sync_triangle_weights_from_ratios(); + void start_ratio_editor(size_t idx, wxWindow* anchor, const wxRect& anchor_rect); + void commit_ratio_editor(bool apply); + void commit_ratio_editor_from_background(wxMouseEvent& e); + void update_preview(); + void update_ok_button_state(); + void update_gradient_direction_items(); + void update_component_count_ui(); + // Picks dialog (width, height) based on current state so the gradient curve + // editor and the recommendation list stay visible at the same time. + wxSize compute_dialog_size() const; + void rebuild_all_combos(); + void rebuild_recommendation_items(); + void refresh_curve_editor_colors(); + void paint_warning_panel(wxPaintEvent& evt); + + wxBitmap make_swatch_bitmap(size_t idx); + + // Helpers for component/ratio access + size_t num_components() const { return m_result.components.size(); } + unsigned int comp(size_t i) const { return (i < m_result.components.size()) ? m_result.components[i] : 1; } + int ratio(size_t i) const { return (i < m_result.ratios.size()) ? m_result.ratios[i] : 0; } + wxColour comp_colour(size_t i) const; + + MixedFilamentResult m_result; + bool m_edit_mode{false}; + std::vector m_physical_colors; + std::vector m_physical_names; + std::vector m_physical_types; + wxString m_type_mismatch_msg; + + // Combo item index -> 1-based physical filament index (per combo) + std::vector> m_combo_to_physical; + + // UI controls + wxPanel* m_preview_canvas{nullptr}; + wxPanel* m_summary_panel{nullptr}; + std::vector m_combo_filaments; + wxBoxSizer* m_material_rows_sizer{nullptr}; + wxPanel* m_ratio_bar{nullptr}; + wxPanel* m_triangle_panel{nullptr}; + RatioLabelPanel* m_label_ratio_a{nullptr}; + RatioLabelPanel* m_label_ratio_b{nullptr}; + wxPanel* m_ratio_editor_panel{nullptr}; + wxTextCtrl* m_ratio_editor{nullptr}; + CheckBox* m_chk_gradient{nullptr}; + wxStaticText* m_label_gradient{nullptr}; + ComboBox* m_combo_gradient_dir{nullptr}; + wxBoxSizer* m_gradient_sizer{nullptr}; + GradientCurveEditor* m_curve_editor{nullptr}; + wxBoxSizer* m_curve_sizer{nullptr}; + CheckBox* m_chk_per_part_gradient{nullptr}; + wxStaticText* m_label_per_part_gradient{nullptr}; + wxBoxSizer* m_per_part_gradient_sizer{nullptr}; + Button* m_btn_add_material{nullptr}; + Button* m_btn_remove_material{nullptr}; + Button* m_btn_ok{nullptr}; + Button* m_btn_cancel{nullptr}; + wxBoxSizer* m_warning_sizer{nullptr}; + wxPanel* m_warning_panel{nullptr}; + + wxBoxSizer* m_ratio_sizer{nullptr}; + wxBoxSizer* m_triangle_sizer{nullptr}; + wxBoxSizer* m_right_sizer{nullptr}; + + wxScrolledWindow* m_recommendation_scroll{nullptr}; + wxWrapSizer* m_recommendation_grid{nullptr}; + + // Cached preview bitmaps (loaded once at construction) + wxBitmap m_preview_bmp_two; + wxBitmap m_preview_bmp_three; + + // Drag state + bool m_dragging{false}; + std::vector m_ratio_manual_order; + size_t m_ratio_editor_idx{0}; + bool m_ratio_editor_committing{false}; + wxWindow* m_ratio_editor_anchor{nullptr}; + // Triangle picker drag point (barycentric weights) + double m_tri_wx{0.333}, m_tri_wy{0.333}, m_tri_wz{0.334}; + + // Cached triangle color bitmap (invalidated when colors or size change) + wxBitmap m_tri_cache_bmp; + wxColour m_tri_cache_c0, m_tri_cache_c1, m_tri_cache_c2; + wxSize m_tri_cache_size; + std::array m_triangle_ratio_labels{nullptr, nullptr, nullptr}; +}; + +} // namespace GUI +} // namespace Slic3r + +#endif // slic3r_MixedFilamentDialog_hpp_ diff --git a/src/slic3r/GUI/NotificationManager.hpp b/src/slic3r/GUI/NotificationManager.hpp index 22720bf33e..7a3e6b8bb5 100644 --- a/src/slic3r/GUI/NotificationManager.hpp +++ b/src/slic3r/GUI/NotificationManager.hpp @@ -162,6 +162,8 @@ enum class NotificationType //BBL: plugin install hint BBLPluginInstallHint, BBLFlushingVolumeZero, + // A mixed-color filament references a deleted component, or its components disagree in type. + BBLMixedFilamentBroken, BBLPluginUpdateAvailable, BBLPreviewOnlyMode, BBLPrinterConfigUpdateAvailable, diff --git a/src/slic3r/GUI/PartPlate.cpp b/src/slic3r/GUI/PartPlate.cpp index 910c761c06..1bbe1fc034 100644 --- a/src/slic3r/GUI/PartPlate.cpp +++ b/src/slic3r/GUI/PartPlate.cpp @@ -6,6 +6,7 @@ #include #include #include "libslic3r/MultiNozzleUtils.hpp" +#include "libslic3r/FilamentMixer.hpp" #include #include #include @@ -1675,6 +1676,25 @@ std::vector PartPlate::get_extruders(bool conside_custom_gcode) const std::sort(plate_extruders.begin(), plate_extruders.end()); auto it_end = std::unique(plate_extruders.begin(), plate_extruders.end()); plate_extruders.resize(std::distance(plate_extruders.begin(), it_end)); + + // Expand mixed filament slots to their physical components. A mixed slot is virtual and + // is never loaded into a tray, so callers (AMS mapping, filament checks) must see the + // physical filaments it resolves to instead. + { + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + auto* comp_strs_opt = project_config.option("filament_mixed_components"); + if (is_mixed_opt && comp_strs_opt && has_any_mixed_filament(is_mixed_opt->values)) { + std::vector ext_0based; + for (int e : plate_extruders) + if (e >= 1) ext_0based.push_back((unsigned int)(e - 1)); + auto expanded = expand_mixed_filaments(ext_0based, is_mixed_opt->values, comp_strs_opt->values); + plate_extruders.clear(); + for (unsigned int e : expanded) + plate_extruders.push_back((int)(e + 1)); + } + } + return plate_extruders; } @@ -1836,6 +1856,24 @@ std::vector PartPlate::get_extruders_under_cli(bool conside_custom_gcode, D std::sort(plate_extruders.begin(), plate_extruders.end()); auto it_end = std::unique(plate_extruders.begin(), plate_extruders.end()); plate_extruders.resize(std::distance(plate_extruders.begin(), it_end)); + + // Expand mixed filament slots to their physical components. A mixed slot is virtual and + // is never loaded into a tray, so callers (AMS mapping, filament checks) must see the + // physical filaments it resolves to instead. + { + auto* is_mixed_opt = full_config.option("filament_is_mixed"); + auto* comp_strs_opt = full_config.option("filament_mixed_components"); + if (is_mixed_opt && comp_strs_opt && has_any_mixed_filament(is_mixed_opt->values)) { + std::vector ext_0based; + for (int e : plate_extruders) + if (e >= 1) ext_0based.push_back((unsigned int)(e - 1)); + auto expanded = expand_mixed_filaments(ext_0based, is_mixed_opt->values, comp_strs_opt->values); + plate_extruders.clear(); + for (unsigned int e : expanded) + plate_extruders.push_back((int)(e + 1)); + } + } + return plate_extruders; } @@ -1889,6 +1927,25 @@ std::vector PartPlate::get_extruders_without_support(bool conside_custom_gc std::sort(plate_extruders.begin(), plate_extruders.end()); auto it_end = std::unique(plate_extruders.begin(), plate_extruders.end()); plate_extruders.resize(std::distance(plate_extruders.begin(), it_end)); + + // Expand mixed filament slots to their physical components. A mixed slot is virtual and + // is never loaded into a tray, so callers (AMS mapping, filament checks) must see the + // physical filaments it resolves to instead. + { + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + auto* comp_strs_opt = project_config.option("filament_mixed_components"); + if (is_mixed_opt && comp_strs_opt && has_any_mixed_filament(is_mixed_opt->values)) { + std::vector ext_0based; + for (int e : plate_extruders) + if (e >= 1) ext_0based.push_back((unsigned int)(e - 1)); + auto expanded = expand_mixed_filaments(ext_0based, is_mixed_opt->values, comp_strs_opt->values); + plate_extruders.clear(); + for (unsigned int e : expanded) + plate_extruders.push_back((int)(e + 1)); + } + } + return plate_extruders; } @@ -1990,6 +2047,55 @@ bool PartPlate::check_tpu_printable_status(const DynamicPrintConfig & config, co return true; } +// A mixed-color filament alternates between its components constantly. On a single-nozzle +// printer every one of those switches is a full filament change plus a purge, so warn the +// user before they commit to it. Printers with more than one nozzle can keep the components +// loaded simultaneously and are not affected. +// +// BBS additionally excludes its H2C/H2D/X2D models by name; those are multi-nozzle machines +// already ruled out by the nozzle_diameter test above, so the name check is dropped here +// rather than carried over as a Bambu-specific special case. +bool PartPlate::check_single_extruder_mixed_filament_risk(const DynamicPrintConfig &config, std::string &warning_text) const +{ + warning_text.clear(); + + auto *nozzle_diameter_opt = config.option("nozzle_diameter"); + if (!nozzle_diameter_opt || nozzle_diameter_opt->values.size() > 1) + return false; + + auto *is_mixed_opt = wxGetApp().preset_bundle->project_config.option("filament_is_mixed"); + if (!is_mixed_opt || !has_any_mixed_filament(is_mixed_opt->values)) + return false; + + auto is_mixed_slot = [&](int extruder_1based) { + size_t idx = (size_t)(extruder_1based - 1); + return idx < is_mixed_opt->values.size() && is_mixed_opt->values[idx]; + }; + + const std::string mixed_warn_msg = _u8L("Printing mixed-color filament on a single-extruder printer requires frequent filament changes and flushing, " + "which may significantly increase waste and the risk of nozzle / waste-chute clogging."); + + for (int obj_idx = 0; obj_idx < (int)m_model->objects.size(); ++obj_idx) { + if (!contain_instance_totally(obj_idx, 0)) + continue; + ModelObject *mo = m_model->objects[obj_idx]; + int obj_ext = mo->config.has("extruder") ? mo->config.extruder() : 1; + if (is_mixed_slot(obj_ext)) { + warning_text = mixed_warn_msg; + return true; + } + for (ModelVolume *mv : mo->volumes) { + int vol_ext = mv->config.has("extruder") ? mv->config.extruder() : obj_ext; + if (is_mixed_slot(vol_ext)) { + warning_text = mixed_warn_msg; + return true; + } + } + } + + return false; +} + bool PartPlate::check_mixture_of_pla_and_petg(const DynamicPrintConfig &config) { bool has_pla = false; diff --git a/src/slic3r/GUI/PartPlate.hpp b/src/slic3r/GUI/PartPlate.hpp index 47481dcad4..5760320b49 100644 --- a/src/slic3r/GUI/PartPlate.hpp +++ b/src/slic3r/GUI/PartPlate.hpp @@ -354,6 +354,9 @@ public: bool check_filament_printable(const DynamicPrintConfig & config, wxString& error_message); bool check_tpu_printable_status(const DynamicPrintConfig & config, const std::vector &tpu_filaments); bool check_mixture_of_pla_and_petg(const DynamicPrintConfig & config); + // Warns when a mixed-color filament is used on a single-nozzle printer, where every + // component switch costs a full filament change and purge. + bool check_single_extruder_mixed_filament_risk(const DynamicPrintConfig &config, std::string &warning_text) const; bool check_mixture_filament_compatible(const DynamicPrintConfig& config, std::string &error_msg); bool check_compatible_of_nozzle_and_filament(const DynamicPrintConfig & config, const std::vector& filament_presets, std::string& error_msg); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index af6b564ab1..676d29b961 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -166,6 +166,12 @@ #include // Needs to be last because reasons :-/ #include #include "WipeTowerDialog.hpp" +#include "MixedFilamentDialog.hpp" +#include "TextureImportDialog.hpp" +#include "libslic3r/TexturePainting.hpp" +#include "ColorDecomposeSupport.hpp" +#include "FilamentBitmapUtils.hpp" +#include "libslic3r/FilamentMixer.hpp" #include "ObjColorDialog.hpp" #include "libslic3r/CustomGCode.hpp" @@ -202,6 +208,17 @@ static const std::pair THUMBNAIL_SIZE_3MF = { 512, 5 namespace Slic3r { namespace GUI { +// A textured mesh is only worth routing through the import dialog when it actually carries +// decoded image data; UV-only meshes have nothing to sample. +static bool has_importable_texture(const Slic3r::TexturedMesh& textured_mesh) +{ + if (textured_mesh.vertices.empty() || textured_mesh.indices.empty()) + return false; + + return std::any_of(textured_mesh.textures.begin(), textured_mesh.textures.end(), + [](const Slic3r::TextureImage& texture) { return !texture.data.empty(); }); +} + wxDEFINE_EVENT(EVT_SCHEDULE_BACKGROUND_PROCESS, SimpleEvent); wxDEFINE_EVENT(EVT_SLICING_UPDATE, SlicingStatusEvent); wxDEFINE_EVENT(EVT_SLICING_COMPLETED, wxCommandEvent); @@ -718,6 +735,22 @@ struct Sidebar::priv ScalableButton * m_bpButton_set_filament; int m_menu_filament_id = -1; wxScrolledWindow* m_panel_filament_content; + + // Mixed-color filament section. Sits directly under the physical filament list in + // scrolled_sizer. BBS hosts the equivalent widgets inside an m_filament_area_wrapper + // that Orca's sidebar has no counterpart for, so these are parented to p->scrolled. + wxPanel* m_btn_add_mixed_filament{nullptr}; // "+ Add Mixed Filament" full-width button + wxPanel* m_panel_mixed_title{nullptr}; // title row: "Mixed Filament" + add/del buttons + wxStaticText* m_text_mixed_title{nullptr}; + ScalableButton* m_btn_mixed_add{nullptr}; + ScalableButton* m_btn_mixed_del{nullptr}; + wxScrolledWindow* m_mixed_scroll_area{nullptr}; // independent scrollbar for mixed rows + wxPanel* m_panel_mixed_content{nullptr}; + wxBoxSizer* m_sizer_mixed_filaments{nullptr}; // two-column, mirrors sizer_filaments + wxPanel* m_panel_mixed_warning{nullptr}; // red bar for broken/mismatched mixes + wxStaticText* m_text_mixed_warning{nullptr}; + bool m_mixed_filament_broken{false}; + wxScrolledWindow* m_scrolledWindow_filament_content; wxStaticLine* m_staticline2; wxPanel* m_panel_project_title; @@ -2991,6 +3024,123 @@ Sidebar::Sidebar(Plater *parent) update_filaments_area_height(); // ORCA scrolled_sizer->Add(p->m_panel_filament_content, 0, wxEXPAND | wxTOP | wxBOTTOM, FromDIP(SidebarProps::ContentMarginV())); // ORCA use vertical margin on parent otherwise it shows scrollbar even on 1 filament + + // ---- Mixed-color filament section ---- + // A mixed filament is a virtual slot realized from 2-3 physical filaments at slicing time. + // Everything here stays hidden until at least two physical filaments exist, so a single + // filament setup looks exactly as before. + { + // 1) "+ Add Mixed Filament" button, shown only while no mixed filament exists yet. + p->m_btn_add_mixed_filament = new wxPanel(p->scrolled, wxID_ANY); + p->m_btn_add_mixed_filament->SetBackgroundColour(StateColor::darkModeColorFor(wxColour("#F8F8F8"))); + p->m_btn_add_mixed_filament->SetMinSize(wxSize(-1, FromDIP(23))); + { + auto* btn_sizer = new wxBoxSizer(wxHORIZONTAL); + auto* icon_add = new ScalableButton(p->m_btn_add_mixed_filament, wxID_ANY, "add_filament", wxEmptyString, + wxDefaultSize, wxDefaultPosition, wxBU_EXACTFIT | wxNO_BORDER, false, 16); + auto* add_label = new wxStaticText(p->m_btn_add_mixed_filament, wxID_ANY, _L("Add Mixed Filament"), + wxDefaultPosition, wxDefaultSize, 0); + add_label->SetFont(::Label::Body_13); + btn_sizer->AddStretchSpacer(); + btn_sizer->Add(icon_add, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(4)); + btn_sizer->Add(add_label, 0, wxALIGN_CENTER_VERTICAL); + btn_sizer->AddStretchSpacer(); + p->m_btn_add_mixed_filament->SetSizer(btn_sizer); + p->m_btn_add_mixed_filament->SetCursor(wxCursor(wxCURSOR_HAND)); + // Whole panel is the hit target, so forward clicks from the children too. + auto on_click = [this](wxMouseEvent&) { add_mixed_filament(); }; + p->m_btn_add_mixed_filament->Bind(wxEVT_LEFT_UP, on_click); + add_label->Bind(wxEVT_LEFT_UP, on_click); + icon_add->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { add_mixed_filament(); }); + } + scrolled_sizer->Add(p->m_btn_add_mixed_filament, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM, FromDIP(8)); + + // 2) Title row with add / remove buttons, shown once a mixed filament exists. + p->m_panel_mixed_title = new wxPanel(p->scrolled, wxID_ANY); + p->m_panel_mixed_title->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + { + auto* title_sizer = new wxBoxSizer(wxHORIZONTAL); + p->m_text_mixed_title = new wxStaticText(p->m_panel_mixed_title, wxID_ANY, _L("Mixed Filament")); + p->m_text_mixed_title->SetFont(::Label::Head_14); + title_sizer->Add(p->m_text_mixed_title, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(SidebarProps::TitlebarMargin())); + title_sizer->AddStretchSpacer(); + + p->m_btn_mixed_del = new ScalableButton(p->m_panel_mixed_title, wxID_ANY, "delete_filament"); + p->m_btn_mixed_del->SetToolTip(_L("Remove last mixed filament")); + p->m_btn_mixed_del->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { + auto* plater_ptr = dynamic_cast(GetParent()); + if (!plater_ptr) return; + auto mixed_indices = plater_ptr->mixed_filament_config_indices(); + if (!mixed_indices.empty()) + delete_mixed_filament_at(mixed_indices.size() - 1); + }); + title_sizer->Add(p->m_btn_mixed_del, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(SidebarProps::IconSpacing())); + + p->m_btn_mixed_add = new ScalableButton(p->m_panel_mixed_title, wxID_ANY, "add_filament"); + p->m_btn_mixed_add->SetToolTip(_L("Add mixed filament")); + p->m_btn_mixed_add->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { add_mixed_filament(); }); + title_sizer->Add(p->m_btn_mixed_add, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(SidebarProps::IconSpacing())); + title_sizer->AddSpacer(FromDIP(SidebarProps::TitlebarMargin())); + + p->m_panel_mixed_title->SetSizer(title_sizer); + } + scrolled_sizer->Add(p->m_panel_mixed_title, 0, wxEXPAND | wxTOP | wxBOTTOM, FromDIP(8)); + + // 3) Mixed filament rows, in their own scroll area so a long mixed list does not + // push the physical filament list off screen. + p->m_mixed_scroll_area = new wxScrolledWindow(p->scrolled, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL); + p->m_mixed_scroll_area->SetScrollbars(0, 100, 1, 2); + p->m_mixed_scroll_area->SetScrollRate(0, 5); + p->m_mixed_scroll_area->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + { + auto* mix_scroll_sizer = new wxBoxSizer(wxVERTICAL); + p->m_panel_mixed_content = new wxPanel(p->m_mixed_scroll_area, wxID_ANY); + p->m_panel_mixed_content->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE)); + + // Two columns, same idiom as sizer_filaments. + p->m_sizer_mixed_filaments = new wxBoxSizer(wxHORIZONTAL); + p->m_sizer_mixed_filaments->Add(new wxBoxSizer(wxVERTICAL), 1, wxEXPAND); + p->m_sizer_mixed_filaments->Add(new wxBoxSizer(wxVERTICAL), 1, wxEXPAND); + + auto* sizer_mixed2 = new wxBoxSizer(wxVERTICAL); + sizer_mixed2->Add(p->m_sizer_mixed_filaments, 0, wxEXPAND, 0); + p->m_panel_mixed_content->SetSizer(sizer_mixed2); + mix_scroll_sizer->Add(p->m_panel_mixed_content, 0, wxEXPAND | wxBOTTOM, FromDIP(8)); + p->m_mixed_scroll_area->SetSizer(mix_scroll_sizer); + } + p->m_mixed_scroll_area->EnableScrolling(false, true); + p->m_mixed_scroll_area->ShowScrollbars(wxSHOW_SB_NEVER, wxSHOW_SB_DEFAULT); + p->m_mixed_scroll_area->Bind(wxEVT_SIZE, [this](wxSizeEvent& e) { + int w = p->m_mixed_scroll_area->GetClientSize().GetWidth(); + if (w > 0) + p->m_mixed_scroll_area->SetVirtualSize(w, p->m_mixed_scroll_area->GetVirtualSize().GetHeight()); + e.Skip(); + }); + scrolled_sizer->Add(p->m_mixed_scroll_area, 0, wxEXPAND, 0); + + // 4) Warning bar for mixes whose components were deleted or whose types disagree. + p->m_panel_mixed_warning = new wxPanel(p->scrolled, wxID_ANY); + p->m_panel_mixed_warning->SetBackgroundColour(wxColour("#FDE8E8")); + { + auto* warn_sizer = new wxBoxSizer(wxHORIZONTAL); + p->m_text_mixed_warning = new wxStaticText(p->m_panel_mixed_warning, wxID_ANY, + _L("Mixed filament has invalid or mismatched components. Please re-edit affected entries.")); + p->m_text_mixed_warning->SetForegroundColour(wxColour("#D32F2F")); + p->m_text_mixed_warning->SetFont(::Label::Body_12); + p->m_text_mixed_warning->Wrap(FromDIP(360)); + warn_sizer->Add(p->m_text_mixed_warning, 1, wxALL, FromDIP(6)); + p->m_panel_mixed_warning->SetSizer(warn_sizer); + } + scrolled_sizer->Add(p->m_panel_mixed_warning, 0, wxEXPAND | wxLEFT | wxRIGHT, FromDIP(8)); + + // Hidden until update_mixed_filament_list() decides otherwise. + p->m_btn_add_mixed_filament->Hide(); + p->m_panel_mixed_title->Hide(); + p->m_mixed_scroll_area->Hide(); + p->m_panel_mixed_content->Hide(); + p->m_panel_mixed_warning->Hide(); + } + // ---- End mixed-color filament section ---- } { @@ -3696,6 +3846,1110 @@ void Sidebar::change_top_border_for_mode_sizer(bool increase_border) #endif } + +// ---- Mixed-color filament sidebar support ---- +// Ported from BambuStudio's 混色耗材 feature. BBS hosts these widgets in an +// m_filament_area_wrapper that Orca's sidebar has no counterpart for, so the mixed +// section is parented to p->scrolled and sized with Orca's own row-height preference +// (filaments_area_preferred_count) rather than BBS's fixed 3-row / 12-filament cap. +void Sidebar::recalc_filament_scroll_sizes() +{ + if (!p->m_mixed_scroll_area || !p->m_mixed_scroll_area->GetSizer()) + return; + + // Same preferred-row budget the physical list uses, so both lists cap consistently. + auto left_sizer = p->sizer_filaments->GetItem((size_t) 0)->GetSizer(); + auto combo_sizer = left_sizer->GetItem((size_t) 0)->GetSizer(); + const int row_h = combo_sizer ? combo_sizer->GetSize().GetHeight() : 0; + int preferred_rows = std::ceil(0.5 * std::stoi(wxGetApp().app_config->get("filaments_area_preferred_count"))); + const int max_h = (row_h > 0) ? preferred_rows * row_h : -1; + + auto content_size = p->m_mixed_scroll_area->GetSizer()->GetMinSize(); + if (max_h > 0 && content_size.y > max_h) { + p->m_mixed_scroll_area->SetMaxSize({-1, max_h}); + content_size.y = max_h; + } else { + p->m_mixed_scroll_area->SetMaxSize({-1, -1}); + } + p->m_mixed_scroll_area->SetMinSize({0, content_size.y}); +} +static std::string blend_mixed_color(const std::vector &comp_ids, + const std::vector &ratios, + const std::vector &color_strs) +{ + std::vector hex_colors; + hex_colors.reserve(comp_ids.size()); + for (unsigned int id : comp_ids) + hex_colors.push_back((id >= 1 && id <= color_strs.size()) ? color_strs[id - 1] : "#808080"); + return Slic3r::blend_color_multi(hex_colors, ratios); +} + +void Sidebar::update_mixed_filament_list() +{ + auto* plater = dynamic_cast(GetParent()); + if (!plater) return; + + wxWindowUpdateLocker noUpdates(this); + + const wxColour mc_bg = StateColor::darkModeColorFor(*wxWHITE); + const wxColour mc_border = StateColor::darkModeColorFor(wxColour("#CECECE")); + const wxColour mc_text = StateColor::darkModeColorFor(wxColour("#262E30")); + const wxColour mc_dim = StateColor::darkModeColorFor(wxColour("#ACACAC")); + + auto& project_config = wxGetApp().preset_bundle->project_config; + auto mixed_indices = plater->mixed_filament_config_indices(); + size_t num_physical = p->combos_filament.size(); + + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + auto* components_opt = project_config.option("filament_mixed_components"); + auto* ratios_opt = project_config.option("filament_mixed_sublayer_ratios"); + auto* colours_opt = project_config.option("filament_colour"); + auto* grad_opt = project_config.option("filament_mixed_gradient"); + auto* grad_range_opt = project_config.option("filament_mixed_gradient_range"); + + bool can_mix = (num_physical >= 2); + bool has_mixed = can_mix && !mixed_indices.empty(); + + // Check integrity of mixed filament component references + std::vector broken_slots; + if (is_mixed_opt && components_opt) + broken_slots = check_mixed_filament_integrity(is_mixed_opt->values, components_opt->values, num_physical); + std::set broken_set(broken_slots.begin(), broken_slots.end()); + + // Type consistency check + if (is_mixed_opt && components_opt) { + std::vector physical_types; + auto& preset_bundle = *wxGetApp().preset_bundle; + for (size_t i = 0; i < num_physical; ++i) { + std::string ft; + if (i < preset_bundle.filament_presets.size()) { + auto* preset = preset_bundle.filaments.find_preset(preset_bundle.filament_presets[i]); + if (preset) { + std::string display_type; + ft = preset->config.get_filament_type(display_type); + } + } + if (ft.empty()) ft = "PLA"; + physical_types.push_back(ft); + } + auto type_mismatch_slots = check_mixed_filament_type_consistency( + is_mixed_opt->values, components_opt->values, physical_types); + for (size_t s : type_mismatch_slots) + broken_set.insert(s); + broken_slots.insert(broken_slots.end(), type_mismatch_slots.begin(), type_mismatch_slots.end()); + } + + bool at_limit = (wxGetApp().preset_bundle->filament_presets.size() >= size_t(EnforcerBlockerType::ExtruderMax)); + p->m_btn_add_mixed_filament->Show(can_mix && !has_mixed && !at_limit); + p->m_panel_mixed_title->Show(has_mixed); + p->m_mixed_scroll_area->Show(has_mixed); + p->m_panel_mixed_content->Show(has_mixed); + if (p->m_btn_mixed_add) + p->m_btn_mixed_add->Enable(!at_limit); + p->m_panel_mixed_warning->Show(false); + + // Show/dismiss 3D canvas notification for broken mixed filaments + if (has_mixed && !broken_set.empty()) { + auto* notify = wxGetApp().plater()->get_notification_manager(); + if (notify) + notify->push_notification(NotificationType::BBLMixedFilamentBroken, + NotificationManager::NotificationLevel::ErrorNotificationLevel, + _u8L("Mixed filament has invalid or mismatched components. Please re-edit affected entries.")); + } else { + auto* notify = wxGetApp().plater()->get_notification_manager(); + if (notify) + notify->close_notification_of_type(NotificationType::BBLMixedFilamentBroken); + } + + if (has_mixed) { + auto* left_col = p->m_sizer_mixed_filaments->GetItem(size_t(0))->GetSizer(); + auto* right_col = p->m_sizer_mixed_filaments->GetItem(size_t(1))->GetSizer(); + left_col->Clear(true); + right_col->Clear(true); + + std::vector physical_colors; + if (colours_opt) { + for (size_t i = 0; i < num_physical && i < colours_opt->values.size(); ++i) + physical_colors.push_back(colours_opt->values[i]); + } + + auto make_swatch_panel = [this, mc_text](wxWindow* parent, const wxColour& col, unsigned int num) -> wxPanel* { + int swatch_sz = FromDIP(20); + auto* panel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(swatch_sz, swatch_sz)); + panel->SetMinSize(wxSize(swatch_sz, swatch_sz)); + bool is_dark = wxGetApp().dark_mode(); + panel->Bind(wxEVT_PAINT, [panel, col, num, mc_text, is_dark](wxPaintEvent&) { + wxPaintDC dc(panel); + wxSize sz = panel->GetClientSize(); + dc.SetBackground(wxBrush(col)); + dc.Clear(); + if (!is_dark && col.Red() > 224 && col.Green() > 224 && col.Blue() > 224) { + dc.SetPen(wxPen(wxColour(130, 130, 128), 1)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + } + if (is_dark && col.Red() < 45 && col.Green() < 45 && col.Blue() < 45) { + dc.SetPen(wxPen(wxColour(207, 207, 207), 1)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + } + wxString txt = wxString::Format("%u", num); + dc.SetFont(::Label::Body_14); + wxSize txt_sz = dc.GetTextExtent(txt); + dc.SetTextForeground(col.GetLuminance() > 0.5 ? wxColour(50, 58, 61) : *wxWHITE); + dc.DrawText(txt, (sz.GetWidth() - txt_sz.GetWidth()) / 2, + (sz.GetHeight() - txt_sz.GetHeight()) / 2); + }); + return panel; + }; + + for (size_t i = 0; i < mixed_indices.size(); ++i) { + size_t cfg_idx = mixed_indices[i]; + auto* combo_and_btn_sizer = new wxBoxSizer(wxHORIZONTAL); + + combo_and_btn_sizer->Add(FromDIP(10), 0, 0, 0, 0); + + // Parse components and ratios from config strings (supports 2-N components) + std::vector comp_ids; + std::vector comp_ratios; + if (components_opt && cfg_idx < components_opt->values.size()) { + std::istringstream iss(components_opt->values[cfg_idx]); + std::string tok; + while (std::getline(iss, tok, ',')) { + unsigned int v = 0; + if (std::sscanf(tok.c_str(), "%u", &v) == 1) + comp_ids.push_back(v); + } + } + if (ratios_opt && cfg_idx < ratios_opt->values.size()) { + CNumericLocalesSetter c_locale_setter; + std::istringstream iss(ratios_opt->values[cfg_idx]); + std::string tok; + while (std::getline(iss, tok, ',')) { + float v = 0; + if (std::sscanf(tok.c_str(), "%f", &v) == 1) + comp_ratios.push_back((int)(v * 100 + 0.5f)); + } + } + if (!comp_ids.empty() && comp_ratios.size() != comp_ids.size()) { + BOOST_LOG_TRIVIAL(warning) << "Mixed filament slot " << cfg_idx + << ": ratio count (" << comp_ratios.size() + << ") != component count (" << comp_ids.size() + << "), resetting to even distribution"; + int n = (int)comp_ids.size(); + comp_ratios.assign(n, 100 / n); + comp_ratios[0] += 100 - (100 / n) * n; + } + + bool is_broken = broken_set.count(cfg_idx) > 0; + + // Recalculate mixed color based on current physical colors + if (!is_broken && !comp_ids.empty() && comp_ids.size() == comp_ratios.size()) { + std::string new_mixed_color = blend_mixed_color(comp_ids, comp_ratios, physical_colors); + + if (colours_opt && cfg_idx < colours_opt->values.size() && colours_opt->values[cfg_idx] != new_mixed_color) { + colours_opt->values[cfg_idx] = new_mixed_color; + + auto* multi_colour_opt = project_config.option("filament_multi_colour"); + if (multi_colour_opt && cfg_idx < multi_colour_opt->values.size()) { + multi_colour_opt->values[cfg_idx] = new_mixed_color; + } + } + } + + bool is_gradient = false; + int gradient_direction = 0; + if (grad_opt && cfg_idx < grad_opt->values.size()) + is_gradient = grad_opt->values[cfg_idx]; + if (is_gradient && grad_range_opt && cfg_idx < grad_range_opt->values.size()) { + CNumericLocalesSetter c_locale_setter; + float v0 = 0, v1 = 0; + if (std::sscanf(grad_range_opt->values[cfg_idx].c_str(), "%f,%f", &v0, &v1) == 2) + gradient_direction = (v0 > v1) ? 0 : 1; + } + + std::string mix_color_str = (colours_opt && cfg_idx < colours_opt->values.size()) + ? colours_opt->values[cfg_idx] : "#888888"; + wxColour mix_col(mix_color_str); + unsigned int mix_num = (unsigned int)(cfg_idx + 1); + + if (is_gradient && comp_ids.size() == 2) { + unsigned int from_id = (gradient_direction == 0) ? comp_ids[0] : comp_ids[1]; + unsigned int to_id = (gradient_direction == 0) ? comp_ids[1] : comp_ids[0]; + wxColour col_from = (from_id >= 1 && from_id <= physical_colors.size()) + ? wxColour(physical_colors[from_id - 1]) : wxColour("#D9D9D9"); + wxColour col_to = (to_id >= 1 && to_id <= physical_colors.size()) + ? wxColour(physical_colors[to_id - 1]) : wxColour("#D9D9D9"); + int swatch_sz = FromDIP(20); + auto* grad_panel = new wxPanel(p->m_panel_mixed_content, wxID_ANY, + wxDefaultPosition, wxSize(swatch_sz, swatch_sz)); + grad_panel->SetMinSize(wxSize(swatch_sz, swatch_sz)); + grad_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + grad_panel->Bind(wxEVT_PAINT, [grad_panel, col_from, col_to, mix_num, mc_text](wxPaintEvent&) { + wxBufferedPaintDC dc(grad_panel); + wxSize sz = grad_panel->GetClientSize(); + fill_gradient_rect_east(dc, wxRect(0, 0, sz.GetWidth(), sz.GetHeight()), col_from, col_to); + wxString txt = wxString::Format("%u", mix_num); + dc.SetFont(::Label::Body_14); + wxSize txt_sz = dc.GetTextExtent(txt); + wxColour mid( + (col_from.Red() + col_to.Red()) / 2, + (col_from.Green() + col_to.Green()) / 2, + (col_from.Blue() + col_to.Blue()) / 2); + dc.SetTextForeground(mid.GetLuminance() > 0.5 ? mc_text : *wxWHITE); + dc.DrawText(txt, (sz.GetWidth() - txt_sz.GetWidth()) / 2, + (sz.GetHeight() - txt_sz.GetHeight()) / 2); + }); + combo_and_btn_sizer->Add(grad_panel, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(4)); + } else { + combo_and_btn_sizer->Add(make_swatch_panel(p->m_panel_mixed_content, mix_col, mix_num), + 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(4)); + } + + auto* content_panel = new wxPanel(p->m_panel_mixed_content, wxID_ANY); + content_panel->SetBackgroundColour(mc_bg); + content_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + + // Pre-compute all values the paint lambda needs (avoid capturing `this` for FromDIP) + int cp_pad = FromDIP(4); + int cp_swatch_sz = FromDIP(20); + int cp_sep_margin = FromDIP(3); + int cp_pct_left = FromDIP(2); + int cp_gap = FromDIP(2); + int cp_pct_gap = FromDIP(4); + bool cp_is_dark = wxGetApp().dark_mode(); + + // Build per-component colour list for the lambda + std::vector cp_colours; + std::vector cp_valid; + std::vector cp_ids = comp_ids; + std::vector cp_ratios = comp_ratios; + bool cp_is_gradient = is_gradient; + int cp_gradient_dir = gradient_direction; + for (size_t ci = 0; ci < comp_ids.size(); ++ci) { + bool valid = (comp_ids[ci] >= 1 && comp_ids[ci] <= physical_colors.size()); + cp_valid.push_back(valid); + cp_colours.push_back(valid ? wxColour(physical_colors[comp_ids[ci] - 1]) : wxColour("#D9D9D9")); + } + + // Reorder for gradient display: from -> to + std::vector draw_ids; + std::vector draw_ratios; + std::vector draw_colours; + std::vector draw_valid; + if (cp_is_gradient && cp_ids.size() == 2) { + int fi = (cp_gradient_dir == 0) ? 0 : 1; + int ti = 1 - fi; + draw_ids = { cp_ids[fi], cp_ids[ti] }; + draw_ratios = { cp_ratios.size() > (size_t)fi ? cp_ratios[fi] : 0, + cp_ratios.size() > (size_t)ti ? cp_ratios[ti] : 0 }; + draw_colours = { cp_colours[fi], cp_colours[ti] }; + draw_valid = { cp_valid[fi], cp_valid[ti] }; + } else { + draw_ids = cp_ids; + draw_ratios = cp_ratios; + draw_colours = cp_colours; + draw_valid = cp_valid; + } + + content_panel->Bind(wxEVT_PAINT, [content_panel, mc_bg, mc_border, mc_text, mc_dim, + cp_pad, cp_swatch_sz, cp_sep_margin, cp_pct_left, + cp_gap, cp_pct_gap, cp_is_dark, + cp_is_gradient, + draw_ids, draw_ratios, draw_colours, draw_valid](wxPaintEvent&) { + wxBufferedPaintDC dc(content_panel); + wxSize sz = content_panel->GetClientSize(); + + dc.SetBrush(wxBrush(mc_bg)); + dc.SetPen(wxPen(mc_border, 1)); + dc.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight()); + + dc.SetFont(::Label::Body_13); + int x = cp_pad; + int y_swatch = (sz.GetHeight() - cp_swatch_sz) / 2; + int text_h = dc.GetTextExtent(wxT("A")).GetHeight(); + int y_text = y_swatch + (cp_swatch_sz - text_h) / 2; + int avail = sz.GetWidth() - cp_pad; + wxString ellipsis = wxT("..."); + int ellipsis_w = dc.GetTextExtent(ellipsis).GetWidth(); + + auto fits = [&](int needed) -> bool { + return (x + needed) <= (avail - ellipsis_w); + }; + + size_t n = draw_ids.size(); + for (size_t ci = 0; ci < n; ++ci) { + // Separator: "+" or arrow + if (ci > 0) { + wxString sep = cp_is_gradient ? wxT("\u2192") : wxT("+"); + int sep_w = dc.GetTextExtent(sep).GetWidth() + cp_sep_margin * 2; + if (!fits(sep_w + cp_swatch_sz)) { + dc.SetTextForeground(mc_text); + dc.DrawText(ellipsis, x, y_text); + break; + } + dc.SetTextForeground(mc_text); + dc.DrawText(sep, x + cp_sep_margin, y_text); + x += sep_w; + } + + // Swatch + if (!fits(cp_swatch_sz)) { + dc.SetTextForeground(mc_text); + dc.DrawText(ellipsis, x, y_text); + break; + } + + if (draw_valid[ci]) { + wxColour col = draw_colours[ci]; + dc.SetBrush(wxBrush(col)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(x, y_swatch, cp_swatch_sz, cp_swatch_sz); + if (!cp_is_dark && col.Red() > 224 && col.Green() > 224 && col.Blue() > 224) { + dc.SetPen(wxPen(wxColour(130, 130, 128), 1)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.DrawRectangle(x, y_swatch, cp_swatch_sz, cp_swatch_sz); + } + if (cp_is_dark && col.Red() < 45 && col.Green() < 45 && col.Blue() < 45) { + dc.SetPen(wxPen(wxColour(207, 207, 207), 1)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.DrawRectangle(x, y_swatch, cp_swatch_sz, cp_swatch_sz); + } + dc.SetFont(::Label::Body_14); + wxString num = wxString::Format("%u", draw_ids[ci]); + wxSize num_sz = dc.GetTextExtent(num); + dc.SetTextForeground(col.GetLuminance() > 0.5 ? wxColour(50, 58, 61) : *wxWHITE); + dc.DrawText(num, x + (cp_swatch_sz - num_sz.GetWidth()) / 2, + y_swatch + (cp_swatch_sz - num_sz.GetHeight()) / 2); + dc.SetFont(::Label::Body_13); + } else { + dc.SetBrush(wxBrush(mc_bg)); + dc.SetPen(wxPen(mc_dim, 1)); + dc.DrawRectangle(x, y_swatch, cp_swatch_sz, cp_swatch_sz); + wxString dash = wxT("\u2014"); + wxSize dash_sz = dc.GetTextExtent(dash); + dc.SetTextForeground(wxColour("#909090")); + dc.DrawText(dash, x + (cp_swatch_sz - dash_sz.GetWidth()) / 2, + y_swatch + (cp_swatch_sz - dash_sz.GetHeight()) / 2); + } + x += cp_swatch_sz + cp_gap; + + // Ratio text (skip for gradient) + if (!cp_is_gradient) { + int r = (ci < draw_ratios.size()) ? draw_ratios[ci] : 0; + wxString pct = wxString::Format("%d%%", r); + int pct_w = dc.GetTextExtent(pct).GetWidth(); + if (!fits(pct_w)) { + dc.SetTextForeground(mc_text); + dc.DrawText(ellipsis, x, y_text); + break; + } + dc.SetTextForeground(mc_text); + dc.DrawText(pct, x + cp_pct_left, y_text); + x += pct_w + cp_pct_gap; + } + } + }); + + // Tooltip: always show full info + { + wxString tip; + for (size_t ci = 0; ci < draw_ids.size(); ++ci) { + if (ci > 0) tip += cp_is_gradient ? wxT(" \u2192 ") : wxT(" + "); + int r = (ci < draw_ratios.size()) ? draw_ratios[ci] : 0; + tip += wxString::Format("%u (%d%%)", draw_ids[ci], r); + } + content_panel->SetToolTip(tip); + } + + // Repaint on resize so truncation updates + content_panel->Bind(wxEVT_SIZE, [content_panel](wxSizeEvent& e) { + content_panel->Refresh(); + e.Skip(); + }); + + content_panel->SetCursor(wxCursor(wxCURSOR_HAND)); + size_t panel_idx = i; + content_panel->Bind(wxEVT_LEFT_UP, [this, panel_idx](wxMouseEvent&) { edit_mixed_filament(panel_idx); }); + + combo_and_btn_sizer->Add(content_panel, 1, wxALL | wxEXPAND, FromDIP(2))->SetMinSize({-1, FromDIP(30)}); + + auto* menu_btn = new ScalableButton(p->m_panel_mixed_content, wxID_ANY, + is_broken ? "error" : "menu_filament"); + menu_btn->SetToolTip(is_broken ? _L("Mixed filament has broken component references") : _L("Edit / Delete / Merge")); + menu_btn->Bind(wxEVT_BUTTON, [this, panel_idx, cfg_idx](wxCommandEvent&) { + wxMenu menu; + + auto* edit_item = menu.Append(wxID_ANY, _L("Edit")); + menu.Bind(wxEVT_MENU, [this, panel_idx](wxCommandEvent&) { + edit_mixed_filament(panel_idx); + }, edit_item->GetId()); + + auto* del_item = menu.Append(wxID_ANY, _L("Delete")); + menu.Bind(wxEVT_MENU, [this, panel_idx](wxCommandEvent&) { + delete_mixed_filament_at(panel_idx); + }, del_item->GetId()); + + wxMenu* sub_menu = new wxMenu(); + std::vector icons = get_extruder_color_icons(true); + int filaments_cnt = icons.size(); + for (int j = 0; j < filaments_cnt; ++j) { + if ((size_t)j == cfg_idx) + continue; + + wxString item_name; + bool is_target_mixed = wxGetApp().preset_bundle->is_mixed_filament(j); + if (is_target_mixed) { + item_name = wxString::Format(_L("Filament %d"), j + 1); + } else { + auto preset = wxGetApp().preset_bundle->filaments.find_preset( + wxGetApp().preset_bundle->filament_presets[j]); + item_name = preset ? from_u8(preset->label(false)) + : wxString::Format(_L("Filament %d"), j + 1); + } + + auto* mi = new wxMenuItem(sub_menu, wxID_ANY, item_name); +#ifndef __linux__ + mi->SetBitmap(*icons[j]); +#endif + sub_menu->Append(mi); + sub_menu->Bind(wxEVT_MENU, [this, cfg_idx, j](wxCommandEvent&) { + change_filament(cfg_idx, j); + }, mi->GetId()); + } + if (filaments_cnt > 1) + menu.AppendSubMenu(sub_menu, _L("Merge with")); + else + delete sub_menu; + + PopupMenu(&menu); + }); + combo_and_btn_sizer->Add(menu_btn, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(4)); + + combo_and_btn_sizer->Add(FromDIP(16), 0, 0, 0, 0); + + int side = i % 2; + auto* col = (side == 0) ? left_col : right_col; + if (side == 1 && i > 1) col->Remove(i / 2); + col->Add(combo_and_btn_sizer, 1, wxEXPAND); + if (side == 0 && i > 0) { + right_col->AddStretchSpacer(1); + } + } + } + + recalc_filament_scroll_sizes(); + + p->m_panel_filament_content->FitInside(); + p->m_mixed_scroll_area->FitInside(); + p->scrolled->Layout(); + m_scrolled_sizer->Layout(); + p->scrolled->Layout(); + + size_t total = wxGetApp().preset_bundle->filament_presets.size(); + obj_list()->update_objects_list_filament_column(total); + + // Sync mixed filament colors into the config used by 3D view rendering. + plater->update_filament_colors_in_full_config(); + obj_list()->update_filament_colors(); + + // Check if any broken mixed filament is used by objects on current plate. + // Scan raw extruder assignments (object / volume / height-range / painting) + // instead of get_extruders() which expands mixed slots and loses their IDs. + p->m_mixed_filament_broken = false; + if (!broken_slots.empty()) { + std::set broken_1based; + for (size_t s : broken_slots) broken_1based.insert(s + 1); + + auto* curr_plate = plater->get_partplate_list().get_curr_plate(); + if (curr_plate) { + for (auto& obj : plater->model().objects) { + if (!curr_plate->contain_instance_totally(obj, 0)) + continue; + // Check object-level extruder + int obj_ext = obj->config.has("extruder") ? obj->config.extruder() : 1; + if (broken_1based.count((size_t)obj_ext)) { + p->m_mixed_filament_broken = true; + break; + } + bool found = false; + for (auto* vol : obj->volumes) { + // Check volume-level extruder + int vol_ext = vol->config.has("extruder") ? vol->config.extruder() : obj_ext; + if (broken_1based.count((size_t)vol_ext)) { found = true; break; } + // Check color painting data (mmu segmentation facets) + if (vol->is_model_part() && !vol->mmu_segmentation_facets.empty()) { + for (size_t broken_slot : broken_1based) { + if (vol->mmu_segmentation_facets.has_facets(*vol, EnforcerBlockerType(broken_slot))) + { found = true; break; } + } + if (found) break; + } + } + if (found) { p->m_mixed_filament_broken = true; break; } + // Check height range modifier extruder overrides + for (auto& [range, cfg] : obj->layer_config_ranges) { + if (cfg.has("extruder")) { + int layer_ext = cfg.option("extruder")->getInt(); + if (layer_ext > 0 && broken_1based.count((size_t)layer_ext)) + { found = true; break; } + } + } + if (found) { p->m_mixed_filament_broken = true; break; } + } + } + } + + if (plater->canvas3D()) { + plater->canvas3D()->set_as_dirty(); + plater->get_view3D_canvas3D()->reload_scene(false); + } + + if (p->m_mixed_filament_broken) { + auto* mf = wxGetApp().mainframe; + if (mf) + mf->update_slice_print_status(MainFrame::eEventObjectUpdate, false); + } + + if (auto *tab = dynamic_cast(wxGetApp().plate_tab)) + tab->update_mixed_filament_seq_state(); + +} + +bool Sidebar::has_broken_mixed_filament() const +{ + auto* plater = dynamic_cast(GetParent()); + if (!plater) return false; + return has_broken_mixed_filament(plater->get_partplate_list().get_curr_plate()); +} + +bool Sidebar::has_broken_mixed_filament(const PartPlate* plate) const +{ + if (!plate) return false; + auto* plater = dynamic_cast(GetParent()); + if (!plater) return false; + + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + auto* comp_strs_opt = project_config.option("filament_mixed_components"); + if (!is_mixed_opt || !comp_strs_opt) return false; + + size_t num_physical = p->combos_filament.size(); + auto broken_slots = check_mixed_filament_integrity(is_mixed_opt->values, comp_strs_opt->values, num_physical); + + // Type consistency check + { + std::vector physical_types; + auto& preset_bundle = *wxGetApp().preset_bundle; + for (size_t i = 0; i < num_physical; ++i) { + std::string ft; + if (i < preset_bundle.filament_presets.size()) { + auto* preset = preset_bundle.filaments.find_preset(preset_bundle.filament_presets[i]); + if (preset) { + std::string display_type; + ft = preset->config.get_filament_type(display_type); + } + } + if (ft.empty()) ft = "PLA"; + physical_types.push_back(ft); + } + auto type_mismatch_slots = check_mixed_filament_type_consistency( + is_mixed_opt->values, comp_strs_opt->values, physical_types); + broken_slots.insert(broken_slots.end(), type_mismatch_slots.begin(), type_mismatch_slots.end()); + } + + if (broken_slots.empty()) return false; + + std::set broken_1based; + for (size_t s : broken_slots) broken_1based.insert(s + 1); + + // Scan model objects on the given plate for raw extruder assignments + // (don't use get_extruders() which expands mixed slots) + for (auto& entry : plater->model().objects) { + if (!plate->contain_instance_totally(entry, 0)) + continue; + // Check object-level extruder + int obj_ext = entry->config.has("extruder") ? entry->config.extruder() : 1; + if (broken_1based.count((size_t)obj_ext)) + return true; + for (auto* vol : entry->volumes) { + // Check volume-level extruder + int vol_ext = vol->config.has("extruder") ? vol->config.extruder() : obj_ext; + if (broken_1based.count((size_t)vol_ext)) + return true; + // Check color painting data (mmu segmentation facets) + if (vol->is_model_part() && !vol->mmu_segmentation_facets.empty()) { + for (size_t broken_slot : broken_1based) { + if (vol->mmu_segmentation_facets.has_facets(*vol, EnforcerBlockerType(broken_slot))) + return true; + } + } + } + // Check height range modifier extruder overrides + for (auto& [range, cfg] : entry->layer_config_ranges) { + if (cfg.has("extruder")) { + int layer_ext = cfg.option("extruder")->getInt(); + if (layer_ext > 0 && broken_1based.count((size_t)layer_ext)) + return true; + } + } + } + + return false; +} + +void Sidebar::collect_physical_filament_info(std::vector& color_strs, + std::vector& names, + std::vector& types, + std::vector* config_indices) +{ + color_strs.clear(); + names.clear(); + types.clear(); + if (config_indices) + config_indices->clear(); + + size_t num_physical = p->combos_filament.size(); + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + std::vector physical_indices; + const size_t total = wxGetApp().preset_bundle->filament_presets.size(); + physical_indices.reserve(num_physical); + for (size_t i = 0; i < total && physical_indices.size() < num_physical; ++i) { + if (!is_mixed_opt || i >= is_mixed_opt->values.size() || !is_mixed_opt->values[i]) + physical_indices.push_back(i); + } + while (physical_indices.size() < num_physical) + physical_indices.push_back(physical_indices.size()); + if (config_indices) + *config_indices = physical_indices; + + auto* colours_opt = project_config.option("filament_colour"); + if (colours_opt) { + for (size_t i = 0; i < num_physical; ++i) { + const size_t cfg_idx = physical_indices[i]; + if (cfg_idx < colours_opt->values.size()) + color_strs.push_back(colours_opt->values[cfg_idx]); + } + } + + for (size_t i = 0; i < num_physical; ++i) { + auto* combo = p->combos_filament[i]; + names.push_back(combo ? into_u8(combo->GetValue()) : "Filament " + std::to_string(i + 1)); + } + + auto& preset_bundle = *wxGetApp().preset_bundle; + for (size_t i = 0; i < num_physical; ++i) { + const size_t cfg_idx = physical_indices[i]; + Preset* preset = nullptr; + if (cfg_idx < preset_bundle.filament_presets.size()) + preset = preset_bundle.filaments.find_preset(preset_bundle.filament_presets[cfg_idx]); + types.push_back(filament_type_for_color_decompose(preset)); + } +} + +// Serialize the dialog's custom gradient curve only when it deviates from the +// direction-implied two-point linear default. Returning an empty string keeps +// projects with the default shape bit-identical with the legacy 2-field format +// (curve string stays "" so the slicer falls back to gradient_range linear). +// Shared by add_mixed_filament / edit_mixed_filament so the "is default" rule +// stays consistent between both entry points. +static std::string serialize_mixed_gradient_curve_if_custom(const MixedFilamentResult& result) +{ + if (!(result.components.size() == 2 && !result.gradient_curve.empty())) + return {}; + + const double y0 = (result.gradient_direction == 0) ? kGradientMaxRatio : kGradientMinRatio; + const double y1 = (result.gradient_direction == 0) ? kGradientMinRatio : kGradientMaxRatio; + const double eps = 1e-4; + if (result.gradient_curve.size() == 2) { + const auto& a0 = result.gradient_curve[0]; + const auto& a1 = result.gradient_curve[1]; + // Default curve also requires no tangent overrides; any finite tangent + // means the user bent the segment, so we must serialize it. + const bool is_default = + std::abs(a0.x - 0.0) < eps + && std::abs(a1.x - 1.0) < eps + && std::abs(a0.y - y0) < eps + && std::abs(a1.y - y1) < eps + && !std::isfinite(a0.m_in) && !std::isfinite(a0.m_out) + && !std::isfinite(a1.m_in) && !std::isfinite(a1.m_out); + if (is_default) return {}; + } + + Slic3r::GradientCurve gc; + gc.points = result.gradient_curve; + return Slic3r::serialize_gradient_curve(gc); +} + +static bool create_mixed_filament_from_result( + Sidebar* sidebar, + const MixedFilamentResult& result, + const std::vector& color_strs) +{ + if (!sidebar || result.components.size() < 2 || result.ratios.size() < 2) + return false; + if (!dynamic_cast(sidebar->GetParent())) + return false; + + size_t num_physical = sidebar->combos_filament().size(); + if (num_physical < 2) + return false; + if (wxGetApp().preset_bundle->filament_presets.size() >= size_t(EnforcerBlockerType::ExtruderMax)) + return false; + + auto& project_config = wxGetApp().preset_bundle->project_config; + size_t total = wxGetApp().preset_bundle->filament_presets.size(); + size_t new_idx = total; + + std::string mixed_color = blend_mixed_color(result.components, result.ratios, color_strs); + wxGetApp().preset_bundle->set_num_filaments(total + 1, mixed_color); + + auto* multi_colour_opt = project_config.option("filament_multi_colour"); + if (multi_colour_opt) { + while (multi_colour_opt->values.size() <= new_idx) multi_colour_opt->values.push_back(""); + multi_colour_opt->values[new_idx] = mixed_color; + } + + // set_num_filaments() above is what grows these parallel arrays. Guard the writes anyway, + // matching the gradient writes below, so a sizing bug degrades into a no-op rather than a + // heap overwrite. + { + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + while (is_mixed_opt->values.size() <= new_idx) is_mixed_opt->values.push_back(false); + is_mixed_opt->values[new_idx] = true; + } + + std::string comp_str; + for (size_t i = 0; i < result.components.size(); ++i) { + if (i > 0) comp_str += ","; + comp_str += std::to_string(result.components[i]); + } + { + auto* comp_opt = project_config.option("filament_mixed_components"); + while (comp_opt->values.size() <= new_idx) comp_opt->values.push_back(std::string{}); + comp_opt->values[new_idx] = comp_str; + } + + int ratio_sum = 0; + for (int r : result.ratios) ratio_sum += r; + if (ratio_sum <= 0) ratio_sum = 100; + + std::string ratio_str; + { + CNumericLocalesSetter c_locale_setter; + for (size_t i = 0; i < result.ratios.size(); ++i) { + if (i > 0) ratio_str += ","; + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.4f", (float)result.ratios[i] / ratio_sum); + ratio_str += buf; + } + } + { + auto* ratios_opt = project_config.option("filament_mixed_sublayer_ratios"); + while (ratios_opt->values.size() <= new_idx) ratios_opt->values.push_back(std::string{}); + ratios_opt->values[new_idx] = ratio_str; + } + + if (!project_config.option("filament_mixed_gradient")) + project_config.set_key_value("filament_mixed_gradient", new ConfigOptionBools({false})); + if (!project_config.option("filament_mixed_gradient_range")) + project_config.set_key_value("filament_mixed_gradient_range", new ConfigOptionStrings({""}) ); + if (!project_config.option("filament_mixed_gradient_curve")) + project_config.set_key_value("filament_mixed_gradient_curve", new ConfigOptionStrings({""}) ); + if (!project_config.option("filament_mixed_gradient_per_part")) + project_config.set_key_value("filament_mixed_gradient_per_part", new ConfigOptionBools({false})); + + { + auto* grad_opt = project_config.option("filament_mixed_gradient"); + while (grad_opt->values.size() <= new_idx) grad_opt->values.push_back(false); + grad_opt->values[new_idx] = result.gradient_enabled; + } + { + auto* grad_range_opt = project_config.option("filament_mixed_gradient_range"); + while (grad_range_opt->values.size() <= new_idx) grad_range_opt->values.push_back(""); + if (result.gradient_enabled && result.components.size() == 2) { + const char* fmt = (result.gradient_direction == 0) ? "0.9000,0.1000" : "0.1000,0.9000"; + grad_range_opt->values[new_idx] = fmt; + } else { + grad_range_opt->values[new_idx] = ""; + } + } + { + auto* grad_curve_opt = project_config.option("filament_mixed_gradient_curve"); + while (grad_curve_opt->values.size() <= new_idx) grad_curve_opt->values.push_back(""); + grad_curve_opt->values[new_idx] = serialize_mixed_gradient_curve_if_custom(result); + } + { + auto* per_part_opt = project_config.option("filament_mixed_gradient_per_part"); + while (per_part_opt->values.size() <= new_idx) per_part_opt->values.push_back(false); + per_part_opt->values[new_idx] = result.gradient_enabled && result.per_part_gradient; + } + + auto& presets = wxGetApp().preset_bundle->filament_presets; + if (result.components[0] >= 1 && result.components[0] <= num_physical && presets.size() > new_idx) + presets[new_idx] = presets[result.components[0] - 1]; + + size_t filament_count = wxGetApp().preset_bundle->filament_presets.size(); + wxGetApp().plater()->get_partplate_list().on_filament_added(filament_count); + wxGetApp().plater()->on_filament_count_change(filament_count); + wxGetApp().get_tab(Preset::TYPE_PRINT)->update(); + wxGetApp().preset_bundle->export_selections(*wxGetApp().app_config); + + sidebar->update_mixed_filament_list(); + wxGetApp().plater()->update_project_dirty_from_presets(); + wxPostEvent(sidebar, SimpleEvent(EVT_SCHEDULE_BACKGROUND_PROCESS, sidebar)); + return true; +} + +void Sidebar::add_mixed_filament() +{ + auto* plater = dynamic_cast(GetParent()); + if (!plater) return; + + size_t num_physical = p->combos_filament.size(); + if (num_physical < 2) return; + if (wxGetApp().preset_bundle->filament_presets.size() >= size_t(EnforcerBlockerType::ExtruderMax)) return; + + std::vector color_strs, names, types; + collect_physical_filament_info(color_strs, names, types); + + MixedFilamentDialog dlg(this, color_strs, names, types); + if (dlg.ShowModal() == wxID_OK) { + auto result = dlg.get_result(); + create_mixed_filament_from_result(this, result, color_strs); + } +} + +void Sidebar::edit_mixed_filament(size_t panel_idx) +{ + auto* plater = dynamic_cast(GetParent()); + if (!plater) return; + + auto mixed_indices = plater->mixed_filament_config_indices(); + if (panel_idx >= mixed_indices.size()) return; + size_t cfg_idx = mixed_indices[panel_idx]; + + std::vector color_strs, names, types; + collect_physical_filament_info(color_strs, names, types); + + auto& project_config = wxGetApp().preset_bundle->project_config; + MixedFilamentResult existing; + auto* components_opt = project_config.option("filament_mixed_components"); + auto* ratios_opt = project_config.option("filament_mixed_sublayer_ratios"); + + // Parse existing components + if (components_opt && cfg_idx < components_opt->values.size()) { + const std::string& cs = components_opt->values[cfg_idx]; + std::istringstream iss(cs); + std::string tok; + while (std::getline(iss, tok, ',')) { + unsigned int v = 0; + if (std::sscanf(tok.c_str(), "%u", &v) == 1) + existing.components.push_back(v); + } + } + // Parse existing ratios + if (ratios_opt && cfg_idx < ratios_opt->values.size()) { + CNumericLocalesSetter c_locale_setter; + const std::string& rs = ratios_opt->values[cfg_idx]; + std::istringstream iss(rs); + std::string tok; + while (std::getline(iss, tok, ',')) { + float v = 0; + if (std::sscanf(tok.c_str(), "%f", &v) == 1) + existing.ratios.push_back((int)(v * 100 + 0.5f)); + } + } + if (existing.components.size() < 2) { + existing.components = {1, 2}; + existing.ratios = {50, 50}; + } else if (existing.ratios.size() != existing.components.size()) { + BOOST_LOG_TRIVIAL(warning) << "Mixed filament edit: ratio count (" + << existing.ratios.size() << ") != component count (" + << existing.components.size() + << "), resetting to even distribution"; + int n = (int)existing.components.size(); + existing.ratios.assign(n, 100 / n); + existing.ratios[0] += 100 - (100 / n) * n; + } + + // Read gradient settings + auto* grad_opt = project_config.option("filament_mixed_gradient"); + if (grad_opt && cfg_idx < grad_opt->values.size()) + existing.gradient_enabled = grad_opt->values[cfg_idx]; + auto* grad_range_opt = project_config.option("filament_mixed_gradient_range"); + if (existing.gradient_enabled && grad_range_opt && cfg_idx < grad_range_opt->values.size()) { + CNumericLocalesSetter c_locale_setter; + float v0 = 0, v1 = 0; + if (std::sscanf(grad_range_opt->values[cfg_idx].c_str(), "%f,%f", &v0, &v1) == 2) + existing.gradient_direction = (v0 > v1) ? 0 : 1; + } + auto* grad_curve_opt = project_config.option("filament_mixed_gradient_curve"); + if (existing.gradient_enabled && grad_curve_opt && cfg_idx < grad_curve_opt->values.size()) { + auto curve = Slic3r::parse_gradient_curve(grad_curve_opt->values[cfg_idx]); + existing.gradient_curve = curve.points; + } + auto* per_part_opt = project_config.option("filament_mixed_gradient_per_part"); + if (existing.gradient_enabled && per_part_opt && cfg_idx < per_part_opt->values.size()) + existing.per_part_gradient = per_part_opt->values[cfg_idx]; + + MixedFilamentDialog dlg(this, existing, color_strs, names, types); + if (dlg.ShowModal() == wxID_OK) { + auto result = dlg.get_result(); + if (result.components.size() < 2 || result.ratios.size() < 2) return; + + // Serialize components + std::string comp_str; + for (size_t i = 0; i < result.components.size(); ++i) { + if (i > 0) comp_str += ","; + comp_str += std::to_string(result.components[i]); + } + components_opt->values[cfg_idx] = comp_str; + + // Serialize ratios + int ratio_sum = 0; + for (int r : result.ratios) ratio_sum += r; + if (ratio_sum <= 0) ratio_sum = 100; + + std::string ratio_str; + { + CNumericLocalesSetter c_locale_setter; + for (size_t i = 0; i < result.ratios.size(); ++i) { + if (i > 0) ratio_str += ","; + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.4f", (float)result.ratios[i] / ratio_sum); + ratio_str += buf; + } + } + ratios_opt->values[cfg_idx] = ratio_str; + + // Gradient settings — ensure keys exist in dynamic config + if (!project_config.option("filament_mixed_gradient")) + project_config.set_key_value("filament_mixed_gradient", new ConfigOptionBools({false})); + if (!project_config.option("filament_mixed_gradient_range")) + project_config.set_key_value("filament_mixed_gradient_range", new ConfigOptionStrings({""}) ); + if (!project_config.option("filament_mixed_gradient_curve")) + project_config.set_key_value("filament_mixed_gradient_curve", new ConfigOptionStrings({""}) ); + if (!project_config.option("filament_mixed_gradient_per_part")) + project_config.set_key_value("filament_mixed_gradient_per_part", new ConfigOptionBools({false})); + + { + auto* grad_opt = project_config.option("filament_mixed_gradient"); + while (grad_opt->values.size() <= cfg_idx) grad_opt->values.push_back(false); + grad_opt->values[cfg_idx] = result.gradient_enabled; + } + { + auto* grad_range_opt = project_config.option("filament_mixed_gradient_range"); + while (grad_range_opt->values.size() <= cfg_idx) grad_range_opt->values.push_back(""); + if (result.gradient_enabled && result.components.size() == 2) { + const char* fmt = (result.gradient_direction == 0) ? "0.9000,0.1000" : "0.1000,0.9000"; + grad_range_opt->values[cfg_idx] = fmt; + } else { + grad_range_opt->values[cfg_idx] = ""; + } + } + { + auto* grad_curve_opt = project_config.option("filament_mixed_gradient_curve"); + while (grad_curve_opt->values.size() <= cfg_idx) grad_curve_opt->values.push_back(""); + grad_curve_opt->values[cfg_idx] = serialize_mixed_gradient_curve_if_custom(result); + } + { + auto* per_part_opt = project_config.option("filament_mixed_gradient_per_part"); + while (per_part_opt->values.size() <= cfg_idx) per_part_opt->values.push_back(false); + per_part_opt->values[cfg_idx] = result.gradient_enabled && result.per_part_gradient; + } + + // Compute blended color + std::string blended = blend_mixed_color(result.components, result.ratios, color_strs); + auto* colours_opt = project_config.option("filament_colour"); + if (colours_opt && cfg_idx < colours_opt->values.size()) + colours_opt->values[cfg_idx] = blended; + + auto* multi_colour_opt = project_config.option("filament_multi_colour"); + if (multi_colour_opt && cfg_idx < multi_colour_opt->values.size()) + multi_colour_opt->values[cfg_idx] = blended; + + update_mixed_filament_list(); + wxGetApp().plater()->update_project_dirty_from_presets(); + wxPostEvent(this, SimpleEvent(EVT_SCHEDULE_BACKGROUND_PROCESS, this)); + } +} + +void Sidebar::delete_mixed_filament_at(size_t panel_idx) +{ + auto* plater = dynamic_cast(GetParent()); + if (!plater) return; + + auto mixed_indices = plater->mixed_filament_config_indices(); + if (panel_idx >= mixed_indices.size()) return; + size_t cfg_idx = mixed_indices[panel_idx]; + + delete_filament(cfg_idx, -1); +} + +void Sidebar::decompose_filament_color(int filament_idx) +{ + if (filament_idx == kSidebarContextMenuFilamentId) + filament_idx = p->m_menu_filament_id; + if (filament_idx < 0) + return; + + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* colours_opt = project_config.option("filament_colour"); + if (!colours_opt || static_cast(filament_idx) >= colours_opt->values.size()) + return; + + wxColour target_color(colours_opt->values[filament_idx]); + + std::vector color_strs, names, types; + std::vector physical_config_indices; + collect_physical_filament_info(color_strs, names, types, &physical_config_indices); + size_t source_physical_idx = size_t(-1); + for (size_t i = 0; i < physical_config_indices.size(); ++i) { + if (physical_config_indices[i] == static_cast(filament_idx)) { + source_physical_idx = i; + break; + } + } + + ColorDecomposeDialog dlg(this, + source_physical_idx == size_t(-1) ? -1 : static_cast(source_physical_idx), + target_color, color_strs, names, types, + wxGetApp().preset_bundle->filament_presets.size(), + static_cast(EnforcerBlockerType::ExtruderMax), + physical_config_indices); + int modal_res = dlg.ShowModal(); + if (modal_res == wxID_OK) { + ColorDecomposeResult dialog_result = dlg.get_result(); + MixedFilamentResult mixed_result; + std::vector missing_components; + if (!prepare_decompose_mixed_result(dialog_result, static_cast(filament_idx), source_physical_idx, + color_strs, types, physical_config_indices, mixed_result, missing_components)) + return; + + if (!confirm_create_decompose_missing_components(this, missing_components)) + return; + + for (const DecomposeMissingComponent& missing : missing_components) { + size_t before_count = p->combos_filament.size(); + add_custom_filament(wxColour(missing.official_component.color_hex), missing.preset_name, true); + size_t after_count = p->combos_filament.size(); + if (after_count <= before_count) + return; + set_created_standard_component_metadata(before_count, missing.official_component); + if (missing.component_idx < mixed_result.components.size()) + mixed_result.components[missing.component_idx] = static_cast(before_count + 1); + } + + if (!missing_components.empty()) { + collect_physical_filament_info(color_strs, names, types, &physical_config_indices); + } + + create_mixed_filament_from_result(this, mixed_result, color_strs); + } +} + void Sidebar::update_filaments_area_height() // ORCA { @@ -3918,6 +5172,9 @@ void Sidebar::sys_color_changed() p->scrolled->Layout(); + // Mixed rows are custom-drawn, so they need rebuilding for the new theme colours. + update_mixed_filament_list(); + p->searcher.dlg_sys_color_changed(); } @@ -3952,21 +5209,39 @@ void Sidebar::jump_to_option(size_t selected) // BBS. Move logic from Plater::on_extruders_change() to Sidebar::on_filament_count_change(). void Sidebar::on_filament_count_change(size_t num_filaments) { + // num_filaments counts every slot; mixed-color slots are virtual and get no combo of + // their own (they are rendered by update_mixed_filament_list instead), so the physical + // subset drives the combo list. + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + + std::vector physical_indices; + for (size_t i = 0; i < num_filaments; ++i) { + if (!is_mixed_opt || i >= is_mixed_opt->values.size() || !is_mixed_opt->values[i]) + physical_indices.push_back(i); + } + const size_t num_physical = physical_indices.size(); + auto& choices = combos_filament(); - if (num_filaments == choices.size()) + if (num_physical == choices.size()) { + // The ctor pre-creates one combo, so a single-filament project hits this guard before + // any layout pass has sized the scroll areas; refresh them here as well. + recalc_filament_scroll_sizes(); + update_mixed_filament_list(); return; + } - if (choices.size() == 1 || num_filaments == 1) + if (choices.size() == 1 || num_physical == 1) choices[0]->GetDropDown().Invalidate(); wxWindowUpdateLocker noUpdates_scrolled_panel(this); size_t i = choices.size(); - while (i < num_filaments) + while (i < num_physical) { PlaterPresetComboBox* choice/*{ nullptr }*/; - init_filament_combo(&choice, i); + init_filament_combo(&choice, physical_indices[i]); int last_selection = choices.back()->GetSelection(); choices.push_back(choice); @@ -3977,11 +5252,13 @@ void Sidebar::on_filament_count_change(size_t num_filaments) } // remove unused choices if any - remove_unused_filament_combos(num_filaments); + remove_unused_filament_combos(num_physical); show_SEMM_buttons(); // ORCA update_filaments_area_height(); // ORCA + recalc_filament_scroll_sizes(); + update_mixed_filament_list(); Layout(); p->m_panel_filament_title->Refresh(); @@ -4033,6 +5310,8 @@ void Sidebar::on_filaments_delete(size_t filament_id) } update_filaments_area_height(); // ORCA + recalc_filament_scroll_sizes(); + update_mixed_filament_list(); Layout(); p->m_panel_filament_title->Refresh(); @@ -4106,18 +5385,93 @@ void Sidebar::edit_filament() p->editing_filament = p->m_menu_filament_id; // sync with TabPresetComboxBox's m_filament_idx } -void Sidebar::add_custom_filament(wxColour new_col) { +void Sidebar::add_custom_filament(wxColour new_col, const std::string& preset_name, bool /*skip_preset_validation*/) { if (is_new_project_in_gcode3mf()) { return; } if (p->combos_filament.size() >= MAXIMUM_EXTRUDER_NUMBER) return; + if (wxGetApp().preset_bundle->filament_presets.size() >= MAXIMUM_EXTRUDER_NUMBER) return; - int filament_count = p->combos_filament.size() + 1; + // Mixed-color slots are kept at the tail of the filament arrays, so a new physical + // filament has to be inserted just after the last physical one rather than appended. + // total == every slot (physical + mixed); insert_pos == the physical slot count. + size_t total = wxGetApp().preset_bundle->filament_presets.size(); + size_t insert_pos = p->combos_filament.size(); + int filament_count = (int)(total + 1); std::string new_color = new_col.GetAsString(wxC2S_HTML_SYNTAX).ToStdString(); wxGetApp().preset_bundle->set_num_filaments(filament_count, new_color); + + // Maintain physical-first ordering: rotate the new slot from end to insert_pos. + // No mixed slots -> insert_pos == total -> every rotate below is a no-op. + if (insert_pos < total) { + auto& presets = wxGetApp().preset_bundle->filament_presets; + std::rotate(presets.begin() + insert_pos, presets.begin() + total, presets.end()); + + auto& project_config = wxGetApp().preset_bundle->project_config; + auto& ams_mc = wxGetApp().preset_bundle->ams_multi_color_filment; + + auto rotate_strings = [&](const char* key) { + if (auto* opt = project_config.option(key)) + if (opt->values.size() > total) + std::rotate(opt->values.begin() + insert_pos, opt->values.begin() + total, opt->values.end()); + }; + auto rotate_ints = [&](const char* key) { + if (auto* opt = project_config.option(key)) + if (opt->values.size() > total) + std::rotate(opt->values.begin() + insert_pos, opt->values.begin() + total, opt->values.end()); + }; + auto rotate_bools = [&](const char* key) { + if (auto* opt = project_config.option(key)) + if (opt->values.size() > total) + std::rotate(opt->values.begin() + insert_pos, opt->values.begin() + total, opt->values.end()); + }; + + rotate_strings("filament_colour"); + rotate_strings("filament_multi_colour"); + rotate_strings("filament_colour_type"); + rotate_ints("filament_map"); + rotate_ints("filament_nozzle_map"); + rotate_ints("filament_volume_map"); + rotate_bools("filament_is_mixed"); + rotate_strings("filament_mixed_components"); + rotate_strings("filament_mixed_sublayer_ratios"); + rotate_bools("filament_mixed_gradient"); + rotate_strings("filament_mixed_gradient_range"); + rotate_strings("filament_mixed_gradient_curve"); + rotate_bools("filament_mixed_gradient_per_part"); + + if (ams_mc.size() > total) + std::rotate(ams_mc.begin() + insert_pos, ams_mc.begin() + total, ams_mc.end()); + + // Remap object/volume extruder IDs and paint data: anything >= insert_pos+1 (1-based) shifts up by 1 + int threshold_1based = (int)(insert_pos + 1); + auto ebt_threshold = EnforcerBlockerType(threshold_1based); + for (auto* obj : wxGetApp().plater()->model().objects) { + if (obj->config.has("extruder")) { + int ext = obj->config.extruder(); + if (ext >= threshold_1based) + obj->config.set("extruder", ext + 1); + } + for (auto* vol : obj->volumes) { + if (vol->config.has("extruder")) { + int ext = vol->config.extruder(); + if (ext >= threshold_1based) + vol->config.set("extruder", ext + 1); + } + vol->mmu_segmentation_facets.shift_states_above(*vol, ebt_threshold, +1); + } + } + } + + if (!preset_name.empty() && + wxGetApp().preset_bundle->filaments.find_preset(preset_name, false) && + insert_pos < wxGetApp().preset_bundle->filament_presets.size()) { + wxGetApp().preset_bundle->filament_presets[insert_pos] = preset_name; + } + wxGetApp().plater()->get_partplate_list().on_filament_added(filament_count); wxGetApp().plater()->on_filament_count_change(filament_count); wxGetApp().get_tab(Preset::TYPE_PRINT)->update(); wxGetApp().preset_bundle->export_selections(*wxGetApp().app_config); - auto_calc_flushing_volumes(filament_count - 1); + auto_calc_flushing_volumes(insert_pos); } bool Sidebar::is_new_project_in_gcode3mf() @@ -5457,9 +6811,34 @@ struct Plater::priv BoundingBox scaled_bed_shape_bb() const; // BBS: backup & restore + using LoadProgressCallback = std::function; std::vector load_files(const std::vector& input_files, LoadStrategy strategy, bool ask_multi = false); std::vector load_model_objects(const ModelObjectPtrs& model_objects, bool allow_negative_z = false, bool split_object = false, bool auto_drop = true); + // Texture-to-color import: a mesh loaded with UVs + a texture map gets its faces clustered + // into printable colours, which are then matched against (or added to) the filament list. + struct TextureImportResult { + Slic3r::PaintedMesh painted; + std::vector matches; + std::vector> new_filament_colors; + std::vector new_filament_preset_names; + std::vector new_mixed_filaments; + std::vector filament_entries; + size_t existing_filament_count = 0; + bool skipped = false; + bool fallback_to_geometry_only = false; + wxString fallback_warning; + }; + + bool run_textured_mesh_import_dialog(Slic3r::Model& loaded_model, TextureImportResult& result, + std::function cancel_callback = {}, + std::function progress_callback = {}); + void apply_textured_mesh_import_result(Slic3r::Model& loaded_model, const std::vector& obj_idxs, + const TextureImportResult& result, + LoadProgressCallback progress_callback = {}, bool update_scene = true); + void handle_textured_mesh_import(Slic3r::Model& model, const std::vector& obj_idxs, + std::function cancel_callback = {}); + fs::path get_export_file_path(GUI::FileType file_type); wxString get_export_file(GUI::FileType file_type); @@ -7730,6 +9109,38 @@ std::vector Plater::priv::load_files(const std::vector& input_ q->model().load_from(model); load_auxiliary_files(); } + // Texture-to-color: a mesh that arrived with UVs and a decoded texture gets its + // faces clustered into printable colours and matched against the filament list, + // before the objects are handed to the plater. Inert for every other model. + if (model.texture_mesh && has_importable_texture(*model.texture_mesh)) { + TextureImportResult texture_import_result; + auto cancel_cb = [&dlg, &dlg_cont]() { return !dlg_cont || dlg.WasCancelled(); }; + auto progress_cb = [&dlg, &dlg_cont, &progress_percent](int percent) { + progress_percent = std::clamp(percent, 0, 100); + dlg_cont = dlg.Update(progress_percent, _L("Matching textures to filaments")); + return dlg_cont; + }; + if (!run_textured_mesh_import_dialog(model, texture_import_result, cancel_cb, progress_cb)) { + q->skip_thumbnail_invalid = false; + return empty_result; + } + if (texture_import_result.fallback_to_geometry_only && !texture_import_result.fallback_warning.empty()) { + MessageDialog(q, texture_import_result.fallback_warning, + _L("Texture Import Warning"), + wxOK | wxICON_WARNING).ShowModal(); + } + if (!texture_import_result.painted.face_colors.empty()) { + std::vector texture_object_idxs(model.objects.size()); + std::iota(texture_object_idxs.begin(), texture_object_idxs.end(), 0); + auto apply_progress_cb = [&dlg](int percent, const wxString& msg) { + dlg.Update(std::clamp(percent, 0, 100), msg); + return true; + }; + apply_textured_mesh_import_result(model, texture_object_idxs, texture_import_result, + apply_progress_cb, false); + } + } + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ":" << __LINE__ << boost::format(", before load_model_objects, count %1%")%model.objects.size(); auto loaded_idxs = load_model_objects(model.objects, is_project_file); obj_idxs.insert(obj_idxs.end(), loaded_idxs.begin(), loaded_idxs.end()); @@ -11425,6 +12836,9 @@ void Plater::priv::on_filament_color_changed(wxCommandEvent &event) if (wxGetApp().app_config->get("auto_calculate_flush") != "disabled") { sidebar->auto_calc_flushing_volumes(modify_id); } + + // A mixed slot's colour is derived from its components, so recompute the swatches. + sidebar->update_mixed_filament_list(); } void Plater::priv::install_network_plugin(wxCommandEvent &event) @@ -13034,6 +14448,348 @@ void Plater::reset_project_dirty_initial_presets() { p->reset_project_dirty_init void Plater::render_project_state_debug_window() const { p->render_project_state_debug_window(); } #endif // ENABLE_PROJECT_DIRTY_STATE_DEBUG_WINDOW +std::vector Plater::mixed_filament_config_indices() const +{ + std::vector indices; + auto& config = wxGetApp().preset_bundle->project_config; + auto* opt = config.option("filament_is_mixed"); + if (!opt) return indices; + for (size_t i = 0; i < opt->values.size(); ++i) + if (opt->values[i]) indices.push_back(i); + return indices; +} + +std::vector Plater::physical_filament_config_indices() const +{ + std::vector indices; + auto& config = wxGetApp().preset_bundle->project_config; + auto* opt = config.option("filament_is_mixed"); + size_t total = wxGetApp().preset_bundle->filament_presets.size(); + for (size_t i = 0; i < total; ++i) { + if (!opt || i >= opt->values.size() || !opt->values[i]) + indices.push_back(i); + } + return indices; +} + +bool Plater::priv::run_textured_mesh_import_dialog(Slic3r::Model& loaded_model, TextureImportResult& result, + std::function cancel_callback, + std::function progress_callback) +{ + if (!loaded_model.texture_mesh || !has_importable_texture(*loaded_model.texture_mesh)) return false; + + // Defense in depth: if all geometry got dropped earlier (e.g. by a future + // regression of the zero-volume cleanup) but the textured mesh is still + // alive, there is nothing for the dialog to paint onto. Skip the dialog + // gracefully so load_files() can fall through to its "no geometry" + // message instead of making the user round-trip a meaningless matcher. + if (loaded_model.objects.empty()) { + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: skipping dialog because the loaded model has no geometry objects"; + loaded_model.texture_mesh.reset(); + result.skipped = true; + return true; + } + + const wxString fallback_warning = _L("Texture import failed. The model appears to contain texture data, but the texture import process could not be completed. The model will be imported as geometry only."); + + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: opening texture import dialog"; + + std::vector filament_entries; + { + auto& preset_bundle = *wxGetApp().preset_bundle; + auto& project_config = preset_bundle.project_config; + auto* colours_opt = project_config.option("filament_colour"); + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + auto* type_opt = project_config.option("filament_type"); + auto* components_opt = project_config.option("filament_mixed_components"); + auto* ratios_opt = project_config.option("filament_mixed_sublayer_ratios"); + const size_t total = preset_bundle.filament_presets.size(); + filament_entries.reserve(total); + for (size_t i = 0; i < total; ++i) { + TextureFilamentEntry entry; + entry.kind = (is_mixed_opt && i < is_mixed_opt->values.size() && is_mixed_opt->values[i]) ? + TextureFilamentKind::ExistingMixed : TextureFilamentKind::ExistingPhysical; + entry.dialog_index = (int)filament_entries.size(); + entry.project_config_index = i; + entry.color_hex = (colours_opt && i < colours_opt->values.size()) ? colours_opt->values[i] : "#808080"; + entry.type = (type_opt && i < type_opt->values.size()) ? type_opt->values[i] : ""; + + std::string name; + if (i < preset_bundle.filament_presets.size()) { + auto* preset = preset_bundle.filaments.find_preset(preset_bundle.filament_presets[i]); + if (preset) + name = preset->label(false); + } + if (name.empty()) + name = "Filament " + std::to_string(i + 1); + entry.name = name; + + if (entry.kind == TextureFilamentKind::ExistingMixed) { + if (components_opt && i < components_opt->values.size()) + entry.mixed_components = Slic3r::parse_mixed_components(components_opt->values[i]); + std::vector ratios = Slic3r::parse_mixed_ratios( + ratios_opt && i < ratios_opt->values.size() ? ratios_opt->values[i] : "", + entry.mixed_components.size()); + entry.mixed_ratios.reserve(ratios.size()); + for (double ratio : ratios) + entry.mixed_ratios.push_back((int)std::lround(ratio * 100.0)); + } + filament_entries.push_back(std::move(entry)); + } + } + + TextureImportDialog dlg(q, *loaded_model.texture_mesh, filament_entries, + std::move(cancel_callback), std::move(progress_callback)); + if (dlg.ShowModal() != wxID_OK) { + if (dlg.was_skipped()) { + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: user skipped texture matching"; + result.skipped = true; + loaded_model.texture_mesh.reset(); + return true; + } + if (dlg.fallback_to_geometry_only()) { + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: texture import failed, falling back to geometry-only import"; + result.fallback_to_geometry_only = true; + result.fallback_warning = fallback_warning; + loaded_model.texture_mesh.reset(); + return true; + } + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: user cancelled"; + loaded_model.texture_mesh.reset(); + return false; + } + + auto painted = dlg.get_painted_mesh(); + auto final_matches = dlg.get_matches(); + + if (painted.face_colors.empty() || final_matches.empty()) { + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: no painting result"; + result.fallback_to_geometry_only = true; + result.fallback_warning = fallback_warning; + loaded_model.texture_mesh.reset(); + return true; + } + + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: got " << painted.cluster_colors.size() + << " clusters, skipped=" << dlg.was_skipped(); + + result.painted = std::move(painted); + result.matches = std::move(final_matches); + result.new_filament_colors = dlg.get_new_filament_colors(); + result.new_filament_preset_names = dlg.get_new_filament_preset_names(); + result.new_mixed_filaments = dlg.get_new_mixed_filaments(); + result.filament_entries = dlg.get_filament_entries(); + result.existing_filament_count = dlg.get_existing_filament_count(); + result.skipped = dlg.was_skipped(); + return true; +} + +void Plater::priv::apply_textured_mesh_import_result(Slic3r::Model& loaded_model, const std::vector& obj_idxs, + const TextureImportResult& result, + LoadProgressCallback progress_callback, bool update_scene) +{ + auto update_apply_progress = [&progress_callback](int percent, const wxString& message) { + return !progress_callback || progress_callback(std::clamp(percent, 0, 100), message); + }; + + const auto& painted = result.painted; + const auto& final_matches = result.matches; + + if (painted.face_colors.empty() || final_matches.empty()) { + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: no painting result"; + loaded_model.texture_mesh.reset(); + return; + } + + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: got " << painted.cluster_colors.size() + << " clusters, skipped=" << result.skipped; + if (!update_apply_progress(0, _L("Applying texture colors..."))) + return; + + auto collect_physical_color_strs = []() { + std::vector colors; + auto& project_config = wxGetApp().preset_bundle->project_config; + auto* colours_opt = project_config.option("filament_colour"); + auto* is_mixed_opt = project_config.option("filament_is_mixed"); + const size_t total = wxGetApp().preset_bundle->filament_presets.size(); + for (size_t i = 0; i < total; ++i) { + const bool is_mixed = is_mixed_opt && i < is_mixed_opt->values.size() && is_mixed_opt->values[i]; + if (!is_mixed) + colors.push_back(colours_opt && i < colours_opt->values.size() ? colours_opt->values[i] : "#808080"); + } + return colors; + }; + + const auto& entries = result.filament_entries; + std::vector filament_index_remap(entries.size(), -1); + size_t existing_physical_count = 0; + size_t new_physical_count = 0; + for (const auto& entry : entries) { + if (entry.kind == TextureFilamentKind::ExistingPhysical) + ++existing_physical_count; + else if (entry.kind == TextureFilamentKind::NewPhysical) + ++new_physical_count; + } + + for (const auto& entry : entries) { + if (entry.dialog_index < 0 || entry.dialog_index >= (int)filament_index_remap.size()) + continue; + if (entry.kind == TextureFilamentKind::ExistingPhysical) { + filament_index_remap[entry.dialog_index] = (int)entry.project_config_index; + } else if (entry.kind == TextureFilamentKind::ExistingMixed) { + filament_index_remap[entry.dialog_index] = (int)(entry.project_config_index + new_physical_count); + } + } + + size_t new_physical_order = 0; + for (const auto& entry : entries) { + if (entry.kind != TextureFilamentKind::NewPhysical) + continue; + wxColour new_col(entry.color_hex); + const size_t final_idx = existing_physical_count + new_physical_order; + sidebar->add_custom_filament(new_col, entry.preset_name); + if (entry.dialog_index >= 0 && entry.dialog_index < (int)filament_index_remap.size()) + filament_index_remap[entry.dialog_index] = (int)final_idx; + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: created pending physical filament dialog=" + << entry.dialog_index << " final=" << final_idx + << " color=" << entry.color_hex + << " preset=" << entry.preset_name; + ++new_physical_order; + } + + std::vector physical_colors_for_mixing = collect_physical_color_strs(); + for (const auto& mixed : result.new_mixed_filaments) { + MixedFilamentResult mixed_result; + mixed_result.ratios = mixed.ratios; + mixed_result.components.reserve(mixed.component_dialog_indices.size()); + bool valid_components = true; + for (int component_dialog_idx : mixed.component_dialog_indices) { + if (component_dialog_idx < 0 || component_dialog_idx >= (int)filament_index_remap.size() || + filament_index_remap[component_dialog_idx] < 0) { + valid_components = false; + break; + } + mixed_result.components.push_back((unsigned int)(filament_index_remap[component_dialog_idx] + 1)); + } + if (!valid_components || mixed_result.components.size() < 2 || + mixed_result.components.size() != mixed_result.ratios.size()) { + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: invalid pending mixed filament dialog=" + << mixed.dialog_index; + continue; + } + + const int final_idx = (int)wxGetApp().preset_bundle->filament_presets.size(); + if (create_mixed_filament_from_result(sidebar, mixed_result, physical_colors_for_mixing)) { + if (mixed.dialog_index >= 0 && mixed.dialog_index < (int)filament_index_remap.size()) + filament_index_remap[mixed.dialog_index] = final_idx; + physical_colors_for_mixing = collect_physical_color_strs(); + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: created pending mixed filament dialog=" + << mixed.dialog_index << " final=" << final_idx; + } + } + + std::vector remapped_matches = final_matches; + for (auto& m : remapped_matches) { + if (m.filament_index < 0) + continue; + if (m.filament_index < (int)filament_index_remap.size() && filament_index_remap[m.filament_index] >= 0) { + m.filament_index = filament_index_remap[m.filament_index]; + } else { + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: invalid filament index " + << m.filament_index << " in texture mapping"; + m.filament_index = -1; + } + } + + int min_used_filament_1based = -1; + { + std::map, int> color_to_filament; + for (const auto& m : remapped_matches) { + if (m.cluster_index >= 0 && m.cluster_index < (int)painted.cluster_colors.size() && m.filament_index >= 0) + color_to_filament[painted.cluster_colors[m.cluster_index]] = m.filament_index + 1; + } + for (const auto& face_color : painted.face_colors) { + auto it = color_to_filament.find(face_color); + if (it == color_to_filament.end()) + continue; + if (min_used_filament_1based < 0 || it->second < min_used_filament_1based) + min_used_filament_1based = it->second; + } + } + if (min_used_filament_1based < 0) + BOOST_LOG_TRIVIAL(warning) << "handle_textured_mesh_import: cannot determine base filament from painted faces"; + + if (!update_apply_progress(25, _L("Applying texture colors..."))) + return; + + for (size_t obj_order = 0; obj_order < obj_idxs.size(); ++obj_order) { + size_t idx = obj_idxs[obj_order]; + if (idx >= loaded_model.objects.size()) continue; + ModelObject* obj = loaded_model.objects[idx]; + if (!obj) continue; + + // painted is derived from the whole textured mesh and is meaningful + // only against a single MODEL_PART volume. Applying it to every + // volume of a multi-part / modifier object would overwrite each + // volume with the same painted geometry. Restrict to the first + // model_part and warn when the object holds more than one. + ModelVolume* target = nullptr; + int part_count = 0; + for (ModelVolume* vol : obj->volumes) { + if (vol && vol->is_model_part()) { + ++part_count; + if (!target) target = vol; + } + } + if (!target) continue; + if (part_count > 1) { + BOOST_LOG_TRIVIAL(warning) + << "handle_textured_mesh_import: object has " << part_count + << " model parts; painting only applied to the first part."; + } + if (Slic3r::apply_painted_mesh_to_volume(painted, remapped_matches, *target) + && min_used_filament_1based > 0) { + target->config.set("extruder", min_used_filament_1based); + obj->config.set("extruder", min_used_filament_1based); + if (update_scene) { + if (auto* obj_list = wxGetApp().obj_list()) { + obj_list->update_objects_list_filament_column(std::max( + wxGetApp().filaments_cnt(), (size_t)min_used_filament_1based)); + obj_list->update_info_items(idx); + } + } + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: set base filament to " + << min_used_filament_1based << " for object index " << idx + << ", object extruder=" << obj->config.extruder() + << ", volume extruder=" << target->config.extruder(); + } + // bbox invalidation is performed inside apply_painted_mesh_to_volume. + obj->ensure_on_bed(); + const int object_percent = 25 + (int)(60 * (obj_order + 1) / std::max(obj_idxs.size(), 1)); + if (!update_apply_progress(object_percent, _L("Applying texture colors..."))) + return; + } + + BOOST_LOG_TRIVIAL(info) << "handle_textured_mesh_import: painting applied to model volumes"; + loaded_model.texture_mesh.reset(); + if (update_scene) { + if (!update_apply_progress(90, _L("Updating 3D view..."))) + return; + update(); + } + update_apply_progress(100, _L("Texture colors applied.")); +} + +void Plater::priv::handle_textured_mesh_import(Slic3r::Model& loaded_model, const std::vector& obj_idxs, + std::function cancel_callback) +{ + TextureImportResult result; + if (!run_textured_mesh_import_dialog(loaded_model, result, std::move(cancel_callback))) + return; + if (!result.painted.face_colors.empty()) + apply_textured_mesh_import_result(loaded_model, obj_idxs, result); +} + Sidebar& Plater::sidebar() { return *p->sidebar; } const Model& Plater::model() const { return p->model; } Model& Plater::model() { return p->model; } diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 6a42f61fd5..49bc247c59 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -86,6 +86,10 @@ using t_optgroups = std::vector >; class Plater; enum class ActionButtonType : int; +// Sentinel filament id meaning "use the slot the sidebar context menu was opened on" +// (Sidebar::priv::m_menu_filament_id) rather than an explicit index. +inline constexpr int kSidebarContextMenuFilamentId = -2; + #define EVT_PUBLISHING_START 1 #define EVT_PUBLISHING_STOP 2 @@ -188,7 +192,7 @@ public: void delete_filament(size_t filament_id = size_t(-1), int replace_filament_id = -1); // 0 base, -1 means default void change_filament(size_t from_id, size_t to_id); // 0 base void edit_filament(); - void add_custom_filament(wxColour new_col); + void add_custom_filament(wxColour new_col, const std::string& preset_name = std::string(), bool skip_preset_validation = false); bool is_new_project_in_gcode3mf(); // BBS void on_bed_type_change(BedType bed_type); @@ -262,6 +266,20 @@ public: std::vector& combos_filament(); void clear_combos_filament_badge(); void udpate_combos_filament_badge(); + + // Mixed-color filament sidebar section + void add_mixed_filament(); + void edit_mixed_filament(size_t idx); + void delete_mixed_filament_at(size_t idx); + void decompose_filament_color(int filament_idx); + void recalc_filament_scroll_sizes(); + void update_mixed_filament_list(); + bool has_broken_mixed_filament() const; + bool has_broken_mixed_filament(const PartPlate* plate) const; + void collect_physical_filament_info(std::vector& color_strs, + std::vector& names, + std::vector& types, + std::vector* config_indices = nullptr); Search::OptionsSearcher& get_searcher(); std::string& get_search_line(); void update_printer_thumbnail(); @@ -313,6 +331,11 @@ public: const SLAPrint& sla_print() const; SLAPrint& sla_print(); + // Helper: returns config indices where filament_is_mixed == true + std::vector mixed_filament_config_indices() const; + // Helper: returns config indices where filament_is_mixed == false + std::vector physical_filament_config_indices() const; + int new_project(bool skip_confirm = false, bool silent = false, const wxString& project_name = wxString()); // BBS: save & backup void load_project(wxString const & filename = "", wxString const & originfile = "-"); diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index bec3bed20b..42796dd3e5 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -4,6 +4,7 @@ #include "PresetHints.hpp" #include "libslic3r/PresetBundle.hpp" #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/FilamentMixer.hpp" #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" @@ -3497,6 +3498,21 @@ void TabPrintModel::activate_selected_page(std::function throw_if_cancel f->set_value(boost::any(), false); } } + if (m_type == Preset::TYPE_PLATE) + static_cast(this)->update_mixed_filament_seq_state(); +} + +// A mixed-color slot resolves to a different physical filament per layer, so a +// user-defined filament print order cannot be honoured while one exists. +void TabPrintPlate::update_mixed_filament_seq_state() +{ + if (!m_active_page) return; + auto &proj_cfg = m_preset_bundle->project_config; + auto *opt = proj_cfg.option("filament_is_mixed"); + bool has_mixed = opt && has_any_mixed_filament(opt->values); + + toggle_option("first_layer_sequence_choice", !has_mixed); + toggle_option("other_layers_sequence_choice", !has_mixed); } void TabPrintModel::on_value_change(const std::string& opt_id, const boost::any& value) diff --git a/src/slic3r/GUI/Tab.hpp b/src/slic3r/GUI/Tab.hpp index 19eb0b849d..4f9cbf7b72 100644 --- a/src/slic3r/GUI/Tab.hpp +++ b/src/slic3r/GUI/Tab.hpp @@ -545,6 +545,8 @@ public: void build() override; void reset_model_config() override; int show_spiral_mode_settings_dialog(bool is_object_config) { return m_config_manipulation.show_spiral_mode_settings_dialog(is_object_config); } + // Disables the user-defined filament print order while a mixed-color filament exists. + void update_mixed_filament_seq_state(); protected: virtual void on_value_change(const std::string& opt_key, const boost::any& value) override; diff --git a/src/slic3r/GUI/TextureImportDialog.cpp b/src/slic3r/GUI/TextureImportDialog.cpp new file mode 100644 index 0000000000..c9e5fb2147 --- /dev/null +++ b/src/slic3r/GUI/TextureImportDialog.cpp @@ -0,0 +1,4333 @@ +#include +#include "OpenGLManager.hpp" + +#include "TextureImportDialog.hpp" +#include "I18N.hpp" +#include "GUI_App.hpp" +#include "MsgDialog.hpp" +#include "ColorDecomposeDialog.hpp" +#include "ColorDecomposeSupport.hpp" +#include "Widgets/StateColor.hpp" +#include "Widgets/StaticLine.hpp" +#include "libslic3r/ColorDecomposeRecipe.hpp" +#include "libslic3r/MeshBoolean.hpp" +#include "libslic3r/TriangleSelector.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static constexpr const char* DEFAULT_VIRTUAL_FILAMENT_BASIC_TYPE = "PLA Basic"; +static constexpr const char* DEFAULT_VIRTUAL_FILAMENT_SHORT_TYPE = "PLA"; +static constexpr const char* DEFAULT_VIRTUAL_FILAMENT_NAME = "Bambu PLA Basic"; + +static bool is_dark() { return Slic3r::GUI::wxGetApp().dark_mode(); } + +static wxColour dark_or(const wxColour& light, const wxColour& dark) +{ + return is_dark() ? dark : light; +} + +static wxColour texture_import_gray9000() +{ + return wxColour(38, 46, 48); +} + +static wxColour texture_import_text_colour() +{ + return StateColor::darkModeColorFor(texture_import_gray9000()); +} + +static wxColour texture_import_separator_colour() +{ + return StateColor::darkModeColorFor(wxColour("#CECECE")); +} + +static wxFont texture_import_section_title_font(wxWindow* win) +{ + wxFont font = win ? win->GetFont() : wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); + font.MakeBold(); + return font; +} + +static wxSize gl_viewport_size(wxWindow* win, const wxSize& logical_size) +{ + wxSize viewport_size = logical_size; +#ifdef __APPLE__ + const double scale = win ? win->GetContentScaleFactor() : 1.0; + if (scale > 0.0) { + viewport_size.x = std::max(1, (int)std::round(viewport_size.x * scale)); + viewport_size.y = std::max(1, (int)std::round(viewport_size.y * scale)); + } +#else + (void)win; +#endif + return viewport_size; +} + +class ScopedInteractiveBusyCursorSuspender +{ +public: + ScopedInteractiveBusyCursorSuspender() + { +#if defined(__WXMSW__) || defined(__APPLE__) + while (wxIsBusy()) { + wxEndBusyCursor(); + ++m_suspended_count; + } +#endif + } + + ~ScopedInteractiveBusyCursorSuspender() + { +#if defined(__WXMSW__) || defined(__APPLE__) + for (int i = 0; i < m_suspended_count; ++i) + wxBeginBusyCursor(); +#endif + } + +private: + int m_suspended_count = 0; +}; + +static bool needs_filament_swatch_border(const wxColour& colour) +{ + if (is_dark()) + return colour.Red() < 45 && colour.Green() < 45 && colour.Blue() < 45; + return colour.Red() > 224 && colour.Green() > 224 && colour.Blue() > 224; +} + +static wxColour filament_swatch_border_colour() +{ + return is_dark() ? wxColour(207, 207, 207) : wxColour(130, 130, 128); +} + +static void draw_filament_swatch_border(wxDC& dc, const wxColour& colour, + int x, int y, int w, int h, int radius = 0) +{ + if (!needs_filament_swatch_border(colour)) + return; + + dc.SetPen(wxPen(filament_swatch_border_colour(), 1)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + if (radius > 0) + dc.DrawRoundedRectangle(x, y, w, h, radius); + else + dc.DrawRectangle(x, y, w, h); +} + +static void draw_filament_swatch_ellipse_border(wxDC& dc, const wxColour& colour, + int x, int y, int w, int h) +{ + if (!needs_filament_swatch_border(colour)) + return; + + dc.SetPen(wxPen(filament_swatch_border_colour(), 1)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.DrawEllipse(x, y, w, h); +} + +static wxString ellipsize_text(wxDC& dc, wxString text, int max_width) +{ + if (max_width <= 0) + return wxEmptyString; + if (dc.GetTextExtent(text).x <= max_width) + return text; + + const wxString ellipsis = "..."; + while (!text.empty() && dc.GetTextExtent(text + ellipsis).x > max_width) + text.RemoveLast(); + if (text.empty() && dc.GetTextExtent(ellipsis).x > max_width) + return wxString(); + return text + ellipsis; +} + +static int draw_brand_icon_and_strip(wxDC& dc, wxWindow* win, wxString& name, int x, int cy) +{ + int icon_sz = win->FromDIP(16); + if (name.StartsWith("Bambu ")) { + name = name.Mid(6); + wxBitmap bmp = create_scaled_bitmap("BambuStudioBlack", win, 16); + if (bmp.IsOk()) + dc.DrawBitmap(bmp, x, cy - icon_sz / 2, true); + x += icon_sz + win->FromDIP(4); + } + return x; +} + +// ============================================================ +// GreenSlider — thin track + green triangle thumb +// ============================================================ + +class GreenSlider : public wxPanel { +public: + GreenSlider(wxWindow* parent, int value, int minVal, int maxVal, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize); + int GetValue() const; + void SetValue(int val); + bool Enable(bool enable = true) override; +private: + void OnPaint(wxPaintEvent&); + void OnMouse(wxMouseEvent&); + int xFromValue() const; + int valueFromX(int x) const; + int m_value, m_min, m_max; + bool m_dragging = false; +}; + +GreenSlider::GreenSlider(wxWindow* parent, int value, int minVal, int maxVal, + const wxPoint& pos, const wxSize& size) + : wxPanel(parent, wxID_ANY, pos, size.IsFullySpecified() ? size : wxSize(-1, parent->FromDIP(24)), + wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE) + , m_value(std::clamp(value, minVal, maxVal)), m_min(minVal), m_max(maxVal) +{ + SetBackgroundStyle(wxBG_STYLE_PAINT); + SetMinSize(wxSize(-1, FromDIP(24))); + + Bind(wxEVT_PAINT, &GreenSlider::OnPaint, this); + Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) { + evt.Skip(); + Refresh(); + }); + Bind(wxEVT_LEFT_DOWN, &GreenSlider::OnMouse, this); + Bind(wxEVT_LEFT_UP, &GreenSlider::OnMouse, this); + Bind(wxEVT_MOTION, &GreenSlider::OnMouse, this); +} + +int GreenSlider::GetValue() const { return m_value; } + +void GreenSlider::SetValue(int val) +{ + val = std::clamp(val, m_min, m_max); + if (val != m_value) { m_value = val; Refresh(); } +} + +bool GreenSlider::Enable(bool enable) +{ + bool ok = wxPanel::Enable(enable); + Refresh(); + return ok; +} + +int GreenSlider::xFromValue() const +{ + wxSize sz = GetClientSize(); + int margin = FromDIP(6); + int track_w = sz.x - 2 * margin; + if (m_max <= m_min || track_w <= 0) return margin; + return margin + (m_value - m_min) * track_w / (m_max - m_min); +} + +int GreenSlider::valueFromX(int x) const +{ + wxSize sz = GetClientSize(); + int margin = FromDIP(6); + int track_w = sz.x - 2 * margin; + if (track_w <= 0 || m_max <= m_min) return m_min; + int val = m_min + (x - margin) * (m_max - m_min) / track_w; + return std::clamp(val, m_min, m_max); +} + +void GreenSlider::OnPaint(wxPaintEvent&) +{ + wxAutoBufferedPaintDC dc(this); + wxSize sz = GetClientSize(); + + dc.SetBackground(wxBrush(GetParent()->GetBackgroundColour())); + dc.Clear(); + + int margin = FromDIP(6); + int track_y = sz.y / 2; + int ts = FromDIP(8); + int pen_w = FromDIP(2); + + wxColour greenClr = IsEnabled() ? wxColour(0, 174, 66) + : dark_or(wxColour(180, 180, 180), wxColour(90, 90, 96)); + wxColour grayClr = IsEnabled() ? dark_or(wxColour(200, 200, 200), wxColour(90, 90, 96)) + : dark_or(wxColour(220, 220, 220), wxColour(70, 70, 76)); + + int tx = xFromValue(); + + dc.SetPen(wxPen(greenClr, pen_w)); + dc.DrawLine(margin, track_y, tx, track_y); + + dc.SetPen(wxPen(grayClr, pen_w)); + dc.DrawLine(tx, track_y, sz.x - margin, track_y); + + wxPoint tri[3] = { + {tx, track_y + FromDIP(1)}, + {tx - ts / 2, track_y + FromDIP(1) + ts}, + {tx + ts / 2, track_y + FromDIP(1) + ts} + }; + dc.SetBrush(wxBrush(greenClr)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawPolygon(3, tri); +} + +void GreenSlider::OnMouse(wxMouseEvent& evt) +{ + if (!IsEnabled()) return; + + auto update = [&](int x) { + int nv = valueFromX(x); + if (nv != m_value) { + m_value = nv; + Refresh(); + wxCommandEvent e(wxEVT_SLIDER, GetId()); + e.SetEventObject(this); + ProcessWindowEvent(e); + } + }; + + if (evt.LeftDown()) { + m_dragging = true; + CaptureMouse(); + update(evt.GetX()); + } else if (evt.LeftUp()) { + m_dragging = false; + if (HasCapture()) ReleaseMouse(); + } else if (evt.Dragging() && m_dragging) { + update(evt.GetX()); + } +} + +namespace Slic3r { namespace GUI { + +wxDEFINE_EVENT(EVT_TEXTURE_COMPUTE_DONE, wxCommandEvent); +wxDEFINE_EVENT(EVT_TEXTURE_COMPUTE_PROGRESS, wxCommandEvent); +wxDEFINE_EVENT(EVT_TEXTURE_COMPUTE_ERROR, wxCommandEvent); +wxDEFINE_EVENT(EVT_TEXTURE_MESH_REPAIR_DECISION, wxCommandEvent); + +static std::array parse_color_string(const std::string& hex) +{ + std::array c = {1.f, 1.f, 1.f, 1.f}; + if (hex.size() >= 7 && hex[0] == '#') { + unsigned long val = std::strtoul(hex.c_str() + 1, nullptr, 16); + c[0] = ((val >> 16) & 0xFF) / 255.f; + c[1] = ((val >> 8) & 0xFF) / 255.f; + c[2] = ((val ) & 0xFF) / 255.f; + } + return c; +} + +static wxString rgb_to_hex(const std::array& c) +{ + return wxString::Format("#%02X%02X%02X", + (unsigned)c[0], (unsigned)c[1], (unsigned)c[2]); +} + +static wxString filament_name_to_wx_string(const std::string& name) +{ + wxString utf8_name = wxString::FromUTF8(name.c_str()); + if (!utf8_name.empty() || name.empty()) + return utf8_name; + return wxString(name); +} + +static std::string texture_normalize_color_hex(std::string hex) +{ + if (hex.empty()) + return "#808080"; + if (hex.front() != '#') + hex = "#" + hex; + return decompose_normalize_color_hex(std::move(hex)); +} + +static std::string texture_rgba_to_hex(const std::array& rgba) +{ + return wxString::Format("#%02X%02X%02X", + (unsigned char)std::clamp(rgba[0] * 255.f, 0.f, 255.f), + (unsigned char)std::clamp(rgba[1] * 255.f, 0.f, 255.f), + (unsigned char)std::clamp(rgba[2] * 255.f, 0.f, 255.f)).ToStdString(); +} + +static bool texture_entry_is_physical(TextureFilamentKind kind) +{ + return kind == TextureFilamentKind::ExistingPhysical || kind == TextureFilamentKind::NewPhysical; +} + +static bool texture_entry_is_mixed(TextureFilamentKind kind) +{ + return kind == TextureFilamentKind::ExistingMixed || kind == TextureFilamentKind::NewMixed; +} + +static bool texture_entry_is_pla_basic(const TextureFilamentEntry& entry) +{ + return entry.type == DEFAULT_VIRTUAL_FILAMENT_SHORT_TYPE || entry.type == DEFAULT_VIRTUAL_FILAMENT_BASIC_TYPE || + entry.name.find(DEFAULT_VIRTUAL_FILAMENT_BASIC_TYPE) != std::string::npos || + entry.preset_name.find(DEFAULT_VIRTUAL_FILAMENT_BASIC_TYPE) != std::string::npos; +} + +static bool texture_entry_official_basic(const TextureFilamentEntry& entry) +{ + if (!texture_entry_is_physical(entry.kind)) + return false; + // NewPhysical entries are created by add_virtual_filament with a fixed Bambu Basic name. + if (entry.kind == TextureFilamentKind::NewPhysical) + return !official_basic_type_from_preset_name(entry.name).empty(); + // ExistingPhysical: resolve the filament preset name from project_config_index. + auto& pb = *wxGetApp().preset_bundle; + const size_t cfg = entry.project_config_index; + if (cfg < pb.filament_presets.size()) + return !official_basic_type_from_preset_name(pb.filament_presets[cfg]).empty(); + return false; +} + +static Slic3r::ColorDecomposeRecipeMode texture_recipe_mode(TextureAutoMixMode mode) +{ + return mode == TextureAutoMixMode::CMYW ? Slic3r::ColorDecomposeRecipeMode::CMYW : + Slic3r::ColorDecomposeRecipeMode::RYBW; +} + +static bool starts_with_preset_name(const std::string& name, const char* prefix) +{ + const size_t prefix_len = std::strlen(prefix); + return name.size() >= prefix_len && name.compare(0, prefix_len, prefix) == 0; +} + +static std::string resolve_default_virtual_filament_preset_name() +{ + auto* preset_bundle = wxGetApp().preset_bundle; + if (!preset_bundle) + return {}; + + auto valid_preset_name = [preset_bundle](const std::string& name) -> bool { + return !name.empty() && preset_bundle->filaments.find_preset(name, false) != nullptr; + }; + + const auto* default_profiles = preset_bundle->printers.get_selected_preset() + .config.option("default_filament_profile"); + if (default_profiles) { + for (const std::string& name : default_profiles->values) { + if (starts_with_preset_name(name, DEFAULT_VIRTUAL_FILAMENT_NAME) && valid_preset_name(name)) + return name; + } + } + + for (const Preset& preset : preset_bundle->filaments.get_presets()) { + if (preset.is_system && preset.is_visible && preset.is_compatible && + starts_with_preset_name(preset.name, DEFAULT_VIRTUAL_FILAMENT_NAME)) { + return preset.name; + } + } + + for (const Preset& preset : preset_bundle->filaments.get_presets()) { + if (preset.is_visible && preset.is_compatible && + starts_with_preset_name(preset.name, DEFAULT_VIRTUAL_FILAMENT_NAME)) { + return preset.name; + } + } + + std::string selected = preset_bundle->filaments.get_selected_preset_name(); + return valid_preset_name(selected) ? selected : std::string(); +} + +static wxString auto_mix_mode_label(TextureAutoMixMode mode) +{ + return mode == TextureAutoMixMode::CMYW ? _L("One-click CMYW auto-mix") : + _L("One-click RYBW auto-mix"); +} + +static wxPoint constrained_dialog_position(wxWindow* anchor, const wxSize& dialog_size) +{ + if (!anchor) + return wxDefaultPosition; + + wxSize size = dialog_size; + if (size.x <= 0 || size.y <= 0) + size = wxSize(anchor->FromDIP(450), anchor->FromDIP(350)); + + wxPoint pos = anchor->ClientToScreen(wxPoint(0, anchor->GetSize().y)); + wxRect display_rect; + int display_idx = wxDisplay::GetFromPoint(pos); + if (display_idx != wxNOT_FOUND) + display_rect = wxDisplay(display_idx).GetClientArea(); + else + display_rect = wxDisplay().GetClientArea(); + + pos.x = std::clamp(pos.x, display_rect.GetLeft(), + std::max(display_rect.GetLeft(), display_rect.GetRight() - size.x)); + pos.y = std::clamp(pos.y, display_rect.GetTop(), + std::max(display_rect.GetTop(), display_rect.GetBottom() - size.y)); + return pos; +} + +// ============================================================ +// FilamentSelectPopup +// ============================================================ + +class FilamentSelectPopup : public PopupWindow +{ +public: + FilamentSelectPopup(wxWindow* parent, + const std::vector& entries, + const std::vector>& colors_rgba, + const std::vector& names, + size_t existing_count, + int popup_width, + wxWindow* dialog_anchor, + std::function on_select, + std::function on_add_filament, + std::function on_decompose_color, + std::function can_add_filament, + std::function on_close) + : PopupWindow(parent, wxBORDER_NONE | wxPU_CONTAINS_CONTROLS) + , m_entries(entries) + , m_colors_rgba(colors_rgba) + , m_names(names) + , m_existing_count(existing_count) + , m_dialog_anchor(dialog_anchor) + , m_on_select(std::move(on_select)) + , m_on_add_filament(std::move(on_add_filament)) + , m_on_decompose_color(std::move(on_decompose_color)) + , m_can_add_filament(std::move(can_add_filament)) + , m_on_close(std::move(on_close)) + { + wxColour pop_bg = dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)); + SetBackgroundColour(pop_bg); + + m_content = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxVSCROLL); + m_content->SetBackgroundColour(pop_bg); + m_content->SetScrollRate(0, FromDIP(5)); + auto* outer = new wxBoxSizer(wxVERTICAL); + + const int pop_w = std::max(FromDIP(213), popup_width); + const int row_h = FromDIP(32); + const int pad = FromDIP(8); + const int max_visible_rows = 10; + const wxColour header_clr = dark_or(wxColour(0xAC, 0xAC, 0xAC), wxColour(0x81, 0x81, 0x83)); + + auto add_section_header = [&](const wxString& label) { + auto* hdr = new wxStaticText(m_content, wxID_ANY, label); + wxFont hf = hdr->GetFont(); + hf.SetPointSize(9); + hdr->SetFont(hf); + hdr->SetForegroundColour(header_clr); + outer->Add(hdr, 0, wxLEFT | wxRIGHT | wxTOP, pad); + auto* line = new StaticLine(m_content); + line->SetLineColour(texture_import_separator_colour()); + outer->Add(line, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM, pad); + }; + + auto add_section = [&](const wxString& label, TextureFilamentKind kind) { + bool has_any = false; + for (const auto& entry : m_entries) { + if (entry.kind == kind) { + has_any = true; + break; + } + } + if (!has_any) + return; + add_section_header(label); + for (const auto& entry : m_entries) { + if (entry.kind != kind) + continue; + wxPanel* row = texture_entry_is_mixed(entry.kind) ? create_mixed_item_row(entry, row_h) + : create_item_row((size_t)entry.dialog_index, row_h); + outer->Add(row, 0, wxEXPAND | wxLEFT | wxRIGHT, pad); + } + }; + + add_section(_L("Project Physical Filaments"), TextureFilamentKind::ExistingPhysical); + add_section(_L("Project Mixed Filaments"), TextureFilamentKind::ExistingMixed); + add_section(_L("New Physical Filaments"), TextureFilamentKind::NewPhysical); + add_section(_L("New Mixed Filaments"), TextureFilamentKind::NewMixed); + + auto* decompose_label = new wxStaticText(this, wxID_ANY, _L("Decompose Color")); + auto* add_label = new wxStaticText(this, wxID_ANY, _L("+ Add Material")); + wxFont af = add_label->GetFont(); + af.SetPointSize(10); + add_label->SetFont(af); + decompose_label->SetFont(af); + const bool add_enabled = !m_can_add_filament || m_can_add_filament(); + add_label->SetForegroundColour(add_enabled ? wxColour(0x00, 0xAE, 0x42) : header_clr); + decompose_label->SetForegroundColour(add_enabled ? wxColour(0x00, 0xAE, 0x42) : header_clr); + add_label->SetCursor(wxCursor(add_enabled ? wxCURSOR_HAND : wxCURSOR_ARROW)); + decompose_label->SetCursor(wxCursor(add_enabled ? wxCURSOR_HAND : wxCURSOR_ARROW)); + if (!add_enabled) + add_label->SetToolTip(wxString::Format( + _L("The project supports up to %d filaments. Extra filaments will be discarded."), + (int)EnforcerBlockerType::ExtruderMax)); + decompose_label->Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent&) { + if (m_can_add_filament && !m_can_add_filament()) + return; + auto on_decompose_color = m_on_decompose_color; + m_closing_from_action = true; + Dismiss(); + if (on_decompose_color) + on_decompose_color(); + }); + add_label->Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent&) { + if (m_can_add_filament && !m_can_add_filament()) { + return; + } + auto on_add_filament = m_on_add_filament; + wxWindow* popup_parent = GetParent(); + wxWindow* color_anchor = m_dialog_anchor ? m_dialog_anchor : popup_parent; + m_closing_from_action = true; + Dismiss(); + wxColourData cd; + cd.SetChooseFull(true); + wxColourDialog dlg(popup_parent, &cd); + auto move_color_dialog = [&dlg, color_anchor]() { + dlg.Move(constrained_dialog_position(color_anchor, dlg.GetBestSize())); + }; + dlg.Bind(wxEVT_SHOW, [move_color_dialog](wxShowEvent& e) mutable { + e.Skip(); + if (e.IsShown()) + move_color_dialog(); + }); + move_color_dialog(); + if (dlg.ShowModal() == wxID_OK) { + wxColour clr = dlg.GetColourData().GetColour(); + if (on_add_filament) on_add_filament(clr); + } + }); + + m_content->SetSizer(outer); + m_content->FitInside(); + + auto* top_sizer = new wxBoxSizer(wxVERTICAL); + int list_h = outer->GetMinSize().y; + if (m_colors_rgba.size() > max_visible_rows) + list_h -= ((int)m_colors_rgba.size() - max_visible_rows) * row_h; + m_content->SetMinSize(wxSize(pop_w, list_h)); + m_content->SetMaxSize(wxSize(pop_w, list_h)); + top_sizer->Add(m_content, 0, wxEXPAND); + + top_sizer->AddSpacer(FromDIP(4)); + auto* sep_line = new StaticLine(this); + sep_line->SetLineColour(texture_import_separator_colour()); + top_sizer->Add(sep_line, 0, wxEXPAND | wxLEFT | wxRIGHT, pad); + top_sizer->Add(decompose_label, 0, wxALIGN_CENTER_HORIZONTAL | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM, pad); + auto* sep_line2 = new StaticLine(this); + sep_line2->SetLineColour(texture_import_separator_colour()); + top_sizer->Add(sep_line2, 0, wxEXPAND | wxLEFT | wxRIGHT, pad); + top_sizer->Add(add_label, 0, wxALIGN_CENTER_HORIZONTAL | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM, pad); + SetSizerAndFit(top_sizer); + + SetSize(pop_w, top_sizer->GetMinSize().y); + } + +private: + void OnDismiss() override + { + restore_cursor_state(); + if (m_on_close) m_on_close(m_closing_from_action); + m_closing_from_action = false; + wxPopupTransientWindow::OnDismiss(); + schedule_destroy(); + } + + void restore_cursor_state() + { + SetCursor(wxNullCursor); + if (m_content) + m_content->SetCursor(wxNullCursor); + if (m_dialog_anchor) + m_dialog_anchor->SetCursor(wxCursor(wxCURSOR_HAND)); + wxSetCursor(wxNullCursor); + } + + void schedule_destroy() + { + if (m_destroy_scheduled) + return; + m_destroy_scheduled = true; + CallAfter([this]() { Destroy(); }); + } + + wxPanel* create_item_row(size_t idx, int row_h) + { + wxColour row_bg = dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)); + wxColour hover_bg = dark_or(wxColour(245, 245, 245), wxColour(0x3C, 0x3C, 0x42)); + wxColour name_fg = texture_import_text_colour(); + + wxPanel* row = new wxPanel(m_content, wxID_ANY, wxDefaultPosition, wxSize(-1, row_h)); + row->SetBackgroundColour(row_bg); + row->SetBackgroundStyle(wxBG_STYLE_PAINT); + row->SetCursor(wxCursor(wxCURSOR_HAND)); + + const int sq = row->FromDIP(24); + const int sq_r = row->FromDIP(2); + const int sq_x = row->FromDIP(4); + const int gap1 = row->FromDIP(8); + + wxColour fil_clr = idx < m_colors_rgba.size() + ? wxColour((unsigned char)(m_colors_rgba[idx][0] * 255.f), + (unsigned char)(m_colors_rgba[idx][1] * 255.f), + (unsigned char)(m_colors_rgba[idx][2] * 255.f)) + : wxColour(128, 128, 128); + + wxString name_str = (idx < m_names.size()) ? filament_name_to_wx_string(m_names[idx]) + : wxString::Format("Filament %d", (int)(idx + 1)); + row->SetToolTip(name_str); + + row->Bind(wxEVT_PAINT, [this, idx, sq, sq_r, sq_x, gap1, fil_clr, name_str, row_bg, hover_bg, name_fg](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + + bool hovered = (m_hover_idx == (int)idx); + dc.SetBrush(wxBrush(hovered ? hover_bg : row_bg)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + + int sq_y = (sz.y - sq) / 2; + wxColour paint_clr = fil_clr; + if (idx < m_colors_rgba.size()) { + paint_clr = wxColour((unsigned char)(m_colors_rgba[idx][0] * 255.f), + (unsigned char)(m_colors_rgba[idx][1] * 255.f), + (unsigned char)(m_colors_rgba[idx][2] * 255.f)); + } + dc.SetBrush(wxBrush(paint_clr)); + dc.DrawRoundedRectangle(sq_x, sq_y, sq, sq, sq_r); + draw_filament_swatch_border(dc, paint_clr, sq_x, sq_y, sq, sq, sq_r); + + { + wxFont nf = p->GetFont(); + nf.SetPointSize(9); + dc.SetFont(nf); + dc.SetTextForeground(paint_clr.GetLuminance() < 0.6 ? *wxWHITE : texture_import_gray9000()); + wxString ns = wxString::Format("%d", (int)(idx + 1)); + wxSize tsz = dc.GetTextExtent(ns); + dc.DrawText(ns, sq_x + (sq - tsz.x) / 2, sq_y + (sq - tsz.y) / 2); + } + + // Brand icon + material name + { + wxFont mf = p->GetFont(); + mf.SetPointSize(10); + dc.SetFont(mf); + dc.SetTextForeground(name_fg); + wxString display = name_str; + int tx = draw_brand_icon_and_strip(dc, p, display, sq_x + sq + gap1, sz.y / 2); + display = ellipsize_text(dc, display, sz.x - tx - p->FromDIP(4)); + wxSize tsz = dc.GetTextExtent(display); + if (!display.empty()) + dc.DrawText(display, tx, (sz.y - tsz.y) / 2); + } + }); + + row->Bind(wxEVT_MOTION, [this, idx](wxMouseEvent& evt) { + if (m_hover_idx != (int)idx) { + m_hover_idx = (int)idx; + m_content->Refresh(); + } + evt.Skip(); + }); + row->Bind(wxEVT_LEAVE_WINDOW, [this](wxMouseEvent& evt) { + if (m_hover_idx != -1) { + m_hover_idx = -1; + m_content->Refresh(); + } + evt.Skip(); + }); + + row->Bind(wxEVT_LEFT_DOWN, [this, idx](wxMouseEvent&) { + if (m_on_select) m_on_select((int)idx); + m_closing_from_action = true; + Dismiss(); + }); + + return row; + } + + wxPanel* create_mixed_item_row(const TextureFilamentEntry& entry, int row_h) + { + wxColour row_bg = dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)); + wxColour hover_bg = dark_or(wxColour(245, 245, 245), wxColour(0x3C, 0x3C, 0x42)); + wxColour name_fg = texture_import_text_colour(); + wxColour plus_fg = dark_or(wxColour(38, 46, 48), wxColour(0xE6, 0xE6, 0xE8)); + const int idx = entry.dialog_index; + + wxPanel* row = new wxPanel(m_content, wxID_ANY, wxDefaultPosition, wxSize(-1, row_h)); + row->SetBackgroundColour(row_bg); + row->SetBackgroundStyle(wxBG_STYLE_PAINT); + row->SetCursor(wxCursor(wxCURSOR_HAND)); + row->SetToolTip(entry.name.empty() ? wxString::Format("Filament %d", idx + 1) : filament_name_to_wx_string(entry.name)); + + row->Bind(wxEVT_PAINT, [this, entry, idx, row_bg, hover_bg, name_fg, plus_fg](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + const bool hovered = (m_hover_idx == idx); + dc.SetBrush(wxBrush(hovered ? hover_bg : row_bg)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + + wxFont font = p->GetFont(); + font.SetPointSize(9); + dc.SetFont(font); + int x = p->FromDIP(2); + const int sw = p->FromDIP(22); + const int sw_r = p->FromDIP(2); + const int y = (sz.y - sw) / 2; + + for (size_t ci = 0; ci < entry.mixed_components.size() && ci < entry.mixed_ratios.size(); ++ci) { + if (ci > 0) { + dc.SetTextForeground(plus_fg); + wxString plus = "+"; + wxSize psz = dc.GetTextExtent(plus); + dc.DrawText(plus, x, (sz.y - psz.y) / 2); + x += psz.x + p->FromDIP(4); + } + + const unsigned int comp_id = entry.mixed_components[ci]; + const int comp_dialog_idx = comp_id >= 1 ? (int)comp_id - 1 : -1; + wxColour comp_clr("#D9D9D9"); + if (comp_dialog_idx >= 0 && comp_dialog_idx < (int)m_colors_rgba.size()) { + const auto& c = m_colors_rgba[comp_dialog_idx]; + comp_clr = wxColour((unsigned char)(c[0] * 255.f), + (unsigned char)(c[1] * 255.f), + (unsigned char)(c[2] * 255.f)); + } + + dc.SetBrush(wxBrush(comp_clr)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRoundedRectangle(x, y, sw, sw, sw_r); + draw_filament_swatch_border(dc, comp_clr, x, y, sw, sw, sw_r); + + wxString num = wxString::Format("%u", comp_id); + wxSize nsz = dc.GetTextExtent(num); + dc.SetTextForeground(comp_clr.GetLuminance() < 0.6 ? *wxWHITE : texture_import_gray9000()); + dc.DrawText(num, x + (sw - nsz.x) / 2, y + (sw - nsz.y) / 2); + x += sw + p->FromDIP(4); + + dc.SetTextForeground(name_fg); + wxString pct = wxString::Format("%d%%", entry.mixed_ratios[ci]); + wxSize psz = dc.GetTextExtent(pct); + dc.DrawText(pct, x, (sz.y - psz.y) / 2); + x += psz.x + p->FromDIP(4); + } + }); + + row->Bind(wxEVT_MOTION, [this, idx](wxMouseEvent& evt) { + if (m_hover_idx != idx) { + m_hover_idx = idx; + m_content->Refresh(); + } + evt.Skip(); + }); + row->Bind(wxEVT_LEAVE_WINDOW, [this](wxMouseEvent& evt) { + if (m_hover_idx != -1) { + m_hover_idx = -1; + m_content->Refresh(); + } + evt.Skip(); + }); + row->Bind(wxEVT_LEFT_DOWN, [this, idx](wxMouseEvent&) { + if (m_on_select) m_on_select(idx); + m_closing_from_action = true; + Dismiss(); + }); + + return row; + } + + wxScrolledWindow* m_content = nullptr; + std::vector m_entries; + std::vector> m_colors_rgba; + std::vector m_names; + size_t m_existing_count = 0; + wxWindow* m_dialog_anchor = nullptr; + std::function m_on_select; + std::function m_on_add_filament; + std::function m_on_decompose_color; + std::function m_can_add_filament; + std::function m_on_close; + int m_hover_idx = -1; + bool m_closing_from_action = false; + bool m_destroy_scheduled = false; +}; + +// ============================================================ +// AutoMixSelectPopup +// ============================================================ + +class AutoMixSelectPopup : public PopupWindow +{ +public: + AutoMixSelectPopup(wxWindow* parent, + TextureAutoMixMode current_mode, + int popup_width, + int font_point_size, + std::function on_select, + std::function on_close) + : PopupWindow(parent, wxBORDER_NONE | wxPU_CONTAINS_CONTROLS) + , m_current_mode(current_mode) + , m_font_point_size(font_point_size) + , m_on_select(std::move(on_select)) + , m_on_close(std::move(on_close)) + { + wxColour pop_bg = dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)); + SetBackgroundColour(pop_bg); + + auto* content = new wxPanel(this, wxID_ANY); + content->SetBackgroundColour(pop_bg); + content->SetBackgroundStyle(wxBG_STYLE_PAINT); + + auto* sizer = new wxBoxSizer(wxVERTICAL); + const int row_h = FromDIP(36); + const int pop_w = std::max(FromDIP(216), popup_width); + sizer->Add(create_item_row(content, TextureAutoMixMode::CMYW, row_h), 0, wxEXPAND); + sizer->Add(create_item_row(content, TextureAutoMixMode::RYBW, row_h), 0, wxEXPAND); + content->SetSizer(sizer); + content->SetMinSize(wxSize(pop_w, row_h * 2)); + + auto* top_sizer = new wxBoxSizer(wxVERTICAL); + top_sizer->Add(content, 0, wxEXPAND | wxALL, FromDIP(4)); + SetSizerAndFit(top_sizer); + SetSize(pop_w, top_sizer->GetMinSize().y); + } + +private: + void OnDismiss() override + { + if (m_on_close) + m_on_close(); + wxPopupTransientWindow::OnDismiss(); + CallAfter([this]() { Destroy(); }); + } + + wxPanel* create_item_row(wxWindow* parent, TextureAutoMixMode mode, int row_h) + { + wxColour row_bg = dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)); + wxColour hover_bg = dark_or(wxColour(245, 245, 245), wxColour(0x3C, 0x3C, 0x42)); + wxColour text_fg = texture_import_text_colour(); + wxColour green = wxColour(0, 174, 66); + + wxPanel* row = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(-1, row_h), + wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE); + row->SetBackgroundColour(row_bg); + row->SetBackgroundStyle(wxBG_STYLE_PAINT); + row->SetCursor(wxCursor(wxCURSOR_HAND)); + + const int row_idx = mode == TextureAutoMixMode::CMYW ? 0 : 1; + row->Bind(wxEVT_PAINT, [this, row_bg, hover_bg, text_fg, green, mode, row_idx](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + const bool hovered = (m_hover_idx == row_idx); + const bool selected = (m_current_mode == mode); + + dc.SetBrush(wxBrush(hovered ? hover_bg : row_bg)); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + + wxFont font = p->GetFont(); + font.SetPointSize(m_font_point_size); + dc.SetFont(font); + dc.SetTextForeground(text_fg); + wxString label = auto_mix_mode_label(mode); + wxSize tsz = dc.GetTextExtent(label); + dc.DrawText(label, p->FromDIP(12), (sz.y - tsz.y) / 2); + + if (selected) { + wxFont check_font = p->GetFont(); + check_font.SetPointSize(12); + check_font.MakeBold(); + dc.SetFont(check_font); + dc.SetTextForeground(green); + wxString check = wxString::FromUTF8("✓"); + wxSize csz = dc.GetTextExtent(check); + dc.DrawText(check, sz.x - p->FromDIP(16) - csz.x, (sz.y - csz.y) / 2); + } + }); + + row->Bind(wxEVT_MOTION, [this, row, row_idx](wxMouseEvent& evt) { + if (m_hover_idx != row_idx) { + m_hover_idx = row_idx; + row->Refresh(); + } + evt.Skip(); + }); + row->Bind(wxEVT_LEAVE_WINDOW, [this, row](wxMouseEvent& evt) { + m_hover_idx = -1; + row->Refresh(); + evt.Skip(); + }); + row->Bind(wxEVT_LEFT_DOWN, [this, mode](wxMouseEvent&) { + if (m_on_select) + m_on_select(mode); + Dismiss(); + }); + + return row; + } + + TextureAutoMixMode m_current_mode; + int m_font_point_size = 10; + int m_hover_idx = -1; + std::function m_on_select; + std::function m_on_close; +}; + +// ============================================================ +// TexturePreviewCanvas +// ============================================================ + +TexturePreviewCanvas::TexturePreviewCanvas(wxWindow* parent, const wxGLAttributes& attrs) + : wxGLCanvas(parent, attrs, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE) +{ + m_context = new wxGLContext(this); + + Bind(wxEVT_PAINT, &TexturePreviewCanvas::on_paint, this); + Bind(wxEVT_SIZE, &TexturePreviewCanvas::on_size, this); + Bind(wxEVT_MOUSEWHEEL, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_LEFT_DOWN, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_LEFT_UP, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_RIGHT_DOWN, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_RIGHT_UP, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_MIDDLE_DOWN, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_MIDDLE_UP, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_MOTION, &TexturePreviewCanvas::on_mouse, this); + Bind(wxEVT_LEAVE_WINDOW, &TexturePreviewCanvas::on_mouse, this); +} + +TexturePreviewCanvas::~TexturePreviewCanvas() +{ + if (m_context) { + SetCurrent(*m_context); + if (m_tex_id) + glDeleteTextures(1, &m_tex_id); + for (unsigned int id : m_gl_tex_ids) + if (id) glDeleteTextures(1, &id); + for (unsigned int id : {m_reset_icon_tex, m_reset_icon_hover_tex, + m_reset_icon_dark_tex, m_reset_icon_dark_hover_tex}) + if (id) glDeleteTextures(1, &id); + delete m_context; + } +} + +void TexturePreviewCanvas::set_mesh_data( + const std::vector>& vertices, + const std::vector>& indices) +{ + m_vertices = vertices; + m_indices = indices; + update_bounding_box(); + compute_smooth_normals(); + Refresh(); +} + +void TexturePreviewCanvas::compute_smooth_normals() +{ + m_vertex_normals.clear(); + if (m_vertices.empty() || m_indices.empty()) return; + + m_vertex_normals.resize(m_vertices.size(), {0.f, 0.f, 0.f}); + + for (const auto& face : m_indices) { + int i0 = face[0], i1 = face[1], i2 = face[2]; + if (i0 < 0 || i0 >= (int)m_vertices.size() || + i1 < 0 || i1 >= (int)m_vertices.size() || + i2 < 0 || i2 >= (int)m_vertices.size()) + continue; + + const auto& v0 = m_vertices[i0]; + const auto& v1 = m_vertices[i1]; + const auto& v2 = m_vertices[i2]; + + float nx = (v1[1]-v0[1])*(v2[2]-v0[2]) - (v1[2]-v0[2])*(v2[1]-v0[1]); + float ny = (v1[2]-v0[2])*(v2[0]-v0[0]) - (v1[0]-v0[0])*(v2[2]-v0[2]); + float nz = (v1[0]-v0[0])*(v2[1]-v0[1]) - (v1[1]-v0[1])*(v2[0]-v0[0]); + + m_vertex_normals[i0][0] += nx; m_vertex_normals[i0][1] += ny; m_vertex_normals[i0][2] += nz; + m_vertex_normals[i1][0] += nx; m_vertex_normals[i1][1] += ny; m_vertex_normals[i1][2] += nz; + m_vertex_normals[i2][0] += nx; m_vertex_normals[i2][1] += ny; m_vertex_normals[i2][2] += nz; + } + + for (auto& n : m_vertex_normals) { + float len = std::sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); + if (len > 1e-8f) { n[0] /= len; n[1] /= len; n[2] /= len; } + } +} + +void TexturePreviewCanvas::set_texture_data( + const std::vector>& uvs, + const unsigned char* tex_data, int tex_w, int tex_h, int tex_channels) +{ + m_uvs = uvs; + m_tex_w = tex_w; + m_tex_h = tex_h; + m_tex_channels = tex_channels; + m_tex_dirty = true; + + size_t sz = (size_t)tex_w * tex_h * tex_channels; + m_tex_data.assign(tex_data, tex_data + sz); + Refresh(); +} + +void TexturePreviewCanvas::set_texture_render_data( + const std::vector>& tex_pixels_rgb, + const std::vector& tex_widths, + const std::vector& tex_heights, + const std::vector, 3>>& face_uvs, + const std::vector& face_tex_ids) +{ + m_tex_pixels_rgb = tex_pixels_rgb; + m_tex_widths = tex_widths; + m_tex_heights = tex_heights; + m_face_uvs = face_uvs; + m_face_tex_ids = face_tex_ids; + m_multi_tex_dirty = true; + Refresh(); +} + +void TexturePreviewCanvas::upload_textures() +{ + if (!m_multi_tex_dirty) return; + m_multi_tex_dirty = false; + + for (unsigned int id : m_gl_tex_ids) + if (id) glDeleteTextures(1, &id); + m_gl_tex_ids.clear(); + + m_gl_tex_ids.resize(m_tex_pixels_rgb.size(), 0); + for (size_t i = 0; i < m_tex_pixels_rgb.size(); ++i) { + if (m_tex_pixels_rgb[i].empty() || m_tex_widths[i] <= 0 || m_tex_heights[i] <= 0) + continue; + GLuint tex_id = 0; + glGenTextures(1, &tex_id); + glBindTexture(GL_TEXTURE_2D, tex_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_tex_widths[i], m_tex_heights[i], + 0, GL_RGB, GL_UNSIGNED_BYTE, m_tex_pixels_rgb[i].data()); + m_gl_tex_ids[i] = tex_id; + } + glBindTexture(GL_TEXTURE_2D, 0); +} + +void TexturePreviewCanvas::set_painted_mesh_data( + const std::vector>& vertices, + const std::vector>& indices) +{ + m_painted_vertices = vertices; + m_painted_indices = indices; + Refresh(); +} + +static void convert_face_colors(const std::vector>& src, + std::vector>& dst) +{ + dst.resize(src.size()); + for (size_t i = 0; i < src.size(); ++i) + dst[i] = { src[i][0] / 255.f, src[i][1] / 255.f, src[i][2] / 255.f }; +} + +void TexturePreviewCanvas::set_face_colors(const std::vector>& face_colors) +{ + convert_face_colors(face_colors, m_face_colors_rgb); + Refresh(); +} + +void TexturePreviewCanvas::set_original_face_colors(const std::vector>& face_colors) +{ + convert_face_colors(face_colors, m_original_face_colors_rgb); + Refresh(); +} + +void TexturePreviewCanvas::set_filament_color_map( + const std::map, std::array>& color_map) +{ + m_color_map = color_map; + m_filament_colors_rgb.resize(m_face_colors_rgb.size()); + for (size_t i = 0; i < m_face_colors_rgb.size(); ++i) { + std::array key = { + (std::size_t)(m_face_colors_rgb[i][0] * 255.f + 0.5f), + (std::size_t)(m_face_colors_rgb[i][1] * 255.f + 0.5f), + (std::size_t)(m_face_colors_rgb[i][2] * 255.f + 0.5f) + }; + auto it = color_map.find(key); + if (it != color_map.end()) + m_filament_colors_rgb[i] = it->second; + else + m_filament_colors_rgb[i] = m_face_colors_rgb[i]; + } + Refresh(); +} + +void TexturePreviewCanvas::set_render_mode(RenderMode mode) +{ + if (m_mode != mode) { + m_mode = mode; + Refresh(); + } +} + +void TexturePreviewCanvas::set_computing_overlay(bool /*show*/) +{ + Refresh(); +} + +void TexturePreviewCanvas::reset_view() +{ + m_zoom = 1.0f; + m_rot_x = -30.0f; + m_rot_y = 30.0f; + m_pan_x = 0.0f; + m_pan_y = 0.0f; + Refresh(); +} + +wxRect TexturePreviewCanvas::reset_overlay_rect() const +{ + wxSize sz = GetClientSize(); + const int button_size = FromDIP(40); + const int margin = FromDIP(20); + return wxRect( + std::max(margin, sz.x - button_size - margin), + std::max(margin, sz.y - button_size - margin), + button_size, + button_size); +} + +unsigned int TexturePreviewCanvas::upload_reset_icon_texture(const std::string& icon_name) +{ + wxBitmap bmp = create_scaled_bitmap(icon_name, this, 40); + if (!bmp.IsOk()) + return 0; + + wxImage image = bmp.ConvertToImage(); + if (!image.IsOk()) + return 0; + + const int w = image.GetWidth(); + const int h = image.GetHeight(); + const unsigned char* rgb = image.GetData(); + const unsigned char* alpha = image.HasAlpha() ? image.GetAlpha() : nullptr; + if (!rgb || w <= 0 || h <= 0) + return 0; + + std::vector rgba((size_t)w * h * 4); + for (int i = 0; i < w * h; ++i) { + rgba[(size_t)i * 4 + 0] = rgb[i * 3 + 0]; + rgba[(size_t)i * 4 + 1] = rgb[i * 3 + 1]; + rgba[(size_t)i * 4 + 2] = rgb[i * 3 + 2]; + rgba[(size_t)i * 4 + 3] = alpha ? alpha[i] : 255; + } + + GLuint tex_id = 0; + glGenTextures(1, &tex_id); + glBindTexture(GL_TEXTURE_2D, tex_id); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba.data()); + glBindTexture(GL_TEXTURE_2D, 0); + return tex_id; +} + +void TexturePreviewCanvas::upload_reset_icon_textures() +{ + if (m_reset_icon_tex && m_reset_icon_hover_tex && m_reset_icon_dark_tex && m_reset_icon_dark_hover_tex) + return; + + if (!m_reset_icon_tex) + m_reset_icon_tex = upload_reset_icon_texture("fit_camera"); + if (!m_reset_icon_hover_tex) + m_reset_icon_hover_tex = upload_reset_icon_texture("fit_camera_hover"); + if (!m_reset_icon_dark_tex) + m_reset_icon_dark_tex = upload_reset_icon_texture("fit_camera_dark"); + if (!m_reset_icon_dark_hover_tex) + m_reset_icon_dark_hover_tex = upload_reset_icon_texture("fit_camera_dark_hover"); +} + +bool TexturePreviewCanvas::handle_reset_overlay_mouse(wxMouseEvent& evt) +{ + if (evt.Leaving()) { + if (m_reset_overlay_pressed) { + m_reset_overlay_hovered = false; + m_reset_overlay_pressed = false; + SetCursor(wxCursor(wxCURSOR_ARROW)); + if (HasCapture()) + ReleaseMouse(); + Refresh(); + return true; + } + if (m_reset_overlay_hovered) { + m_reset_overlay_hovered = false; + SetCursor(wxCursor(wxCURSOR_ARROW)); + Refresh(); + } + return false; + } + + const bool over = reset_overlay_rect().Contains(evt.GetPosition()); + if (over != m_reset_overlay_hovered) { + m_reset_overlay_hovered = over; + SetCursor(wxCursor(over ? wxCURSOR_HAND : wxCURSOR_ARROW)); + Refresh(); + } + + if (m_drag_mode != DragMode::None && !m_reset_overlay_pressed) + return false; + + if (evt.LeftDown() && over) { + m_reset_overlay_pressed = true; + if (!HasCapture()) + CaptureMouse(); + Refresh(); + return true; + } + + if (evt.LeftUp() && m_reset_overlay_pressed) { + const bool activate = over; + m_reset_overlay_pressed = false; + if (HasCapture()) + ReleaseMouse(); + if (activate) + reset_view(); + else + Refresh(); + return true; + } + + return over; +} + +void TexturePreviewCanvas::update_bounding_box() +{ + if (m_vertices.empty()) return; + std::array mn = m_vertices[0], mx = m_vertices[0]; + for (const auto& v : m_vertices) { + for (int i = 0; i < 3; ++i) { + mn[i] = std::min(mn[i], v[i]); + mx[i] = std::max(mx[i], v[i]); + } + } + m_center = { (mn[0]+mx[0])/2, (mn[1]+mx[1])/2, (mn[2]+mx[2])/2 }; + float dx = mx[0]-mn[0], dy = mx[1]-mn[1], dz = mx[2]-mn[2]; + m_radius = std::sqrt(dx*dx + dy*dy + dz*dz) / 2.0f; + if (m_radius < 1e-6f) m_radius = 1.0f; +} + +void TexturePreviewCanvas::ensure_gl_ready() +{ + if (m_gl_initialized) return; + + // BBS loads GL entry points here with GLEW. Orca uses glad and centralises loading in + // OpenGLManager, which has already run by the time any canvas is realized, so just + // verify the loader is up and drain any stale error state. + // glad leaves unresolved entry points as null pointers, so this is a cheap guard against + // painting before OpenGLManager::init_gl() has run. + if (glGetString == nullptr) { + BOOST_LOG_TRIVIAL(error) << "TexturePreviewCanvas: OpenGL functions are not loaded yet"; + return; + } + while (glGetError() != GL_NO_ERROR) {} + + m_gl_initialized = true; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_COLOR_MATERIAL); + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + + GLfloat light_pos[] = { 0.5f, 1.0f, 1.0f, 0.0f }; + GLfloat light_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f }; + GLfloat light_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; + glLightfv(GL_LIGHT0, GL_POSITION, light_pos); + glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); +} + +void TexturePreviewCanvas::on_paint(wxPaintEvent&) +{ + wxPaintDC dc(this); + if (!m_context) return; + SetCurrent(*m_context); + ensure_gl_ready(); + render(); + SwapBuffers(); +} + +void TexturePreviewCanvas::on_size(wxSizeEvent&) +{ + Refresh(); +} + +void TexturePreviewCanvas::on_mouse(wxMouseEvent& evt) +{ + if (handle_reset_overlay_mouse(evt)) + return; + + if (evt.LeftDown()) { + m_drag_mode = DragMode::Rotate; + m_last_mouse_pos = evt.GetPosition(); + if (!HasCapture()) CaptureMouse(); + } + else if (evt.LeftUp()) { + if (m_drag_mode == DragMode::Rotate) { + m_drag_mode = DragMode::None; + if (HasCapture()) ReleaseMouse(); + } + } + else if (evt.RightDown()) { + m_drag_mode = DragMode::Pan; + m_last_mouse_pos = evt.GetPosition(); + if (!HasCapture()) CaptureMouse(); + } + else if (evt.MiddleDown()) { + m_drag_mode = DragMode::Pan; + m_last_mouse_pos = evt.GetPosition(); + if (!HasCapture()) CaptureMouse(); + } + else if (evt.RightUp() || evt.MiddleUp()) { + if (m_drag_mode == DragMode::Pan) { + m_drag_mode = DragMode::None; + if (HasCapture()) ReleaseMouse(); + } + } + else if (evt.Dragging() && m_drag_mode != DragMode::None) { + wxPoint pos = evt.GetPosition(); + float dx = (float)(pos.x - m_last_mouse_pos.x); + float dy = (float)(pos.y - m_last_mouse_pos.y); + + if (m_drag_mode == DragMode::Rotate) { + m_rot_y += dx * 0.5f; + m_rot_x += dy * 0.5f; + m_rot_x = std::max(-89.0f, std::min(89.0f, m_rot_x)); + } else if (m_drag_mode == DragMode::Pan) { + wxSize sz = GetClientSize(); + if (sz.x > 0) + m_pan_x += dx / (float)sz.x * m_radius * 2.0f / m_zoom; + if (sz.y > 0) + m_pan_y -= dy / (float)sz.y * m_radius * 2.0f / m_zoom; + } + + m_last_mouse_pos = pos; + Refresh(); + } + else if (evt.GetWheelRotation() != 0) { + float delta = evt.GetWheelRotation() > 0 ? 1.1f : 0.9f; + m_zoom *= delta; + m_zoom = std::max(0.1f, std::min(20.0f, m_zoom)); + Refresh(); + } +} + +void TexturePreviewCanvas::render() +{ + wxSize sz = GetClientSize(); + if (sz.x <= 0 || sz.y <= 0) return; + + wxSize viewport_sz = gl_viewport_size(this, sz); + glViewport(0, 0, viewport_sz.x, viewport_sz.y); + if (is_dark()) + glClearColor(0.24f, 0.24f, 0.27f, 1.0f); + else + glClearColor(0.933f, 0.933f, 0.933f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + float aspect = (float)viewport_sz.x / (float)viewport_sz.y; + float dist = m_radius * 3.0f / m_zoom; + float near_plane = dist * 0.01f; + float far_plane = dist * 10.0f; + float fov_rad = 45.0f * static_cast(M_PI) / 180.0f; + float f = 1.0f / std::tan(fov_rad / 2.0f); + float proj[16] = {}; + proj[0] = f / aspect; + proj[5] = f; + proj[10] = (far_plane + near_plane) / (near_plane - far_plane); + proj[11] = -1.0f; + proj[14] = (2.0f * far_plane * near_plane) / (near_plane - far_plane); + glMultMatrixf(proj); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -dist); + glTranslatef(m_pan_x, m_pan_y, 0.0f); + glRotatef(m_rot_x, 1.0f, 0.0f, 0.0f); + glRotatef(m_rot_y, 0.0f, 1.0f, 0.0f); + glTranslatef(-m_center[0], -m_center[1], -m_center[2]); + + render_mesh(); + render_reset_overlay(sz, viewport_sz); +} + +void TexturePreviewCanvas::render_reset_overlay(const wxSize& logical_size, const wxSize& viewport_size) +{ + if (logical_size.x <= 0 || logical_size.y <= 0 || viewport_size.x <= 0 || viewport_size.y <= 0) + return; + + upload_reset_icon_textures(); + + const unsigned int tex_id = is_dark() + ? (m_reset_overlay_hovered ? m_reset_icon_dark_hover_tex : m_reset_icon_dark_tex) + : (m_reset_overlay_hovered ? m_reset_icon_hover_tex : m_reset_icon_tex); + if (!tex_id) + return; + + wxRect rc = reset_overlay_rect(); + const float sx = (float)viewport_size.x / (float)logical_size.x; + const float sy = (float)viewport_size.y / (float)logical_size.y; + const float x0 = rc.GetLeft() * sx; + const float y0 = rc.GetTop() * sy; + const float x1 = (rc.GetLeft() + rc.GetWidth()) * sx; + const float y1 = (rc.GetTop() + rc.GetHeight()) * sy; + const float alpha = m_reset_overlay_hovered ? 1.0f : 0.78f; + + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, tex_id); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0.0, viewport_size.x, viewport_size.y, 0.0, -1.0, 1.0); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex2f(x0, y0); + glTexCoord2f(1.0f, 0.0f); glVertex2f(x1, y0); + glTexCoord2f(1.0f, 1.0f); glVertex2f(x1, y1); + glTexCoord2f(0.0f, 1.0f); glVertex2f(x0, y1); + glEnd(); + + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + + glBindTexture(GL_TEXTURE_2D, 0); + glPopAttrib(); +} + +void TexturePreviewCanvas::render_textured_original() +{ + if (m_vertices.empty() || m_indices.empty()) return; + if (m_face_uvs.empty() || m_face_tex_ids.empty()) return; + if (m_face_uvs.size() != m_indices.size()) return; + + upload_textures(); + + const bool has_smooth = (m_vertex_normals.size() == m_vertices.size()); + + // Group faces by texture id for batch rendering + std::map> tex_groups; + for (size_t fi = 0; fi < m_indices.size(); ++fi) { + int tid = (fi < m_face_tex_ids.size()) ? m_face_tex_ids[fi] : -1; + tex_groups[tid].push_back(fi); + } + + glEnable(GL_LIGHTING); + glColor3f(1.0f, 1.0f, 1.0f); + + for (const auto& [tid, face_list] : tex_groups) { + bool tex_bound = false; + if (tid >= 0 && tid < (int)m_gl_tex_ids.size() && m_gl_tex_ids[tid] != 0) { + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_gl_tex_ids[tid]); + tex_bound = true; + } else { + glDisable(GL_TEXTURE_2D); + } + + glBegin(GL_TRIANGLES); + for (size_t fi : face_list) { + const auto& face = m_indices[fi]; + const auto& uvs = m_face_uvs[fi]; + + if (!tex_bound) { + if (fi < m_original_face_colors_rgb.size()) + glColor3fv(m_original_face_colors_rgb[fi].data()); + else + glColor3f(0.7f, 0.7f, 0.7f); + } + + for (int vi = 0; vi < 3; ++vi) { + int idx = face[vi]; + if (idx < 0 || idx >= (int)m_vertices.size()) continue; + + if (has_smooth) { + glNormal3fv(m_vertex_normals[idx].data()); + } else if (vi == 0) { + const auto& v0 = m_vertices[face[0]]; + const auto& v1 = m_vertices[face[1]]; + const auto& v2 = m_vertices[face[2]]; + float nx = (v1[1]-v0[1])*(v2[2]-v0[2]) - (v1[2]-v0[2])*(v2[1]-v0[1]); + float ny = (v1[2]-v0[2])*(v2[0]-v0[0]) - (v1[0]-v0[0])*(v2[2]-v0[2]); + float nz = (v1[0]-v0[0])*(v2[1]-v0[1]) - (v1[1]-v0[1])*(v2[0]-v0[0]); + float len = std::sqrt(nx*nx + ny*ny + nz*nz); + if (len > 1e-8f) { nx /= len; ny /= len; nz /= len; } + glNormal3f(nx, ny, nz); + } + + if (tex_bound) + glTexCoord2fv(uvs[vi].data()); + glVertex3fv(m_vertices[idx].data()); + } + } + glEnd(); + } + + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); +} + +void TexturePreviewCanvas::render_mesh() +{ + if (m_vertices.empty() || m_indices.empty()) return; + + // Original mode with texture data: use proper texture mapping + if (m_mode == RenderMode::Original && !m_face_uvs.empty()) { + render_textured_original(); + return; + } + + // For Multi-Color / FilamentMap, use the painted (remeshed) geometry if available; + // the face color arrays match the painted mesh, not the original mesh. + const bool use_painted = (m_mode != RenderMode::Original) + && !m_painted_vertices.empty() + && !m_painted_indices.empty(); + + const auto& verts = use_painted ? m_painted_vertices : m_vertices; + const auto& faces = use_painted ? m_painted_indices : m_indices; + + const std::vector>* colors_ptr = nullptr; + if (m_mode == RenderMode::Original && !m_original_face_colors_rgb.empty() + && m_original_face_colors_rgb.size() == m_indices.size()) { + colors_ptr = &m_original_face_colors_rgb; + } else if (m_mode == RenderMode::FilamentMap && !m_filament_colors_rgb.empty() + && m_filament_colors_rgb.size() == faces.size()) { + colors_ptr = &m_filament_colors_rgb; + } else if (!m_face_colors_rgb.empty() && m_face_colors_rgb.size() == faces.size()) { + colors_ptr = &m_face_colors_rgb; + } + + // Use smooth normals for the original mesh when available + const bool has_smooth = !use_painted + && (m_vertex_normals.size() == m_vertices.size()); + + glDisable(GL_TEXTURE_2D); + glEnable(GL_LIGHTING); + + glBegin(GL_TRIANGLES); + for (size_t fi = 0; fi < faces.size(); ++fi) { + if (colors_ptr) + glColor3fv((*colors_ptr)[fi].data()); + else + glColor3f(0.7f, 0.7f, 0.7f); + + const auto& face = faces[fi]; + for (int vi = 0; vi < 3; ++vi) { + int idx = face[vi]; + if (idx < 0 || idx >= (int)verts.size()) continue; + + if (has_smooth && idx < (int)m_vertex_normals.size()) { + glNormal3fv(m_vertex_normals[idx].data()); + } else if (vi == 0) { + const auto& v0 = verts[face[0]]; + const auto& v1 = verts[face[1]]; + const auto& v2 = verts[face[2]]; + float nx = (v1[1]-v0[1])*(v2[2]-v0[2]) - (v1[2]-v0[2])*(v2[1]-v0[1]); + float ny = (v1[2]-v0[2])*(v2[0]-v0[0]) - (v1[0]-v0[0])*(v2[2]-v0[2]); + float nz = (v1[0]-v0[0])*(v2[1]-v0[1]) - (v1[1]-v0[1])*(v2[0]-v0[0]); + float len = std::sqrt(nx*nx + ny*ny + nz*nz); + if (len > 1e-8f) { nx /= len; ny /= len; nz /= len; } + glNormal3f(nx, ny, nz); + } + + glVertex3fv(verts[idx].data()); + } + } + glEnd(); +} + + +// ============================================================ +// TextureImportDialog +// ============================================================ + +wxBEGIN_EVENT_TABLE(TextureImportDialog, DPIDialog) + EVT_BUTTON(TextureImportDialog::ID_COLOR_4, TextureImportDialog::on_color_preset_clicked) + EVT_BUTTON(TextureImportDialog::ID_COLOR_8, TextureImportDialog::on_color_preset_clicked) + EVT_BUTTON(TextureImportDialog::ID_COLOR_16, TextureImportDialog::on_color_preset_clicked) + EVT_BUTTON(TextureImportDialog::ID_COLOR_AUTO, TextureImportDialog::on_color_preset_clicked) + EVT_BUTTON(TextureImportDialog::ID_BTN_APPLY, TextureImportDialog::on_apply_clicked) + EVT_BUTTON(TextureImportDialog::ID_BTN_SKIP, TextureImportDialog::on_skip_clicked) + EVT_BUTTON(wxID_OK, TextureImportDialog::on_ok_clicked) +wxEND_EVENT_TABLE() + +TextureImportDialog::TextureImportDialog( + wxWindow* parent, + const Slic3r::TexturedMesh& textured_mesh, + const std::vector& filament_entries, + std::function initial_cancel_callback, + std::function initial_progress_callback) + : DPIDialog(parent, wxID_ANY, _L("Import Model"), + wxDefaultPosition, wxDefaultSize, + (wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) & ~(wxMINIMIZE_BOX | wxMAXIMIZE_BOX)) + , m_textured_mesh(textured_mesh) + , m_filament_entries(filament_entries) + , m_initial_cancel_callback(std::move(initial_cancel_callback)) + , m_initial_progress_callback(std::move(initial_progress_callback)) +{ + SetSize(wxSize(FromDIP(960), FromDIP(640))); + + m_filament_colors_rgba.reserve(m_filament_entries.size()); + m_filament_color_strs.reserve(m_filament_entries.size()); + m_filament_names.reserve(m_filament_entries.size()); + for (size_t i = 0; i < m_filament_entries.size(); ++i) { + auto& entry = m_filament_entries[i]; + entry.dialog_index = (int)i; + entry.color_hex = texture_normalize_color_hex(entry.color_hex); + if (entry.name.empty()) + entry.name = "Filament " + std::to_string(i + 1); + m_filament_color_strs.push_back(entry.color_hex); + m_filament_names.push_back(entry.name); + m_filament_colors_rgba.push_back(parse_color_string(entry.color_hex)); + } + + m_existing_filament_count = m_filament_colors_rgba.size(); + m_default_virtual_filament_preset_name = resolve_default_virtual_filament_preset_name(); + + Bind(EVT_TEXTURE_COMPUTE_DONE, &TextureImportDialog::on_computation_complete, this); + Bind(EVT_TEXTURE_COMPUTE_PROGRESS, &TextureImportDialog::on_computation_progress, this); + Bind(EVT_TEXTURE_COMPUTE_ERROR, &TextureImportDialog::on_computation_error, this); + Bind(EVT_TEXTURE_MESH_REPAIR_DECISION, &TextureImportDialog::on_mesh_repair_decision_required, this); + + build_ui(); + SetMinSize(wxSize(FromDIP(800), FromDIP(500))); + CenterOnParent(); + wxGetApp().UpdateDlgDarkUI(this); + + m_preview_canvas->set_mesh_data(m_textured_mesh.vertices, m_textured_mesh.indices); + + // Prepare texture rendering data for the Original tab + if (!m_textured_mesh.textures.empty()) { + std::vector> tex_pixels_rgb; + std::vector tex_widths, tex_heights; + tex_pixels_rgb.reserve(m_textured_mesh.textures.size()); + tex_widths.reserve(m_textured_mesh.textures.size()); + tex_heights.reserve(m_textured_mesh.textures.size()); + + for (const auto& ti : m_textured_mesh.textures) { + std::vector bgr_pixels; + int w = 0, h = 0; + if (Slic3r::decode_texture_to_pixels(ti, bgr_pixels, w, h) && !bgr_pixels.empty()) { + // Convert BGR to RGB for OpenGL + for (size_t p = 0; p < bgr_pixels.size(); p += 3) + std::swap(bgr_pixels[p], bgr_pixels[p + 2]); + tex_pixels_rgb.push_back(std::move(bgr_pixels)); + } else { + tex_pixels_rgb.push_back({}); + } + tex_widths.push_back(w); + tex_heights.push_back(h); + } + + const size_t nf = m_textured_mesh.indices.size(); + const bool has_mapping = !m_textured_mesh.material_texture_map.empty(); + + // Build per-face UV array + std::vector, 3>> face_uvs(nf); + for (size_t fi = 0; fi < nf; ++fi) { + if (m_textured_mesh.has_face_uvs()) { + const auto& ui = m_textured_mesh.uv_indices[fi]; + for (int vi = 0; vi < 3; ++vi) { + int idx = ui[vi]; + if (idx >= 0 && static_cast(idx) < m_textured_mesh.uv_coords.size()) + face_uvs[fi][vi] = m_textured_mesh.uv_coords[idx]; + else + face_uvs[fi][vi] = {0.f, 0.f}; + } + } else if (!m_textured_mesh.uvs.empty()) { + const auto& face = m_textured_mesh.indices[fi]; + for (int vi = 0; vi < 3; ++vi) { + int idx = face[vi]; + if (idx >= 0 && static_cast(idx) < m_textured_mesh.uvs.size()) + face_uvs[fi][vi] = m_textured_mesh.uvs[idx]; + else + face_uvs[fi][vi] = {0.f, 0.f}; + } + } + } + + // Build per-face texture index + std::vector face_tex_ids(nf, 0); + for (size_t fi = 0; fi < nf; ++fi) { + int mat_idx = (fi < m_textured_mesh.material_ids.size()) + ? m_textured_mesh.material_ids[fi] : -1; + if (has_mapping && mat_idx >= 0 + && static_cast(mat_idx) < m_textured_mesh.material_texture_map.size()) + face_tex_ids[fi] = m_textured_mesh.material_texture_map[mat_idx]; + else if (!tex_pixels_rgb.empty()) + face_tex_ids[fi] = 0; + else + face_tex_ids[fi] = -1; + } + + m_preview_canvas->set_texture_render_data( + tex_pixels_rgb, tex_widths, tex_heights, face_uvs, face_tex_ids); + + // Still sample per-face colors as fallback + std::vector> orig_colors; + if (Slic3r::sample_original_face_colors(m_textured_mesh, orig_colors)) + m_preview_canvas->set_original_face_colors(orig_colors); + } + + set_state(TextureImportState::Idle); +} + +TextureImportDialog::~TextureImportDialog() +{ + dismiss_auto_mix_popup(); + dismiss_filament_popup(); + m_cancel_flag = true; + if (m_worker && m_worker->joinable()) + m_worker->join(); +} + +int TextureImportDialog::ShowModal() +{ + if (m_state == TextureImportState::Idle && m_painted.face_colors.empty()) { + start_computation(true, true); + + while (m_initial_computation_pending) { + if (auto* event_loop = wxEventLoopBase::GetActive()) + event_loop->Yield(); + else + wxYield(); + if (m_progress_dlg && m_progress_dlg->WasCancelled()) + m_cancel_flag = true; + if (m_initial_cancel_callback && m_initial_cancel_callback()) + m_cancel_flag = true; + wxMilliSleep(10); + } + + if (m_worker && m_worker->joinable()) + m_worker->join(); + m_worker.reset(); + + if (m_initial_computation_cancelled || m_initial_computation_failed) + return wxID_CANCEL; + } + + ScopedInteractiveBusyCursorSuspender busy_cursor_suspender; + return DPIDialog::ShowModal(); +} + +void TextureImportDialog::build_ui() +{ + const wxColour dialog_bg = dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)); + SetBackgroundColour(dialog_bg); + SetForegroundColour(dark_or(wxColour(50, 58, 61), wxColour(0xEF, 0xEF, 0xF0))); + + wxBoxSizer* root_sizer = new wxBoxSizer(wxVERTICAL); + + auto line_top = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 1)); + line_top->SetBackgroundColour(dark_or(wxColour(166, 169, 170), wxColour(80, 80, 86))); + root_sizer->Add(line_top, 0, wxEXPAND); + + wxBoxSizer* main_sizer = new wxBoxSizer(wxHORIZONTAL); + + wxBoxSizer* left_sizer = new wxBoxSizer(wxVERTICAL); + build_preview_panel(this, left_sizer); + main_sizer->Add(left_sizer, 3, wxEXPAND | wxALL, FromDIP(8)); + + wxBoxSizer* right_sizer = new wxBoxSizer(wxVERTICAL); + build_params_panel(this, right_sizer); + build_mapping_panel(this, right_sizer); + build_bottom_buttons(right_sizer); + main_sizer->Add(right_sizer, 2, wxEXPAND | wxALL, FromDIP(8)); + + root_sizer->Add(main_sizer, 1, wxEXPAND); + + SetSizer(root_sizer); + Layout(); + Bind(wxEVT_MOUSEWHEEL, &TextureImportDialog::dismiss_filament_popup_on_wheel, this); + +#ifdef __WXMSW__ + wxPanel* size_grip_cover = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE); + size_grip_cover->SetBackgroundColour(dialog_bg); + size_grip_cover->SetBackgroundStyle(wxBG_STYLE_COLOUR); + + auto update_size_grip_cover = [this, size_grip_cover]() { + const int cover_size = FromDIP(20); + wxSize client_size = GetClientSize(); + size_grip_cover->SetSize(client_size.x - cover_size, client_size.y - cover_size, cover_size, cover_size); + size_grip_cover->Raise(); + }; + update_size_grip_cover(); + + Bind(wxEVT_SIZE, [update_size_grip_cover](wxSizeEvent& e) { + e.Skip(); + update_size_grip_cover(); + }); +#endif +} + +void TextureImportDialog::build_preview_panel(wxWindow* parent, wxSizer* sizer) +{ + wxColour preview_bg = dark_or(wxColour(238, 238, 238), wxColour(0x3E, 0x3E, 0x45)); + wxColour preview_bd = dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)); + + wxPanel* preview_container = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE); + preview_container->SetBackgroundColour(preview_bg); + preview_container->SetBackgroundStyle(wxBG_STYLE_PAINT); + preview_container->Bind(wxEVT_PAINT, [preview_bg, preview_bd](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + dc.SetBrush(wxBrush(p->GetParent()->GetBackgroundColour())); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + dc.SetBrush(wxBrush(preview_bg)); + dc.SetPen(wxPen(preview_bd, 1)); + dc.DrawRoundedRectangle(0, 0, sz.x, sz.y, 4); + }); + + wxBoxSizer* container_sizer = new wxBoxSizer(wxVERTICAL); + + wxGLAttributes canvas_attrs; + canvas_attrs.PlatformDefaults().RGBA().DoubleBuffer().Depth(24).EndList(); + m_preview_canvas = new TexturePreviewCanvas(preview_container, canvas_attrs); + container_sizer->Add(m_preview_canvas, 1, wxEXPAND | wxALL, FromDIP(1)); + + preview_container->SetSizer(container_sizer); + sizer->Add(preview_container, 1, wxEXPAND); + + m_tab_panel = new wxPanel(preview_container, wxID_ANY); + m_tab_panel->SetBackgroundColour(preview_bg); + + m_btn_view_original = new Button(m_tab_panel, _L("Original")); + m_btn_view_original->SetId(ID_VIEW_ORIGINAL); + m_btn_view_multicolor = new Button(m_tab_panel, _L("Multi-Color")); + m_btn_view_multicolor->SetId(ID_VIEW_MULTICOLOR); + + const int view_button_height = FromDIP(27); + m_btn_view_original->SetCornerRadius(view_button_height / 2); + m_btn_view_original->SetMinSize(wxSize(FromDIP(57), view_button_height)); + m_btn_view_original->SetFont(m_btn_view_original->GetFont().Bold()); + m_btn_view_original->SetToolTip(_L("Your input texture model")); + m_btn_view_multicolor->SetCornerRadius(view_button_height / 2); + m_btn_view_multicolor->SetMinSize(wxSize(FromDIP(57), view_button_height)); + m_btn_view_multicolor->SetFont(m_btn_view_multicolor->GetFont().Bold()); + m_btn_view_multicolor->SetToolTip(_L("Processed multi-color model")); + + wxBoxSizer* tab_sizer = new wxBoxSizer(wxHORIZONTAL); + tab_sizer->Add(m_btn_view_original, 0, wxRIGHT, FromDIP(2)); + tab_sizer->Add(m_btn_view_multicolor, 0); + m_tab_panel->SetSizer(tab_sizer); + m_tab_panel->Fit(); + + m_btn_view_multicolor->Hide(); + + auto preview_original = [this](wxMouseEvent& e) { + if (m_preview_canvas) { + m_preview_canvas->set_render_mode(TexturePreviewCanvas::RenderMode::Original); + highlight_view_button(0); + } + e.Skip(); + }; + auto preview_multicolor = [this](wxMouseEvent& e) { + if (m_preview_canvas) { + m_preview_canvas->set_render_mode(TexturePreviewCanvas::RenderMode::MultiColor); + highlight_view_button(1); + } + e.Skip(); + }; + auto restore_filament_if_outside = [this](wxMouseEvent& e) { + if (m_preview_canvas && m_tab_panel) { + wxWindow* event_window = wxDynamicCast(e.GetEventObject(), wxWindow); + wxPoint screen_pos = event_window ? event_window->ClientToScreen(e.GetPosition()) : wxGetMousePosition(); + wxPoint panel_pos = m_tab_panel->ScreenToClient(screen_pos); + if (!m_tab_panel->GetClientRect().Contains(panel_pos)) { + const bool mapping_ready = (m_state == TextureImportState::Ready); + m_preview_canvas->set_render_mode(mapping_ready ? TexturePreviewCanvas::RenderMode::FilamentMap : + TexturePreviewCanvas::RenderMode::Original); + highlight_view_button(-1); + } + } + e.Skip(); + }; + + m_btn_view_original->Bind(wxEVT_ENTER_WINDOW, preview_original); + m_btn_view_multicolor->Bind(wxEVT_ENTER_WINDOW, preview_multicolor); + m_btn_view_original->Bind(wxEVT_LEAVE_WINDOW, restore_filament_if_outside); + m_btn_view_multicolor->Bind(wxEVT_LEAVE_WINDOW, restore_filament_if_outside); + m_tab_panel->Bind(wxEVT_LEAVE_WINDOW, restore_filament_if_outside); + + auto update_preview_overlay_buttons = [this]() { + if (m_tab_panel) { + m_tab_panel->Fit(); + m_tab_panel->SetPosition(wxPoint(FromDIP(8), FromDIP(8))); + m_tab_panel->Raise(); + } + }; + + preview_container->Bind(wxEVT_SIZE, [update_preview_overlay_buttons](wxSizeEvent& e) { + e.Skip(); + update_preview_overlay_buttons(); + }); + update_preview_overlay_buttons(); + + highlight_view_button(-1); +} + +void TextureImportDialog::build_params_panel(wxWindow* parent, wxSizer* sizer) +{ + wxColour label_fg = dark_or(wxColour(50, 58, 61), wxColour(0xEF, 0xEF, 0xF0)); + + wxBoxSizer* color_header_sizer = new wxBoxSizer(wxHORIZONTAL); + wxStaticText* lbl_colors = new wxStaticText(parent, wxID_ANY, _L("Color Count")); + lbl_colors->SetForegroundColour(label_fg); + lbl_colors->SetFont(lbl_colors->GetFont().Bold()); + color_header_sizer->Add(lbl_colors, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(8)); + + m_btn_color_4 = new Button(parent, "4"); + m_btn_color_4->SetId(ID_COLOR_4); + m_btn_color_8 = new Button(parent, "8"); + m_btn_color_8->SetId(ID_COLOR_8); + m_btn_color_16 = new Button(parent, "16"); + m_btn_color_16->SetId(ID_COLOR_16); + m_btn_color_auto = new Button(parent, _L("Auto")); + m_btn_color_auto->SetId(ID_COLOR_AUTO); + + { + StateColor preset_bg( + std::pair(wxColour(27, 136, 68), StateColor::Pressed | StateColor::Checked), + std::pair(wxColour(61, 203, 115), StateColor::Hovered | StateColor::Checked), + std::pair(wxColour(0, 174, 66), StateColor::Checked), + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Pressed), + std::pair(dark_or(wxColour(238, 238, 238), wxColour(0x4C, 0x4C, 0x55)), StateColor::Hovered), + std::pair(dark_or(wxColour(255, 255, 255), wxColour(0x2D, 0x2D, 0x31)), StateColor::Normal)); + StateColor preset_bd( + std::pair(wxColour(0, 174, 66), StateColor::Checked), + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor preset_text( + std::pair(wxColour(255, 255, 255), StateColor::Checked), + std::pair(dark_or(wxColour(50, 58, 61), wxColour(0xEF, 0xEF, 0xF0)), StateColor::Normal)); + + for (auto* btn : {m_btn_color_4, m_btn_color_8, m_btn_color_16}) { + btn->SetCornerRadius(FromDIP(12)); + btn->SetMinSize(wxSize(FromDIP(28), FromDIP(28))); + btn->SetBackgroundColor(preset_bg); + btn->SetBorderColor(preset_bd); + btn->SetTextColor(preset_text); + } + } + + update_color_count_preset_buttons(); + + color_header_sizer->Add(m_btn_color_4, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(2)); + color_header_sizer->Add(m_btn_color_8, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(2)); + color_header_sizer->Add(m_btn_color_16, 0, wxALIGN_CENTER_VERTICAL); + sizer->Add(color_header_sizer, 0, wxBOTTOM, FromDIP(4)); + + wxBoxSizer* color_slider_sizer = new wxBoxSizer(wxHORIZONTAL); + m_color_slider = new GreenSlider(parent, m_param_color_count, 1, (int)max_filament_count()); + m_color_spin = new SpinInput(parent, wxString::Format("%d", m_param_color_count), + wxEmptyString, wxDefaultPosition, + wxSize(FromDIP(60), FromDIP(28)), + wxTE_PROCESS_ENTER, 1, (int)max_filament_count(), m_param_color_count); + + m_color_slider->Bind(wxEVT_SLIDER, &TextureImportDialog::on_color_slider_changed, this); + m_color_spin->Bind(wxEVT_SPINCTRL, &TextureImportDialog::on_color_spin_changed, this); + m_color_spin->Bind(EVT_SPINCTRL_TEXT, &TextureImportDialog::on_color_spin_text_changed, this); + + color_slider_sizer->Add(m_color_slider, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(4)); + color_slider_sizer->Add(m_color_spin, 0, wxALIGN_CENTER_VERTICAL); + sizer->Add(color_slider_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(8)); + + wxStaticText* lbl_smooth = new wxStaticText(parent, wxID_ANY, _L("Smooth Level")); + lbl_smooth->SetForegroundColour(label_fg); + lbl_smooth->SetFont(lbl_smooth->GetFont().Bold()); + sizer->Add(lbl_smooth, 0, wxBOTTOM, FromDIP(4)); + + wxBoxSizer* smooth_sizer = new wxBoxSizer(wxHORIZONTAL); + m_smooth_slider = new GreenSlider(parent, m_param_smooth, 0, 10); + m_smooth_spin = new SpinInput(parent, wxString::Format("%d", m_param_smooth), + wxEmptyString, wxDefaultPosition, + wxSize(FromDIP(60), FromDIP(28)), + wxTE_PROCESS_ENTER, 0, 10, m_param_smooth); + + m_smooth_slider->Bind(wxEVT_SLIDER, &TextureImportDialog::on_smooth_slider_changed, this); + m_smooth_spin->Bind(wxEVT_SPINCTRL, &TextureImportDialog::on_smooth_spin_changed, this); + m_smooth_spin->Bind(EVT_SPINCTRL_TEXT, &TextureImportDialog::on_smooth_spin_text_changed, this); + + smooth_sizer->Add(m_smooth_slider, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, FromDIP(4)); + smooth_sizer->Add(m_smooth_spin, 0, wxALIGN_CENTER_VERTICAL); + sizer->Add(smooth_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(4)); + + m_btn_apply = new Button(parent, _L("Apply")); + m_btn_apply->SetId(ID_BTN_APPLY); + + { + StateColor btn_bg_white( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Pressed), + std::pair(dark_or(wxColour(238, 238, 238), wxColour(0x4C, 0x4C, 0x55)), StateColor::Hovered), + std::pair(dark_or(wxColour(255, 255, 255), wxColour(0x2D, 0x2D, 0x31)), StateColor::Normal)); + StateColor btn_bd_green( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor btn_text_green( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + + m_btn_color_auto->SetCornerRadius(FromDIP(12)); + m_btn_color_auto->SetMinSize(wxSize(FromDIP(60), FromDIP(28))); + m_btn_color_auto->SetBackgroundColor(btn_bg_white); + m_btn_color_auto->SetBorderColor(btn_bd_green); + m_btn_color_auto->SetTextColor(btn_text_green); + + m_btn_apply->SetCornerRadius(FromDIP(12)); + m_btn_apply->SetMinSize(wxSize(FromDIP(60), FromDIP(28))); + m_btn_apply->SetBackgroundColor(btn_bg_white); + m_btn_apply->SetBorderColor(btn_bd_green); + m_btn_apply->SetTextColor(btn_text_green); + } + + // Defer attaching the Auto/Apply tooltips until the dialog has actually + // been shown. On macOS, AppKit creates NSTrackingArea and dispatches a + // synthetic mouseEntered: as soon as the window first becomes visible, + // which would otherwise pop the native tooltip without the user actually + // hovering when the cursor happens to land on these buttons as the dialog + // appears. + Bind(wxEVT_SHOW, [this](wxShowEvent& e) { + e.Skip(); + if (!e.IsShown() || m_initial_tooltips_set) + return; + m_initial_tooltips_set = true; + CallAfter([this]() { + if (m_btn_color_auto) + m_btn_color_auto->SetToolTip(_L("Automatically determine the optimal color count only and recompute filament mapping")); + if (m_btn_apply) + m_btn_apply->SetToolTip(_L("Convert texture to painting using the specified color count and smooth level")); + }); + }); + + wxBoxSizer* apply_sizer = new wxBoxSizer(wxHORIZONTAL); + apply_sizer->Add(m_btn_color_auto, 0, wxRIGHT, FromDIP(4)); + apply_sizer->Add(m_btn_apply, 0); + sizer->Add(apply_sizer, 0, wxALIGN_RIGHT | wxBOTTOM, FromDIP(8)); + + m_hint_label = new wxStaticText(parent, wxID_ANY, + _L("Reminder: parameters changed, click Apply to take effect")); + m_hint_label->SetForegroundColour(wxColour(0xFF, 0x6F, 0x00)); + m_hint_label->SetFont(texture_import_section_title_font(parent)); + m_hint_label->Hide(); + sizer->Add(m_hint_label, 0, wxBOTTOM, FromDIP(4)); + + auto* mapping_separator = new StaticLine(parent); + mapping_separator->SetLineColour(texture_import_separator_colour()); + sizer->Add(mapping_separator, 0, wxEXPAND | wxBOTTOM, FromDIP(8)); +} + +void TextureImportDialog::build_mapping_panel(wxWindow* parent, wxSizer* sizer) +{ + wxColour secondary_fg = dark_or(wxColour(107, 107, 107), wxColour(0x81, 0x81, 0x83)); + + wxBoxSizer* header_sizer = new wxBoxSizer(wxHORIZONTAL); + + wxStaticText* lbl_mapping = new wxStaticText(parent, wxID_ANY, _L("Filament Mapping")); + lbl_mapping->SetForegroundColour(secondary_fg); + lbl_mapping->SetFont(texture_import_section_title_font(parent)); + m_auto_mix_font_point_size = lbl_mapping->GetFont().GetPointSize(); + header_sizer->Add(lbl_mapping, 0, wxALIGN_CENTER_VERTICAL); + + m_btn_mix_reset = new Button(parent, "", "revert_btn", wxBORDER_NONE, 16); + m_btn_mix_reset->SetCanFocus(false); + m_btn_mix_reset->SetPaddingSize(wxSize(FromDIP(2), FromDIP(2))); + { + StateColor reset_bg( + std::pair(dark_or(wxColour(245, 245, 245), wxColour(0x3C, 0x3C, 0x42)), StateColor::Pressed), + std::pair(dark_or(wxColour(248, 248, 248), wxColour(0x35, 0x35, 0x3A)), StateColor::Hovered), + std::pair(dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)), StateColor::Normal)); + m_btn_mix_reset->SetBackgroundColor(reset_bg); + m_btn_mix_reset->SetBorderColor(StateColor()); + } + m_btn_mix_reset->SetToolTip(_L("Reset filament mapping to the state before one-click mixing")); + m_btn_mix_reset->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) { + reset_auto_mix(); + evt.Skip(); + }); + m_btn_mix_reset->Hide(); + header_sizer->Add(m_btn_mix_reset, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, FromDIP(6)); + + header_sizer->AddStretchSpacer(); + + m_btn_auto_mix = new Button(parent, auto_mix_mode_label(m_auto_mix_mode)); + { + wxFont btn_font = m_btn_auto_mix->GetFont(); + btn_font.SetPointSize(m_auto_mix_font_point_size); + m_btn_auto_mix->SetFont(btn_font); + } + m_btn_auto_mix->SetCornerRadius(FromDIP(14)); + m_btn_auto_mix->SetMinSize(wxSize(FromDIP(178), FromDIP(28))); + { + StateColor btn_bg( + std::pair(dark_or(wxColour(245, 245, 245), wxColour(0x3C, 0x3C, 0x42)), StateColor::Pressed), + std::pair(dark_or(wxColour(248, 248, 248), wxColour(0x35, 0x35, 0x3A)), StateColor::Hovered), + std::pair(dark_or(*wxWHITE, wxColour(0x2D, 0x2D, 0x31)), StateColor::Normal)); + StateColor btn_bd( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor btn_text( + std::pair(texture_import_text_colour(), StateColor::Normal)); + m_btn_auto_mix->SetBackgroundColor(btn_bg); + m_btn_auto_mix->SetBorderColor(btn_bd); + m_btn_auto_mix->SetTextColor(btn_text); + } + m_btn_auto_mix->SetToolTip(_L("Choose the one-click auto-mix mode for texture color import")); + m_btn_auto_mix->Bind(wxEVT_ENTER_WINDOW, [this](wxMouseEvent& evt) { + show_auto_mix_popup(); + evt.Skip(); + }); + m_btn_auto_mix->Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent& evt) { + show_auto_mix_popup(); + evt.Skip(); + }); + header_sizer->Add(m_btn_auto_mix, 0, wxALIGN_CENTER_VERTICAL); + + sizer->Add(header_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(4)); + + wxBoxSizer* merge_sizer = new wxBoxSizer(wxHORIZONTAL); + m_auto_merge_cb = new wxCheckBox(parent, wxID_ANY, _L("Auto-merge same filament")); + m_auto_merge_cb->SetToolTip(_L("Automatically merge identical filaments into existing filaments in the project")); + m_auto_merge_cb->SetForegroundColour(secondary_fg); + m_auto_merge_cb->SetValue(true); + m_auto_merge_cb->Bind(wxEVT_CHECKBOX, &TextureImportDialog::on_auto_merge_toggled, this); + merge_sizer->Add(m_auto_merge_cb, 0, wxALIGN_CENTER_VERTICAL); + + sizer->Add(merge_sizer, 0, wxEXPAND | wxBOTTOM, FromDIP(8)); + + m_mapping_scroll = new wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, + wxSize(-1, FromDIP(300))); + m_mapping_scroll->SetScrollRate(0, FromDIP(10)); + m_mapping_scroll->SetBackgroundColour(dark_or(wxColour(255, 255, 255), wxColour(0x2D, 0x2D, 0x31))); + m_mapping_scroll->Bind(wxEVT_MOUSEWHEEL, &TextureImportDialog::dismiss_filament_popup_on_wheel, this); + + m_mapping_sizer = new wxBoxSizer(wxVERTICAL); + m_mapping_scroll->SetSizer(m_mapping_sizer); + + sizer->Add(m_mapping_scroll, 1, wxEXPAND | wxBOTTOM, FromDIP(8)); +} + +void TextureImportDialog::build_bottom_buttons(wxSizer* sizer) +{ + m_drop_warning_label = new wxStaticText(this, wxID_ANY, + wxString::Format( + _L("The project supports up to %d filaments. Extra filaments will be discarded."), + (int)max_filament_count())); + m_drop_warning_label->SetForegroundColour(wxColour(0xFF, 0x6F, 0x00)); + m_drop_warning_label->SetFont(texture_import_section_title_font(this)); + m_drop_warning_label->Hide(); + sizer->Add(m_drop_warning_label, 0, wxALIGN_LEFT | wxBOTTOM, FromDIP(4)); + + wxBoxSizer* btn_sizer = new wxBoxSizer(wxHORIZONTAL); + m_btn_skip = new Button(this, _L("Skip Matching")); + m_btn_skip->SetId(ID_BTN_SKIP); + m_btn_skip->SetToolTip(_L("Skip filament mapping and import as a single-color model")); + m_btn_skip->SetCornerRadius(FromDIP(20)); + m_btn_skip->SetMinSize(wxSize(FromDIP(136), FromDIP(40))); + { + StateColor skip_bg( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Pressed), + std::pair(dark_or(wxColour(238, 238, 238), wxColour(0x4C, 0x4C, 0x55)), StateColor::Hovered), + std::pair(dark_or(wxColour(255, 255, 255), wxColour(0x2D, 0x2D, 0x31)), StateColor::Normal)); + StateColor skip_bd( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor skip_text( + std::pair(dark_or(wxColour(107, 107, 107), wxColour(0xB3, 0xB3, 0xB5)), StateColor::Normal)); + m_btn_skip->SetBackgroundColor(skip_bg); + m_btn_skip->SetBorderColor(skip_bd); + m_btn_skip->SetTextColor(skip_text); + } + + m_btn_ok = new Button(this, _L("Confirm")); + m_btn_ok->SetId(wxID_OK); + m_btn_ok->SetCornerRadius(FromDIP(20)); + m_btn_ok->SetMinSize(wxSize(FromDIP(156), FromDIP(40))); + { + StateColor ok_bg( + std::pair(wxColour(27, 136, 68), StateColor::Pressed), + std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor ok_bd( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor ok_text( + std::pair(wxColour("#FFFFFE"), StateColor::Normal)); + m_btn_ok->SetBackgroundColor(ok_bg); + m_btn_ok->SetBorderColor(ok_bd); + m_btn_ok->SetTextColor(ok_text); + } + + btn_sizer->AddStretchSpacer(); + btn_sizer->Add(m_btn_skip, 0, wxRIGHT, FromDIP(16)); + btn_sizer->Add(m_btn_ok, 0); + + sizer->Add(btn_sizer, 0, wxEXPAND | wxTOP, FromDIP(8)); +} + +// ---- State machine ---- + +void TextureImportDialog::set_state(TextureImportState new_state) +{ + m_state = new_state; + update_ui_for_state(); +} + +void TextureImportDialog::update_ui_for_state() +{ + bool computing = (m_state == TextureImportState::Computing); + bool ready = (m_state == TextureImportState::Ready); + bool idle = (m_state == TextureImportState::Idle); + bool valid = has_valid_result(); + + m_color_slider->Enable(!computing); + m_color_spin->Enable(!computing); + m_smooth_slider->Enable(!computing); + m_smooth_spin->Enable(!computing); + m_btn_apply->Enable(!computing); + m_btn_color_4->Enable(!computing); + m_btn_color_8->Enable(!computing); + m_btn_color_16->Enable(!computing); + m_btn_color_auto->Enable(!computing); + if (m_btn_auto_mix) + m_btn_auto_mix->Enable(!computing); + if (m_btn_mix_reset) + m_btn_mix_reset->Enable(!computing); + if (computing) + dismiss_auto_mix_popup(); + + m_btn_ok->Enable(ready && valid); + m_btn_skip->Enable(ready || idle); + + m_auto_merge_cb->Enable(!computing); + + m_preview_canvas->set_computing_overlay(computing); + + if (ready && valid && is_params_dirty()) { + m_btn_ok->Enable(true); + StateColor gray_bg( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor gray_bd( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor gray_text( + std::pair(dark_or(wxColour(107, 107, 107), wxColour(0xB3, 0xB3, 0xB5)), StateColor::Normal)); + m_btn_ok->SetBackgroundColor(gray_bg); + m_btn_ok->SetBorderColor(gray_bd); + m_btn_ok->SetTextColor(gray_text); + m_btn_ok->SetToolTip(_L("Reminder: parameters changed, click Apply to take effect")); + if (m_hint_label) m_hint_label->Show(); + } else if (ready && valid) { + StateColor ok_bg( + std::pair(wxColour(27, 136, 68), StateColor::Pressed), + std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor ok_bd( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor ok_text( + std::pair(wxColour("#FFFFFE"), StateColor::Normal)); + m_btn_ok->SetBackgroundColor(ok_bg); + m_btn_ok->SetBorderColor(ok_bd); + m_btn_ok->SetTextColor(ok_text); + m_btn_ok->UnsetToolTip(); + if (m_hint_label) m_hint_label->Hide(); + } else { + if (m_hint_label) m_hint_label->Hide(); + } + + m_btn_ok->Refresh(); + Layout(); +} + +// ---- Async computation ---- + +void TextureImportDialog::start_computation(bool auto_color, bool initial) +{ + cancel_computation(); + + m_cancel_flag = false; + m_current_computation_initial = initial; + m_current_computation_auto_color = auto_color; + if (initial) { + m_initial_computation_pending = true; + m_initial_computation_cancelled = false; + m_initial_computation_failed = false; + } + set_state(TextureImportState::Computing); + + bool silent_initial = initial && static_cast(m_initial_cancel_callback); + if (!silent_initial) { + m_progress_dlg = new ProgressDialog( + _L("Processing"), _L("Computing texture colors..."), + 100, initial ? GetParent() : this, wxPD_APP_MODAL | wxPD_CAN_ABORT | wxPD_AUTO_HIDE); + } + + Slic3r::TexturePaintingSettings settings; + settings.target_colors_num = auto_color ? 0 : (size_t)m_param_color_count; + settings.smooth_weight = m_param_smooth / 10.0; + settings.mesh_repair_decision = m_mesh_repair_decision; + // BBS repairs the mesh through the Windows 3D SDK, which only exists on Windows and only + // when the SDK is present at build time. Orca already ships a CGAL-based repair + // (MeshBoolean::cgal::repair) that works on all three platforms, so use that instead — + // this makes the repair path available on Linux and macOS too. + settings.mesh_repair_callback = [](const indexed_triangle_set& mesh, + indexed_triangle_set& repaired_mesh, + std::function progress_callback, + std::function cancel_callback, + std::string* error_message) -> bool { + if (cancel_callback && cancel_callback()) + return false; + if (progress_callback) + progress_callback(_u8L("Repairing mesh").c_str(), 0); + + TriangleMesh tm(mesh); + if (!MeshBoolean::cgal::repair(tm, nullptr, error_message)) + return false; + + if (cancel_callback && cancel_callback()) + return false; + repaired_mesh = tm.its; + if (progress_callback) + progress_callback(_u8L("Repairing mesh").c_str(), 100); + return true; + }; + + Slic3r::TexturedMesh mesh_copy = m_textured_mesh; + wxEvtHandler* handler = this; + + m_worker = std::make_unique([this, settings, mesh_copy, handler]() { + Slic3r::PaintedMesh result; + + auto progress_cb = [handler](int percent, const char*) { + auto* evt = new wxCommandEvent(EVT_TEXTURE_COMPUTE_PROGRESS); + evt->SetInt(percent); + wxQueueEvent(handler, evt); + }; + + auto cancel_cb = [this]() -> bool { + return m_cancel_flag.load(); + }; + + auto worker_settings = settings; + bool mesh_repair_decision_required = false; + worker_settings.mesh_repair_decision_required = &mesh_repair_decision_required; + bool ok = Slic3r::texture_to_painting(mesh_copy, result, worker_settings, progress_cb, cancel_cb); + + if (m_cancel_flag.load()) { + wxQueueEvent(handler, new wxCommandEvent(EVT_TEXTURE_COMPUTE_ERROR)); + return; + } + + if (!ok && mesh_repair_decision_required) { + wxQueueEvent(handler, new wxCommandEvent(EVT_TEXTURE_MESH_REPAIR_DECISION)); + return; + } + + { + std::lock_guard lock(m_result_mutex); + m_pending_result = std::move(result); + } + + if (ok) { + wxQueueEvent(handler, new wxCommandEvent(EVT_TEXTURE_COMPUTE_DONE)); + } else { + wxQueueEvent(handler, new wxCommandEvent(EVT_TEXTURE_COMPUTE_ERROR)); + } + }); +} + +void TextureImportDialog::cancel_computation() +{ + m_cancel_flag = true; + if (m_worker && m_worker->joinable()) + m_worker->join(); + m_worker.reset(); + + if (m_progress_dlg) { + m_progress_dlg->Destroy(); + m_progress_dlg = nullptr; + } + + if (m_current_computation_initial) { + m_initial_computation_cancelled = true; + m_initial_computation_pending = false; + m_current_computation_initial = false; + } +} + +void TextureImportDialog::on_computation_progress(wxCommandEvent& evt) +{ + if (m_progress_dlg) { + if (!m_progress_dlg->Update(evt.GetInt())) + m_cancel_flag = true; + } else if (m_current_computation_initial && m_initial_progress_callback) { + if (!m_initial_progress_callback(evt.GetInt())) + m_cancel_flag = true; + } +} + +void TextureImportDialog::on_computation_complete(wxCommandEvent&) +{ + bool initial = m_current_computation_initial; + + if (m_progress_dlg) { + m_progress_dlg->Destroy(); + m_progress_dlg = nullptr; + } + + { + std::lock_guard lock(m_result_mutex); + m_painted = std::move(m_pending_result); + } + + int actual_colors = (int)m_painted.cluster_colors.size(); + if (actual_colors >= 2 && actual_colors <= (int)max_filament_count()) { + set_color_count_value(actual_colors, true); + } + + m_preview_canvas->set_painted_mesh_data(m_painted.vertices, m_painted.indices); + m_preview_canvas->set_face_colors(m_painted.face_colors); + + // A fresh texture computation replaces m_painted, so virtual filaments from + // the previous computation must not consume capacity when deciding whether + // this run drops extra colors. Rebuild virtual filaments from this result. + m_current_matches.clear(); + if (m_filament_colors_rgba.size() > m_existing_filament_count) + m_filament_colors_rgba.resize(m_existing_filament_count); + if (m_filament_color_strs.size() > m_existing_filament_count) + m_filament_color_strs.resize(m_existing_filament_count); + if (m_filament_names.size() > m_existing_filament_count) + m_filament_names.resize(m_existing_filament_count); + if (m_filament_entries.size() > m_existing_filament_count) + m_filament_entries.resize(m_existing_filament_count); + while (m_filament_entries.size() < m_existing_filament_count) { + TextureFilamentEntry entry; + entry.kind = TextureFilamentKind::ExistingPhysical; + entry.dialog_index = (int)m_filament_entries.size(); + entry.project_config_index = m_filament_entries.size(); + m_filament_entries.push_back(entry); + } + for (size_t i = 0; i < m_filament_entries.size(); ++i) { + m_filament_entries[i].dialog_index = (int)i; + m_filament_entries[i].color_hex = i < m_filament_color_strs.size() ? + texture_normalize_color_hex(m_filament_color_strs[i]) : "#808080"; + m_filament_entries[i].name = i < m_filament_names.size() ? + m_filament_names[i] : "Filament " + std::to_string(i + 1); + } + m_new_filament_colors.clear(); + m_new_filament_preset_names.clear(); + m_new_mixed_filaments.clear(); + + do_auto_match(); + compact_used_virtual_filaments(); + sort_current_matches_by_filament_index(); + update_filament_color_map(); + rebuild_mapping_rows(); + + m_applied_color_count = m_param_color_count; + m_applied_smooth = m_param_smooth; + + set_state(TextureImportState::Ready); + update_drop_warning_visibility(); + update_auto_mix_reset_visibility(); + + m_btn_view_multicolor->Show(); + if (m_tab_panel) { + m_tab_panel->GetSizer()->Layout(); + m_tab_panel->Fit(); + m_tab_panel->SetPosition(wxPoint(FromDIP(8), FromDIP(8))); + } + GetSizer()->Layout(); + + m_preview_canvas->set_render_mode(TexturePreviewCanvas::RenderMode::FilamentMap); + highlight_view_button(-1); + + if (initial) { + m_initial_computation_pending = false; + m_current_computation_initial = false; + } +} + +void TextureImportDialog::on_computation_error(wxCommandEvent&) +{ + bool initial = m_current_computation_initial; + + if (m_progress_dlg) { + m_progress_dlg->Destroy(); + m_progress_dlg = nullptr; + } + + if (m_cancel_flag.load()) { + if (initial) { + m_initial_computation_cancelled = true; + m_initial_computation_pending = false; + m_current_computation_initial = false; + return; + } + if (has_valid_result()) { + if (m_applied_color_count >= 0) { + m_param_color_count = m_applied_color_count; + m_color_slider->SetValue(m_param_color_count); + m_color_spin->SetValue(m_param_color_count); + update_color_count_preset_buttons(); + } + if (m_applied_smooth >= 0) { + m_param_smooth = m_applied_smooth; + m_smooth_slider->SetValue(m_param_smooth); + m_smooth_spin->SetValue(m_param_smooth); + } + set_state(TextureImportState::Ready); + return; + } + set_state(TextureImportState::Idle); + return; + } + + if (initial) { + m_initial_computation_failed = true; + m_initial_computation_pending = false; + m_current_computation_initial = false; + m_fallback_to_geometry_only = true; + return; + } + + set_state(TextureImportState::Error); + Slic3r::GUI::MessageDialog dlg(initial ? GetParent() : this, + _L("Computation failed. Please adjust parameters and retry."), + _L("Error"), wxOK | wxICON_ERROR); + dlg.ShowModal(); +} + +void TextureImportDialog::on_mesh_repair_decision_required(wxCommandEvent&) +{ + bool initial = m_current_computation_initial; + bool auto_color = m_current_computation_auto_color; + + if (m_progress_dlg) { + m_progress_dlg->Destroy(); + m_progress_dlg = nullptr; + } + +#ifdef HAS_WIN10SDK + Slic3r::GUI::MessageDialog dlg(initial ? GetParent() : this, + _L("The mesh has non-manifold geometry or open boundaries. You can import it as-is or repair it with Windows 3D repair service before importing."), + _L("Mesh repair"), wxYES_NO | wxICON_WARNING | wxYES_DEFAULT); + dlg.SetButtonLabel(wxID_YES, _L("Import without repair")); + dlg.SetButtonLabel(wxID_NO, _L("Repair and import"), true); + StateColor primary_bg( + std::pair(wxColour(27, 136, 68), StateColor::Pressed), + std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor primary_bd( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor primary_text( + std::pair(wxColour("#FFFFFE"), StateColor::Normal)); + StateColor secondary_bg( + std::pair(wxColour("#CECECE"), StateColor::Pressed), + std::pair(wxColour("#EEEEEE"), StateColor::Hovered), + std::pair(*wxWHITE, StateColor::Normal)); + StateColor secondary_bd( + std::pair(texture_import_gray9000(), StateColor::Normal)); + StateColor secondary_text( + std::pair(texture_import_gray9000(), StateColor::Normal)); + if (auto* yes_btn = dynamic_cast(dlg.FindWindow(wxID_YES))) { + yes_btn->SetMinSize(wxSize(FromDIP(180), FromDIP(24))); + yes_btn->SetBackgroundColor(secondary_bg); + yes_btn->SetBorderColor(secondary_bd); + yes_btn->SetTextColor(secondary_text); + } + if (auto* no_btn = dynamic_cast(dlg.FindWindow(wxID_NO))) { + no_btn->SetMinSize(wxSize(FromDIP(160), FromDIP(24))); + no_btn->SetBackgroundColor(primary_bg); + no_btn->SetBorderColor(primary_bd); + no_btn->SetTextColor(primary_text); + } + dlg.Layout(); + dlg.Fit(); + dlg.CenterOnParent(); + int ret = dlg.ShowModal(); + m_mesh_repair_decision = (ret == wxID_NO) + ? Slic3r::TexturePaintingSettings::MeshRepairDecision::RepairAndImport + : Slic3r::TexturePaintingSettings::MeshRepairDecision::ImportWithoutRepair; +#else + Slic3r::GUI::MessageDialog dlg(initial ? GetParent() : this, + _L("Please note that the mesh has non-manifold geometry or open boundaries."), + _L("Mesh issue"), wxOK | wxCANCEL | wxICON_WARNING | wxOK_DEFAULT); + dlg.SetButtonLabel(wxID_OK, _L("Continue"), true); + dlg.SetButtonLabel(wxID_CANCEL, _L("Cancel")); + int ret = dlg.ShowModal(); + if (ret != wxID_OK) { + m_cancel_flag = true; + if (initial) { + m_initial_computation_cancelled = true; + m_initial_computation_pending = false; + m_current_computation_initial = false; + } else if (has_valid_result()) { + set_state(TextureImportState::Ready); + } else { + set_state(TextureImportState::Idle); + } + return; + } + m_mesh_repair_decision = Slic3r::TexturePaintingSettings::MeshRepairDecision::ImportWithoutRepair; +#endif + + start_computation(auto_color, initial); +} + +// ---- Mapping ---- + +void TextureImportDialog::update_filament_color_map() +{ + std::map, std::array> color_map; + for (const auto& m : m_current_matches) { + if (m.filament_index >= 0 && m.filament_index < (int)m_filament_colors_rgba.size()) { + color_map[m.cluster_color] = { + m_filament_colors_rgba[m.filament_index][0], + m_filament_colors_rgba[m.filament_index][1], + m_filament_colors_rgba[m.filament_index][2] + }; + } + } + m_preview_canvas->set_filament_color_map(color_map); +} + +// Canonical ordering used on the very first display after a computation: +// sort ascending by filament_index, and push unmapped (filament_index < 0) +// entries to the end. This gives the user a stable, predictable mapping +// layout regardless of the cluster discovery order. +void TextureImportDialog::sort_current_matches_by_filament_index() +{ + std::stable_sort(m_current_matches.begin(), m_current_matches.end(), + [](const auto& lhs, const auto& rhs) { + const bool lhs_valid = lhs.filament_index >= 0; + const bool rhs_valid = rhs.filament_index >= 0; + + if (lhs_valid != rhs_valid) + return lhs_valid; + if (!lhs_valid) + return false; + + return lhs.filament_index < rhs.filament_index; + }); +} + +// Preserve the row order the user is currently looking at across a +// re-computation (e.g. when auto-merge is toggled). We key on cluster_index +// because it survives compact_used_virtual_filaments() and filament-index +// renumbering, whereas filament_index does not. +// +// Behaviour: +// * Entries whose cluster_index appeared in `previous_matches` keep their +// previous relative order. +// * Entries whose cluster_index is new (not in `previous_matches`) are +// appended at the end, in their current relative order. +// +// Assumption: each cluster_index appears at most once in both vectors. This +// is currently guaranteed by do_auto_match(), which emits exactly one match +// per cluster. If that invariant ever changes, the std::map::emplace below +// silently keeps only the first occurrence and the order will be wrong. +void TextureImportDialog::restore_current_match_order(const std::vector& previous_matches) +{ + if (previous_matches.empty() || m_current_matches.size() < 2) + return; + + std::map previous_order_by_cluster; + for (size_t i = 0; i < previous_matches.size(); ++i) { + if (previous_matches[i].cluster_index >= 0) + previous_order_by_cluster.emplace(previous_matches[i].cluster_index, i); + } + + std::stable_sort(m_current_matches.begin(), m_current_matches.end(), + [&previous_order_by_cluster](const auto& lhs, const auto& rhs) { + const auto lhs_it = previous_order_by_cluster.find(lhs.cluster_index); + const auto rhs_it = previous_order_by_cluster.find(rhs.cluster_index); + const bool lhs_known = lhs_it != previous_order_by_cluster.end(); + const bool rhs_known = rhs_it != previous_order_by_cluster.end(); + + if (lhs_known != rhs_known) + return lhs_known; + if (!lhs_known) + return false; + + return lhs_it->second < rhs_it->second; + }); +} + +size_t TextureImportDialog::max_filament_count() const +{ + return static_cast(EnforcerBlockerType::ExtruderMax); +} + +bool TextureImportDialog::can_add_virtual_filament() const +{ + return m_filament_colors_rgba.size() < max_filament_count(); +} + +int TextureImportDialog::find_closest_filament_index(const std::array& color) const +{ + int best_idx = -1; + double best_delta = std::numeric_limits::max(); + const size_t filament_count = std::min(m_filament_colors_rgba.size(), max_filament_count()); + for (size_t i = 0; i < filament_count; ++i) { + const double delta = Slic3r::compute_delta_e(color, m_filament_colors_rgba[i]); + if (delta < best_delta) { + best_delta = delta; + best_idx = (int)i; + } + } + return best_idx; +} + +int TextureImportDialog::add_virtual_filament(const std::array& rgba, const std::string& hex, + const std::string& preset_name) +{ + if (m_filament_color_strs.size() != m_filament_colors_rgba.size() || + m_filament_names.size() != m_filament_colors_rgba.size() || + m_filament_entries.size() != m_filament_colors_rgba.size()) { + return -1; + } + if (!can_add_virtual_filament()) { + // Mark that this do_auto_match() run hit the filament cap and had to + // drop at least one cluster. The mapping itself still falls back via + // find_closest_filament_index() below; this flag only drives the + // inline orange warning above the bottom buttons. + // Note: only the false -> true transition happens here; the flag is + // cleared exclusively at the entry of do_auto_match() so it always + // reflects the most recent match, never an accumulated history. + m_filaments_dropped = true; + return -1; + } + + const int new_idx = (int)m_filament_colors_rgba.size(); + m_filament_colors_rgba.push_back(rgba); + m_filament_color_strs.push_back(hex); + m_filament_names.push_back(DEFAULT_VIRTUAL_FILAMENT_NAME); + TextureFilamentEntry entry; + entry.kind = TextureFilamentKind::NewPhysical; + entry.dialog_index = new_idx; + entry.project_config_index = size_t(-1); + entry.color_hex = texture_normalize_color_hex(hex); + entry.name = DEFAULT_VIRTUAL_FILAMENT_NAME; + entry.preset_name = preset_name.empty() ? m_default_virtual_filament_preset_name : preset_name; + m_filament_entries.push_back(entry); + m_new_filament_colors.push_back(rgba); + m_new_filament_preset_names.push_back(preset_name.empty() ? m_default_virtual_filament_preset_name : preset_name); + return new_idx; +} + +int TextureImportDialog::add_virtual_mixed_filament(const std::string& color_hex, + const std::vector& component_dialog_indices, + const std::vector& ratios) +{ + if (m_filament_color_strs.size() != m_filament_colors_rgba.size() || + m_filament_names.size() != m_filament_colors_rgba.size() || + m_filament_entries.size() != m_filament_colors_rgba.size()) { + return -1; + } + if (component_dialog_indices.size() < 2 || component_dialog_indices.size() != ratios.size()) + return -1; + for (int idx : component_dialog_indices) { + if (idx < 0 || idx >= (int)m_filament_entries.size() || + !texture_entry_is_physical(m_filament_entries[idx].kind)) { + return -1; + } + } + if (!can_add_virtual_filament()) { + m_filaments_dropped = true; + return -1; + } + + const int new_idx = (int)m_filament_colors_rgba.size(); + TextureFilamentEntry entry; + entry.kind = TextureFilamentKind::NewMixed; + entry.dialog_index = new_idx; + entry.project_config_index = size_t(-1); + entry.color_hex = texture_normalize_color_hex(color_hex); + entry.name = DEFAULT_VIRTUAL_FILAMENT_NAME; + entry.mixed_ratios = ratios; + for (int idx : component_dialog_indices) + entry.mixed_components.push_back((unsigned int)(idx + 1)); + + TextureNewMixedFilament mixed; + mixed.dialog_index = entry.dialog_index; + mixed.color_hex = entry.color_hex; + mixed.component_dialog_indices = component_dialog_indices; + mixed.ratios = ratios; + + m_filament_entries.push_back(entry); + m_filament_color_strs.push_back(entry.color_hex); + m_filament_names.push_back(entry.name); + m_filament_colors_rgba.push_back(parse_color_string(entry.color_hex)); + m_new_mixed_filaments.push_back(mixed); + return entry.dialog_index; +} + +void TextureImportDialog::compact_used_virtual_filaments() +{ + if (m_current_matches.empty()) + return; + + const std::vector> old_colors = m_filament_colors_rgba; + const std::vector old_color_strs = m_filament_color_strs; + const std::vector old_names = m_filament_names; + const std::vector old_entries = m_filament_entries; + + auto old_new_mixed_has_valid_components = [&old_entries, &old_colors](const TextureFilamentEntry& entry) { + if (entry.kind != TextureFilamentKind::NewMixed) + return true; + if (entry.mixed_components.size() < 2 || entry.mixed_components.size() != entry.mixed_ratios.size()) + return false; + for (unsigned int comp : entry.mixed_components) { + int comp_idx = comp >= 1 ? (int)comp - 1 : -1; + if (comp_idx < 0 || comp_idx >= (int)old_entries.size() || comp_idx >= (int)old_colors.size() || + !texture_entry_is_physical(old_entries[comp_idx].kind)) { + return false; + } + } + return true; + }; + + std::set used_virtual_indices; + for (const auto& m : m_current_matches) { + if (m.filament_index >= (int)m_existing_filament_count && + m.filament_index < (int)old_colors.size()) { + if (m.filament_index < (int)old_entries.size() && + old_entries[m.filament_index].kind == TextureFilamentKind::NewMixed && + !old_new_mixed_has_valid_components(old_entries[m.filament_index])) { + continue; + } + used_virtual_indices.insert(m.filament_index); + } + } + bool added_dependency = true; + while (added_dependency) { + added_dependency = false; + std::vector current_used(used_virtual_indices.begin(), used_virtual_indices.end()); + for (int used_idx : current_used) { + if (used_idx < 0 || used_idx >= (int)old_entries.size() || + old_entries[used_idx].kind != TextureFilamentKind::NewMixed || + !old_new_mixed_has_valid_components(old_entries[used_idx])) + continue; + for (unsigned int comp : old_entries[used_idx].mixed_components) { + int comp_idx = comp >= 1 ? (int)comp - 1 : -1; + if (comp_idx >= (int)m_existing_filament_count && comp_idx < (int)old_entries.size() && + used_virtual_indices.insert(comp_idx).second) { + added_dependency = true; + } + } + } + } + + std::vector> compact_colors; + std::vector compact_color_strs; + std::vector compact_names; + std::vector compact_entries; + compact_colors.reserve(m_existing_filament_count + used_virtual_indices.size()); + compact_color_strs.reserve(m_existing_filament_count + used_virtual_indices.size()); + compact_names.reserve(m_existing_filament_count + used_virtual_indices.size()); + compact_entries.reserve(m_existing_filament_count + used_virtual_indices.size()); + + const size_t existing_count = std::min(m_existing_filament_count, old_colors.size()); + for (size_t i = 0; i < existing_count; ++i) { + compact_colors.push_back(old_colors[i]); + compact_color_strs.push_back(i < old_color_strs.size() ? old_color_strs[i] : ""); + compact_names.push_back(i < old_names.size() ? old_names[i] : "Filament " + std::to_string(i + 1)); + TextureFilamentEntry entry = i < old_entries.size() ? old_entries[i] : TextureFilamentEntry{}; + entry.dialog_index = (int)i; + entry.color_hex = texture_normalize_color_hex(compact_color_strs.back()); + entry.name = compact_names.back(); + compact_entries.push_back(entry); + } + + std::map old_to_new; + std::vector> compact_new_colors; + std::vector compact_new_preset_names; + compact_new_colors.reserve(used_virtual_indices.size()); + compact_new_preset_names.reserve(used_virtual_indices.size()); + + for (int old_idx : used_virtual_indices) { + old_to_new[old_idx] = (int)compact_colors.size(); + compact_colors.push_back(old_colors[old_idx]); + compact_color_strs.push_back(old_idx < (int)old_color_strs.size() ? old_color_strs[old_idx] : ""); + compact_names.push_back(old_idx < (int)old_names.size() ? old_names[old_idx] : DEFAULT_VIRTUAL_FILAMENT_NAME); + TextureFilamentEntry entry = old_idx < (int)old_entries.size() ? old_entries[old_idx] : TextureFilamentEntry{}; + entry.dialog_index = (int)compact_entries.size(); + entry.color_hex = texture_normalize_color_hex(compact_color_strs.back()); + entry.name = compact_names.back(); + if (entry.kind == TextureFilamentKind::NewPhysical) { + compact_new_colors.push_back(old_colors[old_idx]); + compact_new_preset_names.push_back(entry.preset_name.empty() ? m_default_virtual_filament_preset_name : entry.preset_name); + } + compact_entries.push_back(entry); + } + + m_filament_colors_rgba = std::move(compact_colors); + m_filament_color_strs = std::move(compact_color_strs); + m_filament_names = std::move(compact_names); + m_filament_entries = std::move(compact_entries); + m_new_filament_colors = std::move(compact_new_colors); + m_new_filament_preset_names = std::move(compact_new_preset_names); + m_new_mixed_filaments.clear(); + std::set invalid_compacted_mixed_indices; + for (auto& entry : m_filament_entries) { + if (entry.kind != TextureFilamentKind::NewMixed) + continue; + TextureNewMixedFilament mixed; + mixed.dialog_index = entry.dialog_index; + mixed.color_hex = entry.color_hex; + mixed.ratios = entry.mixed_ratios; + mixed.component_dialog_indices.reserve(entry.mixed_components.size()); + bool valid_components = entry.mixed_components.size() >= 2 && + entry.mixed_components.size() == entry.mixed_ratios.size(); + for (unsigned int comp : entry.mixed_components) { + int old_comp_idx = comp >= 1 ? (int)comp - 1 : -1; + if (old_comp_idx < 0) { + valid_components = false; + break; + } + auto remap_it = old_to_new.find(old_comp_idx); + int new_comp_idx = remap_it != old_to_new.end() ? remap_it->second : old_comp_idx; + if (new_comp_idx < 0 || new_comp_idx >= (int)m_filament_entries.size() || + !texture_entry_is_physical(m_filament_entries[new_comp_idx].kind)) { + valid_components = false; + break; + } + mixed.component_dialog_indices.push_back(new_comp_idx); + } + if (!valid_components) { + invalid_compacted_mixed_indices.insert(entry.dialog_index); + continue; + } + entry.mixed_components.clear(); + for (int comp_idx : mixed.component_dialog_indices) + entry.mixed_components.push_back((unsigned int)(comp_idx + 1)); + m_new_mixed_filaments.push_back(mixed); + } + + auto find_closest_physical_filament_index = [this](const std::array& color) { + int best_idx = -1; + double best_delta = std::numeric_limits::max(); + const size_t filament_count = std::min(m_filament_colors_rgba.size(), max_filament_count()); + for (size_t i = 0; i < filament_count && i < m_filament_entries.size(); ++i) { + if (!texture_entry_is_physical(m_filament_entries[i].kind)) + continue; + const double delta = Slic3r::compute_delta_e(color, m_filament_colors_rgba[i]); + if (delta < best_delta) { + best_delta = delta; + best_idx = (int)i; + } + } + return best_idx; + }; + + for (auto& m : m_current_matches) { + auto it = old_to_new.find(m.filament_index); + if (it != old_to_new.end()) { + m.filament_index = it->second; + } else if (m.filament_index >= (int)m_existing_filament_count) { + m.filament_index = find_closest_filament_index(m.cluster_color); + } + if (invalid_compacted_mixed_indices.count(m.filament_index) > 0) { + int fallback_idx = find_closest_physical_filament_index(m.cluster_color); + m.filament_index = fallback_idx >= 0 ? fallback_idx : find_closest_filament_index(m.cluster_color); + } + + if (m.filament_index >= 0 && m.filament_index < (int)m_filament_colors_rgba.size()) { + m.filament_color = m_filament_colors_rgba[m.filament_index]; + m.delta_e = Slic3r::compute_delta_e(m.cluster_color, m.filament_color); + if (m.filament_index >= (int)m_existing_filament_count) + m.delta_e = 0.0; + } + } +} + +void TextureImportDialog::dismiss_filament_popup() +{ + if (!m_filament_popup) { + m_filament_popup_row = -1; + return; + } + + FilamentSelectPopup* popup = m_filament_popup; + m_filament_popup = nullptr; + m_filament_popup_row = -1; + if (popup->IsShown()) + popup->Dismiss(); + else + popup->Destroy(); +} + +void TextureImportDialog::show_auto_mix_popup() +{ + if (!m_btn_auto_mix || !m_btn_auto_mix->IsEnabled()) + return; + + if (m_auto_mix_popup && m_auto_mix_popup->IsShown()) + return; + dismiss_auto_mix_popup(); + + auto on_select = [this](TextureAutoMixMode mode) { + set_auto_mix_mode(mode); + }; + auto on_close = [this]() { + m_auto_mix_popup = nullptr; + }; + + auto* popup = new AutoMixSelectPopup(this, m_auto_mix_mode, m_btn_auto_mix->GetSize().x, + m_auto_mix_font_point_size, + on_select, on_close); + wxPoint pos = m_btn_auto_mix->ClientToScreen(wxPoint(0, m_btn_auto_mix->GetSize().y)); + wxRect display_rect; + int display_idx = wxDisplay::GetFromPoint(pos); + if (display_idx != wxNOT_FOUND) + display_rect = wxDisplay(display_idx).GetClientArea(); + else + display_rect = wxDisplay().GetClientArea(); + pos.x = std::clamp(pos.x, display_rect.GetLeft(), + std::max(display_rect.GetLeft(), display_rect.GetRight() - popup->GetSize().x)); + popup->Position(pos, wxSize(0, 0)); + popup->Bind(wxEVT_DESTROY, [this, popup](wxWindowDestroyEvent& e) { + e.Skip(); + if (m_auto_mix_popup == popup) + m_auto_mix_popup = nullptr; + }); + m_auto_mix_popup = popup; + popup->Popup(); +} + +void TextureImportDialog::dismiss_auto_mix_popup() +{ + if (!m_auto_mix_popup) + return; + + AutoMixSelectPopup* popup = m_auto_mix_popup; + m_auto_mix_popup = nullptr; + if (popup->IsShown()) + popup->Dismiss(); + else + popup->Destroy(); +} + +void TextureImportDialog::set_auto_mix_mode(TextureAutoMixMode mode) +{ + m_auto_mix_mode = mode; + if (m_btn_auto_mix) { + m_btn_auto_mix->SetLabel(auto_mix_mode_label(mode)); + m_btn_auto_mix->Refresh(); + } + apply_auto_standard_mix(mode); +} + +void TextureImportDialog::apply_auto_standard_mix(TextureAutoMixMode mode) +{ + if (m_mapping_rows.empty()) + return; + m_filaments_dropped = false; + + auto find_or_add_base_physical = [this](const std::string& color_hex) -> int { + const std::string normalized = texture_normalize_color_hex(color_hex); + for (const auto& entry : m_filament_entries) { + if (!texture_entry_is_physical(entry.kind)) + continue; + if (texture_normalize_color_hex(entry.color_hex) != normalized) + continue; + if (texture_entry_is_pla_basic(entry)) + return entry.dialog_index; + } + + std::array rgba = parse_color_string(normalized); + int idx = add_virtual_filament(rgba, normalized, m_default_virtual_filament_preset_name); + if (idx >= 0 && idx < (int)m_filament_entries.size()) { + m_filament_entries[idx].type = DEFAULT_VIRTUAL_FILAMENT_BASIC_TYPE; + m_filament_entries[idx].name = DEFAULT_VIRTUAL_FILAMENT_NAME; + } + return idx; + }; + + auto find_existing_mixed = [this](const std::vector& component_indices, const std::vector& ratios) -> int { + for (const auto& entry : m_filament_entries) { + if (!texture_entry_is_mixed(entry.kind) || entry.mixed_components.size() != component_indices.size() || + entry.mixed_ratios.size() != ratios.size()) + continue; + bool same = true; + for (size_t i = 0; i < component_indices.size(); ++i) { + if (entry.mixed_components[i] != (unsigned int)(component_indices[i] + 1) || + entry.mixed_ratios[i] != ratios[i]) { + same = false; + break; + } + } + if (same) + return entry.dialog_index; + } + return -1; + }; + + bool changed = false; + const auto recipe_mode = texture_recipe_mode(mode); + for (size_t row_index = 0; row_index < m_mapping_rows.size(); ++row_index) { + Slic3r::ColorDecomposeRgb target_rgb; + if (!Slic3r::color_decompose_hex_to_rgb(m_mapping_rows[row_index].source_hex, target_rgb)) + continue; + + auto recipe = Slic3r::lookup_standard_recipe(target_rgb, recipe_mode, DEFAULT_VIRTUAL_FILAMENT_BASIC_TYPE); + if (!recipe.valid || recipe.components.size() < 2) + continue; + + std::vector component_dialog_indices; + std::vector ratios; + for (const auto& comp : recipe.components) { + int component_idx = find_or_add_base_physical(comp.color_hex); + if (component_idx < 0) { + component_dialog_indices.clear(); + break; + } + component_dialog_indices.push_back(component_idx); + ratios.push_back(comp.ratio); + } + if (component_dialog_indices.size() < 2 || component_dialog_indices.size() != ratios.size()) + continue; + + int mixed_idx = find_existing_mixed(component_dialog_indices, ratios); + if (mixed_idx < 0) + mixed_idx = add_virtual_mixed_filament(recipe.matched_color_hex, component_dialog_indices, ratios); + if (mixed_idx < 0) + continue; + + m_mapping_rows[row_index].target_filament_idx = mixed_idx; + if (row_index < m_current_matches.size()) { + m_current_matches[row_index].filament_index = mixed_idx; + m_current_matches[row_index].filament_color = m_filament_colors_rgba[mixed_idx]; + m_current_matches[row_index].delta_e = Slic3r::compute_delta_e( + m_current_matches[row_index].cluster_color, m_current_matches[row_index].filament_color); + if (mixed_idx >= (int)m_existing_filament_count) + m_current_matches[row_index].delta_e = 0.0; + } + changed = true; + } + + if (!changed) + return; + + m_auto_mix_applied = true; + compact_used_virtual_filaments(); + update_filament_color_map(); + rebuild_mapping_rows(); + update_drop_warning_visibility(); + update_auto_mix_reset_visibility(); +} + +void TextureImportDialog::reset_auto_mix() +{ + if (m_state != TextureImportState::Ready || !m_auto_mix_applied) + return; + + dismiss_auto_mix_popup(); + + // Clear mixed filament references so the compact inside do_auto_match() + // removes them (and their exclusively-owned base physicals) from the + // filament arrays, giving the baseline matching a clean starting state. + for (auto& m : m_current_matches) { + if (m.filament_index >= 0 && m.filament_index < (int)m_filament_entries.size() && + texture_entry_is_mixed(m_filament_entries[m.filament_index].kind)) { + m.filament_index = -1; + } + } + + // Re-run the baseline auto-match (same flow as the auto-merge toggle) so the + // mapping reverts to the pre-mix state: every colour matches an existing + // physical filament or a virtual physical filament, with no mixed filaments. + const auto previous_matches = m_current_matches; + do_auto_match(); + restore_current_match_order(previous_matches); + compact_used_virtual_filaments(); + update_filament_color_map(); + rebuild_mapping_rows(); + update_drop_warning_visibility(); + update_auto_mix_reset_visibility(); +} + +void TextureImportDialog::update_auto_mix_reset_visibility() +{ + if (!m_btn_mix_reset) + return; + if (m_btn_mix_reset->Show(m_auto_mix_applied)) { + if (wxWindow* parent = m_btn_mix_reset->GetParent()) + parent->Layout(); + } +} + +bool TextureImportDialog::add_decomposed_mixed_filament(size_t row_index) +{ + if (row_index >= m_mapping_rows.size()) + return false; + + std::vector physical_colors; + std::vector physical_names; + std::vector physical_types; + std::vector physical_dialog_indices; + std::vector physical_config_indices; + auto& preset_bundle = *wxGetApp().preset_bundle; + for (const auto& entry : m_filament_entries) { + if (entry.kind != TextureFilamentKind::ExistingPhysical) + continue; + physical_colors.push_back(entry.color_hex); + physical_names.push_back(entry.name); + const size_t cfg_idx = entry.project_config_index; + Preset* preset = nullptr; + if (cfg_idx < preset_bundle.filament_presets.size()) + preset = preset_bundle.filaments.find_preset(preset_bundle.filament_presets[cfg_idx]); + physical_types.push_back(filament_type_for_color_decompose(preset)); + physical_dialog_indices.push_back(entry.dialog_index); + physical_config_indices.push_back(cfg_idx); + } + if (physical_colors.empty()) + return false; + + wxColour target(m_mapping_rows[row_index].source_hex); + ColorDecomposeDialog dlg(this, -1, target, physical_colors, physical_names, physical_types, + m_filament_entries.size(), max_filament_count(), + std::move(physical_config_indices)); + // Count "new physical filaments" with the exact reuse rule of the write-back + // loop below: a base color is only new if no existing OR virtual official + // Bambu Basic filament already carries that color. This keeps the dialog's + // filament-limit pre-check consistent with what add_decomposed_mixed_filament + // will actually create, so already-present virtual base colors are not + // double counted (which previously could wrongly disable OK). + dlg.set_missing_physical_calculator([this](const ColorDecomposeResult& result) -> size_t { + size_t missing = 0; + for (const DecomposeComponent& comp : result.components) { + if (comp.filament_index > 0) + continue; // reuses a physical slot passed to the dialog, no new filament + const std::string comp_hex = texture_normalize_color_hex( + comp.colour.GetAsString(wxC2S_HTML_SYNTAX).ToStdString()); + bool found = false; + for (const auto& entry : m_filament_entries) { + if (!texture_entry_is_physical(entry.kind)) + continue; + if (texture_normalize_color_hex(entry.color_hex) != comp_hex) + continue; + if (texture_entry_official_basic(entry)) { + found = true; + break; + } + } + if (!found) + ++missing; + } + return missing; + }); + if (dlg.ShowModal() != wxID_OK) + return false; + + ColorDecomposeResult result = dlg.get_result(); + std::vector component_dialog_indices; + std::vector ratios; + for (const DecomposeComponent& comp : result.components) { + ratios.push_back(comp.ratio); + if (comp.filament_index > 0) { + const size_t physical_idx = (size_t)(comp.filament_index - 1); + if (physical_idx >= physical_dialog_indices.size()) + return false; + component_dialog_indices.push_back(physical_dialog_indices[physical_idx]); + continue; + } + + const std::string comp_hex = texture_normalize_color_hex(comp.colour.GetAsString(wxC2S_HTML_SYNTAX).ToStdString()); + int existing_idx = -1; + for (const auto& entry : m_filament_entries) { + if (!texture_entry_is_physical(entry.kind)) + continue; + if (texture_normalize_color_hex(entry.color_hex) != comp_hex) + continue; + if (texture_entry_official_basic(entry)) { + existing_idx = entry.dialog_index; + break; + } + } + if (existing_idx < 0) { + std::array rgba = parse_color_string(comp_hex); + existing_idx = add_virtual_filament(rgba, comp_hex); + if (existing_idx < 0) + return false; + } + component_dialog_indices.push_back(existing_idx); + } + + if (component_dialog_indices.size() < 2 || component_dialog_indices.size() != ratios.size()) + return false; + + const std::string mixed_hex = texture_normalize_color_hex( + result.matched_color.GetAsString(wxC2S_HTML_SYNTAX).ToStdString()); + int mixed_idx = add_virtual_mixed_filament(mixed_hex, component_dialog_indices, ratios); + if (mixed_idx < 0) + return false; + + m_mapping_rows[row_index].target_filament_idx = mixed_idx; + if (row_index < m_current_matches.size()) + m_current_matches[row_index].filament_index = mixed_idx; + rebuild_mapping_rows(); + update_filament_color_map(); + return true; +} + +void TextureImportDialog::dismiss_filament_popup_on_wheel(wxMouseEvent& evt) +{ + dismiss_filament_popup(); + dismiss_auto_mix_popup(); + evt.Skip(); +} + +void TextureImportDialog::show_filament_popup(size_t row_index) +{ + if (row_index >= m_mapping_rows.size()) return; + + if (m_skip_next_filament_popup_row == (int)row_index) { + m_skip_next_filament_popup_row = -1; + return; + } + + if (m_filament_popup && m_filament_popup->IsShown()) { + if (m_filament_popup_row == (int)row_index) { + dismiss_filament_popup(); + return; + } + dismiss_filament_popup(); + } + + auto on_select = [this, row_index](int idx) { + if (row_index >= m_mapping_rows.size()) return; + m_mapping_rows[row_index].target_filament_idx = idx; + if (row_index < m_current_matches.size()) + m_current_matches[row_index].filament_index = idx; + if (m_mapping_rows[row_index].target_panel) { + wxString label = (idx >= 0 && idx < (int)m_filament_names.size()) + ? filament_name_to_wx_string(m_filament_names[idx]) + : wxString::Format("Filament %d", idx + 1); + m_mapping_rows[row_index].target_panel->SetToolTip(label); + m_mapping_rows[row_index].target_panel->Refresh(); + } + update_filament_color_map(); + }; + + auto on_add_filament = [this, row_index](wxColour clr) { + std::array rgba = {clr.Red() / 255.f, clr.Green() / 255.f, + clr.Blue() / 255.f, 1.0f}; + std::string hex = wxString::Format("#%02X%02X%02X", + clr.Red(), clr.Green(), clr.Blue()).ToStdString(); + int new_idx = add_virtual_filament(rgba, hex); + if (new_idx < 0) + return; + + if (row_index < m_mapping_rows.size()) { + m_mapping_rows[row_index].target_filament_idx = new_idx; + if (row_index < m_current_matches.size()) + m_current_matches[row_index].filament_index = new_idx; + } + rebuild_mapping_rows(); + update_filament_color_map(); + }; + + auto on_decompose_color = [this, row_index]() { + CallAfter([this, row_index]() { + add_decomposed_mixed_filament(row_index); + }); + }; + + wxPanel* tp = m_mapping_rows[row_index].target_panel; + if (!tp) return; + + auto on_close = [this, row_index](bool closed_by_action) { + if (m_filament_popup_row == (int)row_index) { + m_filament_popup = nullptr; + m_filament_popup_row = -1; + } + if (!closed_by_action) { + m_skip_next_filament_popup_row = (int)row_index; + CallAfter([this, row_index]() { + if (m_skip_next_filament_popup_row == (int)row_index) + m_skip_next_filament_popup_row = -1; + }); + } + }; + + auto* popup = new FilamentSelectPopup( + this, m_filament_entries, m_filament_colors_rgba, m_filament_names, + m_existing_filament_count, tp->GetSize().x, tp, on_select, on_add_filament, + on_decompose_color, + [this]() { return can_add_virtual_filament(); }, + on_close); + + wxPoint pos = tp->ClientToScreen(wxPoint(0, tp->GetSize().y)); + wxRect display_rect; + int display_idx = wxDisplay::GetFromPoint(pos); + if (display_idx != wxNOT_FOUND) + display_rect = wxDisplay(display_idx).GetClientArea(); + else + display_rect = wxDisplay().GetClientArea(); + pos.x = std::clamp(pos.x, display_rect.GetLeft(), + std::max(display_rect.GetLeft(), display_rect.GetRight() - popup->GetSize().x)); + popup->Position(pos, wxSize(0, 0)); + popup->Bind(wxEVT_DESTROY, [this, popup](wxWindowDestroyEvent& e) { + e.Skip(); + if (m_filament_popup == popup) { + m_filament_popup = nullptr; + m_filament_popup_row = -1; + } + }); + m_filament_popup = popup; + m_filament_popup_row = (int)row_index; + popup->Popup(); +} + +void TextureImportDialog::do_auto_match() +{ + if (m_painted.cluster_colors.empty()) return; + + // do_auto_match() always rebuilds the baseline mapping without any mixed + // filaments, so it is the common entry for every "revert one-click mix" + // path. Clear the applied flag here; callers refresh the reset button. + m_auto_mix_applied = false; + + // Reset the "filaments were dropped" flag at the start of every run, so it + // strictly reflects what happens during *this* match (no historical + // accumulation). add_virtual_filament() will flip it back to true if and + // only if it hits the global filament cap below. + m_filaments_dropped = false; + + // Drop any virtual filaments left over from previous match runs that the + // current m_current_matches no longer references. Without this, the + // residual virtual filaments inflate m_filament_colors_rgba.size() at the + // entry of this match, which can make add_virtual_filament() fail (and + // wrongly flip m_filaments_dropped to true) even when the *real* count + // of needed virtual filaments for this run is well below the global cap. + // This is purely a state cleanup; it does not change any mapping rule. + compact_used_virtual_filaments(); + + const auto previous_matches = m_current_matches; + + std::map, int> previous_virtual_by_cluster; + for (const auto& match : previous_matches) { + if (match.filament_index >= (int)m_existing_filament_count && + match.filament_index < (int)m_filament_entries.size() && + texture_entry_is_physical(m_filament_entries[match.filament_index].kind)) { + previous_virtual_by_cluster[match.cluster_color] = match.filament_index; + } + } + + auto find_virtual_filament_by_color = [this](const std::array& color) -> int { + std::string hex = rgb_to_hex(color).ToStdString(); + for (size_t i = m_existing_filament_count; i < m_filament_color_strs.size(); ++i) { + if (m_filament_color_strs[i] == hex && + i < m_filament_entries.size() && texture_entry_is_physical(m_filament_entries[i].kind)) + return (int)i; + } + return -1; + }; + + auto get_or_add_virtual_filament = [this, &previous_virtual_by_cluster, &find_virtual_filament_by_color]( + const std::array& color) -> int { + auto previous_it = previous_virtual_by_cluster.find(color); + if (previous_it != previous_virtual_by_cluster.end() && + previous_it->second >= (int)m_existing_filament_count && + previous_it->second < (int)m_filament_colors_rgba.size()) { + return previous_it->second; + } + + int existing_idx = find_virtual_filament_by_color(color); + if (existing_idx >= 0) + return existing_idx; + + std::array rgba = { + color[0] / 255.f, + color[1] / 255.f, + color[2] / 255.f, + 1.f + }; + return add_virtual_filament(rgba, rgb_to_hex(color).ToStdString()); + }; + + if (m_auto_merge_cb && m_auto_merge_cb->GetValue()) { + // Match clusters to closest existing filaments + std::vector names; + for (size_t i = 0; i < m_existing_filament_count; ++i) + names.push_back(m_filament_names.size() > i ? m_filament_names[i] : "Filament " + std::to_string(i + 1)); + + std::vector> existing_filament_colors( + m_filament_colors_rgba.begin(), + m_filament_colors_rgba.begin() + std::min(m_existing_filament_count, m_filament_colors_rgba.size())); + + m_current_matches = Slic3r::match_clusters_to_filaments( + m_painted.cluster_colors, existing_filament_colors, names); + + // For clusters with poor match (CIEDE2000 ΔE > 5), create virtual filaments. + constexpr double NEW_FILAMENT_THRESHOLD = 5.0; + std::map, int> virtual_color_index; + + for (auto& m : m_current_matches) { + if (m.delta_e <= NEW_FILAMENT_THRESHOLD) + continue; + + auto it = virtual_color_index.find(m.cluster_color); + if (it != virtual_color_index.end()) { + m.filament_index = it->second; + } else { + int new_idx = get_or_add_virtual_filament(m.cluster_color); + if (new_idx >= 0) + virtual_color_index[m.cluster_color] = new_idx; + m.filament_index = new_idx >= 0 ? new_idx : find_closest_filament_index(m.cluster_color); + } + if (m.filament_index >= 0 && m.filament_index < (int)m_filament_colors_rgba.size()) { + m.filament_color = m_filament_colors_rgba[m.filament_index]; + m.delta_e = Slic3r::compute_delta_e(m.cluster_color, m.filament_color); + if (m.filament_index >= (int)m_existing_filament_count) + m.delta_e = 0.0; + } + } + } else { + // Keep all virtual filaments in this dialog; unused ones are pruned only on OK. + m_current_matches.clear(); + std::map, int> virtual_map; + + for (size_t i = 0; i < m_painted.cluster_colors.size(); ++i) { + const auto& cc = m_painted.cluster_colors[i]; + Slic3r::FilamentMatch fm; + fm.cluster_index = (int)i; + fm.cluster_color = cc; + + auto it = virtual_map.find(cc); + if (it != virtual_map.end()) { + fm.filament_index = it->second; + } else { + int idx = get_or_add_virtual_filament(cc); + if (idx >= 0) + virtual_map[cc] = idx; + fm.filament_index = idx >= 0 ? idx : find_closest_filament_index(cc); + } + if (fm.filament_index >= 0 && fm.filament_index < (int)m_filament_colors_rgba.size()) { + fm.filament_color = m_filament_colors_rgba[fm.filament_index]; + fm.delta_e = Slic3r::compute_delta_e(fm.cluster_color, fm.filament_color); + if (fm.filament_index >= (int)m_existing_filament_count) + fm.delta_e = 0.0; + } + m_current_matches.push_back(fm); + } + } + + update_filament_color_map(); +} + +void TextureImportDialog::rebuild_mapping_rows() +{ + m_mapping_scroll->Freeze(); + m_mapping_sizer->Clear(true); + m_mapping_rows.clear(); + + if (m_current_matches.empty()) { + m_mapping_scroll->FitInside(); + m_mapping_scroll->Thaw(); + return; + } + + auto get_target_wxcolor = [this](int idx) -> wxColour { + if (idx >= 0 && idx < (int)m_filament_colors_rgba.size()) { + const auto& c = m_filament_colors_rgba[idx]; + return wxColour((unsigned char)(c[0] * 255.f), + (unsigned char)(c[1] * 255.f), + (unsigned char)(c[2] * 255.f)); + } + return wxColour(128, 128, 128); + }; + + auto get_filament_label = [this](int idx) -> wxString { + if (idx >= 0 && idx < (int)m_filament_names.size()) + return filament_name_to_wx_string(m_filament_names[idx]); + return wxString::Format("Filament %d", idx + 1); + }; + + const wxColour dash_clr = dark_or(wxColour(179, 179, 179), wxColour(100, 100, 106)); + const wxColour hex_fg = texture_import_text_colour(); + const wxColour card_bg = dark_or(wxColour(235, 235, 235), wxColour(0x3C, 0x3C, 0x42)); + const wxColour card_bd = dark_or(wxColour(224, 224, 224), wxColour(0x46, 0x46, 0x4C)); + const wxColour name_fg = texture_import_text_colour(); + const wxColour chev_clr = dark_or(wxColour(107, 107, 107), wxColour(0xB3, 0xB3, 0xB5)); + + m_mapping_rows.resize(m_current_matches.size()); + for (size_t ci = 0; ci < m_current_matches.size(); ++ci) { + auto& row = m_mapping_rows[ci]; + row.cluster_id = m_current_matches[ci].cluster_index; + row.source_color = m_current_matches[ci].cluster_color; + row.source_hex = rgb_to_hex(row.source_color).ToStdString(); + row.target_filament_idx = m_current_matches[ci].filament_index; + + wxColour src_wx_color( + (unsigned char)row.source_color[0], + (unsigned char)row.source_color[1], + (unsigned char)row.source_color[2]); + + // --- Row container --- + wxPanel* row_panel = new wxPanel(m_mapping_scroll, wxID_ANY); + row_panel->SetBackgroundColour(m_mapping_scroll->GetBackgroundColour()); + row_panel->Bind(wxEVT_MOUSEWHEEL, &TextureImportDialog::dismiss_filament_popup_on_wheel, this); + wxBoxSizer* row_sizer = new wxBoxSizer(wxHORIZONTAL); + + // --- Source card (dashed border, circle + hex) --- + const int src_w = FromDIP(138); + const int target_min_w = FromDIP(239); + const int row_h = FromDIP(44); + row.source_panel = new wxPanel(row_panel, wxID_ANY, wxDefaultPosition, wxSize(src_w, row_h), + wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE); + row.source_panel->SetMinSize(wxSize(src_w, row_h)); + row.source_panel->SetMaxSize(wxSize(src_w, row_h)); + row.source_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + + row.source_panel->Bind(wxEVT_PAINT, [this, ci, src_wx_color, dash_clr, hex_fg](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + + dc.SetBrush(wxBrush(p->GetParent()->GetBackgroundColour())); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + + wxPen dash_pen(dash_clr, 1, wxPENSTYLE_SHORT_DASH); + dc.SetPen(dash_pen); + dc.SetBrush(wxBrush(p->GetParent()->GetBackgroundColour())); + int r = p->FromDIP(8); + dc.DrawRoundedRectangle(0, 0, sz.x, sz.y, r); + + // Color circle 24px + int cd = p->FromDIP(24); + int cx = p->FromDIP(10); + int cy = (sz.y - cd) / 2; + dc.SetPen(*wxTRANSPARENT_PEN); + dc.SetBrush(wxBrush(src_wx_color)); + dc.DrawEllipse(cx, cy, cd, cd); + draw_filament_swatch_ellipse_border(dc, src_wx_color, cx, cy, cd, cd); + + if (ci < m_mapping_rows.size()) { + wxFont hex_font = p->GetFont(); + hex_font.SetPointSize(9); + dc.SetFont(hex_font); + dc.SetTextForeground(hex_fg); + wxString hex_str = wxString::Format("# %s", m_mapping_rows[ci].source_hex.substr(1)); + wxSize tsz = dc.GetTextExtent(hex_str); + dc.DrawText(hex_str, cx + cd + p->FromDIP(6), (sz.y - tsz.y) / 2); + } + }); + row.source_panel->Bind(wxEVT_SIZE, [](wxSizeEvent& e) { + e.Skip(); + static_cast(e.GetEventObject())->Refresh(); + }); + row.source_panel->Bind(wxEVT_MOUSEWHEEL, &TextureImportDialog::dismiss_filament_popup_on_wheel, this); + + row_sizer->Add(row.source_panel, 0, wxEXPAND); + + // --- Arrow panel (dashed arrow) --- + const int arrow_w = FromDIP(24); + wxPanel* arrow_panel = new wxPanel(row_panel, wxID_ANY, wxDefaultPosition, wxSize(arrow_w, row_h)); + arrow_panel->SetMinSize(wxSize(arrow_w, row_h)); + arrow_panel->SetMaxSize(wxSize(arrow_w, row_h)); + arrow_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + arrow_panel->Bind(wxEVT_MOUSEWHEEL, &TextureImportDialog::dismiss_filament_popup_on_wheel, this); + arrow_panel->Bind(wxEVT_PAINT, [dash_clr](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + + dc.SetBrush(wxBrush(p->GetParent()->GetBackgroundColour())); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + + int mid_y = sz.y / 2; + int margin = p->FromDIP(2); + int arrow_tip = sz.x - margin; + int arrow_start = margin; + + wxPen dash_pen(dash_clr, p->FromDIP(1), wxPENSTYLE_SHORT_DASH); + dc.SetPen(dash_pen); + dc.DrawLine(arrow_start, mid_y, arrow_tip - p->FromDIP(4), mid_y); + + int ah = p->FromDIP(4); + wxPoint tri[3] = { + {arrow_tip, mid_y}, + {arrow_tip - ah, mid_y - ah / 2}, + {arrow_tip - ah, mid_y + ah / 2} + }; + dc.SetPen(*wxTRANSPARENT_PEN); + dc.SetBrush(wxBrush(dash_clr)); + dc.DrawPolygon(3, tri); + }); + + row_sizer->Add(arrow_panel, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, FromDIP(4)); + + // --- Target card (numbered square + material name + chevron) --- + row.target_panel = new wxPanel(row_panel, wxID_ANY, wxDefaultPosition, wxSize(-1, row_h), + wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE); + row.target_panel->SetMinSize(wxSize(target_min_w, row_h)); + row.target_panel->SetToolTip(get_filament_label(row.target_filament_idx)); + row.target_panel->SetBackgroundStyle(wxBG_STYLE_PAINT); + row.target_panel->SetCursor(wxCursor(wxCURSOR_HAND)); + + row.target_panel->Bind(wxEVT_PAINT, [this, ci, get_target_wxcolor, get_filament_label, + card_bg, card_bd, name_fg, chev_clr](wxPaintEvent& e) { + auto* p = static_cast(e.GetEventObject()); + wxAutoBufferedPaintDC dc(p); + wxSize sz = p->GetClientSize(); + + dc.SetBrush(wxBrush(p->GetParent()->GetBackgroundColour())); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.DrawRectangle(0, 0, sz.x, sz.y); + + if (ci >= m_mapping_rows.size()) return; + int fil_idx = m_mapping_rows[ci].target_filament_idx; + + int r = p->FromDIP(8); + dc.SetBrush(wxBrush(card_bg)); + dc.SetPen(wxPen(card_bd, 1)); + dc.DrawRoundedRectangle(0, 0, sz.x, sz.y, r); + + if (fil_idx >= 0 && fil_idx < (int)m_filament_entries.size() && + texture_entry_is_mixed(m_filament_entries[fil_idx].kind)) { + const TextureFilamentEntry& entry = m_filament_entries[fil_idx]; + wxFont mixed_font = p->GetFont(); + mixed_font.SetPointSize(10); + dc.SetFont(mixed_font); + + int x = p->FromDIP(10); + const int sw = p->FromDIP(28); + const int sw_r = p->FromDIP(6); + const int sw_y = (sz.y - sw) / 2; + for (size_t mi = 0; mi < entry.mixed_components.size() && mi < entry.mixed_ratios.size(); ++mi) { + if (mi > 0) { + dc.SetTextForeground(name_fg); + wxString plus = "+"; + wxSize psz = dc.GetTextExtent(plus); + dc.DrawText(plus, x, (sz.y - psz.y) / 2); + x += psz.x + p->FromDIP(4); + } + + const unsigned int comp_id = entry.mixed_components[mi]; + const int comp_idx = comp_id >= 1 ? (int)comp_id - 1 : -1; + wxColour comp_clr("#D9D9D9"); + if (comp_idx >= 0 && comp_idx < (int)m_filament_colors_rgba.size()) { + const auto& c = m_filament_colors_rgba[comp_idx]; + comp_clr = wxColour((unsigned char)(c[0] * 255.f), + (unsigned char)(c[1] * 255.f), + (unsigned char)(c[2] * 255.f)); + } + + dc.SetPen(*wxTRANSPARENT_PEN); + dc.SetBrush(wxBrush(comp_clr)); + dc.DrawRoundedRectangle(x, sw_y, sw, sw, sw_r); + draw_filament_swatch_border(dc, comp_clr, x, sw_y, sw, sw, sw_r); + + wxString num_str = wxString::Format("%u", comp_id); + wxSize nsz = dc.GetTextExtent(num_str); + dc.SetTextForeground(comp_clr.GetLuminance() < 0.6 ? *wxWHITE : texture_import_gray9000()); + dc.DrawText(num_str, x + (sw - nsz.x) / 2, sw_y + (sw - nsz.y) / 2); + x += sw + p->FromDIP(5); + + dc.SetTextForeground(name_fg); + wxString pct = wxString::Format("%d%%", entry.mixed_ratios[mi]); + wxSize pct_sz = dc.GetTextExtent(pct); + dc.DrawText(pct, x, (sz.y - pct_sz.y) / 2); + x += pct_sz.x + p->FromDIP(5); + if (x > sz.x - p->FromDIP(34)) + break; + } + + int chev_cx = sz.x - p->FromDIP(14); + int chev_cy = sz.y / 2; + int hw = p->FromDIP(3); + int hh = p->FromDIP(2); + dc.SetPen(wxPen(chev_clr, p->FromDIP(1) > 0 ? p->FromDIP(1) : 1)); + dc.DrawLine(chev_cx - hw, chev_cy - hh, chev_cx, chev_cy + hh); + dc.DrawLine(chev_cx, chev_cy + hh, chev_cx + hw, chev_cy - hh); + return; + } + + // Numbered color square 32x32, rounded 6px + int sq = p->FromDIP(32); + int sq_x = p->FromDIP(6); + int sq_y = (sz.y - sq) / 2; + int sq_r = p->FromDIP(6); + wxColour fil_clr = get_target_wxcolor(fil_idx); + dc.SetPen(*wxTRANSPARENT_PEN); + dc.SetBrush(wxBrush(fil_clr)); + dc.DrawRoundedRectangle(sq_x, sq_y, sq, sq, sq_r); + draw_filament_swatch_border(dc, fil_clr, sq_x, sq_y, sq, sq, sq_r); + + { + wxFont num_font = p->GetFont(); + num_font.SetPointSize(10); + dc.SetFont(num_font); + dc.SetTextForeground(fil_clr.GetLuminance() < 0.6 ? *wxWHITE : texture_import_gray9000()); + wxString num_str = wxString::Format("%d", fil_idx + 1); + wxSize nsz = dc.GetTextExtent(num_str); + dc.DrawText(num_str, sq_x + (sq - nsz.x) / 2, sq_y + (sq - nsz.y) / 2); + } + + // Brand icon + material name + { + wxFont name_font = p->GetFont(); + name_font.SetPointSize(9); + dc.SetFont(name_font); + dc.SetTextForeground(name_fg); + wxString name_str = get_filament_label(fil_idx); + int text_x = draw_brand_icon_and_strip(dc, p, name_str, sq_x + sq + p->FromDIP(8), sz.y / 2); + int max_text_w = sz.x - text_x - p->FromDIP(24); + if (max_text_w > 0) { + name_str = ellipsize_text(dc, name_str, max_text_w); + wxSize tsz = dc.GetTextExtent(name_str); + dc.DrawText(name_str, text_x, (sz.y - tsz.y) / 2); + } + } + + // Dropdown chevron at right edge + { + int chev_cx = sz.x - p->FromDIP(14); + int chev_cy = sz.y / 2; + int hw = p->FromDIP(3); + int hh = p->FromDIP(2); + dc.SetPen(wxPen(chev_clr, p->FromDIP(1) > 0 ? p->FromDIP(1) : 1)); + dc.DrawLine(chev_cx - hw, chev_cy - hh, chev_cx, chev_cy + hh); + dc.DrawLine(chev_cx, chev_cy + hh, chev_cx + hw, chev_cy - hh); + } + }); + + row.target_panel->Bind(wxEVT_LEFT_DOWN, [this, ci](wxMouseEvent&) { + show_filament_popup(ci); + }); + row.target_panel->Bind(wxEVT_SIZE, [](wxSizeEvent& e) { + e.Skip(); + static_cast(e.GetEventObject())->Refresh(); + }); + row.target_panel->Bind(wxEVT_MOUSEWHEEL, &TextureImportDialog::dismiss_filament_popup_on_wheel, this); + + row_sizer->Add(row.target_panel, 1, wxEXPAND); + + row_panel->SetSizer(row_sizer); + m_mapping_sizer->Add(row_panel, 0, wxEXPAND | wxBOTTOM, FromDIP(12)); + } + + m_mapping_scroll->FitInside(); + m_mapping_scroll->Layout(); + m_mapping_scroll->Thaw(); +} + +std::vector TextureImportDialog::build_matches_from_rows() const +{ + std::vector matches(m_mapping_rows.size()); + for (size_t i = 0; i < m_mapping_rows.size(); ++i) { + auto& m = matches[i]; + m.cluster_index = m_mapping_rows[i].cluster_id; + m.cluster_color = m_mapping_rows[i].source_color; + + int sel = m_mapping_rows[i].target_filament_idx; + if (sel >= 0 && sel < (int)m_filament_colors_rgba.size()) { + m.filament_index = sel; + m.filament_color = m_filament_colors_rgba[sel]; + m.delta_e = Slic3r::compute_delta_e(m.cluster_color, m.filament_color); + } + } + return matches; +} + +// ---- Event handlers ---- + +void TextureImportDialog::update_color_count_preset_buttons() +{ + if (m_btn_color_4) m_btn_color_4->SetValue(m_param_color_count == 4); + if (m_btn_color_8) m_btn_color_8->SetValue(m_param_color_count == 8); + if (m_btn_color_16) m_btn_color_16->SetValue(m_param_color_count == 16); +} + +void TextureImportDialog::set_color_count_value(int value, bool update_spin) +{ + m_param_color_count = std::clamp(value, 1, (int)max_filament_count()); + m_color_slider->SetValue(m_param_color_count); + if (update_spin) + m_color_spin->SetValue(m_param_color_count); + update_color_count_preset_buttons(); + update_confirm_button_state(); +} + +void TextureImportDialog::set_smooth_value(int value, bool update_spin) +{ + m_param_smooth = std::clamp(value, 0, 10); + m_smooth_slider->SetValue(m_param_smooth); + if (update_spin) + m_smooth_spin->SetValue(m_param_smooth); + update_confirm_button_state(); +} + +void TextureImportDialog::preview_spin_text_value(SpinInput* spin, GreenSlider* slider, int& param, + int min_value, int max_value, const wxString& text, + std::function on_value_changed) +{ + long value; + if (!text.ToLong(&value)) + return; + + wxTextCtrl* tc = spin->GetTextCtrl(); + long parsed = value; + value = std::clamp((int)parsed, min_value, max_value); + + wxString normalized = text; + if (parsed > max_value || (text.length() > 1 && text[0] == '0')) + normalized = wxString::Format("%ld", value); + + if (normalized != text) { + long pos = tc->GetInsertionPoint(); + tc->ChangeValue(normalized); + if (parsed > max_value) + tc->SetInsertionPointEnd(); + else + tc->SetInsertionPoint(std::min(normalized.length(), std::max(0L, pos - 1))); + } + + param = (int)value; + slider->SetValue(param); + if (on_value_changed) + on_value_changed(); + update_confirm_button_state(); +} + +void TextureImportDialog::on_color_preset_clicked(wxCommandEvent& evt) +{ + int id = evt.GetId(); + int color_count = m_param_color_count; + if (id == ID_COLOR_4) { color_count = 4; } + if (id == ID_COLOR_8) { color_count = 8; } + if (id == ID_COLOR_16) { color_count = 16; } + + if (id == ID_COLOR_AUTO) { + start_computation(true); + return; + } + + set_color_count_value(color_count, true); +} + +void TextureImportDialog::on_color_slider_changed(wxCommandEvent&) +{ + set_color_count_value(m_color_slider->GetValue(), true); +} + +void TextureImportDialog::on_color_spin_changed(wxCommandEvent&) +{ + set_color_count_value(m_color_spin->GetValue(), true); +} + +void TextureImportDialog::on_color_spin_text_changed(wxCommandEvent& evt) +{ + preview_spin_text_value(m_color_spin, m_color_slider, m_param_color_count, + 1, (int)max_filament_count(), evt.GetString(), + [this]() { update_color_count_preset_buttons(); }); +} + +void TextureImportDialog::on_smooth_slider_changed(wxCommandEvent&) +{ + set_smooth_value(m_smooth_slider->GetValue(), true); +} + +void TextureImportDialog::on_smooth_spin_changed(wxCommandEvent&) +{ + set_smooth_value(m_smooth_spin->GetValue(), true); +} + +void TextureImportDialog::on_smooth_spin_text_changed(wxCommandEvent& evt) +{ + preview_spin_text_value(m_smooth_spin, m_smooth_slider, m_param_smooth, + 0, 10, evt.GetString()); +} + +void TextureImportDialog::on_apply_clicked(wxCommandEvent&) +{ + start_computation(); +} + +void TextureImportDialog::on_auto_merge_toggled(wxCommandEvent&) +{ + bool auto_merge_enabled = !m_auto_merge_cb || m_auto_merge_cb->GetValue(); + m_auto_merge_enabled = auto_merge_enabled; + + if (m_state == TextureImportState::Ready) { + const auto previous_matches = m_current_matches; + do_auto_match(); + restore_current_match_order(previous_matches); + compact_used_virtual_filaments(); + update_filament_color_map(); + rebuild_mapping_rows(); + update_drop_warning_visibility(); + update_auto_mix_reset_visibility(); + } +} + +void TextureImportDialog::highlight_view_button(int view_index) +{ + Button* btns[] = { m_btn_view_original, m_btn_view_multicolor }; + + StateColor active_bg( + std::pair(wxColour(27, 136, 68), StateColor::Pressed), + std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor active_bd( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor active_text( + std::pair(wxColour(255, 255, 255), StateColor::Normal)); + + StateColor inactive_bg( + std::pair(dark_or(wxColour(245, 245, 245), wxColour(0x5C, 0x5C, 0x64)), StateColor::Pressed), + std::pair(dark_or(wxColour(255, 255, 255), wxColour(0x66, 0x66, 0x6E)), StateColor::Hovered), + std::pair(dark_or(wxColour(255, 255, 255), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor inactive_bd( + std::pair(dark_or(wxColour(255, 255, 255), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor inactive_text( + std::pair(dark_or(wxColour(104, 104, 104), wxColour(0xD0, 0xD0, 0xD2)), StateColor::Normal)); + + for (int i = 0; i < 2; ++i) { + if (!btns[i]) continue; + if (i == view_index) { + btns[i]->SetBackgroundColor(active_bg); + btns[i]->SetBorderColor(active_bd); + btns[i]->SetTextColor(active_text); + } else { + btns[i]->SetBackgroundColor(inactive_bg); + btns[i]->SetBorderColor(inactive_bd); + btns[i]->SetTextColor(inactive_text); + } + btns[i]->Refresh(); + } +} + +void TextureImportDialog::on_skip_clicked(wxCommandEvent&) +{ + m_skipped = true; + m_new_filament_colors.clear(); + m_new_filament_preset_names.clear(); + m_new_mixed_filaments.clear(); + m_current_matches.clear(); + cancel_computation(); + EndModal(wxID_CANCEL); +} + +bool TextureImportDialog::has_valid_result() const +{ + if (m_painted.face_colors.empty() || m_current_matches.empty() || m_mapping_rows.empty()) + return false; + + if (m_mapping_rows.size() != m_current_matches.size()) + return false; + + const int filament_count = (int)std::min(m_filament_colors_rgba.size(), max_filament_count()); + for (const auto& row : m_mapping_rows) { + if (row.target_filament_idx < 0 || row.target_filament_idx >= filament_count) + return false; + } + return true; +} + +bool TextureImportDialog::is_params_dirty() const +{ + if (m_applied_color_count < 0) + return false; + return m_param_color_count != m_applied_color_count + || m_param_smooth != m_applied_smooth; +} + +void TextureImportDialog::update_drop_warning_visibility() +{ + if (!m_drop_warning_label) return; + // Show only when the most recent do_auto_match() ran into the filament + // cap AND we are in the Ready state. The flag is reset at every + // do_auto_match() entry, so any "clean" re-run automatically hides the + // warning even if a previous run had dropped clusters. + const bool show = (m_state == TextureImportState::Ready) && m_filaments_dropped; + if (m_drop_warning_label->IsShown() == show) return; + m_drop_warning_label->Show(show); + Layout(); +} + +void TextureImportDialog::update_confirm_button_state() +{ + if (m_state != TextureImportState::Ready) + return; + + if (!has_valid_result()) { + m_btn_ok->Enable(false); + if (m_hint_label) m_hint_label->Hide(); + m_btn_ok->Refresh(); + Layout(); + return; + } + + bool dirty = is_params_dirty(); + + m_btn_ok->Enable(true); + + if (dirty) { + StateColor gray_bg( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor gray_bd( + std::pair(dark_or(wxColour(206, 206, 206), wxColour(0x54, 0x54, 0x5B)), StateColor::Normal)); + StateColor gray_text( + std::pair(dark_or(wxColour(107, 107, 107), wxColour(0xB3, 0xB3, 0xB5)), StateColor::Normal)); + m_btn_ok->SetBackgroundColor(gray_bg); + m_btn_ok->SetBorderColor(gray_bd); + m_btn_ok->SetTextColor(gray_text); + m_btn_ok->SetToolTip(_L("Reminder: parameters changed, click Apply to take effect")); + if (m_hint_label) m_hint_label->Show(); + } else { + StateColor ok_bg( + std::pair(wxColour(27, 136, 68), StateColor::Pressed), + std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor ok_bd( + std::pair(wxColour(0, 174, 66), StateColor::Normal)); + StateColor ok_text( + std::pair(wxColour(255, 255, 255), StateColor::Normal)); + m_btn_ok->SetBackgroundColor(ok_bg); + m_btn_ok->SetBorderColor(ok_bd); + m_btn_ok->SetTextColor(ok_text); + m_btn_ok->UnsetToolTip(); + if (m_hint_label) m_hint_label->Hide(); + } + + m_btn_ok->Refresh(); + Layout(); +} + +void TextureImportDialog::on_ok_clicked(wxCommandEvent&) +{ + if (m_state != TextureImportState::Ready || !has_valid_result() || is_params_dirty()) + return; + + m_current_matches = build_matches_from_rows(); + if (m_current_matches.empty()) + return; + + compact_used_virtual_filaments(); + + EndModal(wxID_OK); +} + +// ---- Result accessors ---- + +Slic3r::PaintedMesh TextureImportDialog::get_painted_mesh() const +{ + return m_painted; +} + +std::vector TextureImportDialog::get_matches() const +{ + if (!m_current_matches.empty()) + return m_current_matches; + return build_matches_from_rows(); +} + +void TextureImportDialog::on_dpi_changed(const wxRect&) +{ + // All control sizes below are baked into persistent properties (min size, + // corner radius, fixed wxSize) using FromDIP() at build time. The base + // DPIAware::rescale() only rescales fonts; it does not recompute these + // stored pixel values. Re-apply them here so the layout stays consistent + // when the dialog is dragged to a screen with a different DPI. + SetMinSize(wxSize(FromDIP(800), FromDIP(500))); + + const int view_button_height = FromDIP(27); + for (Button* btn : {m_btn_view_original, m_btn_view_multicolor}) { + if (btn) { + btn->SetCornerRadius(view_button_height / 2); + btn->SetMinSize(wxSize(FromDIP(57), view_button_height)); + } + } + + for (Button* btn : {m_btn_color_4, m_btn_color_8, m_btn_color_16}) { + if (btn) { + btn->SetCornerRadius(FromDIP(12)); + btn->SetMinSize(wxSize(FromDIP(28), FromDIP(28))); + } + } + + if (m_btn_color_auto) { + m_btn_color_auto->SetCornerRadius(FromDIP(12)); + m_btn_color_auto->SetMinSize(wxSize(FromDIP(60), FromDIP(28))); + } + if (m_btn_apply) { + m_btn_apply->SetCornerRadius(FromDIP(12)); + m_btn_apply->SetMinSize(wxSize(FromDIP(60), FromDIP(28))); + } + if (m_btn_auto_mix) { + m_btn_auto_mix->SetCornerRadius(FromDIP(14)); + m_btn_auto_mix->SetMinSize(wxSize(FromDIP(178), FromDIP(28))); + } + if (m_btn_mix_reset) + m_btn_mix_reset->SetPaddingSize(wxSize(FromDIP(2), FromDIP(2))); + + if (m_color_spin) + m_color_spin->SetMinSize(wxSize(FromDIP(60), FromDIP(28))); + if (m_smooth_spin) + m_smooth_spin->SetMinSize(wxSize(FromDIP(60), FromDIP(28))); + + if (m_mapping_scroll) { + m_mapping_scroll->SetMinSize(wxSize(-1, FromDIP(300))); + m_mapping_scroll->SetScrollRate(0, FromDIP(10)); + } + + if (m_btn_skip) { + m_btn_skip->SetCornerRadius(FromDIP(20)); + m_btn_skip->SetMinSize(wxSize(FromDIP(136), FromDIP(40))); + } + if (m_btn_ok) { + m_btn_ok->SetCornerRadius(FromDIP(20)); + m_btn_ok->SetMinSize(wxSize(FromDIP(156), FromDIP(40))); + } + + // Mapping rows store their panel sizes (source/target/arrow/row height) + // as fixed FromDIP min/max sizes, so rebuild them to pick up the new DPI. + rebuild_mapping_rows(); + + if (wxSizer* sizer = GetSizer()) + sizer->Layout(); + Layout(); + Refresh(); + wxGetApp().UpdateDlgDarkUI(this); +} + +}} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/TextureImportDialog.hpp b/src/slic3r/GUI/TextureImportDialog.hpp new file mode 100644 index 0000000000..63e30dfc5b --- /dev/null +++ b/src/slic3r/GUI/TextureImportDialog.hpp @@ -0,0 +1,398 @@ +#pragma once + +#include "GUI_Utils.hpp" +#include "Widgets/ProgressDialog.hpp" +#include "libslic3r/TexturePainting.hpp" + +#include +#include +#include "Widgets/PopupWindow.hpp" +#include +#include +#include +#include "Widgets/SpinInput.hpp" +#include +#include +#include "Widgets/Button.hpp" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class GreenSlider; + +namespace Slic3r { namespace GUI { + +wxDECLARE_EVENT(EVT_TEXTURE_COMPUTE_DONE, wxCommandEvent); +wxDECLARE_EVENT(EVT_TEXTURE_COMPUTE_PROGRESS, wxCommandEvent); +wxDECLARE_EVENT(EVT_TEXTURE_COMPUTE_ERROR, wxCommandEvent); +wxDECLARE_EVENT(EVT_TEXTURE_MESH_REPAIR_DECISION, wxCommandEvent); + +enum class TextureImportState { + Idle, + Computing, + Ready, + Error +}; + +enum class TextureAutoMixMode { + CMYW, + RYBW +}; + +enum class TextureFilamentKind { + ExistingPhysical, + ExistingMixed, + NewPhysical, + NewMixed +}; + +struct TextureFilamentEntry { + TextureFilamentKind kind{TextureFilamentKind::ExistingPhysical}; + int dialog_index{-1}; + size_t project_config_index{size_t(-1)}; + std::string color_hex; + std::string name; + std::string type; + std::string preset_name; + std::vector mixed_components; + std::vector mixed_ratios; +}; + +struct TextureNewMixedFilament { + int dialog_index{-1}; + std::string color_hex; + std::vector component_dialog_indices; + std::vector ratios; +}; + +struct FilamentMappingRow { + int cluster_id = -1; + std::array source_color = {0, 0, 0}; + std::string source_hex; + int target_filament_idx = 0; + wxPanel* source_panel = nullptr; + wxPanel* target_panel = nullptr; +}; + +class FilamentSelectPopup; +class AutoMixSelectPopup; +// Lightweight 3D preview panel using wxGLCanvas. +// Renders: original textured, multi-color, or filament-mapped. +class TexturePreviewCanvas : public wxGLCanvas +{ +public: + enum class RenderMode { Original, MultiColor, FilamentMap }; + + TexturePreviewCanvas(wxWindow* parent, const wxGLAttributes& attrs); + ~TexturePreviewCanvas(); + + void set_mesh_data( + const std::vector>& vertices, + const std::vector>& indices); + + void set_texture_data( + const std::vector>& uvs, + const unsigned char* tex_data, int tex_w, int tex_h, int tex_channels); + + void set_texture_render_data( + const std::vector>& tex_pixels_rgb, + const std::vector& tex_widths, + const std::vector& tex_heights, + const std::vector, 3>>& face_uvs, + const std::vector& face_tex_ids); + + void set_painted_mesh_data( + const std::vector>& vertices, + const std::vector>& indices); + void set_face_colors(const std::vector>& face_colors); + void set_original_face_colors(const std::vector>& face_colors); + void set_filament_color_map(const std::map, std::array>& color_map); + + void set_render_mode(RenderMode mode); + RenderMode get_render_mode() const { return m_mode; } + void set_computing_overlay(bool show); + void reset_view(); + +private: + void on_paint(wxPaintEvent& evt); + void on_size(wxSizeEvent& evt); + void on_mouse(wxMouseEvent& evt); + void ensure_gl_ready(); + void render(); + void render_mesh(); + void render_textured_original(); + void render_reset_overlay(const wxSize& logical_size, const wxSize& viewport_size); + void upload_reset_icon_textures(); + unsigned int upload_reset_icon_texture(const std::string& icon_name); + wxRect reset_overlay_rect() const; + bool handle_reset_overlay_mouse(wxMouseEvent& evt); + void upload_textures(); + void compute_smooth_normals(); + void update_bounding_box(); + + wxGLContext* m_context = nullptr; + bool m_gl_initialized = false; + RenderMode m_mode = RenderMode::Original; + + float m_zoom = 1.0f; + float m_rot_x = -30.0f; + float m_rot_y = 30.0f; + float m_pan_x = 0.0f; + float m_pan_y = 0.0f; + wxPoint m_last_mouse_pos; + enum class DragMode { None, Rotate, Pan }; + DragMode m_drag_mode = DragMode::None; + + std::vector> m_vertices; + std::vector> m_indices; + std::vector> m_uvs; + std::vector> m_painted_vertices; + std::vector> m_painted_indices; + std::vector> m_face_colors_rgb; + std::vector> m_original_face_colors_rgb; + std::vector> m_filament_colors_rgb; + std::map, std::array> m_color_map; + + unsigned int m_tex_id = 0; + int m_tex_w = 0; + int m_tex_h = 0; + int m_tex_channels = 3; + bool m_tex_dirty = false; + std::vector m_tex_data; + + std::vector m_gl_tex_ids; + std::vector> m_tex_pixels_rgb; + std::vector m_tex_widths; + std::vector m_tex_heights; + std::vector, 3>> m_face_uvs; + std::vector m_face_tex_ids; + bool m_multi_tex_dirty = false; + + std::vector> m_vertex_normals; + + std::array m_center = {0, 0, 0}; + float m_radius = 1.0f; + + unsigned int m_reset_icon_tex = 0; + unsigned int m_reset_icon_hover_tex = 0; + unsigned int m_reset_icon_dark_tex = 0; + unsigned int m_reset_icon_dark_hover_tex = 0; + bool m_reset_overlay_hovered = false; + bool m_reset_overlay_pressed = false; +}; + + +class TextureImportDialog : public DPIDialog +{ +public: + TextureImportDialog(wxWindow* parent, + const Slic3r::TexturedMesh& textured_mesh, + const std::vector& filament_entries, + std::function initial_cancel_callback = {}, + std::function initial_progress_callback = {}); + ~TextureImportDialog(); + + int ShowModal() override; + void on_dpi_changed(const wxRect& suggested_rect) override; + + Slic3r::PaintedMesh get_painted_mesh() const; + std::vector get_matches() const; + bool was_skipped() const { return m_skipped; } + bool fallback_to_geometry_only() const { return m_fallback_to_geometry_only; } + // Colors of virtual filaments that need to be created after dialog confirmation. + // Index i corresponds to filament index (m_existing_filament_count + i). + const std::vector>& get_new_filament_colors() const { return m_new_filament_colors; } + const std::vector& get_new_filament_preset_names() const { return m_new_filament_preset_names; } + const std::vector& get_new_mixed_filaments() const { return m_new_mixed_filaments; } + const std::vector& get_filament_entries() const { return m_filament_entries; } + size_t get_existing_filament_count() const { return m_existing_filament_count; } + +private: + void build_ui(); + void build_preview_panel(wxWindow* parent, wxSizer* sizer); + void build_params_panel(wxWindow* parent, wxSizer* sizer); + void build_mapping_panel(wxWindow* parent, wxSizer* sizer); + void build_bottom_buttons(wxSizer* sizer); + + void set_state(TextureImportState new_state); + void update_ui_for_state(); + + void start_computation(bool auto_color = false, bool initial = false); + void cancel_computation(); + void on_computation_complete(wxCommandEvent& evt); + void on_computation_progress(wxCommandEvent& evt); + void on_computation_error(wxCommandEvent& evt); + void on_mesh_repair_decision_required(wxCommandEvent& evt); + + void rebuild_mapping_rows(); + void do_auto_match(); + // Reorder m_current_matches into a canonical, predictable order (ascending + // filament_index, with unmapped entries pushed to the end). Used right + // after the initial computation so the first view the user sees has a + // stable, intuitive layout. + void sort_current_matches_by_filament_index(); + // Reorder m_current_matches so they appear in the same order as + // `previous_matches` (keyed by cluster_index). Entries whose cluster_index + // was not present before are appended at the end, preserving their current + // relative order. Used when the user toggles auto-merge so the rows do not + // visually jump around. Assumes each cluster_index appears at most once in + // both vectors (this invariant is currently guaranteed by do_auto_match, + // which produces one match per cluster). + void restore_current_match_order(const std::vector& previous_matches); + std::vector build_matches_from_rows() const; + void update_filament_color_map(); + void show_filament_popup(size_t row_index); + void dismiss_filament_popup(); + void dismiss_filament_popup_on_wheel(wxMouseEvent& evt); + void show_auto_mix_popup(); + void dismiss_auto_mix_popup(); + void set_auto_mix_mode(TextureAutoMixMode mode); + void apply_auto_standard_mix(TextureAutoMixMode mode); + void reset_auto_mix(); + void update_auto_mix_reset_visibility(); + bool add_decomposed_mixed_filament(size_t row_index); + int add_virtual_filament(const std::array& rgba, const std::string& hex, + const std::string& preset_name = std::string()); + int add_virtual_mixed_filament(const std::string& color_hex, + const std::vector& component_dialog_indices, + const std::vector& ratios); + size_t max_filament_count() const; + bool can_add_virtual_filament() const; + // Recomputes m_drop_warning_label visibility from m_filaments_dropped and + // m_state. Safe to call whether or not the label has been created yet. + // Visibility reflects ONLY the result of the most recent do_auto_match(): + // if the latest match did not drop any cluster, the label is hidden even + // if a previous match had dropped (no historical accumulation). + void update_drop_warning_visibility(); + void compact_used_virtual_filaments(); + int find_closest_filament_index(const std::array& color) const; + + void on_color_preset_clicked(wxCommandEvent& evt); + void on_color_slider_changed(wxCommandEvent& evt); + void on_color_spin_changed(wxCommandEvent& evt); + void on_color_spin_text_changed(wxCommandEvent& evt); + void on_smooth_slider_changed(wxCommandEvent& evt); + void on_smooth_spin_changed(wxCommandEvent& evt); + void on_smooth_spin_text_changed(wxCommandEvent& evt); + void on_apply_clicked(wxCommandEvent& evt); + void on_auto_merge_toggled(wxCommandEvent& evt); + void highlight_view_button(int view_index); + void on_skip_clicked(wxCommandEvent& evt); + void on_ok_clicked(wxCommandEvent& evt); + + void set_color_count_value(int value, bool update_spin); + void set_smooth_value(int value, bool update_spin); + void preview_spin_text_value(SpinInput* spin, GreenSlider* slider, int& param, + int min_value, int max_value, const wxString& text, + std::function on_value_changed = {}); + void update_color_count_preset_buttons(); + + bool has_valid_result() const; + bool is_params_dirty() const; + void update_confirm_button_state(); + + Slic3r::TexturedMesh m_textured_mesh; + std::vector m_filament_color_strs; // existing + virtual + std::vector m_filament_names; // existing + virtual + std::vector> m_filament_colors_rgba; // existing + virtual + std::vector m_filament_entries; // aligned with m_filament_colors_rgba + size_t m_existing_filament_count = 0; + std::vector> m_new_filament_colors; // only virtual (to be created) + std::vector m_new_filament_preset_names; // only virtual, aligned with m_new_filament_colors + std::vector m_new_mixed_filaments; + std::string m_default_virtual_filament_preset_name; + + TextureImportState m_state = TextureImportState::Idle; + bool m_skipped = false; + bool m_fallback_to_geometry_only = false; + // True iff *the most recent* do_auto_match() ran into the global filament + // limit and had to drop one or more clusters. Reset to false on every + // do_auto_match() entry so it never accumulates across runs: a run that + // does not drop anything must observe false here, regardless of whether + // previous runs dropped. Drives the inline orange warning above the + // bottom buttons; never affects the mapping itself. + bool m_filaments_dropped = false; + bool m_auto_merge_enabled = true; + TextureAutoMixMode m_auto_mix_mode = TextureAutoMixMode::CMYW; + int m_auto_mix_font_point_size = 10; + + Slic3r::PaintedMesh m_painted; + std::vector m_current_matches; + + std::unique_ptr m_worker; + std::atomic m_cancel_flag{false}; + std::mutex m_result_mutex; + Slic3r::PaintedMesh m_pending_result; + std::function m_initial_cancel_callback; + std::function m_initial_progress_callback; + bool m_current_computation_initial = false; + bool m_initial_computation_pending = false; + bool m_initial_computation_cancelled = false; + bool m_initial_computation_failed = false; + bool m_initial_tooltips_set = false; + bool m_current_computation_auto_color = false; + Slic3r::TexturePaintingSettings::MeshRepairDecision m_mesh_repair_decision = + Slic3r::TexturePaintingSettings::MeshRepairDecision::Ask; + + Button* m_btn_color_4 = nullptr; + Button* m_btn_color_8 = nullptr; + Button* m_btn_color_16 = nullptr; + Button* m_btn_color_auto = nullptr; + GreenSlider* m_color_slider = nullptr; + SpinInput* m_color_spin = nullptr; + GreenSlider* m_smooth_slider = nullptr; + SpinInput* m_smooth_spin = nullptr; + Button* m_btn_apply = nullptr; + + wxCheckBox* m_auto_merge_cb = nullptr; + Button* m_btn_auto_mix = nullptr; + Button* m_btn_mix_reset = nullptr; + bool m_auto_mix_applied = false; + AutoMixSelectPopup* m_auto_mix_popup = nullptr; + wxScrolledWindow* m_mapping_scroll = nullptr; + wxBoxSizer* m_mapping_sizer = nullptr; + std::vector m_mapping_rows; + FilamentSelectPopup* m_filament_popup = nullptr; + int m_filament_popup_row = -1; + int m_skip_next_filament_popup_row = -1; + + TexturePreviewCanvas* m_preview_canvas = nullptr; + wxPanel* m_tab_panel = nullptr; + Button* m_btn_view_original = nullptr; + Button* m_btn_view_multicolor = nullptr; + + ProgressDialog* m_progress_dlg = nullptr; + + Button* m_btn_skip = nullptr; + Button* m_btn_ok = nullptr; + wxStaticText* m_drop_warning_label = nullptr; + + int m_param_color_count = 4; + int m_param_smooth = 5; + + int m_applied_color_count = -1; + int m_applied_smooth = -1; + wxStaticText* m_hint_label = nullptr; + + static const int ID_COLOR_4 = wxID_HIGHEST + 200; + static const int ID_COLOR_8 = wxID_HIGHEST + 201; + static const int ID_COLOR_16 = wxID_HIGHEST + 202; + static const int ID_COLOR_AUTO = wxID_HIGHEST + 203; + static const int ID_BTN_APPLY = wxID_HIGHEST + 204; + static const int ID_BTN_SKIP = wxID_HIGHEST + 205; + static const int ID_VIEW_ORIGINAL = wxID_HIGHEST + 206; + static const int ID_VIEW_MULTICOLOR = wxID_HIGHEST + 207; + + wxDECLARE_EVENT_TABLE(); +}; + +}} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/Widgets/ComboBox.cpp b/src/slic3r/GUI/Widgets/ComboBox.cpp index 783e1caadf..b6f6d42450 100644 --- a/src/slic3r/GUI/Widgets/ComboBox.cpp +++ b/src/slic3r/GUI/Widgets/ComboBox.cpp @@ -87,10 +87,18 @@ void ComboBox::SetSelection(int n) return; drop.SetSelection(n); SetLabel(drop.GetValue()); - if (drop.selection >= 0 && drop.iconSize.y > 0 && items[drop.selection].icon_textctrl.IsOk()) - SetIcon(items[drop.selection].icon_textctrl); - else + if (drop.selection >= 0 && drop.iconSize.y > 0 && items[drop.selection].icon_textctrl.IsOk()) { + if (m_keep_drop_arrow) { + SetIcon("drop_down"); + SetIcon_1(items[drop.selection].icon_textctrl); + } else { + SetIcon(items[drop.selection].icon_textctrl); + } + } else { SetIcon("drop_down"); + if (m_keep_drop_arrow) + SetIcon_1(wxNullBitmap); + } if (drop.selection >= 0) { SetStaticTips(items[drop.selection].text_static_tips, wxNullBitmap); @@ -120,10 +128,18 @@ void ComboBox::SetValue(const wxString &value) { drop.SetValue(value); SetLabel(value); - if (drop.selection >= 0 && drop.iconSize.y > 0 && items[drop.selection].icon_textctrl.IsOk()) - SetIcon(items[drop.selection].icon_textctrl); - else + if (drop.selection >= 0 && drop.iconSize.y > 0 && items[drop.selection].icon_textctrl.IsOk()) { + if (m_keep_drop_arrow) { + SetIcon("drop_down"); + SetIcon_1(items[drop.selection].icon_textctrl); + } else { + SetIcon(items[drop.selection].icon_textctrl); + } + } else { SetIcon("drop_down"); + if (m_keep_drop_arrow) + SetIcon_1(wxNullBitmap); + } if (drop.selection >= 0) { SetStaticTips(items[drop.selection].text_static_tips, wxNullBitmap); diff --git a/src/slic3r/GUI/Widgets/ComboBox.hpp b/src/slic3r/GUI/Widgets/ComboBox.hpp index 552909b477..91c34d53aa 100644 --- a/src/slic3r/GUI/Widgets/ComboBox.hpp +++ b/src/slic3r/GUI/Widgets/ComboBox.hpp @@ -16,6 +16,7 @@ class ComboBox : public wxWindowWithItems bool drop_down = false; bool text_off = false; bool is_replace_text_to_image = false; + bool m_keep_drop_arrow = false; // When true, item icon goes to icon_1, keeping drop_down arrow wxString replace_text; wxString image_for_text; @@ -31,6 +32,11 @@ public: DropDown & GetDropDown() { return drop; } + // When true, item icon is shown as icon_1 (secondary), preserving drop_down arrow. + // Note: item bitmaps are set via raw wxBitmap (not ScalableBitmap), so they won't + // auto-rescale on DPI change. Caller should recreate items after DPI change. + void SetKeepDropArrow(bool keep) { m_keep_drop_arrow = keep; } + virtual bool SetFont(wxFont const & font) override; public: diff --git a/src/slic3r/GUI/Widgets/DropDown.cpp b/src/slic3r/GUI/Widgets/DropDown.cpp index 973113d0ac..a44303169a 100644 --- a/src/slic3r/GUI/Widgets/DropDown.cpp +++ b/src/slic3r/GUI/Widgets/DropDown.cpp @@ -360,6 +360,9 @@ void DropDown::render(wxDC &dc) for (int i = 0; i < items.size(); ++i) { auto &item = items[i]; int states2 = states; + // Dimmed items stay selectable but render greyed out (used by the mixed-filament + // dialog to show components that are already consumed by another mix). + bool is_dimmed = (item.style & DD_ITEM_STYLE_DIMMED) != 0; if ((item.style & DD_ITEM_STYLE_DISABLED) != 0) states2 &= ~StateColor::Enabled; // Skip by group @@ -427,7 +430,7 @@ void DropDown::render(wxDC &dc) } pt.y += (rcContent.height - textSize.y) / 2; dc.SetFont(GetFont()); - dc.SetTextForeground(text_color.colorForStates(states2)); + dc.SetTextForeground(is_dimmed ? wxColour(0xCE, 0xCE, 0xCE) : text_color.colorForStates(states2)); dc.DrawText(text, pt); if (group.IsEmpty() && !item.group_key.IsEmpty()) { auto szBmp = arrow_bitmap.GetBmpSize(); diff --git a/src/slic3r/GUI/Widgets/DropDown.hpp b/src/slic3r/GUI/Widgets/DropDown.hpp index 09041e3dc0..bcd0a58c41 100644 --- a/src/slic3r/GUI/Widgets/DropDown.hpp +++ b/src/slic3r/GUI/Widgets/DropDown.hpp @@ -13,6 +13,7 @@ #define DD_ITEM_STYLE_SPLIT_ITEM 0x0001 // ----text----, text with horizontal line arounds #define DD_ITEM_STYLE_DISABLED 0x0002 // ----text----, text with horizontal line arounds +#define DD_ITEM_STYLE_DIMMED 0x0004 // gray text, but still selectable wxDECLARE_EVENT(EVT_DISMISS, wxCommandEvent); diff --git a/src/slic3r/GUI/Widgets/SpinInput.cpp b/src/slic3r/GUI/Widgets/SpinInput.cpp index fba5a45233..538f010383 100644 --- a/src/slic3r/GUI/Widgets/SpinInput.cpp +++ b/src/slic3r/GUI/Widgets/SpinInput.cpp @@ -9,6 +9,8 @@ #include "../GUI_Utils.hpp" #endif +wxDEFINE_EVENT(EVT_SPINCTRL_TEXT, wxCommandEvent); + BEGIN_EVENT_TABLE(SpinInput, StaticBox) EVT_KEY_DOWN(SpinInput::keyPressed) @@ -74,6 +76,7 @@ void SpinInput::Create(wxWindow *parent, state_handler.attach_child(text_ctrl); text_ctrl->Bind(wxEVT_KILL_FOCUS, &SpinInput::onTextLostFocus, this); text_ctrl->Bind(wxEVT_TEXT_ENTER, &SpinInput::onTextEnter, this); + text_ctrl->Bind(wxEVT_TEXT, &SpinInput::onTextChanged, this); text_ctrl->Bind(wxEVT_KEY_DOWN, &SpinInput::keyPressed, this); text_ctrl->Bind(wxEVT_RIGHT_DOWN, [this](auto &e) {}); // disable context menu button_inc = createButton(true); @@ -300,6 +303,19 @@ void SpinInput::onTextEnter(wxCommandEvent &event) ProcessEventLocally(event); } +void SpinInput::onTextChanged(wxCommandEvent &event) +{ + long value; + if (text_ctrl->GetValue().ToLong(&value)) { + wxCommandEvent e(EVT_SPINCTRL_TEXT, GetId()); + e.SetEventObject(this); + e.SetInt((int) value); + e.SetString(text_ctrl->GetValue()); + GetEventHandler()->ProcessEvent(e); + } + event.Skip(); +} + void SpinInput::mouseWheelMoved(wxMouseEvent &event) { auto delta = event.GetWheelRotation() < 0 ? 1 : -1; diff --git a/src/slic3r/GUI/Widgets/SpinInput.hpp b/src/slic3r/GUI/Widgets/SpinInput.hpp index 275d42a95d..caf2bf3843 100644 --- a/src/slic3r/GUI/Widgets/SpinInput.hpp +++ b/src/slic3r/GUI/Widgets/SpinInput.hpp @@ -9,6 +9,10 @@ class Button; +// Fired on every keystroke that leaves a parseable integer in the field, so callers can +// react live rather than only on commit (wxEVT_SPINCTRL) or Enter. Ported from BambuStudio. +wxDECLARE_EVENT(EVT_SPINCTRL_TEXT, wxCommandEvent); + class SpinInput : public wxNavigationEnabled { wxSize labelSize; @@ -98,6 +102,7 @@ private: void keyPressed(wxKeyEvent& event); void onTimer(wxTimerEvent &evnet); void onTextLostFocus(wxEvent &event); + void onTextChanged(wxCommandEvent &event); void onTextEnter(wxCommandEvent &event); void sendSpinEvent(); diff --git a/src/slic3r/GUI/Widgets/TextInput.cpp b/src/slic3r/GUI/Widgets/TextInput.cpp index 49605e048d..b6e60f60d7 100644 --- a/src/slic3r/GUI/Widgets/TextInput.cpp +++ b/src/slic3r/GUI/Widgets/TextInput.cpp @@ -139,6 +139,15 @@ void TextInput::SetIcon_1(const wxString &icon) { Rescale(); } +// Set icon_1 from a raw bitmap. Note: won't auto-rescale on DPI change +// since ScalableBitmap::name() will be empty. Caller should re-set after DPI change. +void TextInput::SetIcon_1(const wxBitmap &icon) { + this->icon_1 = ScalableBitmap(); + if (icon.IsOk()) + this->icon_1.bmp() = icon; + Rescale(); +} + void TextInput::SetLabelColor(StateColor const &color) { label_color = color; diff --git a/src/slic3r/GUI/Widgets/TextInput.hpp b/src/slic3r/GUI/Widgets/TextInput.hpp index 9aca7037c4..44562a895d 100644 --- a/src/slic3r/GUI/Widgets/TextInput.hpp +++ b/src/slic3r/GUI/Widgets/TextInput.hpp @@ -54,6 +54,7 @@ public: void SetIcon(const wxString & icon); void SetIcon_1(const wxString &icon); + void SetIcon_1(const wxBitmap &icon); void SetLabelColor(StateColor const &color); diff --git a/src/slic3r/GUI/WipeTowerDialog.cpp b/src/slic3r/GUI/WipeTowerDialog.cpp index abf8baf086..f1d0946bf5 100644 --- a/src/slic3r/GUI/WipeTowerDialog.cpp +++ b/src/slic3r/GUI/WipeTowerDialog.cpp @@ -256,6 +256,41 @@ static std::vector MatrixFlatten(const WipingDialog::VolumeMatrix& matrix return vec; } +// Mixed-color slots are virtual: they are never loaded into a tray and so have no flushing +// volumes of their own. The dialog therefore shows only the physical filaments, which means +// converting between the full config matrix (indexed by config slot) and a dense physical +// sub-matrix (indexed by row/column in the table). +static std::vector extract_physical_sub_matrix( + const std::vector& full_matrix, size_t full_n, + const std::vector& indices) +{ + size_t p = indices.size(); + std::vector sub(p * p, 0.0); + if (full_matrix.size() < full_n * full_n) + return sub; + for (size_t pi = 0; pi < p; ++pi) + for (size_t pj = 0; pj < p; ++pj) + sub[pi * p + pj] = full_matrix[indices[pi] * full_n + indices[pj]]; + return sub; +} + +// Write the edited physical sub-matrix back into a copy of the full matrix, leaving the +// entries that belong to mixed slots untouched. +static std::vector expand_physical_to_full_matrix( + const std::vector& sub_matrix, + const std::vector& indices, size_t full_n, + const std::vector& original_matrix) +{ + std::vector full = original_matrix; + if (full.size() < full_n * full_n) + return full; + size_t p = indices.size(); + for (size_t pi = 0; pi < p; ++pi) + for (size_t pj = 0; pj < p; ++pj) + full[indices[pi] * full_n + indices[pj]] = sub_matrix[pi * p + pj]; + return full; +} + wxString WipingDialog::BuildTableObjStr() { auto full_config = wxGetApp().preset_bundle->full_config(); @@ -265,9 +300,22 @@ wxString WipingDialog::BuildTableObjStr() auto raw_matrix_data = full_config.option("flush_volumes_matrix")->values; auto nozzle_flush_dataset = full_config.option("nozzle_flush_dataset")->values; + // Restrict the table to physical filaments; mixed slots have no flushing volumes. + m_physical_indices = wxGetApp().preset_bundle->physical_filament_config_indices(); + const size_t full_n = filament_colors.size(); + { + std::vector physical_colors; + physical_colors.reserve(m_physical_indices.size()); + for (size_t i : m_physical_indices) + if (i < filament_colors.size()) + physical_colors.push_back(filament_colors[i]); + filament_colors = std::move(physical_colors); + } + std::vector> flush_matrixs; for (int idx = 0; idx < nozzle_num; ++idx) { - flush_matrixs.emplace_back(get_flush_volumes_matrix(raw_matrix_data, idx, nozzle_num)); + auto fm = get_flush_volumes_matrix(raw_matrix_data, idx, nozzle_num); + flush_matrixs.emplace_back(extract_physical_sub_matrix(fm, full_n, m_physical_indices)); } flush_multiplier.resize(nozzle_num, 1); @@ -372,7 +420,7 @@ WipingDialog::WipingDialog(wxWindow* parent, const int max_flush_volume) : wxBoxSizer* main_sizer = new wxBoxSizer(wxVERTICAL); this->SetSizer(main_sizer); this->SetBackgroundColour(*wxWHITE); - auto filament_count = wxGetApp().preset_bundle->project_config.option("filament_colour")->values.size(); + auto filament_count = wxGetApp().preset_bundle->physical_filament_config_indices().size(); // Estimate table scroll area size based on filament count // Each table cell is ~60x25 DIP, plus headers and borders @@ -592,11 +640,29 @@ void WipingDialog::StoreFlushData(int extruder_num, const std::vector WipingDialog::ExpandToFullMatrix(const std::vector& sub_matrix, int nozzle_idx) const +{ + const auto& project_config = wxGetApp().preset_bundle->project_config; + const size_t full_n = project_config.option("filament_colour")->values.size(); + if (m_physical_indices.size() == full_n) + return sub_matrix; // no mixed slots: sub-matrix already is the full matrix + + auto raw = project_config.option("flush_volumes_matrix")->values; + int nozzle_num = (int)wxGetApp().preset_bundle->project_config.option("flush_multiplier")->values.size(); + if (nozzle_num < 1) nozzle_num = 1; + auto original = get_flush_volumes_matrix(raw, nozzle_idx, nozzle_num); + return expand_physical_to_full_matrix(sub_matrix, m_physical_indices, full_n, original); +} + std::vector WipingDialog::GetFlattenMatrix()const { std::vector ret; - for (auto& matrix : m_raw_matrixs) { - ret.insert(ret.end(), matrix.begin(), matrix.end()); + for (size_t idx = 0; idx < m_raw_matrixs.size(); ++idx) { + auto full = ExpandToFullMatrix(m_raw_matrixs[idx], (int)idx); + ret.insert(ret.end(), full.begin(), full.end()); } return ret; } diff --git a/src/slic3r/GUI/WipeTowerDialog.hpp b/src/slic3r/GUI/WipeTowerDialog.hpp index 64e5758534..91944cfc78 100644 --- a/src/slic3r/GUI/WipeTowerDialog.hpp +++ b/src/slic3r/GUI/WipeTowerDialog.hpp @@ -58,12 +58,16 @@ private: wxString BuildTableObjStr(); wxString BuildTextObjStr(bool multi_language = true); void StoreFlushData(int extruder_num, const std::vector>& flush_volume_vecs, const std::vector& flush_multipliers); + // Maps the physical-only matrix shown in the table back onto the full config-indexed matrix. + std::vector ExpandToFullMatrix(const std::vector& sub_matrix, int nozzle_idx) const; wxWebView* m_webview; int m_max_flush_volume; VolumeMatrix m_raw_matrixs; std::vector m_flush_multipliers; + // Config indices of the physical (non-mixed) filaments, in table order. + std::vector m_physical_indices; bool m_submit_flag{ false }; }; diff --git a/tests/libslic3r/CMakeLists.txt b/tests/libslic3r/CMakeLists.txt index 28c39c2d6a..2d575ab989 100644 --- a/tests/libslic3r/CMakeLists.txt +++ b/tests/libslic3r/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(${_TEST_NAME}_tests test_vendor_cache.cpp test_elephant_foot_compensation.cpp test_fill_corner_smoothing.cpp + test_filament_mixer.cpp test_fill_plane_path.cpp test_geometry.cpp test_multimaterial_segmentation.cpp diff --git a/tests/libslic3r/test_filament_mixer.cpp b/tests/libslic3r/test_filament_mixer.cpp new file mode 100644 index 0000000000..7b5842334f --- /dev/null +++ b/tests/libslic3r/test_filament_mixer.cpp @@ -0,0 +1,182 @@ +#include + +#include "libslic3r/FilamentMixer.hpp" + +using namespace Slic3r; + +TEST_CASE("parse_mixed_components reads 1-based component ids", "[FilamentMixer]") +{ + REQUIRE(parse_mixed_components("1,3") == std::vector{1, 3}); + REQUIRE(parse_mixed_components("2, 4 ,5") == std::vector{2, 4, 5}); + + SECTION("Malformed input yields no components") { + REQUIRE(parse_mixed_components("").empty()); + REQUIRE(parse_mixed_components("abc").empty()); + } +} + +TEST_CASE("parse_mixed_ratios normalizes to sum 1.0", "[FilamentMixer]") +{ + auto r = parse_mixed_ratios("0.7,0.3", 2); + REQUIRE(r.size() == 2); + REQUIRE_THAT(r[0], Catch::Matchers::WithinAbs(0.7, 1e-9)); + REQUIRE_THAT(r[1], Catch::Matchers::WithinAbs(0.3, 1e-9)); + + SECTION("Unnormalized input is rescaled") { + auto v = parse_mixed_ratios("2,2", 2); + REQUIRE_THAT(v[0], Catch::Matchers::WithinAbs(0.5, 1e-9)); + REQUIRE_THAT(v[1], Catch::Matchers::WithinAbs(0.5, 1e-9)); + } + + SECTION("Empty or mismatched input falls back to equal shares") { + auto v = parse_mixed_ratios("", 3); + REQUIRE(v.size() == 3); + for (double x : v) + REQUIRE_THAT(x, Catch::Matchers::WithinAbs(1.0 / 3.0, 1e-9)); + } +} + +TEST_CASE("has_any_mixed_filament detects mixed slots", "[FilamentMixer]") +{ + REQUIRE_FALSE(has_any_mixed_filament({})); + REQUIRE_FALSE(has_any_mixed_filament({0, 0, 0})); + REQUIRE(has_any_mixed_filament({0, 1, 0})); +} + +TEST_CASE("expand_mixed_filaments replaces mixed slots with their components", "[FilamentMixer]") +{ + // Slot 2 (0-based) is a mix of physical filaments 1 and 2 (1-based) => 0 and 1 (0-based). + const std::vector is_mixed = {0, 0, 1}; + const std::vector comp_strs = {"", "", "1,2"}; + + REQUIRE(expand_mixed_filaments({2}, is_mixed, comp_strs) == std::vector{0, 1}); + + SECTION("Non-mixed entries pass through, result is sorted and deduplicated") { + REQUIRE(expand_mixed_filaments({2, 0}, is_mixed, comp_strs) == std::vector{0, 1}); + } +} + +TEST_CASE("check_mixed_filament_integrity flags dangling component references", "[FilamentMixer]") +{ + const std::vector is_mixed = {0, 0, 1}; + + SECTION("All components resolve") { + REQUIRE(check_mixed_filament_integrity(is_mixed, {"", "", "1,2"}, 2).empty()); + } + + SECTION("A component past the physical filament count is broken") { + auto broken = check_mixed_filament_integrity(is_mixed, {"", "", "1,9"}, 2); + REQUIRE(broken == std::vector{2}); + } +} + +TEST_CASE("remap_mixed_components_on_delete rewrites ids around the deleted slot", "[FilamentMixer]") +{ + const std::vector is_mixed = {0, 0, 0, 1}; + std::vector comps = {"", "", "", "1,3"}; + + SECTION("Deleting a filament below the references shifts them down") { + remap_mixed_components_on_delete(is_mixed, comps, 2); + REQUIRE(comps[3] == "1,2"); + } + + SECTION("Deleting a referenced filament zeroes that component") { + remap_mixed_components_on_delete(is_mixed, comps, 1); + // 1 -> 0 (deleted sentinel), 3 -> 2 + REQUIRE(comps[3] == "0,2"); + } +} + +TEST_CASE("check_mixed_filament_type_consistency flags mismatched component types", "[FilamentMixer]") +{ + const std::vector is_mixed = {0, 0, 1}; + const std::vector comp_strs = {"", "", "1,2"}; + + REQUIRE(check_mixed_filament_type_consistency(is_mixed, comp_strs, {"PLA", "PLA"}).empty()); + + auto bad = check_mixed_filament_type_consistency(is_mixed, comp_strs, {"PLA", "PETG"}); + REQUIRE(bad == std::vector{2}); +} + +TEST_CASE("gradient curves round-trip and sample monotonically", "[FilamentMixer]") +{ + SECTION("Empty input yields an empty curve") { + REQUIRE(parse_gradient_curve("").empty()); + REQUIRE(serialize_gradient_curve(GradientCurve{}).empty()); + } + + SECTION("Legacy 2-field anchors survive a parse/serialize round trip") { + GradientCurve c = parse_gradient_curve("0,0.15|0.5,0.5|1,0.85"); + REQUIRE(c.points.size() == 3); + + // Anchors with no tangent override serialize back to the 2-field legacy form + // (canonical fixed-precision, so compare by re-parsing rather than by string). + const std::string round_tripped = serialize_gradient_curve(c); + REQUIRE(round_tripped.find(",nan") == std::string::npos); + + GradientCurve c2 = parse_gradient_curve(round_tripped); + REQUIRE(c2.points.size() == c.points.size()); + for (size_t i = 0; i < c.points.size(); ++i) { + REQUIRE_THAT(c2.points[i].x, Catch::Matchers::WithinAbs(c.points[i].x, 1e-4)); + REQUIRE_THAT(c2.points[i].y, Catch::Matchers::WithinAbs(c.points[i].y, 1e-4)); + } + } + + SECTION("Sampling is clamped at the ends and monotone in between") { + GradientCurve c = parse_gradient_curve("0,0.15|0.5,0.5|1,0.85"); + REQUIRE_THAT(sample_gradient_curve(c, 0.0), Catch::Matchers::WithinAbs(0.15, 1e-9)); + REQUIRE_THAT(sample_gradient_curve(c, 1.0), Catch::Matchers::WithinAbs(0.85, 1e-9)); + // Outside the control point range the end values are held. + REQUIRE_THAT(sample_gradient_curve(c, -1.0), Catch::Matchers::WithinAbs(0.15, 1e-9)); + REQUIRE_THAT(sample_gradient_curve(c, 2.0), Catch::Matchers::WithinAbs(0.85, 1e-9)); + + double prev = sample_gradient_curve(c, 0.0); + for (int i = 1; i <= 20; ++i) { + double v = sample_gradient_curve(c, i / 20.0); + REQUIRE(v >= prev - 1e-9); + prev = v; + } + } + + SECTION("A curve with fewer than two points falls back to 0.5") { + GradientCurve c = parse_gradient_curve("0.5,0.7"); + REQUIRE_THAT(sample_gradient_curve(c, 0.3), Catch::Matchers::WithinAbs(0.5, 1e-9)); + } +} + +TEST_CASE("blend_color mixes two hex colors", "[FilamentMixer]") +{ + // ratio 0 keeps the first color, ratio 1 the second. + REQUIRE(blend_color("#FF0000", "#0000FF", 0.0f) == "#FF0000"); + REQUIRE(blend_color("#FF0000", "#0000FF", 1.0f) == "#0000FF"); + + SECTION("Blue and yellow make green, not grey (pigment mixing)") { + // The polynomial model approximates subtractive pigment behaviour. + std::string mixed = blend_color("#0021D0", "#FCD300", 0.5f); + REQUIRE(mixed.size() == 7); + REQUIRE(mixed[0] == '#'); + auto comp = [&](int i) { return std::stoi(mixed.substr(1 + 2 * i, 2), nullptr, 16); }; + // Green channel should dominate red and blue. + REQUIRE(comp(1) > comp(0)); + REQUIRE(comp(1) > comp(2)); + } +} + +TEST_CASE("blend_color_multi weights components", "[FilamentMixer]") +{ + SECTION("A single component is returned unchanged") { + REQUIRE(blend_color_multi({"#FF0000"}, {1}) == "#FF0000"); + } + + SECTION("Mixing a color with itself stays close to that color") { + // The mixer is a degree-4 polynomial fit of pigment behaviour, so a round trip through + // it is near-identity rather than exact (the model documents a mean Delta-E around 2). + std::string mixed = blend_color_multi({"#123456", "#123456"}, {1, 1}); + REQUIRE(mixed.size() == 7); + auto comp = [](const std::string &hex, int i) { + return std::stoi(hex.substr(1 + 2 * i, 2), nullptr, 16); + }; + for (int i = 0; i < 3; ++i) + REQUIRE(std::abs(comp(mixed, i) - comp("#123456", i)) <= 8); + } +} diff --git a/tests/libslic3r/test_preset_bundle_loading.cpp b/tests/libslic3r/test_preset_bundle_loading.cpp index 037a76a805..5d9e60d6ef 100644 --- a/tests/libslic3r/test_preset_bundle_loading.cpp +++ b/tests/libslic3r/test_preset_bundle_loading.cpp @@ -566,3 +566,46 @@ TEST_CASE("A printer specific filament supersedes the generic library filament w CHECK(is_compatible_with_printer(generic_lib, PresetWithVendorProfile(*printer_c, nullptr))); } + +// Mixed-color filament metadata lives in project_config as parallel per-filament arrays. +// set_num_filaments() is the single place that grows them alongside filament_colour; if it +// misses them, creating a mixed slot writes past the end of the short arrays. +TEST_CASE("set_num_filaments keeps mixed-color arrays in step with the filament count", "[Preset][Bundle][FilamentMixer]") +{ + static const char *kMixedKeys[] = { + "filament_is_mixed", + "filament_mixed_components", + "filament_mixed_sublayer_ratios", + "filament_mixed_gradient", + "filament_mixed_gradient_range", + "filament_mixed_gradient_curve", + "filament_mixed_gradient_per_part", + }; + + auto mixed_array_size = [](const DynamicPrintConfig &cfg, const std::string &key) -> size_t { + if (const auto *b = cfg.option(key)) + return b->values.size(); + if (const auto *s = cfg.option(key)) + return s->values.size(); + return size_t(-1); // key missing entirely + }; + + PresetBundle bundle; + + const unsigned int n = GENERATE(2u, 4u, 8u); + bundle.set_num_filaments(n, std::string("#FF0000")); + + REQUIRE(bundle.project_config.option("filament_colour")->values.size() == n); + for (const char *key : kMixedKeys) { + DYNAMIC_SECTION("grown: " << key) { + CHECK(mixed_array_size(bundle.project_config, key) == n); + } + } + + SECTION("shrinking keeps them in step too") { + bundle.set_num_filaments(1, std::string("#00FF00")); + REQUIRE(bundle.project_config.option("filament_colour")->values.size() == 1); + for (const char *key : kMixedKeys) + CHECK(mixed_array_size(bundle.project_config, key) == 1); + } +}