Skip to content

Instantly share code, notes, and snippets.

@dougbinks
Last active April 29, 2024 05:48
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save dougbinks/8089b4bbaccaaf6fa204236978d165a9 to your computer and use it in GitHub Desktop.
Save dougbinks/8089b4bbaccaaf6fa204236978d165a9 to your computer and use it in GitHub Desktop.
#pragma once
#include "IconsFontAwesome.h" // from https://github.com/juliettef/IconFontCppHeaders
namespace ImGui
{
inline void SetupImGuiStyle( bool bStyleDark_, float alpha_ )
{
ImGuiStyle& style = ImGui::GetStyle();
// light style from Pacôme Danhiez (user itamago) https://github.com/ocornut/imgui/pull/511#issuecomment-175719267
style.Alpha = 1.0f;
style.FrameRounding = 3.0f;
style.Colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 0.94f);
style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.39f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.59f, 0.59f, 0.59f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);
style.Colors[ImGuiCol_ComboBg] = ImVec4(0.86f, 0.86f, 0.86f, 0.99f);
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
style.Colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.50f);
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
style.Colors[ImGuiCol_CloseButton] = ImVec4(0.59f, 0.59f, 0.59f, 0.50f);
style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
style.Colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
if( bStyleDark_ )
{
for (int i = 0; i <= ImGuiCol_COUNT; i++)
{
ImVec4& col = style.Colors[i];
float H, S, V;
ImGui::ColorConvertRGBtoHSV( col.x, col.y, col.z, H, S, V );
if( S < 0.1f )
{
V = 1.0f - V;
}
ImGui::ColorConvertHSVtoRGB( H, S, V, col.x, col.y, col.z );
if( col.w < 1.00f )
{
col.w *= alpha_;
}
}
}
else
{
for (int i = 0; i <= ImGuiCol_COUNT; i++)
{
ImVec4& col = style.Colors[i];
if( col.w < 1.00f )
{
col.x *= alpha_;
col.y *= alpha_;
col.z *= alpha_;
col.w *= alpha_;
}
}
}
}
inline bool CheckBoxFont( const char* name_, bool* pB_, const char* pOn_ = "[X]", const char* pOff_="[ ]" )
{
if( *pB_ )
{
ImGui::Text(pOn_);
}
else
{
ImGui::Text(pOff_);
}
bool bHover = false;
bHover = bHover || ImGui::IsItemHovered();
ImGui::SameLine();
ImGui::Text( name_ );
bHover = bHover || ImGui::IsItemHovered();
if( bHover && ImGui::IsMouseClicked(0) )
{
*pB_ = ! *pB_;
return true;
}
return false;
}
inline bool CheckBoxTick( const char* name_, bool* pB_ )
{
return CheckBoxFont( name_, pB_, ICON_FA_CHECK_SQUARE_O, ICON_FA_SQUARE_O );
}
inline bool MenuItemCheckbox( const char* name_, bool* pB_ )
{
bool retval = ImGui::MenuItem( name_ );
ImGui::SameLine();
if( *pB_ )
{
ImGui::Text(ICON_FA_CHECK_SQUARE_O);
}
else
{
ImGui::Text(ICON_FA_SQUARE_O);
}
if( retval )
{
*pB_ = ! *pB_;
}
return retval;
}
struct FrameTimeHistogram
{
// configuration params - modify these at will
static const int NUM = 101; //last value is from T-1 to inf.
float dT = 0.001f; // in seconds, default 1ms
float refresh = 1.0f/60.0f;// set this to your target refresh rate
static const int NUM_MARKERS = 2;
float markers[NUM_MARKERS] = { 0.99f, 0.999f };
// data
ImVec2 size = ImVec2( 3.0f * NUM, 40.0f );
float lastdT = 0.0f;
float timesTotal;
float countsTotal;
float times[ NUM];
float counts[NUM];
float hitchTimes[ NUM];
float hitchCounts[NUM];
FrameTimeHistogram()
{
Clear();
}
void Clear()
{
timesTotal = 0.0f;
countsTotal = 0.0f;
memset(times, 0, sizeof(times) );
memset(counts, 0, sizeof(counts) );
memset(hitchTimes, 0, sizeof(hitchTimes) );
memset(hitchCounts, 0, sizeof(hitchCounts) );
}
int GetBin( float time_ )
{
int bin = (int)floor( time_ / dT );
if( bin >= NUM )
{
bin = NUM - 1;
}
return bin;
}
void Update( float deltaT_ )
{
if( deltaT_ < 0.0f )
{
assert(false);
return;
}
int bin = GetBin( deltaT_ );
times[ bin] += deltaT_;
timesTotal += deltaT_;
counts[bin] += 1.0f;
countsTotal += 1.0f;
float hitch = abs( lastdT - deltaT_ );
int deltaBin = GetBin( hitch );
hitchTimes[ deltaBin] += hitch;
hitchCounts[deltaBin] += 1.0f;
lastdT = deltaT_;
}
void PlotRefreshLines( float total_ = 0.0f, float* pValues_ = NULL)
{
ImDrawList* draw = ImGui::GetWindowDrawList();
const ImGuiStyle& style = ImGui::GetStyle();
ImVec2 pad = style.FramePadding;
ImVec2 min = ImGui::GetItemRectMin();
min.x += pad.x;
ImVec2 max = ImGui::GetItemRectMax();
max.x -= pad.x;
float xRefresh = (max.x - min.x) * refresh / ( dT * NUM );
float xCurr = xRefresh + min.x;
while( xCurr < max.x )
{
float xP = ceil( xCurr ); // use ceil to get integer coords or else lines look odd
draw->AddLine( ImVec2( xP, min.y ), ImVec2( xP, max.y ), 0x50FFFFFF );
xCurr += xRefresh;
}
if( pValues_ )
{
// calc markers
float currTotal = 0.0f;
int mark = 0;
for( int i = 0; i < NUM && mark < NUM_MARKERS; ++i )
{
currTotal += pValues_[i];
if( total_ * markers[mark] < currTotal )
{
float xP = ceil( (float)(i+1)/(float)NUM * ( max.x - min.x ) + min.x ); // use ceil to get integer coords or else lines look odd
draw->AddLine( ImVec2( xP, min.y ), ImVec2( xP, max.y ), 0xFFFF0000 );
++mark;
}
}
}
}
void CalcHistogramSize( int numShown_ )
{
ImVec2 wRegion = ImGui::GetContentRegionMax();
float heightGone = 7.0f * ImGui::GetItemsLineHeightWithSpacing();
wRegion.y -= heightGone;
wRegion.y /= (float) numShown_;
const ImGuiStyle& style = ImGui::GetStyle();
ImVec2 pad = style.FramePadding;
wRegion.x -= 2.0f * pad.x;
size = wRegion;
}
void Draw(const char* name_, bool* pOpen_ = NULL )
{
if (ImGui::Begin( name_, pOpen_ ))
{
int numShown = 0;
if(ImGui::CollapsingHeader("Time Histogram"))
{
++numShown;
ImGui::PlotHistogram("", times, NUM, 0, NULL, FLT_MAX, FLT_MAX, size );
PlotRefreshLines( timesTotal, times );
}
if(ImGui::CollapsingHeader("Count Histogram"))
{
++numShown;
ImGui::PlotHistogram("", counts, NUM, 0, NULL, FLT_MAX, FLT_MAX, size );
PlotRefreshLines( countsTotal, counts );
}
if(ImGui::CollapsingHeader("Hitch Time Histogram"))
{
++numShown;
ImGui::PlotHistogram("", hitchTimes, NUM, 0, NULL, FLT_MAX, FLT_MAX, size );
PlotRefreshLines();
}
if(ImGui::CollapsingHeader("Hitch Count Histogram"))
{
++numShown;
ImGui::PlotHistogram("", hitchCounts, NUM, 0, NULL, FLT_MAX, FLT_MAX, size );
PlotRefreshLines();
}
if( ImGui::Button("Clear") )
{
Clear();
}
CalcHistogramSize( numShown );
}
ImGui::End();
}
};
};
@SuperZikoure
Copy link

I recently switched from nanogui to imgui in a personnal project.
What license is this released in ? Could I use this (with proper attribution of course) in an open source project ?

Thank you in advance!

@dougbinks
Copy link
Author

This work is 'public domain' but if you need to use a license you can use the same as Dear ImGui, which is MIT.

Note that the theme in SetupImGuiStyle is from ocornut/imgui#511 (comment) and I can't myself add a license to that, but it was contributed to the comments on Dear ImGui repo so normally one would assume the same license as the project.

My current version of SetupImGuiStyle is a lot simpler due to more recent styling changes to Dear ImGui:

    inline void SetupImGuiStyle( bool bStyleDark_, float alpha_  )
    {
        ImGuiStyle& style = ImGui::GetStyle();

        if( bStyleDark_ )
        {
            StyleColorsDark();
        }
        else
        {
            StyleColorsLight();
        }

        for (int i = 0; i <= ImGuiCol_COUNT; i++)
        {
            ImGuiCol_ ei = (ImGuiCol_)i;
            ImVec4& col = style.Colors[i];
            if(  (ImGuiCol_ModalWindowDimBg  != ei ) &&
                ( ImGuiCol_NavWindowingDimBg != ei ) &&
                ( col.w < 1.00f || ( ImGuiCol_FrameBg  == ei )
                                || ( ImGuiCol_WindowBg == ei ) 
                                || ( ImGuiCol_ChildBg  == ei ) ) )
            {
                col.w = alpha_ * col.w;
            }
        }
        
        style.ChildBorderSize = 1.0f;
        style.FrameBorderSize = 0.0f;
        style.PopupBorderSize = 1.0f;
        style.WindowBorderSize = 0.0f;
        style.FrameRounding = 3.0f;
        style.Alpha = 1.0f;
    }

@SuperZikoure
Copy link

Thank you very much for your qick answer. I'll be sure to credit both of you !

@pthom
Copy link

pthom commented Nov 14, 2022

Just a quick note: there is a of by one error in the loop: for (int i = 0; i < ImGuiCol_COUNT; i++)

@dougbinks
Copy link
Author

Yes, that's correct, thanks!

@TempAccountNull
Copy link

TempAccountNull commented May 10, 2023

This work is 'public domain' but if you need to use a license you can use the same as Dear ImGui, which is MIT.

Note that the theme in SetupImGuiStyle is from ocornut/imgui#511 (comment) and I can't myself add a license to that, but it was contributed to the comments on Dear ImGui repo so normally one would assume the same license as the project.

My current version of SetupImGuiStyle is a lot simpler due to more recent styling changes to Dear ImGui:

    inline void SetupImGuiStyle( bool bStyleDark_, float alpha_  )
    {
        ImGuiStyle& style = ImGui::GetStyle();

        if( bStyleDark_ )
        {
            StyleColorsDark();
        }
        else
        {
            StyleColorsLight();
        }

        for (int i = 0; i <= ImGuiCol_COUNT; i++)
        {
            ImGuiCol_ ei = (ImGuiCol_)i;
            ImVec4& col = style.Colors[i];
            if(  (ImGuiCol_ModalWindowDimBg  != ei ) &&
                ( ImGuiCol_NavWindowingDimBg != ei ) &&
                ( col.w < 1.00f || ( ImGuiCol_FrameBg  == ei )
                                || ( ImGuiCol_WindowBg == ei ) 
                                || ( ImGuiCol_ChildBg  == ei ) ) )
            {
                col.w = alpha_ * col.w;
            }
        }
        
        style.ChildBorderSize = 1.0f;
        style.FrameBorderSize = 0.0f;
        style.PopupBorderSize = 1.0f;
        style.WindowBorderSize = 0.0f;
        style.FrameRounding = 3.0f;
        style.Alpha = 1.0f;
    }

Just to make it a tad bit better.

inline void SetupImGuiStyle(bool is_dark_style, float alpha_threshold)
{
   //Use a ternary operator
    is_dark_style ? StyleColorsDark() : StyleColorsLight();

    ImGuiStyle& style = ImGui::GetStyle();

    // Adjusts the alpha values of the ImGui colors based on the alpha threshold.
    for (int i = 0; i < ImGuiCol_COUNT; i++)
    {
        const auto color_id = static_cast<ImGuiCol>(i);
        auto& color = style.Colors[i];
        if (color.w < alpha_threshold || color_id == ImGuiCol_FrameBg || color_id == ImGuiCol_WindowBg || color_id == ImGuiCol_ChildBg)
        {
            color.w *= alpha_threshold;
        }
    }

    // Sets the border sizes and rounding.
    style.ChildBorderSize = 1.0f;
    style.FrameBorderSize = 0.0f;
    style.PopupBorderSize = 1.0f;
    style.WindowBorderSize = 0.0f;
    style.FrameRounding = 3.0f;
    style.Alpha = 1.0f;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment