-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcolor_schemes.ml
149 lines (135 loc) · 5.22 KB
/
color_schemes.ml
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
(*---------------------------------------------------------------------------
Copyright (c) 2013 The gg programmers. All rights reserved.
SPDX-License-Identifier: ISC
---------------------------------------------------------------------------*)
open Gg
open Gg_kit
open Vg
(* TODO code a proper Vz color scheme explorator with sliders etc. *)
let log fmt = Format.printf (fmt ^^ "@.")
(* Color schemes to display. *)
let schemes =
let h = Float.rad_of_deg in
[ `C (true, Color_scheme.sequential_wijffelaars ~w:0. ~h:(h 255.) (), 9.);
`D (true,
Color_scheme.sequential_wijffelaars' ~w:0. ~h:(h 255.) ~size:9 (), 9);
`D (true,
Color_scheme.sequential_wijffelaars' ~w:0.15 ~h:(h 255.) ~size:9 (), 9);
`D (true,
Color_scheme.sequential_wijffelaars' ~w:0.9 ~h:(h 255.) ~size:9 (), 9);
`D (true,
Color_scheme.sequential_wijffelaars' ~w:0.5 ~h:(h 10.) ~size:9 (), 9);
`D (true,
Color_scheme.sequential_wijffelaars' ~h:(h 10.) ~size:9 (), 9);
`D (true,
Color_scheme.sequential_wijffelaars'
~s:0.5 ~b:1. ~h:(h 255.) ~size:9 (), 9);
`Blank;
`C (true, Color_scheme.sequential_turbo (), 9.);
`C (true, Color_scheme.sequential_magma (), 9.);
`C (true, Color_scheme.sequential_inferno (), 9.);
`C (true, Color_scheme.sequential_plasma (), 9.);
`C (true, Color_scheme.sequential_viridis (), 9.);
`Blank;
`C (true,
Color_scheme.diverging_wijffelaars ~w:0.15 ~h0:(h 255.) ~h1:(h 10.) (),
9.);
`D (true,
Color_scheme.diverging_wijffelaars'
~w:0.15 ~h0:(h 255.) ~h1:(h 10.) ~size:9 (), 9);
`D (true,
Color_scheme.diverging_wijffelaars'
~m:0.6 ~w:0.15 ~h0:(h 255.) ~h1:(h 10.) ~size:9 (), 9);
`C (true,
Color_scheme.diverging_wijffelaars
~m:0.6 ~w:0.15 ~h0:(h 255.) ~h1:(h 10.) (), 9.);
`D (true,
Color_scheme.diverging_wijffelaars'
~w:0.15 ~h0:(h 255.) ~h1:(h 10.) ~size:8 (), 8);
`D (true,
Color_scheme.diverging_wijffelaars'
~w:0. ~h0:(h 255.) ~h1:(h 10.) ~size:9 (), 9);
`D (true,
Color_scheme.diverging_wijffelaars'
~w:1. ~s:0.7 ~h0:(h 120.) ~h1:(h 10.) ~size:9 (), 9);
`D (true,
Color_scheme.diverging_wijffelaars'
~h0:(h 255.) ~h1:(h 10.) ~s:0.3 ~c:0.9 ~b:0.5 ~size:6 (), 6);
`Blank;
`C (true, Color_scheme.cyclic_sinebow (), 9.);
`Blank;
`D (false, Color_scheme.qualitative `Brewer_accent_8 (), 8);
`D (false, Color_scheme.qualitative `Brewer_dark2_8 (), 8);
`D (false, Color_scheme.qualitative `Brewer_paired_12 (), 12);
`D (false, Color_scheme.qualitative `Brewer_pastel1_9 (), 9);
`D (false, Color_scheme.qualitative `Brewer_pastel2_8 (), 8);
`D (false, Color_scheme.qualitative `Brewer_set1_9 (), 9);
`D (false, Color_scheme.qualitative `Brewer_set2_8 (), 8);
`D (false, Color_scheme.qualitative `Brewer_set3_12 (), 12);
`D (false, Color_scheme.qualitative `Tableau_10 (), 10);
`D (false, Color_scheme.qualitative `Wijffelaars_17 (), 17);
`D (false, Color_scheme.qualitative_wijffelaars ~size:8 (), 8);
`D (false, Color_scheme.qualitative_wijffelaars ~c:0.8 ~size:8 (), 8); ]
(* Continuous and discrete color schemes as images *)
let range ?(min = 0.) ?(max = 1.) dt f acc =
let n = truncate (((max -. min) /. dt) +. 1.) in
let maxi = n - 1 in
let rec loop acc i =
if i < maxi then loop (f acc (min +. (float i) *. dt)) (i + 1) else
f acc max
in
loop (f acc min) 1
let colors_continuous ?(rev = false) cs len =
(* scheme sampled every 0.05 unit of len *)
let dt = 1. /. (floor ((len /. 0.05) +. 1.)) in
let add_sample acc t = ((if rev then 1. -. t else t), cs t) :: acc in
let stops =
let stops = range dt add_sample [] in
if rev then stops else List.rev stops
in
let bounds = P.empty |> P.rect (Box2.v P2.o (Size2.v 1. len)) in
I.axial stops P2.o (P2.v 0. len) |> I.cut bounds
let colors_discrete ?(rev = false) cs n =
let cs = Array.init n cs in
let sq =
let sq = Box2.v P2.o (Size2.v 1. 1.01 (* overlap *)) in
P.empty |> P.rect sq
in
let bounds =
let n = Array.length cs in
P.empty |> P.rect (Box2.v P2.o (Size2.v 1. (float n)))
in
let mv = P2.v 0. 1.0 in
let add acc c = acc |> I.move mv |> I.blend (I.const c |> I.cut sq) in
let colors =
if rev then Array.fold_left add I.void cs else
Array.fold_right (fun c acc -> add acc c) cs I.void
in
colors |> I.cut bounds (* cut topmost overlap *)
let size = Size2.v 300. 100. (* mm *)
let view = Box2.v P2.o (Size2.v 60. 20.)
let image =
let add scheme acc =
let i = match scheme with
| `D (rev, cs, n) -> colors_discrete ~rev cs n
| `C (rev, cs, n) -> colors_continuous ~rev cs n
| `Blank -> I.void
in
acc |> I.move (P2.v 1.5 0.0) |> I.blend i
in
List.fold_right add schemes I.void
|> I.scale (V2.v 1. (-1.)) |> I.move (V2.v 0. 20.)
(* Browser bureaucracy. *)
open Brr
open Brr_canvas
let main () =
let body = (Document.body G.document) in
let c = Canvas.create [] in
let () = El.append_children body [Canvas.to_el c] in
let r =
let t = Vgr_htmlc.target (* ~resize:true *) (Obj.magic c) in
Vg.Vgr.create t `Other
in
assert(Vgr.render r (`Image (size, view, image)) = `Ok);
()
let () = main ()