aboutsummaryrefslogtreecommitdiff
path: root/module/calp/html/util.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-02-02 02:36:43 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2022-02-02 02:36:43 +0100
commitcf692903b848d7b02a1966da671f5c653212a25c (patch)
treec2eadcfc96a0f533c09bf2fea30aebb900045104 /module/calp/html/util.scm
parentDocumentation of (hnh util ...) (diff)
downloadcalp-cf692903b848d7b02a1966da671f5c653212a25c.tar.gz
calp-cf692903b848d7b02a1966da671f5c653212a25c.tar.xz
Move calculate-fg-color into calp subtree.
While the algorithm was general, the packing of colors, along with the error handling made it to non-portable.
Diffstat (limited to 'module/calp/html/util.scm')
-rw-r--r--module/calp/html/util.scm20
1 files changed, 20 insertions, 0 deletions
diff --git a/module/calp/html/util.scm b/module/calp/html/util.scm
index ecb54198..75137d4e 100644
--- a/module/calp/html/util.scm
+++ b/module/calp/html/util.scm
@@ -13,3 +13,23 @@
(lambda (ev)
(or (id ev)
(set/r! (id ev) (symbol->string (gensym "__html_id_")))))))
+
+;; 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"
+ )))