Improve color handling

This commit is contained in:
Dimitri Lozeve 2021-04-29 20:04:12 +02:00
parent 516237d2bc
commit 46d5d0a90d
5 changed files with 23 additions and 18 deletions

View file

@ -4,6 +4,9 @@ Unicode plotting library for [[https://cons.io/][Gerbil Scheme]].
Depends on [[https://github.com/dlozeve/fancy][Fancy]] for colors and display. Depends on [[https://github.com/dlozeve/fancy][Fancy]] for colors and display.
#+ATTR_HTML: :width 100% :style margin-left: auto; margin-right: auto;
[[./demo.png]]
** Features ** Features
- Braille canvas for plotting - Braille canvas for plotting

BIN
demo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View file

@ -9,5 +9,6 @@
(def (main . args) (def (main . args)
(let* ((xs (iota 1000 0 0.01)) (let* ((xs (iota 1000 0 0.01))
(ys1 (map cos xs)) (ys1 (map cos xs))
(ys2 (map sin xs))) (ys2 (map (lambda (x) (/ 1 (+ 0.5 x))) xs))
(displayln (line-plot [xs ys1 ys2])))) (ys3 (map (lambda (x) (sin (* 2 x))) xs)))
(displayln (line-plot [xs ys1 ys2 ys3]))))

View file

@ -44,7 +44,7 @@
(def (indexf pred . lsts) (def (indexf pred . lsts)
(find pred (apply map list lsts))) (find pred (apply map list lsts)))
(def (canvases->string canvases (colors '(red green cyan yellow magenta white))) (def (canvases->string canvases colors)
(def canvases-str (map canvas->string canvases)) (def canvases-str (map canvas->string canvases))
(def size (string-length (car canvases-str))) (def size (string-length (car canvases-str)))
(apply str (apply str

View file

@ -6,6 +6,8 @@
:dlozeve/uniplot/braille :dlozeve/uniplot/braille
:dlozeve/fancy/format) :dlozeve/fancy/format)
(def +default-colors+ '(green blue red yellow cyan magenta white))
(def (scale-fn lo hi) (def (scale-fn lo hi)
(lambda (x) (/ (- x lo) (- hi lo)))) (lambda (x) (/ (- x lo) (- hi lo))))
@ -18,20 +20,18 @@
#t)) #t))
canvas) canvas)
(def (line-plot lsts width: (width 160) height: (height 80)) (def (line-plot lsts (colors +default-colors+) width: (width 160) height: (height 80))
(match lsts (canvases->string
([ys] (canvas->string (match lsts
(draw-canvas (iota (length ys)) ys ([ys] [(draw-canvas (iota (length ys)) ys
(scale-fn 0 (length ys)) (scale-fn 0 (length ys))
(scale-fn (apply min ys) (apply max ys)) (scale-fn (apply min ys) (apply max ys))
width: width height: height))) width: width height: height)])
([xs ys] (canvas->string ([xs ys] [(draw-canvas xs ys
(draw-canvas xs ys (scale-fn (apply min xs) (apply max xs))
(scale-fn (apply min xs) (apply max xs)) (scale-fn (apply min ys) (apply max ys))
(scale-fn (apply min ys) (apply max ys)) width: width height: height)])
width: width height: height))) ([xs . yss]
([xs . yss]
(canvases->string
(let* ((x-scale-fn (scale-fn (apply min xs) (apply max xs))) (let* ((x-scale-fn (scale-fn (apply min xs) (apply max xs)))
(all-ys (flatten yss)) (all-ys (flatten yss))
(min-ys (apply min all-ys)) (min-ys (apply min all-ys))
@ -39,4 +39,5 @@
(y-scale-fn (scale-fn min-ys max-ys))) (y-scale-fn (scale-fn min-ys max-ys)))
(for/collect ((ys yss)) (for/collect ((ys yss))
(draw-canvas xs ys x-scale-fn y-scale-fn (draw-canvas xs ys x-scale-fn y-scale-fn
width: width height: height))))))) width: width height: height)))))
colors))