aboutsummaryrefslogtreecommitdiff
path: root/module/calp/html/util.scm
blob: 54c92e92dee9154c6a7c84bdb50306515d234ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(define-module (calp html util)
  :use-module (hnh util))


(define-public (date-link date)
  ((@ (datetime) date->string) date "~Y-~m-~d"))


;; Generate an html id for an event.
;; TODO? same event placed multiple times, when spanning multiple cells
(define-public html-id
  (let ((id (make-object-property)))
    (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)
  ;; TODO what errors can actually appear here?
  (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"
      )))