Skip to content

Commit

Permalink
Use readable-foreground-color
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsius committed Sep 9, 2024
1 parent 2aa8cc4 commit 612eb42
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions lisp/forge-topic.el
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ can be selected from the start."
(format (if local
(pcase-lambda (`(,_id ,name ,color ,_description))
(let* ((background (forge--sanitize-color color))
(foreground (forge--contrast-color background)))
(foreground (readable-foreground-color background)))
(magit--propertize-face
name `(( :background ,background
:foreground ,foreground)
Expand Down Expand Up @@ -1020,7 +1020,7 @@ what subset of KIND is being listed."
(prog1 t
(pcase-dolist (`(,_id ,name ,color ,description) labels)
(let* ((background (forge--sanitize-color color))
(foreground (forge--contrast-color background)))
(foreground (readable-foreground-color background)))
(if separate (insert " ") (setq separate t))
(insert name)
(let ((o (make-overlay (- (point) (length name)) (point))))
Expand Down Expand Up @@ -1555,26 +1555,6 @@ This mode itself is never used directly."
((string-match-p "\\`#.\\{8\\}\\'" color) (substring color 0 6))
(t "#000000"))) ; Use fallback instead of invalid color.

(defun forge--contrast-color (color)
"Return black or white depending on the luminance of COLOR."
(if (> (forge--x-color-luminance color) 0.5) "black" "white"))

;; Copy of `rainbow-x-color-luminance'.
(defun forge--x-color-luminance (color)
"Calculate the luminance of a color string (e.g., \"#ffaa00\", \"blue\").
Return a value between 0 and 1."
(let ((values (color-values color)))
(forge--color-luminance (/ (nth 0 values) 256.0)
(/ (nth 1 values) 256.0)
(/ (nth 2 values) 256.0))))

;; Copy of `rainbow-color-luminance'.
;; Also see https://en.wikipedia.org/wiki/Relative_luminance.
(defun forge--color-luminance (red green blue)
"Calculate the luminance of color composed of RED, GREEN and BLUE.
Return a value between 0 and 1."
(/ (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))

;;; Markdown Utilities

(defun forge--fontify-markdown (text)
Expand Down

0 comments on commit 612eb42

Please sign in to comment.