Browse Source

Add (untested) ImGui AutoColor

Macoy Madson 9 months ago
  1. 204
  2. 1


@ -0,0 +1,204 @@
;; Automatically set ImGui theme colors based on the user's current wallpaper.
(import "ImGui.cake" "TaskSystem.cake" "SDL.cake"
(c-import "<stdlib.h>" "<stdio.h>" "<math.h>")
(var target-base16-colors ([] 16 auto-color))
(var current-base16-colors ([] 16 auto-color)
(array 0x0c 0x0e 0x0d)
(array 0x25 0x1b 0x19)
(array 0x3c 0x30 0x2a)
(array 0x77 0x8e 0xae)
(array 0x5a 0xa8 0xf0)
(array 0xc5 0x8e 0x89)
(array 0x50 0x37 0x30)
(array 0x46 0x4a 0x53)
(array 0x67 0xff 0xff)
(array 0x67 0xc8 0xff)
(array 0x66 0xbb 0xff)
(array 0x7b 0xc0 0xeb)
(array 0xa1 0x9c 0xbc)
(array 0xc5 0x8e 0x89)
(array 0x5a 0xa8 0xf0)
(array 0x77 0x8e 0xae)))
;; Colors are in range 0 to 1.f
(var-global current-interpolate-base16-colors ([] 16 ([] 3 float)) (array 0))
(var target-set-time float 0.f)
(defun-local auto-color-to-imgui-color (color auto-color opacity float &return ImVec4)
(return (ImVec4
(/ (at 0 color) 255.f)
(/ (at 1 color) 255.f)
(/ (at 2 color) 255.f)
(defun-local set-imgui-colors-from-base16 (base16-colors (* auto-color))
(var colors (* ImVec4) (field (imgui-call GetStyle) Colors))
(set (at ImGuiCol_Text colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_TextDisabled colors) (auto-color-to-imgui-color (at 3 base16-colors) 1.f))
(set (at ImGuiCol_WindowBg colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_ChildBg colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_PopupBg colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_Border colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_BorderShadow colors) (auto-color-to-imgui-color (at 14 base16-colors) 1.f))
(set (at ImGuiCol_FrameBg colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_FrameBgHovered colors) (auto-color-to-imgui-color (at 6 base16-colors) 1.f))
(set (at ImGuiCol_FrameBgActive colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_TitleBg colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_TitleBgActive colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_TitleBgCollapsed colors) (auto-color-to-imgui-color (at 6 base16-colors) 1.f))
(set (at ImGuiCol_MenuBarBg colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_ScrollbarBg colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_ScrollbarGrab colors) (auto-color-to-imgui-color (at 13 base16-colors) 1.f))
(set (at ImGuiCol_ScrollbarGrabHovered colors) (auto-color-to-imgui-color (at 14 base16-colors) 1.f))
(set (at ImGuiCol_ScrollbarGrabActive colors) (auto-color-to-imgui-color (at 15 base16-colors) 1.f))
(set (at ImGuiCol_CheckMark colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_SliderGrab colors) (auto-color-to-imgui-color (at 12 base16-colors) 1.f))
(set (at ImGuiCol_SliderGrabActive colors) (auto-color-to-imgui-color (at 6 base16-colors) 1.f))
(set (at ImGuiCol_Button colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_ButtonHovered colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_ButtonActive colors) (auto-color-to-imgui-color (at 6 base16-colors) 1.f))
(set (at ImGuiCol_Header colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_HeaderHovered colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_HeaderActive colors) (auto-color-to-imgui-color (at 9 base16-colors) 1.f))
(set (at ImGuiCol_Separator colors) (auto-color-to-imgui-color (at 10 base16-colors) 1.f))
(set (at ImGuiCol_SeparatorHovered colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_SeparatorActive colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_ResizeGrip colors) (auto-color-to-imgui-color (at 11 base16-colors) 1.f))
(set (at ImGuiCol_ResizeGripHovered colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_ResizeGripActive colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_Tab colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_TabHovered colors) (auto-color-to-imgui-color (at 6 base16-colors) 1.f))
(set (at ImGuiCol_TabActive colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_TabUnfocused colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_TabUnfocusedActive colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_DockingPreview colors) (auto-color-to-imgui-color (at 6 base16-colors) 1.f))
(set (at ImGuiCol_DockingEmptyBg colors) (auto-color-to-imgui-color (at 0 base16-colors) 1.f))
(set (at ImGuiCol_PlotLines colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_PlotLinesHovered colors) (auto-color-to-imgui-color (at 13 base16-colors) 1.f))
(set (at ImGuiCol_PlotHistogram colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_PlotHistogramHovered colors) (auto-color-to-imgui-color (at 5 base16-colors) 1.f))
(set (at ImGuiCol_TableHeaderBg colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_TableBorderStrong colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_TableBorderLight colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_TableRowBg colors) (auto-color-to-imgui-color (at 1 base16-colors) 1.f))
(set (at ImGuiCol_TableRowBgAlt colors) (auto-color-to-imgui-color (at 8 base16-colors) 1.f))
(set (at ImGuiCol_TextSelectedBg colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_DragDropTarget colors) (auto-color-to-imgui-color (at 7 base16-colors) 1.f))
(set (at ImGuiCol_NavHighlight colors) (auto-color-to-imgui-color (at 9 base16-colors) 1.f))
(set (at ImGuiCol_NavWindowingHighlight colors) (auto-color-to-imgui-color (at 2 base16-colors) 1.f))
(set (at ImGuiCol_NavWindowingDimBg colors) (auto-color-to-imgui-color (at 1 base16-colors) 0.8f))
(set (at ImGuiCol_ModalWindowDimBg colors) (auto-color-to-imgui-color (at 2 base16-colors) 0.8f)))
(defun imgui-auto-color-initialize ()
(each-in-array current-base16-colors i
(each-in-range 3 component
(set (at i component current-interpolate-base16-colors)
(/ (type-cast (at i component current-base16-colors) float) 255.f))))
(memcpy target-base16-colors current-base16-colors (sizeof target-base16-colors))
(set-imgui-colors-from-base16 current-base16-colors))
(def-task set-target-base16-colors (base16-colors (* auto-color) succeeded (* bool))
(when (deref succeeded)
(set target-set-time (get-time-since-startup))
(memcpy target-base16-colors base16-colors (sizeof target-base16-colors)))
(free succeeded)
(free base16-colors))
(var last-background-path ([] 1024 char) (array 0))
(defun update-auto-color-theme ()
(unless target-set-time (return))
;; Automatically update the theme on background change
(var current-time float (get-time-since-startup))
(var-static last-background-changed-check float current-time)
(var check-for-background-change-rate (const float) 1.f)
(when (> (- current-time last-background-changed-check)
(set last-background-changed-check current-time)
(var current-background-path ([] 1024 char) (array 0))
(var error-string (* (const char)) null)
(when (auto-color-get-current-background-filename current-background-path
(sizeof current-background-path)
(addr error-string))
;; Don't count the very first one, because the process will update the theme on startup anyways
(if (at 0 last-background-path)
(unless (= 0 (strcmp last-background-path current-background-path))
(fprintf stderr "Wallpaper changed: '%s' != '%s'\n"
last-background-path current-background-path)
(strncpy last-background-path current-background-path (sizeof last-background-path))
(strncpy last-background-path current-background-path (sizeof last-background-path)))))
;; Smoothly interpolate between current and target colors
(var before-update-values ([] 16 auto-color))
(memcpy before-update-values current-base16-colors (sizeof before-update-values))
(var time-delta float (- current-time target-set-time))
(var transition-total-time float 0.5f)
(var transition-speed float (/ time-delta transition-total-time))
(when (< 1.f transition-speed)
(set transition-speed 1.f))
(each-in-array target-base16-colors i
(each-in-range 3 component
(var current-value float (at i component current-interpolate-base16-colors))
(var-cast-to target-value float (at i component target-base16-colors))
(set target-value (/ target-value 255.f))
(set (at i component current-interpolate-base16-colors)
(+ current-value
(* (- target-value current-value)
(set (at i component current-base16-colors)
(type-cast (round (* 255.f (at i component current-interpolate-base16-colors)))
(unsigned char)))))
(unless (= 0 (memcmp before-update-values current-base16-colors (sizeof before-update-values)))
(set-imgui-colors-from-base16 current-base16-colors)))
(def-task pick-colors-from-background (base16-colors (* auto-color)
succeeded-out (* bool))
(fprintf stderr "Picking colors\n")
(set (deref succeeded-out) (auto-color-pick-from-current-background base16-colors))
(fprintf stderr "Picking colors: %s\n" (? (deref succeeded-out) "succeeded" "failed")))
(defun imgui-colors-from-current-background-start ()
(var-cast-to base16-colors (* auto-color) (calloc 16 (sizeof (type auto-color))))
(var-cast-to succeeded (* bool) (malloc (sizeof (type bool))))
(pick-colors-from-background base16-colors succeeded)
(set-target-base16-colors :pin-to-main-thread base16-colors succeeded)))
(c-import "<stdio.h>")
(make-imgui-sdl-gl3-application ;; If you want your own entry point, check this macro's body
"ImGui Auto Color"
(scope ;; Initialization
(fprintf stderr "Initializing\n")
;; Auto color uses time to smoothly transition. Set reference point to now
;; Use task system so the potentially slow background query doesn't block our update loop
;; Start getting colors while the rest of the app initializes
(scope ;; Once per frame
(imgui-call ShowDemoWindow))
(fprintf stderr "Shut down\n")


@ -71,6 +71,7 @@
"../src/DynamicArray.cake" "../src/Introspection.cake" "../src/OpenGL.cake"
"../src/Tracy.cake" "../src/TaskSystem.cake" "../src/Image.cake"
"../src/DataBundle.cake" "../src/TinyCCompiler.cake" "../src/FreeType.cake"
(when test-ogre