@@ -27,7 +27,15 @@ struct SimpleColor
27
27
end
28
28
29
29
SimpleColor (r:: Integer , g:: Integer , b:: Integer ) = SimpleColor ((; r= UInt8 (r), g= UInt8 (g), b= UInt8 (b)))
30
- SimpleColor (rgb:: UInt32 ) = SimpleColor (reverse (reinterpret (UInt8, [rgb]))[2 : end ]. .. )
30
+
31
+ function SimpleColor (rgb:: UInt32 )
32
+ b, g, r, _ = @static if VERSION >= v " 1.10"
33
+ reinterpret (UInt8, [htol (rgb)])
34
+ else
35
+ reinterpret (UInt8, [htol (rgb)])
36
+ end
37
+ SimpleColor (r, g, b)
38
+ end
31
39
32
40
Base. convert (:: Type{SimpleColor} , rgb:: RGBTuple ) = SimpleColor (rgb)
33
41
Base. convert (:: Type{SimpleColor} , namedcolor:: Symbol ) = SimpleColor (namedcolor)
@@ -477,7 +485,7 @@ function withfaces(f, keyvals_itr)
477
485
FACES. current[][name] = face
478
486
elseif face isa Symbol
479
487
FACES. current[][name] =
480
- something (get (old, face, nothing ), get (FACES. current[], face, Face () ))
488
+ something (get (old, face, nothing ), get (Face, FACES. current[], face))
481
489
elseif face isa Vector{Symbol}
482
490
FACES. current[][name] = Face (inherit= face)
483
491
elseif haskey (FACES. current[], name)
@@ -511,25 +519,30 @@ later faces taking priority.
511
519
"""
512
520
function Base. merge (a:: Face , b:: Face )
513
521
if isempty (b. inherit)
514
- Face (ifelse (isnothing (b. font), a. font, b. font),
515
- if isnothing (b. height) a. height
516
- elseif isnothing (a. height) b. height
517
- elseif b. height isa Int b. height
518
- elseif a. height isa Int round (Int, a. height * b. height)
519
- else a. height * b. height end ,
520
- ifelse (isnothing (b. weight), a. weight, b. weight),
521
- ifelse (isnothing (b. slant), a. slant, b. slant),
522
- ifelse (isnothing (b. foreground), a. foreground, b. foreground),
523
- ifelse (isnothing (b. background), a. background, b. background),
524
- ifelse (isnothing (b. underline), a. underline, b. underline),
525
- ifelse (isnothing (b. strikethrough), a. strikethrough, b. strikethrough),
526
- ifelse (isnothing (b. inverse), a. inverse, b. inverse),
522
+ # Extract the heights to help type inference a bit to be able
523
+ # to narrow the types in e.g. `aheight * bheight`
524
+ aheight = a. height
525
+ bheight = b. height
526
+ abheight = if isnothing (bheight) aheight
527
+ elseif isnothing (aheight) bheight
528
+ elseif bheight isa Int bheight
529
+ elseif aheight isa Int round (Int, aheight * bheight)
530
+ else aheight * bheight end
531
+ Face (if isnothing (b. font) a. font else b. font end ,
532
+ abheight,
533
+ if isnothing (b. weight) a. weight else b. weight end ,
534
+ if isnothing (b. slant) a. slant else b. slant end ,
535
+ if isnothing (b. foreground) a. foreground else b. foreground end ,
536
+ if isnothing (b. background) a. background else b. background end ,
537
+ if isnothing (b. underline) a. underline else b. underline end ,
538
+ if isnothing (b. strikethrough) a. strikethrough else b. strikethrough end ,
539
+ if isnothing (b. inverse) a. inverse else b. inverse end ,
527
540
a. inherit)
528
541
else
529
542
b_noinherit = Face (
530
543
b. font, b. height, b. weight, b. slant, b. foreground, b. background,
531
544
b. underline, b. strikethrough, b. inverse, Symbol[])
532
- b_inheritance = map (fname -> get (FACES. current[], fname, Face () ), Iterators. reverse (b. inherit))
545
+ b_inheritance = map (fname -> get (Face, FACES. current[], fname), Iterators. reverse (b. inherit))
533
546
b_resolved = merge (foldl (merge, b_inheritance), b_noinherit)
534
547
merge (a, b_resolved)
535
548
end
@@ -539,6 +552,11 @@ Base.merge(a::Face, b::Face, others::Face...) = merge(merge(a, b), others...)
539
552
540
553
# # Getting the combined face from a set of properties ##
541
554
555
+ # Putting these inside `getface` causes the julia compiler to box it
556
+ _mergedface (face:: Face ) = face
557
+ _mergedface (face:: Symbol ) = get (Face, FACES. current[], face)
558
+ _mergedface (faces:: Vector ) = mapfoldl (_mergedface, merge, Iterators. reverse (faces))
559
+
542
560
"""
543
561
getface(faces)
544
562
@@ -547,10 +565,7 @@ Obtain the final merged face from `faces`, an iterator of
547
565
"""
548
566
function getface (faces)
549
567
isempty (faces) && return FACES. current[][:default ]
550
- mergedface (face:: Face ) = face
551
- mergedface (face:: Symbol ) = get (FACES. current[], face, Face ())
552
- mergedface (faces:: Vector ) = mapfoldl (mergedface, merge, Iterators. reverse (faces))
553
- combined = mapfoldl (mergedface, merge, faces):: Face
568
+ combined = mapfoldl (_mergedface, merge, faces):: Face
554
569
if ! isempty (combined. inherit)
555
570
combined = merge (Face (), combined)
556
571
end
@@ -568,7 +583,7 @@ function getface(annotations::Vector{Pair{Symbol, Any}})
568
583
end
569
584
570
585
getface (face:: Face ) = merge (FACES. current[][:default ], merge (Face (), face))
571
- getface (face:: Symbol ) = getface (get (FACES. current[], face, Face () ))
586
+ getface (face:: Symbol ) = getface (get (Face, FACES. current[], face))
572
587
573
588
"""
574
589
getface()
0 commit comments