Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- in imgui.cpp
- IMGUI_API bool ImGui::MyColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags) {
- ImGuiWindow* window = GetCurrentWindow();
- if (window->SkipItems)
- return false;
- ImGuiContext& g = *GImGui;
- const ImGuiStyle& style = g.Style;
- const ImGuiID id = window->GetID(label);
- const float w_full = CalcItemWidth();
- const float square_sz_with_spacing = (flags & ImGuiColorEditFlags_NoColorSquare) ? 0.0f : (g.FontSize + style.FramePadding.y * 2.0f + style.ItemInnerSpacing.x);
- // If no mode is specified, defaults to RGB
- if (!(flags & ImGuiColorEditFlags_ModeMask_))
- flags |= ImGuiColorEditFlags_RGB;
- // If we're not showing any slider there's no point in querying color mode, nor showing the options menu, nor doing any HSV conversions
- if (flags & ImGuiColorEditFlags_NoSliders)
- flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_NoOptions;
- // Read back edit mode from persistent storage
- if (!(flags & ImGuiColorEditFlags_NoOptions))
- flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | (g.ColorEditModeStorage.GetInt(id, (flags & ImGuiColorEditFlags_ModeMask_)) & ImGuiColorEditFlags_ModeMask_);
- // Check that exactly one of RGB/HSV/HEX is set
- //IM_ASSERT(ImIsPowerOfTwo((int)(flags & ImGuiColorEditFlags_ModeMask_))); //
- float f[4] = { col[0], col[1], col[2], col[3] };
- if (flags & ImGuiColorEditFlags_HSV)
- ImGui::ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
- int i[4] = { IM_F32_TO_INT8(f[0]), IM_F32_TO_INT8(f[1]), IM_F32_TO_INT8(f[2]), IM_F32_TO_INT8(f[3]) };
- bool alpha = (flags & ImGuiColorEditFlags_Alpha) != 0;
- bool value_changed = false;
- int components = alpha ? 4 : 3;
- ImGui::BeginGroup();
- ImGui::PushID(label);
- if ((flags & (ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV)) != 0 && (flags & ImGuiColorEditFlags_NoSliders) == 0) {
- // RGB/HSV 0..255 Sliders
- const float w_items_all = w_full - square_sz_with_spacing;
- const float w_item_one = ImMax(1.0f, (float)(int)((w_items_all - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components));
- const float w_item_last = ImMax(1.0f, (float)(int)(w_items_all - (w_item_one + style.ItemInnerSpacing.x) * (components - 1)));
- const bool hide_prefix = (w_item_one <= CalcTextSize("M:999").x);
- const char* ids[4] = { "##X", "##Y", "##Z", "##W" };
- const char* fmt_table[3][4] =
- {
- { "%3.0f", "%3.0f", "%3.0f", "%3.0f" }, // Short display
- { "R:%3.0f", "G:%3.0f", "B:%3.0f", "A:%3.0f" }, // Long display for RGBA
- { "H:%3.0f", "S:%3.0f", "V:%3.0f", "A:%3.0f" } // Long display for HSVV
- };
- const char** fmt = hide_prefix ? fmt_table[0] : (flags & ImGuiColorEditFlags_HSV) ? fmt_table[2] : fmt_table[1];
- ImGui::PushItemWidth(w_item_one);
- for (int n = 0; n < components; n++) {
- if (n > 0)
- ImGui::SameLine(0, style.ItemInnerSpacing.x);
- if (n + 1 == components)
- ImGui::PushItemWidth(w_item_last);
- value_changed |= ImGui::DragInt(ids[n], &i[n], 1.0f, 0, 255, fmt[n]);
- }
- ImGui::PopItemWidth();
- ImGui::PopItemWidth();
- }
- else if ((flags & ImGuiColorEditFlags_HEX) != 0 && (flags & ImGuiColorEditFlags_NoSliders) == 0) {
- // RGB Hexadecimal Input
- const float w_slider_all = w_full - square_sz_with_spacing;
- char buf[64];
- if (alpha)
- ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", i[0], i[1], i[2], i[3]);
- else
- ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", i[0], i[1], i[2]);
- ImGui::PushItemWidth(w_slider_all);
- if (ImGui::InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase)) {
- value_changed |= true;
- char* p = buf;
- while (*p == '#' || ImCharIsSpace(*p))
- p++;
- i[0] = i[1] = i[2] = i[3] = 0;
- if (alpha)
- sscanf_s(p, "%02X%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]); // Treat at unsigned (%X is unsigned)
- else
- sscanf_s(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]);
- }
- ImGui::PopItemWidth();
- }
- const char* label_display_end = FindTextDisplayEnd(label);
- bool picker_active = false;
- if (!(flags & ImGuiColorEditFlags_NoColorSquare)) {
- if (!(flags & ImGuiColorEditFlags_NoSliders))
- ImGui::SameLine(0, style.ItemInnerSpacing.x);
- const ImVec4 col_display(col[0], col[1], col[2], 1.0f);
- if (ImGui::ColorButton(col_display)) {
- if (!(flags & ImGuiColorEditFlags_NoPicker)) {
- ImGui::OpenPopup("picker");
- ImGui::SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1, style.ItemSpacing.y));
- }
- }
- else if (!(flags & ImGuiColorEditFlags_NoOptions) && ImGui::IsItemHovered() && ImGui::IsMouseClicked(1)) {
- ImGui::OpenPopup("context");
- }
- if (ImGui::BeginPopup("picker")) {
- picker_active = true;
- if (label != label_display_end)
- ImGui::TextUnformatted(label, label_display_end);
- ImGui::PushItemWidth(256.0f + (alpha ? 2 : 1) * (style.ItemInnerSpacing.x));
- value_changed |= ImGui::MyColorPicker4("##picker", col, (flags & ImGuiColorEditFlags_Alpha) | (ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV | ImGuiColorEditFlags_HEX));
- ImGui::PopItemWidth();
- ImGui::EndPopup();
- }
- if (!(flags & ImGuiColorEditFlags_NoOptions) && ImGui::BeginPopup("context")) {
- // FIXME-LOCALIZATION
- if (ImGui::MenuItem("Edit as RGB", NULL, (flags & ImGuiColorEditFlags_RGB) ? 1 : 0)) g.ColorEditModeStorage.SetInt(id, (int)(ImGuiColorEditFlags_RGB));
- if (ImGui::MenuItem("Edit as HSV", NULL, (flags & ImGuiColorEditFlags_HSV) ? 1 : 0)) g.ColorEditModeStorage.SetInt(id, (int)(ImGuiColorEditFlags_HSV));
- if (ImGui::MenuItem("Edit as Hexadecimal", NULL, (flags & ImGuiColorEditFlags_HEX) ? 1 : 0)) g.ColorEditModeStorage.SetInt(id, (int)(ImGuiColorEditFlags_HEX));
- ImGui::EndPopup();
- }
- // Recreate our own tooltip over's ColorButton() one because we want to display correct alpha here
- //if (ImGui::IsItemHovered())
- // ImGui::SetTooltip("Color:\n(%.2f,%.2f,%.2f,%.2f)\n#%02X%02X%02X%02X", col[0], col[1], col[2], col[3], IM_F32_TO_INT8(col[0]), IM_F32_TO_INT8(col[1]), IM_F32_TO_INT8(col[2]), IM_F32_TO_INT8(col[3]));
- //ImGui::SetTooltip("");
- }
- if (label != label_display_end) {
- ImGui::SameLine(0, style.ItemInnerSpacing.x);
- ImGui::TextUnformatted(label, label_display_end);
- }
- // Convert back
- if (!picker_active) {
- for (int n = 0; n < 4; n++)
- f[n] = i[n] / 255.0f;
- if (flags & ImGuiColorEditFlags_HSV)
- ImGui::ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
- if (value_changed) {
- col[0] = f[0];
- col[1] = f[1];
- col[2] = f[2];
- if (alpha)
- col[3] = f[3];
- }
- }
- ImGui::PopID();
- ImGui::EndGroup();
- return value_changed;
- }
- IMGUI_API bool ImGui::MyColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags) {
- float col4[4] = { col[0], col[1], col[2], 1.0f };
- if (!MyColorEdit4(label, col4, flags & ~ImGuiColorEditFlags_Alpha))
- return false;
- col[0] = col4[0]; col[1] = col4[1]; col[2] = col4[2];
- return true;
- }
- in imgui.h
- IMGUI_API bool MyColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); // click on colored squared to open a color picker, right-click for options
- IMGUI_API bool MyColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0x01); // 0x01 = ImGuiColorEditFlags_Alpha = very dodgily backward compatible with 'bool show_alpha=true'
- for example on client.cpp
- ImGui::MyColorEdit3(u8"ЕСП КТ", Settings::Esp::Visuals_Color_CT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement