Browse Source

Add (untested) ImGui AutoColor

master
Macoy Madson 9 months ago
parent
commit
6138c4dd25
  1. 204
      src/ImGuiAutoColor.cake
  2. 1
      test/src/GameLibTests.cake

204
src/ImGuiAutoColor.cake

@ -0,0 +1,204 @@
;; Automatically set ImGui theme colors based on the user's current wallpaper.
(import "ImGui.cake" "TaskSystem.cake" "SDL.cake"
"AutoColor.cake"
"CHelpers.cake")
(c-import "<stdlib.h>" "<stdio.h>" "<math.h>")
(var target-base16-colors ([] 16 auto-color))
(var current-base16-colors ([] 16 auto-color)
(array
(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)
opacity)))
(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)
check-for-background-change-rate)
(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))
(imgui-colors-from-current-background-start))
(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)
transition-speed)))
(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))))
(task-system-execute
(pick-colors-from-background base16-colors succeeded)
(set-target-base16-colors :pin-to-main-thread base16-colors succeeded)))
(comptime-cond
('auto-test
(c-import "<stdio.h>")
(make-imgui-sdl-gl3-application ;; If you want your own entry point, check this macro's body
test--imgui-auto-color
"ImGui Auto Color"
(scope ;; Initialization
(fprintf stderr "Initializing\n")
;; Auto color uses time to smoothly transition. Set reference point to now
(set-startup-time-now)
;; Use task system so the potentially slow background query doesn't block our update loop
(task-system-initialize)
(imgui-auto-color-initialize)
;; Start getting colors while the rest of the app initializes
(imgui-colors-from-current-background-start))
(scope ;; Once per frame
(update-auto-color-theme)
(task-system-run-thread-pinned-tasks)
(imgui-call ShowDemoWindow))
(scope
(fprintf stderr "Shut down\n")
(task-system-shutdown)))))

1
test/src/GameLibTests.cake

@ -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"
"../src/ImGuiAutoColor.cake"
"../src/ProfilerAutoInstrument.cake"))
(when test-ogre

Loading…
Cancel
Save