-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcommon.jl
637 lines (563 loc) · 19 KB
/
common.jl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
const BLANK = 0x0020
const BLANK_BRAILLE = 0x2800
const FULL_BRAILLE = 0x28ff
#! format: off
const BORDER_SOLID = (
tl = '┌',
tr = '┐',
bl = '└',
br = '┘',
t = '─',
l = '│',
b = '─',
r = '│',
)
const BORDER_CORNERS = (
tl = '┌',
tr = '┐',
bl = '└',
br = '┘',
t = ' ',
l = ' ',
b = ' ',
r = ' ',
)
const BORDER_BARPLOT = (
tl = '┌',
tr = '┐',
bl = '└',
br = '┘',
t = ' ',
l = '┤',
b = ' ',
r = ' ',
)
const BORDER_BOLD = (
tl = '┏',
tr = '┓',
bl = '┗',
br = '┛',
t = '━',
l = '┃',
b = '━',
r = '┃',
)
const BORDER_NONE = (
tl = ' ',
tr = ' ',
bl = ' ',
br = ' ',
t = ' ',
l = ' ',
b = ' ',
r = ' ',
)
const BORDER_BNONE = (
tl = Char(BLANK_BRAILLE),
tr = Char(BLANK_BRAILLE),
bl = Char(BLANK_BRAILLE),
br = Char(BLANK_BRAILLE),
t = Char(BLANK_BRAILLE),
l = Char(BLANK_BRAILLE),
b = Char(BLANK_BRAILLE),
r = Char(BLANK_BRAILLE),
)
const BORDER_DASHED = (
tl = '┌',
tr = '┐',
bl = '└',
br = '┘',
t = '╌',
l = '┊',
b = '╌',
r = '┊',
)
const BORDER_DOTTED = (
tl = '⡤',
tr = '⢤',
bl = '⠓',
br = '⠚',
t = '⠤',
l = '⡇',
b = '⠒',
r = '⢸',
)
const BORDER_ASCII = (
tl = '+',
tr = '+',
bl = '+',
br = '+',
t = '-',
l = '|',
b = '-',
r = '|',
)
const BORDERMAP = (
solid = BORDER_SOLID,
corners = BORDER_CORNERS,
barplot = BORDER_BARPLOT,
bold = BORDER_BOLD,
none = BORDER_NONE,
bnone = BORDER_BNONE,
dashed = BORDER_DASHED,
dotted = BORDER_DOTTED,
ascii = BORDER_ASCII,
)
#! format: on
const MARKERS = (
circle = '⚬',
rect = '▫',
diamond = '◇',
hexagon = '⬡',
cross = '✚',
xcross = '✖',
utriangle = '△',
dtriangle = '▽',
rtriangle = '▷',
ltriangle = '◁',
pentagon = '⬠',
star4 = '✦',
star5 = '★',
star6 = '✶',
star8 = '✴',
vline = '|',
hline = '―',
(+) = '+',
(x) = '⨯',
)
const SUPERSCRIPT = Dict(
# '.' => '‧', # U+2027: Hyphenation Point
# '.' => '˙', # U+02D9: Dot Above
# '.' => '⸳', # U+2E33: Raised Dot
# '.' => '⋅', # U+22C5: Dot Operator
# '.' => '·', # U+00B7: Middle Dot
'.' => '⸱', # U+2E31: Word Separator Middle Dot
'-' => '⁻',
'+' => '⁺',
'0' => '⁰',
'1' => '¹',
'2' => '²',
'3' => '³',
'4' => '⁴',
'5' => '⁵',
'6' => '⁶',
'7' => '⁷',
'8' => '⁸',
'9' => '⁹',
'e' => 'ᵉ',
)
############################################################################################
# define types
const MarkerType = Union{Symbol,AbstractChar,AbstractString}
const CrayonColorType = Union{Integer,Symbol,NTuple{3,Integer},Nothing}
const UserColorType = Union{Crayon,CrayonColorType} # allowed color type
const ColorType = UInt32 # internal UnicodePlots color type (on canvas), 8bit or 24bit
############################################################################################
# unicode table
# en.wikipedia.org/wiki/Plane_(Unicode)
const PLANE0_START = 0x00000
const PLANE0_STOP = 0x0ffff
const PLANE1_START = 0x10000
const PLANE1_STOP = 0x1ffff
const PLANE2_START = 0x20000
const PLANE2_STOP = 0x2ffff
# TODO: maybe later support plane 1 (SMP) and plane 2 (CJK) (needs UInt16 -> UInt32 grid change)
const UNICODE_TABLE = Array{Char}(undef, (PLANE0_STOP - PLANE0_START + 1) + length(MARKERS))
for i ∈ PLANE0_START:PLANE0_STOP
UNICODE_TABLE[i + 1] = Char(i)
end
for (j, i) ∈ enumerate(PLANE1_START:(PLANE1_START + (length(MARKERS) - 1)))
UNICODE_TABLE[i + 1] = MARKERS[j]
end
# `UInt32` is enough to hold all values of `UNICODE_TABLE`:
# common supertype for `DotCanvas`, `AsciiCanvas`, `BlockCanvas` `HeatmapCanvas` and `BrailleCanvas`.
const UnicodeType = UInt32
############################################################################################
# colors
const THRESHOLD = UInt32(256^3) # 8bit - 24bit threshold
const COLORMODE = Ref(Crayons.COLORS_256)
const INVALID_COLOR = typemax(ColorType)
const USE_LUT = Ref(false)
const CRAYONS_FAST = Ref(true)
const CRAYONS_EMPTY_STYLES = Tuple(Crayons.ANSIStyle() for _ ∈ 1:9)
const CRAYONS_RESET = Crayons.CSI * "0" * Crayons.END_ANSI
const COLOR_CYCLE_FAINT = :green, :blue, :red, :magenta, :yellow, :cyan
const COLOR_CYCLE_BRIGHT = map(s -> Symbol("light_", s), COLOR_CYCLE_FAINT) # COV_EXCL_LINE
const COLOR_CYCLE = Ref(COLOR_CYCLE_FAINT)
const BORDER_COLOR = Ref(:dark_gray)
############################################################################################
# misc
const FSCALES = (identity = identity, ln = log, log2 = log2, log10 = log10) # forward
const ISCALES = (identity = identity, ln = exp, log2 = exp2, log10 = exp10) # inverse
const BASES = (identity = nothing, ln = "ℯ", log2 = "2", log10 = "10")
const FULL_BLOCK = '█'
const HALF_BLOCK = '▄'
# standard terminals seem to respect a 4:3 aspect ratio
# unix.stackexchange.com/questions/148569/standard-terminal-font-aspect-ratio
# retrocomputing.stackexchange.com/questions/5629/why-did-80x25-become-the-text-monitor-standard
const ASPECT_RATIO = Ref(4 / 3)
# default display size for the default BrailleCanvas (which has aspect ratio = 2) ==> (40, 15)
const DEFAULT_HEIGHT = Ref(15)
const DEFAULT_WIDTH = Ref(round(Int, DEFAULT_HEIGHT[] * 2ASPECT_RATIO[]))
colormode() =
if (cm = COLORMODE[]) ≡ Crayons.COLORS_256
8
elseif cm ≡ Crayons.COLORS_24BIT
24
else
throw(ArgumentError("color mode $cm is unsupported"))
end
colors256!() = COLORMODE[] = Crayons.COLORS_256
truecolors!() = COLORMODE[] = Crayons.COLORS_24BIT
function colormode!(mode)
if mode ≡ 8
colors256!()
elseif mode ≡ 24
truecolors!()
else
throw(ArgumentError("color mode $mode is unsupported, choose 8 or 24"))
end
nothing
end
brightcolors!() = COLOR_CYCLE[] = COLOR_CYCLE_BRIGHT
faintcolors!() = COLOR_CYCLE[] = COLOR_CYCLE_FAINT
# see gist.github.com/XVilka/8346728#checking-for-colorterm
terminal_24bit() = lowercase(get(ENV, "COLORTERM", "")) ∈ ("24bit", "truecolor")
# specific to UnicodePlots
forced_24bit() = lowercase(get(ENV, "UP_COLORMODE", "")) ∈ ("24", "24bit", "truecolor")
forced_8bit() = lowercase(get(ENV, "UP_COLORMODE", "")) ∈ ("8", "8bit")
"""
default_size!(;
height::Union{Integer,Nothing} = nothing,
width::Union{Integer,Nothing} = nothing,
)
Change and return the default plot size (height, width).
"""
function default_size!(;
height::Union{Integer,Nothing} = nothing,
width::Union{Integer,Nothing} = nothing,
)
@assert height ≡ nothing || width ≡ nothing
if height ≢ nothing
DEFAULT_HEIGHT[] = height
DEFAULT_WIDTH[] = round(Int, height * 2ASPECT_RATIO[])
elseif width ≢ nothing
DEFAULT_WIDTH[] = width
DEFAULT_HEIGHT[] = round(Int, width / 2ASPECT_RATIO[])
else
default_size!(height = 15)
end
DEFAULT_HEIGHT[], DEFAULT_WIDTH[] # `displaysize` order convention
end
function char_marker(marker::MarkerType)::Char
if marker isa Symbol
get(MARKERS, marker, MARKERS[:circle])
else
length(marker) ≡ 1 || throw(ArgumentError("`marker` keyword has a non unit length"))
first(marker)
end
end
iterable(obj::AbstractVecOrMat) = obj
iterable(obj) = Iterators.repeated(obj)
function transform_name(tr, basename = "")
name = string(tr isa Union{Symbol,Function} ? tr : typeof(tr))
name == "identity" && return basename
name = occursin('#', name) ? "custom" : name
string(basename, " [", name, "]")
end
meshgrid(x, y) = repeat(x, 1, length(y)), repeat(y', length(x), 1)
as_float(x::AbstractVector{<:AbstractFloat}) = x
as_float(x) = float.(x)
roundable(x::Number) = isinteger(x) && (typemin(Int32) ≤ x ≤ typemax(Int32))
compact_repr(x::Number) = repr(x; context = :compact => true)
function default_formatter(kw)
unicode_exponent = get(kw, :unicode_exponent, PLOT_KEYWORDS.unicode_exponent)
thousands_separator = get(kw, :thousands_separator, PLOT_KEYWORDS.thousands_separator)
x -> nice_repr(x, unicode_exponent, thousands_separator)
end
nice_repr(x::Number, plot) = nice_repr(x, plot.unicode_exponent, plot.thousands_separator)
nice_repr(x::Number, _::Nothing) =
nice_repr(x, PLOT_KEYWORDS.unicode_exponent, PLOT_KEYWORDS.thousands_separator)
function nice_repr(x::Integer, ::Bool, thousands_separator::Char)::String
thousands_separator == '\0' && return string(x)
xs = collect(reverse(string(abs(x))))
n = length(xs)
v = sizehint!(Char[], n + 10)
for (i, c) ∈ enumerate(xs)
push!(v, c)
(i < n && mod1(i, 3) == 3) && push!(v, thousands_separator)
end
reverse!(v)
(sign(x) ≥ 0 ? "" : "-") * String(v)
end
function nice_repr(
x::AbstractFloat,
unicode_exponent::Bool,
thousands_separator::Char,
)::String
xr = (pseudo_int = roundable(x)) ? round(Int, x, RoundNearestTiesUp) : x
iszero(xr) && return "0"
str = compact_repr(xr)
if (parts = split(str, 'e')) |> length == 2 # e.g. 1.0e-17 => 1e⁻¹⁷
left, right = parts
str = *(
replace(left, r"\.0$" => ""),
'e',
unicode_exponent ? superscript(right) : String(right),
)
elseif pseudo_int
str = nice_repr(xr, unicode_exponent, thousands_separator)
end
str
end
# workaround for github.com/JuliaMath/NaNMath.jl/issues/26
nanless_extrema(x) = any(isnan, x) ? NaNMath.extrema(x) : extrema(x)
function ceil_neg_log10(x)
val = -log10(x)
isfinite(val) || return typemin(Int)
roundable(val) ? ceil(Int, val) : floor(Int, val)
end
round_up_subtick(x::T, m) where {T} = T(x == 0 ? 0 : if x > 0
ceil(x; digits = ceil_neg_log10(m) + 1)
else
-floor(-x; digits = ceil_neg_log10(m) + 1)
end)
round_down_subtick(x::T, m) where {T} = T(x == 0 ? 0 : if x > 0
floor(x; digits = ceil_neg_log10(m) + 1)
else
-ceil(-x; digits = ceil_neg_log10(m) + 1)
end)
float_round_log10(x::Integer, m) = float_round_log10(float(x), m)
float_round_log10(x) = x > 0 ? float_round_log10(x, x) : float_round_log10(x, -x)
float_round_log10(x::T, m) where {T<:AbstractFloat} = T(x == 0 ? 0 : if x > 0
+round(+x; digits = ceil_neg_log10(m) + 1)
else
-round(-x; digits = ceil_neg_log10(m) + 1)
end)
floor_base(x, b) = round_base(x, b, RoundDown)
ceil_base(x, b) = round_base(x, b, RoundUp)
round_base(x::T, b, ::RoundingMode{:Down}) where {T} = T(b^floor(log(b, x)))
round_base(x::T, b, ::RoundingMode{:Up}) where {T} = T(b^ceil(log(b, x)))
function unit_str(x, fancy)
io = IOContext(PipeBuffer(), :fancy_exponent => fancy)
show(io, unit(x))
read(io, String)
end
number_unit(x::AbstractVector{<:Quantity}, fancy = true) =
ustrip.(x), unit_str(first(x), fancy)
number_unit(x::Quantity, fancy = true) = ustrip(x), unit_str(x, fancy)
number_unit(x::AbstractVector, args...) = x, nothing
number_unit(x::Number, args...) = x, nothing
unit_label(label::AbstractString, unit::AbstractString) =
(lab_strip = rstrip(label)) |> isempty ? unit : "$lab_strip ($unit)"
unit_label(label::AbstractString, unit::Nothing) = rstrip(label)
function superscript(s::AbstractString)::String
v = collect(s)
for (i, k) ∈ enumerate(v)
v[i] = get(SUPERSCRIPT, k, k)
end
String(v)
end
function plotting_range_narrow(xmin, xmax)
Δ = xmax - xmin
if iszero(Δ) || !isfinite(Δ)
Δ = Float64(xmax) - Float64(xmin) # NOTE: support e.g. xmin == -Inf32 | xmax == Inf32
if iszero(Δ) || !isfinite(Δ)
@warn "Invalid plotting range" xmin xmax
return -Inf, +Inf
end
end
float(round_down_subtick(xmin, Δ)), float(round_up_subtick(xmax, Δ))
end
is_auto(lims) = all(iszero, lims)
# NOTE: we need `SVector{2}(v)` for compiler inferrability, not `Svector(v)` !
autolims(lims) = is_auto(lims) ? SVector(-1.0, 1.0) : SVector{2}(as_float(lims))
autolims(lims, vec::AbstractVector) =
is_auto(lims) && length(vec) > 0 ? SVector{2}(extrema(vec)) : SVector{2}(as_float(lims))
scale_callback(scale::Symbol) = FSCALES[scale]
scale_callback(scale::Function) = scale
extend_limits(vec, lims) = extend_limits(vec, lims, :identity)
function extend_limits(vec, lims, scale::Union{Symbol,Function})
scale = scale_callback(scale)
mi, ma = as_float(extrema(lims))
if mi == ma == 0
mi, ma = as_float(extrema(vec))
end
if mi == ma
mi -= 1
ma += 1
end
if scale == identity
all(iszero.(lims)) ? plotting_range_narrow(mi, ma) : (mi, ma)
else
scale(mi), scale(ma)
end
end
sort_by_keys(dict::Dict) = sort!(collect(dict), by = first)
function sorted_keys_values(dict::Dict; k2s = true)
if k2s # check and force key type to be of AbstractString type if necessary
kt, vt = eltype(dict).types
if !(kt <: AbstractString)
dict = Dict(string(k) => v for (k, v) ∈ pairs(dict))
end
end
keys_vals = sort_by_keys(dict)
first.(keys_vals), last.(keys_vals)
end
crayon_color(::Union{Missing,Nothing}) = Crayons.ANSIColor()
crayon_color(color::ColorType) =
if color ≡ INVALID_COLOR
Crayons.ANSIColor()
elseif color < THRESHOLD # 24bit
Crayons.ANSIColor(red(color), grn(color), blu(color), Crayons.COLORS_24BIT)
else # 8bit
Crayons.ANSIColor(color - THRESHOLD, Crayons.COLORS_256)
end
function print_crayons(io, c, args...)
if CRAYONS_FAST[]
if Crayons.anyactive(c) # bypass crayons checks (_have_color, _force_color)
print(io, Crayons.CSI)
Crayons._print(io, c)
print(io, Crayons.END_ANSI, args..., CRAYONS_RESET)
else
print(io, args...)
end
else
print(io, c, args..., CRAYONS_RESET)
end
end
print_color(io::IO, color::Crayon, args...) = print_crayons(io, color, args...)
print_color(io::IO, color::UserColorType, args...) =
print_color(io, ansi_color(color), args...)
function print_color(io::IO, color::ColorType, args...; bgcol = missing)
if get(io, :color, false)
print_crayons(
io,
Crayon(crayon_color(color), crayon_color(bgcol), CRAYONS_EMPTY_STYLES...),
args...,
)
else
print(io, args...)
end
nothing
end
@inline r32(r::Integer)::UInt32 = (r & 0xffffff) << 16
@inline g32(g::Integer)::UInt32 = (g & 0xffffff) << 8
@inline b32(b::Integer)::UInt32 = (b & 0xffffff)
@inline red(c::UInt32)::UInt8 = (c >> 16) & 0xff
@inline grn(c::UInt32)::UInt8 = (c >> 8) & 0xff
@inline blu(c::UInt32)::UInt8 = c & 0xff
@inline blend_colors(a::UInt32, b::UInt32)::UInt32 = if a ≡ b
a # fastpath
elseif a < THRESHOLD && b < THRESHOLD # 24bit
# physical average (UInt32 to prevent UInt8 overflow)
r32(floor(UInt32, √((UInt32(red(a))^2 + UInt32(red(b))^2) / 2))) +
g32(floor(UInt32, √((UInt32(grn(a))^2 + UInt32(grn(b))^2) / 2))) +
b32(floor(UInt32, √((UInt32(blu(a))^2 + UInt32(blu(b))^2) / 2)))
# binary or
# r32(red(a) | red(b)) + g32(grn(a) | grn(b)) + b32(blu(a) | blu(b))
elseif THRESHOLD ≤ a < INVALID_COLOR && THRESHOLD ≤ b < INVALID_COLOR # 8bit
THRESHOLD + (UInt8(a - THRESHOLD) | UInt8(b - THRESHOLD))
elseif a ≢ INVALID_COLOR && b ≢ INVALID_COLOR
max(a, b)
else
INVALID_COLOR
end::UInt32
ignored_color(color::Symbol) = color ≡ :normal || color ≡ :default || color ≡ :nothing
ignored_color(::Nothing) = true
ignored_color(::Any) = false
no_ansi_escape(str::AbstractString) = replace(str, r"\e\[[0-9;]*[a-zA-Z]" => "")
ansi_4bit_to_8bit(c::UInt8) =
let (q, r) = divrem(c, 0x3c) # UInt8(60)
UInt8(r + (q > 0x0 ? 0x8 : 0x0))
end
c256(c::AbstractFloat) = round(UInt32, 255c)
c256(c::Integer) = c
# `ColorType` conversion - colormaps
ansi_color(rgb::AbstractRGB) = ansi_color((c256(rgb.r), c256(rgb.g), c256(rgb.b)))
ansi_color(rgb::NTuple{3,AbstractFloat}) = ansi_color(c256.(rgb))
ansi_color(color::ColorType)::ColorType = color # no-op
ansi_color(crayon::Crayon) = ansi_color(crayon.fg) # ignore bg & styles
ansi_color(::Missing) = INVALID_COLOR # not a CrayonColorType
function ansi_color(color::CrayonColorType)::ColorType
ignored_color(color) && return INVALID_COLOR
ansi_color(Crayons._parse_color(color))
end
ansi_color(c::Crayons.ANSIColor) = if COLORMODE[] ≡ Crayons.COLORS_24BIT
if c.style ≡ Crayons.COLORS_24BIT
r32(c.r) + g32(c.g) + b32(c.b)
elseif c.style ≡ Crayons.COLORS_256
USE_LUT[] ? LUT_8BIT[c.r + 1] : THRESHOLD + c.r
elseif c.style ≡ Crayons.COLORS_16
c8 = ansi_4bit_to_8bit(c.r)
USE_LUT[] ? LUT_8BIT[c8 + 1] : THRESHOLD + c8
end::ColorType
else # 0-255 ansi stored in a UInt32
THRESHOLD + if c.style ≡ Crayons.COLORS_24BIT
Crayons.to_256_colors(c).r
elseif c.style ≡ Crayons.COLORS_256
c.r
elseif c.style ≡ Crayons.COLORS_16
ansi_4bit_to_8bit(c.r)
end::UInt8
end::ColorType
complement(color::UserColorType)::ColorType = complement(ansi_color(color))
complement(color::ColorType)::ColorType = if color ≡ INVALID_COLOR
INVALID_COLOR
elseif color < THRESHOLD
r32(~red(color)) + g32(~grn(color)) + b32(~blu(color))
elseif color ≢ INVALID_COLOR
THRESHOLD + ~UInt8(color - THRESHOLD)
end::ColorType
out_stream_size(out_stream::Nothing) = displaysize()
out_stream_size(out_stream::IO) = displaysize(out_stream)
out_stream_height(out_stream::Union{Nothing,IO} = nothing) =
out_stream |> out_stream_size |> first
out_stream_width(out_stream::Union{Nothing,IO} = nothing) =
out_stream |> out_stream_size |> last
multiple_series_defaults(y::AbstractMatrix, kw, start) = map(
iterable,
(
get(kw, :name, map(i -> "y$i", start:(start + size(y, 2)))),
get(kw, :color, :auto),
get(kw, :marker, :pixel),
),
)
function colormap_callback(cmap::Symbol)
cdata = ColorSchemes.colorschemes[cmap]
(z, minz, maxz) -> begin
isfinite(z) || return INVALID_COLOR
get(
cdata,
minz == maxz ? zero(z) : (max(minz, min(z, maxz)) - minz) / (maxz - minz),
) |> ansi_color
end::ColorType
end
function colormap_callback(cmap::AbstractVector)
(z, minz, maxz) -> begin
isfinite(z) || return INVALID_COLOR
i = if minz == maxz || z < minz
1
elseif z > maxz
length(cmap)
else
1 + round(Int, ((z - minz) / (maxz - minz)) * (length(cmap) - 1))
end::Int
ansi_color(cmap[i])
end::ColorType
end
colormap_callback(cmap::Nothing) = x -> nothing
colormap_callback(cmap::Function) = cmap
mutable struct ColorMap
border::Symbol
bar::Bool
lim::NTuple{2,Number}
callback::Function
end
split_plot_kw(kw) =
if isempty(kw)
pairs((;)), pairs((;)) # avoids `filter` allocations
else
filter(p -> p.first ∈ PLOT_KEYS, kw), filter(p -> p.first ∉ PLOT_KEYS, kw)
end
warn_on_lost_kw(kw) = (isempty(kw) || @warn "keyword(s) `$kw` will be lost"; nothing)