Skip to content

Instantly share code, notes, and snippets.

@alex-hhh
Created July 1, 2021 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-hhh/b224d2fc87b3bc094e0869a94dbbd76f to your computer and use it in GitHub Desktop.
Save alex-hhh/b224d2fc87b3bc094e0869a94dbbd76f to your computer and use it in GitHub Desktop.
(define (make-grade-color-legend
cm
#:orientation (orientation 'horizontal)
#:length (length #f)
#:height (height 30)
#:invert? (invert? #f))
(define gap 1) ; gap between color tabs
(define color-count (color-map-size cm))
(define tab-length
(if length
(/ (- length (* gap (sub1 color-count))) color-count)
(* 1.62 height)))
(define-values (w h) (if (equal? orientation 'horizontal)
(values tab-length height)
(values height tab-length)))
(define color-tabs
(let* ([colors (color-map->list-of-colors cm)]
[tabs (for/list ([c (in-list colors)])
(filled-rectangle w h #:draw-border? #f #:color c))])
(if invert? (reverse tabs) tabs)))
(define labels
(let* ([label-count (sub1 color-count)]
[half-label-count (exact-floor (/ label-count 2))]
[half-labels (build-list half-label-count (lambda (n) (expt 2 n)))]
[negated (map (lambda (x) (- x)) (reverse half-labels))])
(if (even? label-count)
(append negated half-labels)
(append negated (list 0) half-labels))))
(define label-tabs
(for/list ([l (in-list labels)])
(cc-superimpose
(ghost (rectangle w h))
(text (format "~a%" (exact-floor l)) null (plot-font-size)))))
(unless (equal? orientation 'horizontal)
(set! color-tabs (reverse color-tabs))
(set! label-tabs (reverse label-tabs)))
(define tab-append
(if (equal? orientation 'horizontal) hc-append vc-append))
((if (equal? orientation 'horizontal) vl-append ht-append)
0
(apply tab-append gap color-tabs)
(apply tab-append gap (cons (ghost (rectangle (/ w 2) (/ h 2))) label-tabs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment