From 68bad703eea357cadea9c60472bec00c7bba08e2 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 31 Jan 2015 17:15:07 +0000 Subject: [PATCH] ImColor::HSV() helper + color buttons demo --- imgui.cpp | 13 +++++++++++++ imgui.h | 13 ++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index b404ac08..ac336183 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -7808,6 +7808,19 @@ void ImGui::ShowTestWindow(bool* opened) ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine(); ImGui::RadioButton("radio c", &e, 2); + // Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style. + for (int i = 0; i < 7; i++) + { + if (i > 0) ImGui::SameLine(); + ImGui::PushID(i); + ImGui::PushStyleColor(ImGuiCol_Button, ImColor::HSV(i/7.0f, 0.6f, 0.6f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImColor::HSV(i/7.0f, 0.7f, 0.7f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImColor::HSV(i/7.0f, 0.8f, 0.8f)); + ImGui::Button("Click"); + ImGui::PopStyleColor(3); + ImGui::PopID(); + } + ImGui::Text("Hover me"); if (ImGui::IsItemHovered()) ImGui::SetTooltip("I am a tooltip"); diff --git a/imgui.h b/imgui.h index 0e4ade86..1d18d0c8 100644 --- a/imgui.h +++ b/imgui.h @@ -703,11 +703,14 @@ struct ImColor { ImVec4 Value; - ImColor(int r, int g, int b, int a = 255) { Value.x = r / 255.0f; Value.y = g / 255.0f; Value.z = b / 255.0f; Value.w = a / 255.0f; } - ImColor(const ImVec4& col) { Value = col; } - ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } - operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } - operator ImVec4() const { return Value; } + ImColor(int r, int g, int b, int a = 255) { Value.x = r / 255.0f; Value.y = g / 255.0f; Value.z = b / 255.0f; Value.w = a / 255.0f; } + ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } + ImColor(const ImVec4& col) { Value = col; } + + operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } + operator ImVec4() const { return Value; } + + static ImColor HSV(float h, float s, float v, float a = 1.0f) { float r,g,b; ImGui::ColorConvertHSVtoRGB(h, s, v, r, g, b); return ImColor(r,g,b,a); } }; //-----------------------------------------------------------------------------