OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::anonymous_namespace{SnapmakerPrinterAgent.cpp} Namespace Reference

Functions

template<typename T>
T safe_at (const std::vector< T > &vec, int index, const T &fallback)
std::string find_closest_color_preset_by_vendor_and_type (const PresetCollection &filaments, const std::string &vendor_name, const std::string &filament_type, const std::string &color_rgba)

Variables

constexpr const char * SNAPMAKER_AGENT_VERSION = "0.0.1"

Function Documentation

◆ find_closest_color_preset_by_vendor_and_type()

std::string Slic3r::anonymous_namespace{SnapmakerPrinterAgent.cpp}::find_closest_color_preset_by_vendor_and_type ( const PresetCollection & filaments,
const std::string & vendor_name,
const std::string & filament_type,
const std::string & color_rgba )
33{
34 std::string best_match_id = "";
35 int best_color_distance = 0xffffffff;
36
37 for (const auto& p : filaments.get_presets()) {
38 if (p.is_visible && p.is_compatible &&
39 // Filament profile must be detached from parent to be considered for matching
40 filaments.get_preset_base(p) == &p && p.config.opt_string("filament_vendor", 0u) == vendor_name &&
41 p.config.opt_string("filament_type", 0u) == filament_type) {
42 // The printer returns RGBA in the format RRGGBBAA, but profiles store color as #RRGGBB,
43 // so we must remove # and ignore alpha channel for distance calculation
44 unsigned int target_color_value = std::stoul(color_rgba.substr(0, color_rgba.length() - 2), nullptr, 16);
45
46 std::string p_color = p.config.opt_string("default_filament_colour", 0u);
47 unsigned int p_color_value;
48 if (!p_color.empty()) {
49 size_t hash_pos = p_color.find("#");
50 p_color_value = std::stoul(p_color.substr(hash_pos != std::string::npos ? hash_pos + 1 : 0), nullptr, 16);
51 } else {
52 // Default to black if no color specified in profile. Assume other profiles might be a closer color match.
53 // Could be a problem if the target color is also black and there exist a specific profile for that type, vendor and color
54 // combination.
55 p_color_value = 0;
56 }
57
58 // Calculate Euclidean color distance in RGB space
59 int dr = ((target_color_value & 0xff) - (p_color_value & 0xff));
60 int dg = (((target_color_value >> 8) & 0xff) - ((p_color_value >> 8) & 0xff));
61 int db = (((target_color_value >> 16) & 0xff) - ((p_color_value >> 16) & 0xff));
62 unsigned int distance = dr * dr + dg * dg + db * db;
63
64 if (distance < best_color_distance) {
65 best_color_distance = distance;
66 best_match_id = p.filament_id;
67 }
68 }
69 }
70 return best_match_id;
71}
std::string & opt_string(const t_config_option_key &opt_key, bool create=false)
Definition Config.hpp:3042
const std::deque< Preset > & get_presets() const
Definition Preset.hpp:781
const Preset * get_preset_base(const Preset &child) const
Definition Preset.cpp:3693
DynamicPrintConfig config
Definition Preset.hpp:300
zaa_minimize_perimeter_height filament_type((ConfigOptionBools, filament_soluble))((ConfigOptionStrings

References Slic3r::Preset::config, Slic3r::filament_type(), find_closest_color_preset_by_vendor_and_type(), Slic3r::PresetCollection::get_preset_base(), Slic3r::PresetCollection::get_presets(), and Slic3r::DynamicConfig::opt_string().

Referenced by find_closest_color_preset_by_vendor_and_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_at()

template<typename T>
T Slic3r::anonymous_namespace{SnapmakerPrinterAgent.cpp}::safe_at ( const std::vector< T > & vec,
int index,
const T & fallback )
25{
26 return (index >= 0 && index < static_cast<int>(vec.size())) ? vec[index] : fallback;
27}

References safe_at().

Referenced by safe_at().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ SNAPMAKER_AGENT_VERSION

const char* Slic3r::anonymous_namespace{SnapmakerPrinterAgent.cpp}::SNAPMAKER_AGENT_VERSION = "0.0.1"
constexpr