Skip to content

Instantly share code, notes, and snippets.

@redblobgames
Last active April 25, 2025 10:45
Show Gist options
  • Save redblobgames/eb3c36b008d12e8ab80ec3628dc8aa90 to your computer and use it in GitHub Desktop.
Save redblobgames/eb3c36b008d12e8ab80ec3628dc8aa90 to your computer and use it in GitHub Desktop.
Emacs config to assign a different color per project
;;; my-project-color.el ---
;;; Commentary: assign a color per project, for use in the mode line and other places
;;
;;; Code:
(require 'project)
(require 'color)
(defun project-color-background ()
"Return a background color for the project containing this directory"
(let* ((project (project-current))
(dirhash (sxhash (if project (project-root project) default-directory)))
(hue (/ (mod dirhash 1000) 1000.0))
(saturation (+ 0.3 (* 0.1 (mod (/ dirhash 1000) 3))))
(lightness (+ 0.4 (* 0.05 (mod (/ (/ dirhash 1000) 3) 4))))
(rgb (color-hsl-to-rgb hue saturation lightness)))
(color-rgb-to-hex (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) 2)))
(defun amitp/set-modeline-color ()
"Set mode line color based on current buffer's project"
(let ((color (project-color-background)))
(face-remap-add-relative 'tab-line-tab-current :background color :foreground "white")
(face-remap-add-relative 'mode-line-active :background color :foreground "white")
(face-remap-add-relative 'line-number-current-line :background color :foreground "white")
))
(add-hook 'find-file-hook #'amitp/set-modeline-color)
(add-hook 'dired-mode-hook #'amitp/set-modeline-color)
(add-hook 'change-major-mode-hook #'amitp/set-modeline-color)
(add-hook 'temp-buffer-setup-hook #'amitp/set-modeline-color)
(provide 'my-project-color)
;;; my-project-color.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment