forked from akinomyoga/ble.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.sh
1940 lines (1813 loc) · 62.2 KB
/
color.sh
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# gflags
_ble_color_gflags_Bold=0x01
_ble_color_gflags_Italic=0x02
_ble_color_gflags_Underline=0x04
_ble_color_gflags_Revert=0x08
_ble_color_gflags_Invisible=0x10
_ble_color_gflags_Strike=0x20
_ble_color_gflags_Blink=0x40
_ble_color_gflags_DecorationMask=0x77
_ble_color_gflags_FgMask=0x00000000FFFFFF00
_ble_color_gflags_BgMask=0x00FFFFFF00000000
_ble_color_gflags_FgIndexed=0x0100000000000000
_ble_color_gflags_BgIndexed=0x0200000000000000
_ble_color_index_colors_default=$_ble_term_colors
if [[ $TERM == xterm* || $TERM == *-256color || $TERM == kterm* ]]; then
_ble_color_index_colors_default=256
elif [[ $TERM == *-88color ]]; then
_ble_color_index_colors_default=88
fi
bleopt/declare -v term_true_colors semicolon
bleopt/declare -v term_index_colors auto
function bleopt/check:term_true_colors {
ble/color/g2sgr/.clear-cache
return 0
}
function bleopt/check:term_index_colors {
ble/color/g2sgr/.clear-cache
return 0
}
function ble/color/initialize-term-colors {
local fields
ble/string#split fields \; "$_ble_term_DA2R"
if [[ $bleopt_term_true_colors == auto ]]; then
# truecolor support 自動判定 (暫定実装)
local value=
if [[ $TERM == *-24bit || $TERM == *-direct ]]; then
value=colon
elif [[ $TERM == *-24bits || $TERM == *-truecolor || $COLORTERM == *24bit* || $COLORTERM == *truecolor* ]]; then
value=semicolon
else
case ${fields[0]} in
(83) # screen (truecolor on にしている必要がある。判定方法は不明)
if ((fields[1]>=49900)); then
value=semicolon
fi ;;
(67)
if ((fields[1]>=100000)); then
: # cygwin terminal
else
# contra
value=colon
fi ;;
esac
fi
[[ $value ]] &&
bleopt term_true_colors="$value"
fi
}
blehook term_DA2R!=ble/color/initialize-term-colors
function ble-color-show {
if (($#)); then
ble/base/print-usage-for-no-argument-command 'Update and reload ble.sh.' "$@"
return "$?"
fi
local cols=16
local bg bg0 bgN ret gflags=$((_ble_color_gflags_BgIndexed|_ble_color_gflags_FgIndexed))
for ((bg0=0;bg0<256;bg0+=cols)); do
((bgN=bg0+cols,bgN<256||(bgN=256)))
for ((bg=bg0;bg<bgN;bg++)); do
ble/color/g2sgr "$((gflags|bg<<32))"
printf '%s%03d ' "$ret" "$bg"
done
printf '%s\n' "$_ble_term_sgr0"
for ((bg=bg0;bg<bgN;bg++)); do
ble/color/g2sgr "$((gflags|bg<<32|15<<8))"
printf '%s%03d ' "$ret" "$bg"
done
printf '%s\n' "$_ble_term_sgr0"
done
}
## @fn ble/color/g2sgr g
## @fn ble/color/g2sgr-ansi g
## @param[in] g
## @var[out] ret
##
#
# Note: もし SGR 以外の制御機能を使って (tput 等の出力を用いて) 描画シー
# ケンスを構築する様に拡張する場合には、
# ble/textarea#slice-text-buffer に於いて行っている CR LF の組の検出
# において、間に許容する制御機能の種類に注意する。もし考慮に入れてい
# ない物をここで使いたい時には、それを
# ble/textarea#slice-text-buffer の正規表現に追加しなければならない。
#
_ble_color_g2sgr=()
_ble_color_g2sgr_ansi=()
function ble/color/g2sgr/.impl {
local g=$(($1))
local sgr=0
((g&_ble_color_gflags_Bold)) && sgr="$sgr;${_ble_term_sgr_bold:-1}"
((g&_ble_color_gflags_Italic)) && sgr="$sgr;${_ble_term_sgr_sitm:-3}"
((g&_ble_color_gflags_Underline)) && sgr="$sgr;${_ble_term_sgr_smul:-4}"
((g&_ble_color_gflags_Blink)) && sgr="$sgr;${_ble_term_sgr_blink:-5}"
((g&_ble_color_gflags_Revert)) && sgr="$sgr;${_ble_term_sgr_rev:-7}"
((g&_ble_color_gflags_Invisible)) && sgr="$sgr;${_ble_term_sgr_invis:-8}"
((g&_ble_color_gflags_Strike)) && sgr="$sgr;${_ble_term_sgr_strike:-9}"
if ((g&_ble_color_gflags_FgIndexed)); then
local fg=$((g>>8&0xFF))
ble/color/.color2sgrfg "$fg"
sgr="$sgr;$ret"
elif ((g&_ble_color_gflags_FgMask)); then
local rgb=$((1<<24|g>>8&0xFFFFFF))
ble/color/.color2sgrfg "$rgb"
sgr="$sgr;$ret"
fi
if ((g&_ble_color_gflags_BgIndexed)); then
local bg=$((g>>32&0xFF))
ble/color/.color2sgrbg "$bg"
sgr="$sgr;$ret"
elif ((g&_ble_color_gflags_BgMask)); then
local rgb=$((1<<24|g>>32&0xFFFFFF))
ble/color/.color2sgrbg "$rgb"
sgr="$sgr;$ret"
fi
ret=$'\e['$sgr'm'
_ble_color_g2sgr[$1]=$ret
}
function ble/color/g2sgr/.clear-cache {
_ble_color_g2sgr=()
}
function ble/color/g2sgr {
ret=${_ble_color_g2sgr[$1]}
[[ $ret ]] || ble/color/g2sgr/.impl "$1"
}
function ble/color/g2sgr-ansi/.impl {
local g=$(($1))
local sgr=0
((g&_ble_color_gflags_Bold)) && sgr="$sgr;1"
((g&_ble_color_gflags_Italic)) && sgr="$sgr;3"
((g&_ble_color_gflags_Underline)) && sgr="$sgr;4"
((g&_ble_color_gflags_Blink)) && sgr="$sgr;5"
((g&_ble_color_gflags_Revert)) && sgr="$sgr;7"
((g&_ble_color_gflags_Invisible)) && sgr="$sgr;8"
((g&_ble_color_gflags_Strike)) && sgr="$sgr;9"
if ((g&_ble_color_gflags_FgIndexed)); then
local fg=$((g>>8&0xFF))
sgr="$sgr;38:5:$fg"
elif ((g&_ble_color_gflags_FgMask)); then
local rgb=$((1<<24|g>>8&0xFFFFFF))
local R=$((rgb>>16&0xFF)) G=$((rgb>>8&0xFF)) B=$((rgb&0xFF))
sgr="$sgr;38:2::$r:$g:$b"
fi
if ((g&_ble_color_gflags_BgIndexed)); then
local bg=$((g>>32&0xFF))
sgr="$sgr;48:5:$bg"
elif ((g&_ble_color_gflags_BgMask)); then
local rgb=$((1<<24|g>>32&0xFFFFFF))
local R=$((rgb>>16&0xFF)) G=$((rgb>>8&0xFF)) B=$((rgb&0xFF))
sgr="$sgr;48:2::$r:$g:$b"
fi
ret=$'\e['$sgr'm'
_ble_color_g2sgr_ansi[$1]=$ret
}
function ble/color/g2sgr-ansi {
ret=${_ble_color_g2sgr_ansi[$1]}
[[ $ret ]] || ble/color/g2sgr-ansi/.impl "$1"
}
function ble/color/g#setfg-clear {
(($1&=~(_ble_color_gflags_FgIndexed|_ble_color_gflags_FgMask)))
}
function ble/color/g#setbg-clear {
(($1&=~(_ble_color_gflags_BgIndexed|_ble_color_gflags_BgMask)))
}
function ble/color/g#setfg-index {
local __color=$2
(($1=$1&~_ble_color_gflags_FgMask|_ble_color_gflags_FgIndexed|(__color&0xFF)<<8)) # index color
}
function ble/color/g#setbg-index {
local __color=$2
(($1=$1&~_ble_color_gflags_BgMask|_ble_color_gflags_BgIndexed|(__color&0xFF)<<32)) # index color
}
function ble/color/g#setfg-rgb {
local __R=$2 __G=$3 __B=$4
((__R&=0xFF,__G&=0xFF,__B&=0xFF))
if ((__R==0&&__G==0&&__B==0)); then
ble/color/g#setfg-index "$1" 16
else
(($1=$1&~(_ble_color_gflags_FgIndexed|_ble_color_gflags_FgMask)|__R<<24|__G<<16|__B<<8)) # true color
fi
}
function ble/color/g#setbg-rgb {
local __R=$2 __G=$3 __B=$4
((__R&=0xFF,__G&=0xFF,__B&=0xFF))
if ((__R==0&&__G==0&&__B==0)); then
ble/color/g#setbg-index "$1" 16
else
(($1=$1&~(_ble_color_gflags_BgIndexed|_ble_color_gflags_BgMask)|__R<<48|__G<<40|__B<<32)) # true color
fi
}
function ble/color/g#setfg-cmyk {
local __C=$2 __M=$3 __Y=$4 __K=${5:-0}
((__K=~__K&0xFF,
__C=(~__C&0xFF)*__K/255,
__M=(~__M&0xFF)*__K/255,
__Y=(~__Y&0xFF)*__K/255))
ble/color/g#setfg-rgb "$__C" "$__M" "$__Y"
}
function ble/color/g#setbg-cmyk {
local __C=$2 __M=$3 __Y=$4 __K=${5:-0}
((__K=~__K&0xFF,
__C=(~__C&0xFF)*__K/255,
__M=(~__M&0xFF)*__K/255,
__Y=(~__Y&0xFF)*__K/255))
ble/color/g#setbg-rgb "$1" "$__C" "$__M" "$__Y"
}
function ble/color/g#setfg {
local __color=$2
if ((__color<0)); then
ble/color/g#setfg-clear "$1"
elif ((__color>=0x1000000)); then
if ((__color==0x1000000)); then
ble/color/g#setfg-index "$1" 16
else
(($1=$1&~(_ble_color_gflags_FgIndexed|_ble_color_gflags_FgMask)|(__color&0xFFFFFF)<<8)) # true color
fi
else
ble/color/g#setfg-index "$1" "$__color"
fi
}
function ble/color/g#setbg {
local __color=$2
if ((__color<0)); then
ble/color/g#setbg-clear "$1"
elif ((__color>=0x1000000)); then
if ((__color==0x1000000)); then
ble/color/g#setbg-index "$1" 16
else
(($1=$1&~(_ble_color_gflags_BgIndexed|_ble_color_gflags_BgMask)|(__color&0xFFFFFF)<<32)) # true color
fi
else
ble/color/g#setbg-index "$1" "$__color"
fi
}
## @fn ble/color/g#append g g2
## g に描画属性 g2 を上書きします。
## @param[in,out] g
## @param[in] g2
function ble/color/g#append {
local __g2=$2
((__g2&(_ble_color_gflags_FgMask|_ble_color_gflags_FgIndexed))) &&
(($1&=~(_ble_color_gflags_FgMask|_ble_color_gflags_FgIndexed)))
((__g2&(_ble_color_gflags_BgMask|_ble_color_gflags_BgIndexed))) &&
(($1&=~(_ble_color_gflags_BgMask|_ble_color_gflags_BgIndexed)))
(($1|=__g2))
}
function ble/color/g#compose {
(($1=($2)))
local __g2
for __g2 in "${@:3}"; do
ble/color/g#append "$1" "$__g2"
done
}
function ble/color/g.setfg { ble/color/g#setfg g "$@"; }
function ble/color/g.setbg { ble/color/g#setbg g "$@"; }
function ble/color/g.setfg-clear { ble/color/g#setfg-clear g "$@"; }
function ble/color/g.setbg-clear { ble/color/g#setbg-clear g "$@"; }
function ble/color/g.setfg-index { ble/color/g#setfg-index g "$@"; }
function ble/color/g.setbg-index { ble/color/g#setbg-index g "$@"; }
function ble/color/g.setfg-rgb { ble/color/g#setfg-rgb g "$@"; }
function ble/color/g.setbg-rgb { ble/color/g#setbg-rgb g "$@"; }
function ble/color/g.setfg-cmyk { ble/color/g#setfg-cmyk g "$@"; }
function ble/color/g.setbg-cmyk { ble/color/g#setbg-cmyk g "$@"; }
function ble/color/g.append { ble/color/g#append g "$@"; }
function ble/color/g.compose { ble/color/g#compose g "$@"; }
function ble/color/g#getfg {
local g=$1
if ((g&_ble_color_gflags_FgIndexed)); then
((ret=g>>8&0xFF))
elif ((g&_ble_color_gflags_FgMask)); then
((ret=0x1000000|(g>>8&0xFFFFFF)))
else
((ret=-1))
fi
}
function ble/color/g#getbg {
local g=$1
if ((g&_ble_color_gflags_BgIndexed)); then
((ret=g>>32&0xFF))
elif ((g&_ble_color_gflags_BgMask)); then
((ret=0x1000000|(g>>32&0xFFFFFF)))
else
((ret=-1))
fi
}
function ble/color/g#compute-fg {
local g=$1
if ((g&_ble_color_gflags_Invisible)); then
ble/color/g#compute-bg "$g"
elif ((g&_ble_color_gflags_Revert)); then
ble/color/g#getbg "$g"
else
ble/color/g#getfg "$g"
fi
}
function ble/color/g#compute-bg {
local g=$1
if ((g&_ble_color_gflags_Revert)); then
ble/color/g#getfg "$g"
else
ble/color/g#getbg "$g"
fi
}
## @fn ble/color/gspec2g gspec
## @param[in] gspec
## @var[out] ret
function ble/color/gspec2g {
local g=0 entry
for entry in ${1//,/ }; do
case "$entry" in
(bold) ((g|=_ble_color_gflags_Bold)) ;;
(underline) ((g|=_ble_color_gflags_Underline)) ;;
(blink) ((g|=_ble_color_gflags_Blink)) ;;
(invis) ((g|=_ble_color_gflags_Invisible)) ;;
(reverse) ((g|=_ble_color_gflags_Revert)) ;;
(strike) ((g|=_ble_color_gflags_Strike)) ;;
(italic) ((g|=_ble_color_gflags_Italic)) ;;
(standout) ((g|=_ble_color_gflags_Revert|_ble_color_gflags_Bold)) ;;
(fg=*)
ble/color/.name2color "${entry:3}"
ble/color/g.setfg "$ret" ;;
(bg=*)
ble/color/.name2color "${entry:3}"
ble/color/g.setbg "$ret" ;;
(none)
g=0 ;;
esac
done
ret=$g
}
## @fn ble/color/g2gspec g
## @var[out] ret
function ble/color/g2gspec {
local g=$1 gspec=
if ((g&_ble_color_gflags_FgIndexed)); then
local fg=$((g>>8&0xFF))
ble/color/.color2name "$fg"
gspec=$gspec,fg=$ret
elif ((g&_ble_color_gflags_FgMask)); then
local rgb=$((1<<24|g>>8&0xFFFFFF))
ble/color/.color2name "$rgb"
gspec=$gspec,fg=$ret
fi
if ((g&_ble_color_gflags_BgIndexed)); then
local bg=$((g>>32&0xFF))
ble/color/.color2name "$bg"
gspec=$gspec,bg=$ret
elif ((g&_ble_color_gflags_BgMask)); then
local rgb=$((1<<24|g>>32&0xFFFFFF))
ble/color/.color2name "$rgb"
gspec=$gspec,bg=$ret
fi
((g&_ble_color_gflags_Bold)) && gspec=$gspec,bold
((g&_ble_color_gflags_Underline)) && gspec=$gspec,underline
((g&_ble_color_gflags_Blink)) && gspec=$gspec,blink
((g&_ble_color_gflags_Invisible)) && gspec=$gspec,invis
((g&_ble_color_gflags_Revert)) && gspec=$gspec,reverse
((g&_ble_color_gflags_Strike)) && gspec=$gspec,strike
((g&_ble_color_gflags_Italic)) && gspec=$gspec,italic
gspec=${gspec#,}
ret=${gspec:-none}
}
## @fn ble/color/gspec2sgr gspec
## @param[in] gspec
## @var[out] ret
function ble/color/gspec2sgr {
local sgr=0 entry
for entry in ${1//,/ }; do
case "$entry" in
(bold) sgr="$sgr;${_ble_term_sgr_bold:-1}" ;;
(underline) sgr="$sgr;${_ble_term_sgr_smul:-4}" ;;
(blink) sgr="$sgr;${_ble_term_sgr_blink:-5}" ;;
(invis) sgr="$sgr;${_ble_term_sgr_invis:-8}" ;;
(reverse) sgr="$sgr;${_ble_term_sgr_rev:-7}" ;;
(strike) sgr="$sgr;${_ble_term_sgr_strike:-9}" ;;
(italic) sgr="$sgr;${_ble_term_sgr_sitm:-3}" ;;
(standout) sgr="$sgr;${_ble_term_sgr_bold:-1};${_ble_term_sgr_rev:-7}" ;;
(fg=*)
ble/color/.name2color "${entry:3}"
ble/color/.color2sgrfg "$ret"
sgr="$sgr;$ret" ;;
(bg=*)
ble/color/.name2color "${entry:3}"
ble/color/.color2sgrbg "$ret"
sgr="$sgr;$ret" ;;
(none)
sgr=0 ;;
esac
done
ret="[${sgr}m"
}
function ble/color/.name2color/.clamp {
local text=$1 max=$2
if [[ $text == *% ]]; then
((ret=10#0${text%'%'}*max/100))
else
((ret=10#0$text))
fi
((ret>max)) && ret=max
}
function ble/color/.name2color/.wrap {
local text=$1 max=$2
if [[ $text == *% ]]; then
((ret=10#0${text%'%'}*max/100))
else
((ret=10#0$text))
fi
((ret%=max))
}
function ble/color/.hxx2color {
local H=$1 Min=$2 Range=$3 Unit=$4
local h1 h2 x=$Min y=$Min z=$Min
((h1=H%120,h2=120-h1,
x+=Range*(h2<60?h2:60)/60,
y+=Range*(h1<60?h1:60)/60))
((x=x*255/Unit,
y=y*255/Unit,
z=z*255/Unit))
case "$((H/120))" in
(0) local R=$x G=$y B=$z ;;
(1) local R=$z G=$x B=$y ;;
(2) local R=$y G=$z B=$x ;;
esac
((ret=1<<24|R<<16|G<<8|B))
}
function ble/color/.hsl2color {
local H=$1 S=$2 L=$3 Unit=$4
local Range=$((2*(L<=Unit/2?L:Unit-L)*S/Unit))
local Min=$((L-Range/2))
ble/color/.hxx2color "$H" "$Min" "$Range" "$Unit"
}
function ble/color/.hsb2color {
local H=$1 S=$2 B=$3 Unit=$4
local Range=$((B*S/Unit))
local Min=$((B-Range))
ble/color/.hxx2color "$H" "$Min" "$Range" "$Unit"
}
## @fn ble/color/.name2color colorName
## @var[out] ret
function ble/color/.name2color {
local colorName=$1
if [[ ! ${colorName//[0-9]} ]]; then
((ret=10#0$colorName&255))
elif [[ $colorName == '#'* ]]; then
if local rex='^#[0-9a-fA-F]{3}$'; [[ $colorName =~ $rex ]]; then
let "ret=1<<24|16#${colorName:1:1}*0x11<<16|16#${colorName:2:1}*0x11<<8|16#${colorName:3:1}*0x11"
elif rex='^#[0-9a-fA-F]{6}$'; [[ $colorName =~ $rex ]]; then
let "ret=1<<24|16#${colorName:1:2}<<16|16#${colorName:3:2}<<8|16#${colorName:5:2}"
else
ret=-1
fi
elif [[ $colorName == *:* ]]; then
if local rex='^rgb:([0-9]+%?)/([0-9]+%?)/([0-9]+%?)$'; [[ $colorName =~ $rex ]]; then
ble/color/.name2color/.clamp "${BASH_REMATCH[1]}" 255; local R=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[2]}" 255; local G=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[3]}" 255; local B=$ret
((ret=1<<24|R<<16|G<<8|B))
elif
local rex1='^cmy:([0-9]+%?)/([0-9]+%?)/([0-9]+%?)$'
local rex2='^cmyk:([0-9]+%?)/([0-9]+%?)/([0-9]+%?)/([0-9]+%?)$'
[[ $colorName =~ $rex1 || $colorName =~ $rex2 ]]
then
ble/color/.name2color/.clamp "${BASH_REMATCH[1]}" 255; local C=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[2]}" 255; local M=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[3]}" 255; local Y=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[4]:-0}" 255; local K=$ret
local K=$((~K&0xFF))
local R=$(((~C&0xFF)*K/255))
local G=$(((~M&0xFF)*K/255))
local B=$(((~Y&0xFF)*K/255))
((ret=1<<24|R<<16|G<<8|B))
elif rex='^hs[lvb]:([0-9]+)/([0-9]+%)/([0-9]+%)$'; [[ $colorName =~ $rex ]]; then
ble/color/.name2color/.wrap "${BASH_REMATCH[1]}" 360; local H=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[2]}" 1000; local S=$ret
ble/color/.name2color/.clamp "${BASH_REMATCH[3]}" 1000; local X=$ret
if [[ $colorName == hsl:* ]]; then
ble/color/.hsl2color "$H" "$S" "$X" 1000
else
ble/color/.hsb2color "$H" "$S" "$X" 1000
fi
else
ret=-1
fi
else
case "$colorName" in
(black) ret=0 ;;
(brown) ret=1 ;;
(green) ret=2 ;;
(olive) ret=3 ;;
(navy) ret=4 ;;
(purple) ret=5 ;;
(teal) ret=6 ;;
(silver) ret=7 ;;
(gr[ae]y) ret=8 ;;
(red) ret=9 ;;
(lime) ret=10 ;;
(yellow) ret=11 ;;
(blue) ret=12 ;;
(magenta) ret=13 ;;
(cyan) ret=14 ;;
(white) ret=15 ;;
(orange) ret=202 ;;
(transparent|default) ret=-1 ;;
(*) ret=-1 ;;
esac
fi
}
function ble/color/.color2name {
if (($1>=0x1000000)); then
ble/util/sprintf ret '#%06x' "$(($1&0xFFFFFF))"
return 0
fi
((ret=(10#0$1&255)))
case $ret in
(0) ret=black ;;
(1) ret=brown ;;
(2) ret=green ;;
(3) ret=olive ;;
(4) ret=navy ;;
(5) ret=purple ;;
(6) ret=teal ;;
(7) ret=silver ;;
(8) ret=gray ;;
(9) ret=red ;;
(10) ret=lime ;;
(11) ret=yellow ;;
(12) ret=blue ;;
(13) ret=magenta ;;
(14) ret=cyan ;;
(15) ret=white ;;
(202) ret=orange ;;
esac
}
## @fn ble/color/convert-color88-to-color256 color
## @param[in] color
## @var[out] ret
function ble/color/convert-color88-to-color256 {
local color=$1
if ((color>=16)); then
if ((color>=80)); then
local L=$((((color-80+1)*25+4)/9))
((color=L==0?16:(L==25?231:232+(L-1))))
else
((color-=16))
local R=$((color/16)) G=$((color/4%4)) B=$((color%4))
((R=(R*5+1)/3,G=(G*5+1)/3,B=(B*5+1)/3,
color=16+R*36+G*6+B))
fi
fi
ret=$color
}
## @fn ble/color/convert-color256-to-color88 color
## @param[in] color
## @var[out] ret
function ble/color/convert-color256-to-color88 {
local color=$1
if ((color>=16)); then
if ((color>=232)); then
local L=$((((color-232+1)*9+12)/25))
((color=L==0?16:(L==9?79:80+(L-1))))
else
((color-=16))
local R=$((color/36)) G=$((color/6%6)) B=$((color%6))
((R=(R*3+2)/5,G=(G*3+2)/5,B=(B*3+2)/5,
color=16+R*16+G*4+B))
fi
fi
ret=$color
}
## @fn ble/color/convert-rgb24-to-color256 R G B
## @param[in] R G B
## 0..255 の階調値
## @var[out] ret
function ble/color/convert-rgb24-to-color256 {
local R=$1 G=$2 B=$3
if ((R==G&&G==B)); then
# xterm 24 grayscale: 10k+8 (0..238)
if ((R<=3)); then
# 6x6x6 cube (0,0,0)
ret=16
elif ((R>=247)); then
# 6x6x6 cube (5,5,5)
ret=231
elif ((R>=92&&(R-92)%40<5)); then
# 6x6x6 cube (1,1,1)-(4,4,4)
((ret=59+43*(R-92)/40))
else
local level=$(((R-3)/10))
((ret=232+(level<=23?level:23)))
fi
else
# xterm 6x6x6 cube: k?55+40k:0
((R=R<=47?0:(R<=95?1:(R-35)/40)))
((G=G<=47?0:(G<=95?1:(G-35)/40)))
((B=B<=47?0:(B<=95?1:(B-35)/40)))
((ret=16+36*R+6*G+B))
fi
}
## @fn ble/color/convert-rgb24-to-color88 R G B
## @param[in] R G B
## 0..255 の階調値
## @var[out] ret
function ble/color/convert-rgb24-to-color88 {
local R=$1 G=$2 B=$3
if ((R==G&&G==B)); then
# xterm 8 grayscale: 46+25k = 46,71,96,121,146,171,196,221
if ((R<=22)); then
ret=16 # 4x4x4 cube (0,0,0)=0:0:0
elif ((R>=239)); then
ret=79 # 4x4x4 cube (3,3,3)=255:255:255
elif ((131<=R&&R<=142)); then
ret=37 # 4x4x4 cube (1,1,1)=139:139:139
elif ((197<=R&&R<=208)); then
ret=58 # 4x4x4 cube (2,2,2)=197:197:197
else
local level=$(((R-34)/25))
((ret=80+(level<=7?level:7)))
fi
else
# xterm 4x4x4 cube: (k?81+58k:0) = 0,139,197,255
((R=R<=69?0:(R<=168?1:(R-52)/58)))
((G=G<=69?0:(G<=168?1:(G-52)/58)))
((B=B<=69?0:(B<=168?1:(B-52)/58)))
((ret=16+16*R+4*G+B))
fi
}
## @fn ble/color/.color2sgrfg color
## @fn ble/color/.color2sgrbg color
## @param[in] color
## 0-255 の値は index color を表します。
## 1XXXXXX の値は 24bit color を表します。
## @var[out] ret
function ble/color/.color2sgr-impl {
local ccode=$1 prefix=$2 # 3 for fg, 4 for bg
if ((ccode<0)); then
ret=${prefix}9
elif ((ccode<16&&ccode<_ble_term_colors)); then
if ((prefix==4)); then
ret=${_ble_term_sgr_ab[ccode]}
else
ret=${_ble_term_sgr_af[ccode]}
fi
elif ((ccode<256)); then
local index_colors=$_ble_color_index_colors_default
[[ $bleopt_term_index_colors == auto ]] || ((index_colors=bleopt_term_index_colors))
if ((index_colors>=256)); then
ret="${prefix}8;5;$ccode"
elif ((index_colors>=88)); then
ble/color/convert-color256-to-color88 "$ccode"
ret="${prefix}8;5;$ret"
elif ((ccode<index_colors)); then
ret="${prefix}8;5;$ccode"
elif ((_ble_term_colors>=16||_ble_term_colors==8)); then
if ((ccode>=16)); then
if ((ccode>=232)); then
local L=$((((ccode-232+1)*3+12)/25))
((ccode=L==0?0:(L==1?8:(L==2?7:15))))
else
((ccode-=16))
local R=$((ccode/36)) G=$((ccode/6%6)) B=$((ccode%6))
if ((R==G&&G==B)); then
local L=$(((R*3+2)/5))
((ccode=L==0?0:(L==1?8:(L==2?7:15))))
else
local min max
((R<G?(min=R,max=G):(min=G,max=R),
B<min?(min=B):(B>max&&(max=B))))
local Range=$((max-min))
((R=(R-min+Range/2)/Range,
G=(G-min+Range/2)/Range,
B=(B-min+Range/2)/Range,
ccode=R+G*2+B*4+(min+max>=5?8:0)))
fi
fi
fi
((_ble_term_colors==8&&ccode>=8&&(ccode-=8)))
if ((prefix==4)); then
ret=${_ble_term_sgr_ab[ccode]}
else
ret=${_ble_term_sgr_af[ccode]}
fi
else
ret=${prefix}9
fi
elif ((0x1000000<=ccode&&ccode<0x2000000)); then
# 24bit True Colors
local R=$((ccode>>16&0xFF)) G=$((ccode>>8&0xFF)) B=$((ccode&0xFF))
if [[ $bleopt_term_true_colors == semicolon ]]; then
ret="${prefix}8;2;$R;$G;$B"
elif [[ $bleopt_term_true_colors == colon ]]; then
ret="${prefix}8:2:$R:$G:$B"
else
local index_colors=$_ble_color_index_colors_default
[[ $bleopt_term_index_colors == auto ]] || ((index_colors=bleopt_term_index_colors))
local index=
if ((index_colors>=256)); then
ble/color/convert-rgb24-to-color256 "$R" "$G" "$B"
index=$ret
elif ((index_colors>=88)); then
ble/color/convert-rgb24-to-color88 "$R" "$G" "$B"
index=$ret
else
ble/color/convert-rgb24-to-color256 "$R" "$G" "$B"
if ((ret<index_colors)); then
index=$ret
else
ble/color/.color2sgr-impl "$ret" "$prefix"
fi
fi
[[ $index ]] && ret="${prefix}8;5;$index"
fi
else
ret=${prefix}9
fi
}
## @fn ble/color/.color2sgrfg color_code
## @var[out] ret
function ble/color/.color2sgrfg {
ble/color/.color2sgr-impl "$1" 3
}
## @fn ble/color/.color2sgrbg color_code
## @var[out] ret
function ble/color/.color2sgrbg {
ble/color/.color2sgr-impl "$1" 4
}
#------------------------------------------------------------------------------
## @fn ble/color/read-sgrspec/.arg-next
## @var[in ] fields
## @var[in,out] j
## @var[ out] arg
function ble/color/read-sgrspec/.arg-next {
local _var=arg _ret
if [[ $1 == -v ]]; then
_var=$2
shift 2
fi
if ((j<${#fields[*]})); then
((_ret=10#0${fields[j++]}))
else
((i++))
((_ret=10#0${specs[i]%%:*}))
fi
(($_var=_ret))
}
## @fn ble-color/read-sgrspec sgrspec opts
## @param[in] sgrspec
## @var[in,out] g
function ble/color/read-sgrspec {
local specs i iN
ble/string#split specs \; "$1"
for ((i=0,iN=${#specs[@]};i<iN;i++)); do
local spec=${specs[i]} fields
ble/string#split fields : "$spec"
local arg=$((10#0${fields[0]}))
if ((arg==0)); then
g=0
continue
elif [[ :$opts: != *:ansi:* ]]; then
[[ ${_ble_term_sgr_term2ansi[arg]} ]] &&
arg=${_ble_term_sgr_term2ansi[arg]}
fi
if ((30<=arg&&arg<50)); then
# colors
if ((30<=arg&&arg<38)); then
local color=$((arg-30))
ble/color/g.setfg-index "$color"
elif ((40<=arg&&arg<48)); then
local color=$((arg-40))
ble/color/g.setbg-index "$color"
elif ((arg==38)); then
local j=1 color cspace
ble/color/read-sgrspec/.arg-next -v cspace
if ((cspace==5)); then
ble/color/read-sgrspec/.arg-next -v color
if [[ :$opts: != *:ansi:* ]] && ((bleopt_term_index_colors==88)); then
local ret; ble/color/convert-color88-to-color256 "$color"; color=$ret
fi
ble/color/g.setfg-index "$color"
elif ((cspace==2)); then
local S R G B
((${#fields[@]}>5)) &&
ble/color/read-sgrspec/.arg-next -v S
ble/color/read-sgrspec/.arg-next -v R
ble/color/read-sgrspec/.arg-next -v G
ble/color/read-sgrspec/.arg-next -v B
ble/color/g.setfg-rgb "$R" "$G" "$B"
elif ((cspace==3||cspace==4)); then
local S C M Y K=0
((${#fields[@]}>2+cspace)) &&
ble/color/read-sgrspec/.arg-next -v S
ble/color/read-sgrspec/.arg-next -v C
ble/color/read-sgrspec/.arg-next -v M
ble/color/read-sgrspec/.arg-next -v Y
((cspace==4)) &&
ble/color/read-sgrspec/.arg-next -v K
ble/color/g.setfg-cmyk "$C" "$M" "$Y" "$K"
else
ble/color/g.setfg-clear
fi
elif ((arg==48)); then
local j=1 color cspace
ble/color/read-sgrspec/.arg-next -v cspace
if ((cspace==5)); then
ble/color/read-sgrspec/.arg-next -v color
if [[ :$opts: != *:ansi:* ]] && ((bleopt_term_index_colors==88)); then
local ret; ble/color/convert-color88-to-color256 "$color"; color=$ret
fi
ble/color/g.setbg-index "$color"
elif ((cspace==2)); then
local S R G B
((${#fields[@]}>5)) &&
ble/color/read-sgrspec/.arg-next -v S
ble/color/read-sgrspec/.arg-next -v R
ble/color/read-sgrspec/.arg-next -v G
ble/color/read-sgrspec/.arg-next -v B
ble/color/g.setbg-rgb "$R" "$G" "$B"
elif ((cspace==3||cspace==4)); then
local S C M Y K=0
((${#fields[@]}>2+cspace)) &&
ble/color/read-sgrspec/.arg-next -v S
ble/color/read-sgrspec/.arg-next -v C
ble/color/read-sgrspec/.arg-next -v M
ble/color/read-sgrspec/.arg-next -v Y
((cspace==4)) &&
ble/color/read-sgrspec/.arg-next -v K
ble/color/g.setbg-cmyk "$C" "$M" "$Y" "$K"
else
ble/color/g.setbg-clear
fi
elif ((arg==39)); then
ble/color/g.setfg-clear
elif ((arg==49)); then
ble/color/g.setbg-clear
fi
elif ((90<=arg&&arg<98)); then
local color=$((arg-90+8))
ble/color/g.setfg-index "$color"
elif ((100<=arg&&arg<108)); then
local color=$((arg-100+8))
ble/color/g.setbg-index "$color"
else
case $arg in
(1) ((g|=_ble_color_gflags_Bold)) ;;
(22) ((g&=~_ble_color_gflags_Bold)) ;;
(4) ((g|=_ble_color_gflags_Underline)) ;;
(24) ((g&=~_ble_color_gflags_Underline)) ;;
(7) ((g|=_ble_color_gflags_Revert)) ;;
(27) ((g&=~_ble_color_gflags_Revert)) ;;
(9807) ((g^=_ble_color_gflags_Revert)) ;; # toggle (for internal use)
(3) ((g|=_ble_color_gflags_Italic)) ;;
(23) ((g&=~_ble_color_gflags_Italic)) ;;
(5) ((g|=_ble_color_gflags_Blink)) ;;
(25) ((g&=~_ble_color_gflags_Blink)) ;;
(8) ((g|=_ble_color_gflags_Invisible)) ;;
(28) ((g&=~_ble_color_gflags_Invisible)) ;;
(9) ((g|=_ble_color_gflags_Strike)) ;;
(29) ((g&=~_ble_color_gflags_Strike)) ;;
esac
fi
done
}
## @fn ble/color/sgrspec2g str
## SGRに対する引数から描画属性を構築します。
## @var[out] ret
function ble/color/sgrspec2g {
local g=0
ble/color/read-sgrspec "$1"
ret=$g
}
## @fn ble/color/ansi2g str
## ANSI制御シーケンスから描画属性を構築します。
## Note: canvas.sh を読み込んで以降でないと使えません。
## @var[out] ret
function ble/color/ansi2g {
local x=0 y=0 g=0
ble/function#try ble/canvas/trace "$1" # -> ret
ret=$g
}
#------------------------------------------------------------------------------
# _ble_faces
# 遅延初期化登録
# @hook color_defface_load (defined in src/def.sh)
# @hook color_setface_load (defined in src/def.sh)
# 遅延初期化
if [[ ! ${_ble_faces_count-} ]]; then # reload #D0875
_ble_faces_count=0
_ble_faces=()
fi
## @fn ble/color/setface/.check-argument
## @var[out] ext
function ble/color/setface/.check-argument {
local rex='^[a-zA-Z0-9_]+$'
[[ $# == 2 && $1 =~ $rex && $2 ]] && return 0
local flags=a
while (($#)); do
local arg=$1; shift
case $arg in
(--help) flags=H$flags ;;
(--color|--color=always) flags=c${flags//[ac]} ;;
(--color=auto) flags=a${flags//[ac]} ;;
(--color=never) flags=${flags//[ac]} ;;
(-*)
ble/util/print "${FUNCNAME[1]}: unrecognized option '$arg'." >&2
flags=E$flags ;;
(*)
ble/util/print "${FUNCNAME[1]}: unrecognized argument '$arg'." >&2
flags=E$flags ;;
esac
done
if [[ $flags == *E* ]]; then
ext=2; return 1
elif [[ $flags == *H* ]]; then
ble/util/print-lines \
"usage: $name FACE_NAME [TYPE:]SPEC" \
' Set face.' \
'' \
' TYPE Specifies the format of SPEC. The following values are available.' \
' gspec Comma separated graphic attribute list' \
' g Integer value' \
' ref Face name or id (reference)' \
' copy Face name or id (copy value)' \
' sgrspec Parameters to the control function SGR' \
' ansi ANSI Sequences' >&2
ext=0; return 1
fi
local opts=
[[ $flags == *c* || $flags == *a* && -t 1 ]] && opts=$opts:color
ble/color/list-faces "$opts"; ext=$?; return 1
}
function ble-color-defface {
local ext; ble/color/setface/.check-argument "$@" || return "$ext"
ble/color/defface "$@"
}
function ble-color-setface {
local ext; ble/color/setface/.check-argument "$@" || return "$ext"
ble/color/setface "$@"
}
# 遅延関数 (後で上書き)
function ble/color/defface { local q=\' Q="'\''"; blehook color_defface_load+="ble/color/defface '${1//$q/$Q}' '${2//$q/$Q}'"; }
function ble/color/setface { local q=\' Q="'\''"; blehook color_setface_load+="ble/color/setface '${1//$q/$Q}' '${2//$q/$Q}'"; }
function ble/color/face2g { ble/color/initialize-faces && ble/color/face2g "$@"; }
function ble/color/face2sgr { ble/color/initialize-faces && ble/color/face2sgr "$@"; }
function ble/color/iface2g { ble/color/initialize-faces && ble/color/iface2g "$@"; }
function ble/color/iface2sgr { ble/color/initialize-faces && ble/color/iface2sgr "$@"; }
function ble/color/face2sgr-ansi { ble/color/initialize-faces && ble/color/face2sgr "$@"; }