From 807409d41f8b1a4435ee1cf7ddc3d1a1b9116799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 31 Jan 2022 20:24:18 +0100 Subject: Move stuff from calp/util to hnh/util. This is the first (major) step in splitting the generally useful items into its own library. --- module/hnh/util/color.scm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 module/hnh/util/color.scm (limited to 'module/hnh/util/color.scm') diff --git a/module/hnh/util/color.scm b/module/hnh/util/color.scm new file mode 100644 index 00000000..b626316d --- /dev/null +++ b/module/hnh/util/color.scm @@ -0,0 +1,22 @@ +(define-module (hnh util color) + ) + +;; Returns a color with good contrast to the given background color. +;; https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color/1855903#1855903 +(define-public (calculate-fg-color c) + (catch #t + (lambda () + (define (str->num c n) (string->number (substring/shared c n (+ n 2)) 16)) + ;; (format (current-error-port) "COLOR = ~s~%" c) + (let ((r (str->num c 1)) + (g (str->num c 3)) + (b (str->num c 5))) + (if (< 1/2 (/ (+ (* 0.299 r) + (* 0.587 g) + (* 0.114 b)) + #xFF)) + "#000000" "#FFFFFF"))) + (lambda args + (format (current-error-port) "Error calculating foreground color?~%~s~%" args) + "#FF0000" + ))) -- cgit v1.2.3