From cf692903b848d7b02a1966da671f5c653212a25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 2 Feb 2022 02:36:43 +0100 Subject: 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. --- module/calp/html/util.scm | 20 ++++++++++++++++++++ module/calp/html/vcomponent.scm | 3 +-- module/hnh/util/color.scm | 22 ---------------------- 3 files changed, 21 insertions(+), 24 deletions(-) delete mode 100644 module/hnh/util/color.scm (limited to 'module') 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" + ))) diff --git a/module/calp/html/vcomponent.scm b/module/calp/html/vcomponent.scm index 4c1ebbb3..16f418c5 100644 --- a/module/calp/html/vcomponent.scm +++ b/module/calp/html/vcomponent.scm @@ -10,10 +10,9 @@ :use-module (datetime) :use-module ((text util) :select (add-enumeration-punctuation)) :use-module ((web uri-query) :select (encode-query-parameters)) - :use-module (calp html util) + :use-module ((calp html util) :select (html-id calculate-fg-color)) :use-module ((calp html config) :select (edit-mode debug)) :use-module ((calp html components) :select (btn tabset form with-label)) - :use-module ((hnh util color) :select (calculate-fg-color)) :use-module ((crypto) :select (sha256 checksum->string)) :use-module ((xdg basedir) :prefix xdg-) :use-module ((vcomponent recurrence) :select (repeating?)) diff --git a/module/hnh/util/color.scm b/module/hnh/util/color.scm deleted file mode 100644 index b626316d..00000000 --- a/module/hnh/util/color.scm +++ /dev/null @@ -1,22 +0,0 @@ -(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