diff --git a/resources/images/transparent.svg b/resources/images/transparent.svg new file mode 100644 index 000000000..ee71b59f7 --- /dev/null +++ b/resources/images/transparent.svg @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/slic3r/GUI/PresetComboBoxes.cpp b/src/slic3r/GUI/PresetComboBoxes.cpp index faaa7fdaa..7cf623629 100644 --- a/src/slic3r/GUI/PresetComboBoxes.cpp +++ b/src/slic3r/GUI/PresetComboBoxes.cpp @@ -391,20 +391,7 @@ void PresetComboBox::add_ams_filaments(std::string selected, bool alias_name) const_cast(*iter).is_visible = true; auto color = f.opt_string("filament_colour", 0u); auto name = f.opt_string("tray_name", 0u); - wxColour clr(color); - wxImage img(24, 16); - if (clr.Red() > 224 && clr.Blue() > 224 && clr.Green() > 224) { - img.SetRGB(wxRect({0, 0}, img.GetSize()), 128, 128, 128); - img.SetRGB(wxRect({1, 1}, img.GetSize() - wxSize{2, 2}), clr.Red(), clr.Green(), clr.Blue()); - } else { - img.SetRGB(wxRect({0, 0}, img.GetSize()), clr.Red(), clr.Green(), clr.Blue()); - } - wxBitmap bmp(img); - wxMemoryDC dc(bmp); - dc.SetFont(Label::Body_10); - dc.SetTextForeground(different_color(clr)); - auto size = dc.GetTextExtent(name); - dc.DrawText(name, bmp.GetWidth() / 2 - size.x / 2, bmp.GetHeight() / 2 - size.y / 2); + wxBitmap bmp(*get_extruder_color_icon(color, name, 24, 16)); int item_id = Append(get_preset_name(*iter), bmp.ConvertToImage(), &m_first_ams_filament + (&f - &m_preset_bundle->filament_ams_list.front())); //validate_selection(id->value == selected); // can not select } @@ -683,7 +670,7 @@ PlaterPresetComboBox::PlaterPresetComboBox(wxWindow *parent, Preset::Type preset // BBS if (m_type == Preset::TYPE_FILAMENT) { int em = wxGetApp().em_unit(); - clr_picker = new wxButton(parent, wxID_ANY, "", wxDefaultPosition, wxSize(FromDIP(20), FromDIP(20)), wxBU_EXACTFIT | wxBU_AUTODRAW | wxBORDER_NONE); + clr_picker = new wxBitmapButton(parent, wxID_ANY, {}, wxDefaultPosition, wxSize(FromDIP(20), FromDIP(20)), wxBU_EXACTFIT | wxBU_AUTODRAW | wxBORDER_NONE); clr_picker->SetToolTip(_L("Click to pick filament color")); clr_picker->Bind(wxEVT_BUTTON, [this](wxCommandEvent& e) { m_clrData.SetColour(clr_picker->GetBackgroundColour()); @@ -938,12 +925,7 @@ void PlaterPresetComboBox::update() // filament_color.clear(); // BBS wxColor clr(filament_color); - clr_picker->SetBackgroundColour(clr); - auto diff_clr = different_color(clr); - clr_picker->SetForegroundColour(diff_clr); - auto style = clr_picker->GetWindowStyle() & ~(wxBORDER_NONE | wxBORDER_SIMPLE); - style = clr.Red() > 224 && clr.Blue() > 224 && clr.Green() > 224 ? (style | wxBORDER_SIMPLE) : (style | wxBORDER_NONE); - clr_picker->SetWindowStyle(style); + clr_picker->SetBitmap(*get_extruder_color_icons(true)[m_filament_idx]); #ifdef __WXOSX__ clr_picker->SetLabel(clr_picker->GetLabel()); // Let setBezelStyle: be called clr_picker->Refresh(); diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index b17a58ef4..0e19cfcbc 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -502,8 +502,6 @@ wxBitmap* get_default_extruder_color_icon(bool thin_icon/* = false*/) std::vector get_extruder_color_icons(bool thin_icon/* = false*/) { - static Slic3r::GUI::BitmapCache bmp_cache; - // Create the bitmap with color bars. std::vector bmps; std::vector colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); @@ -518,47 +516,66 @@ std::vector get_extruder_color_icons(bool thin_icon/* = false*/) * and scale them in respect to em_unit value */ const double em = Slic3r::GUI::wxGetApp().em_unit(); - const int icon_width = lround((thin_icon ? 2 : 4.5) * em); + const int icon_width = lround((thin_icon ? 2 : 4.4) * em); const int icon_height = lround(2 * em); bool dark_mode = Slic3r::GUI::wxGetApp().dark_mode(); int index = 0; - wxClientDC cdc((wxWindow*)Slic3r::GUI::wxGetApp().mainframe); - wxMemoryDC dc(&cdc); - dc.SetFont(::Label::Body_12); for (const std::string &color : colors) { auto label = std::to_string(++index); - std::string bitmap_key = color + "-h" + std::to_string(icon_height) + "-w" + std::to_string(icon_width) - + "-i" + label; - - wxBitmap* bitmap = bmp_cache.find(bitmap_key); - if (bitmap == nullptr) { - // Paint the color icon. - //Slic3r::GUI::BitmapCache::parse_color(color, rgb); - // there is no neede to scale created solid bitmap - wxColor clr(color); - bitmap = bmp_cache.insert(bitmap_key, wxBitmap(icon_width, icon_height)); - dc.SelectObject(*bitmap); - dc.SetBackground(wxBrush(clr)); - dc.Clear(); - if (clr.Red() > 224 && clr.Blue() > 224 && clr.Green() > 224) { - dc.SetBrush(wxBrush(clr)); - dc.SetPen(*wxGREY_PEN); - dc.DrawRectangle(0, 0, icon_width, icon_height); - } - auto size = dc.GetTextExtent(wxString(label)); - dc.SetTextForeground(clr.GetLuminance() < 0.51 ? *wxWHITE : *wxBLACK); - dc.DrawText(label, (icon_width - size.x) / 2, (icon_height - size.y) / 2); - dc.SelectObject(wxNullBitmap); - } - bmps.emplace_back(bitmap); + bmps.push_back(get_extruder_color_icon(color, label, icon_width, icon_height)); } return bmps; } +wxBitmap *get_extruder_color_icon(std::string color, std::string label, int icon_width, int icon_height) +{ + static Slic3r::GUI::BitmapCache bmp_cache; + + std::string bitmap_key = color + "-h" + std::to_string(icon_height) + "-w" + std::to_string(icon_width) + "-i" + label; + + wxBitmap *bitmap = bmp_cache.find(bitmap_key); + if (bitmap == nullptr) { + // Paint the color icon. + // Slic3r::GUI::BitmapCache::parse_color(color, rgb); + // there is no neede to scale created solid bitmap + wxColor clr(color); + bitmap = bmp_cache.insert(bitmap_key, wxBitmap(icon_width, icon_height)); + wxClientDC cdc((wxWindow *) Slic3r::GUI::wxGetApp().mainframe); + wxMemoryDC dc(&cdc); + dc.SetFont(::Label::Body_12); + dc.SelectObject(*bitmap); + if (clr.Alpha() == 0) { + int size = icon_height * 2; + static wxBitmap transparent = *Slic3r::GUI::BitmapCache().load_svg("transparent", size, size); + if (transparent.GetHeight() != size) transparent = *Slic3r::GUI::BitmapCache().load_svg("transparent", size, size); + wxPoint pt(0, 0); + while (pt.x < icon_width) { + dc.DrawBitmap(transparent, pt); + pt.x += size; + } + clr.SetRGB(0xffffff); // for text color + dc.SetBrush(*wxTRANSPARENT_BRUSH); + } else { + dc.SetBackground(wxBrush(clr)); + dc.Clear(); + dc.SetBrush(wxBrush(clr)); + } + if (clr.Red() > 224 && clr.Blue() > 224 && clr.Green() > 224) { + dc.SetPen(*wxGREY_PEN); + dc.DrawRectangle(0, 0, icon_width, icon_height); + } + auto size = dc.GetTextExtent(wxString(label)); + dc.SetTextForeground(clr.GetLuminance() < 0.51 ? *wxWHITE : *wxBLACK); + dc.DrawText(label, (icon_width - size.x) / 2, (icon_height - size.y) / 2); + dc.SelectObject(wxNullBitmap); + } + return bitmap; +} + void apply_extruder_selector(Slic3r::GUI::BitmapComboBox** ctrl, wxWindow* parent, diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index e890d30ae..44e289ed0 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -66,7 +66,8 @@ wxBitmap create_scaled_bitmap(const std::string& bmp_name, wxWindow *win = nullp #endif wxBitmap* get_default_extruder_color_icon(bool thin_icon = false); -std::vector get_extruder_color_icons(bool thin_icon = false); +std::vector get_extruder_color_icons(bool thin_icon = false); +wxBitmap * get_extruder_color_icon(std::string color, std::string label, int icon_width, int icon_height); namespace Slic3r { namespace GUI {