@@ -152,8 +152,8 @@ const SUPERSCRIPT = Dict(
152
152
# ###########################################################################################
153
153
# define types
154
154
const MarkerType = Union{Symbol,AbstractChar,AbstractString}
155
- const CrayonColorType = Union{Integer,Symbol,NTuple{ 3 ,Integer},Nothing }
156
- const UserColorType = Union{Crayon,CrayonColorType } # allowed color type
155
+ const StyledStringsColorType = Union{Integer,Symbol,StyledStrings . RGBTuple,UInt32 }
156
+ const UserColorType = Union{StyledStrings . Face,StyledStringsColorType,NTuple{ 3 ,Integer},Nothing } # allowed color type
157
157
const ColorType = UInt32 # internal UnicodePlots color type (on canvas), 8bit or 24bit
158
158
159
159
# ###########################################################################################
@@ -181,20 +181,17 @@ const UnicodeType = UInt32
181
181
182
182
# ###########################################################################################
183
183
# colors
184
+ @enum Colormode COLORMODE_24BIT COLORMODE_8BIT
184
185
const THRESHOLD = UInt32 (256 ^ 3 ) # 8bit - 24bit threshold
185
- const COLORMODE = Ref (Crayons . COLORS_256 )
186
+ const COLORMODE = Ref (COLORMODE_24BIT )
186
187
const INVALID_COLOR = typemax (ColorType)
187
188
const USE_LUT = Ref (false )
188
189
189
- const CRAYONS_FAST = Ref (true )
190
- const CRAYONS_EMPTY_STYLES = Tuple (Crayons. ANSIStyle () for _ ∈ 1 : 9 )
191
- const CRAYONS_RESET = Crayons. CSI * " 0" * Crayons. END_ANSI
192
-
193
190
const COLOR_CYCLE_FAINT = :green , :blue , :red , :magenta , :yellow , :cyan
194
- const COLOR_CYCLE_BRIGHT = map (s -> Symbol (" light_ " , s), COLOR_CYCLE_FAINT) # COV_EXCL_LINE
191
+ const COLOR_CYCLE_BRIGHT = map (s -> Symbol (" bright_ " , s), COLOR_CYCLE_FAINT) # COV_EXCL_LINE
195
192
const COLOR_CYCLE = Ref (COLOR_CYCLE_FAINT)
196
193
197
- const BORDER_COLOR = Ref (:dark_gray )
194
+ const BORDER_COLOR = Ref (:gray )
198
195
199
196
# ###########################################################################################
200
197
@@ -214,17 +211,20 @@ const ASPECT_RATIO = Ref(4 / 3)
214
211
const DEFAULT_HEIGHT = Ref (15 )
215
212
const DEFAULT_WIDTH = Ref (round (Int, DEFAULT_HEIGHT[] * 2 ASPECT_RATIO[]))
216
213
214
+ brightcolors! () = COLOR_CYCLE[] = COLOR_CYCLE_BRIGHT
215
+ faintcolors! () = COLOR_CYCLE[] = COLOR_CYCLE_FAINT
216
+
217
217
colormode () =
218
- if (cm = COLORMODE[]) ≡ Crayons . COLORS_256
218
+ if (cm = COLORMODE[]) ≡ COLORMODE_8BIT
219
219
8
220
- elseif cm ≡ Crayons . COLORS_24BIT
220
+ elseif cm ≡ COLORMODE_24BIT
221
221
24
222
222
else
223
223
throw (ArgumentError (" color mode $cm is unsupported" ))
224
224
end
225
225
226
- colors256! () = COLORMODE[] = Crayons . COLORS_256
227
- truecolors! () = COLORMODE[] = Crayons . COLORS_24BIT
226
+ colors256! () = COLORMODE[] = COLORMODE_8BIT
227
+ truecolors! () = COLORMODE[] = COLORMODE_24BIT
228
228
229
229
function colormode! (mode)
230
230
if mode ≡ 8
@@ -237,11 +237,6 @@ function colormode!(mode)
237
237
nothing
238
238
end
239
239
240
- brightcolors! () = COLOR_CYCLE[] = COLOR_CYCLE_BRIGHT
241
- faintcolors! () = COLOR_CYCLE[] = COLOR_CYCLE_FAINT
242
-
243
- # see gist.github.com/XVilka/8346728#checking-for-colorterm
244
- terminal_24bit () = lowercase (get (ENV , " COLORTERM" , " " )) ∈ (" 24bit" , " truecolor" )
245
240
246
241
# specific to UnicodePlots
247
242
forced_24bit () = lowercase (get (ENV , " UP_COLORMODE" , " " )) ∈ (" 24" , " 24bit" , " truecolor" )
@@ -444,19 +439,8 @@ function sorted_keys_values(dict::Dict; k2s = true)
444
439
first .(keys_vals), last .(keys_vals)
445
440
end
446
441
447
- crayon_color (:: Union{Missing,Nothing} ) = Crayons. ANSIColor ()
448
- crayon_color (color:: ColorType ) =
449
- if color ≡ INVALID_COLOR
450
- Crayons. ANSIColor ()
451
- elseif color < THRESHOLD # 24bit
452
- Crayons. ANSIColor (red (color), grn (color), blu (color), Crayons. COLORS_24BIT)
453
- else # 8bit
454
- Crayons. ANSIColor (color - THRESHOLD, Crayons. COLORS_256)
455
- end
456
-
457
-
458
- ss_color (:: Union{Missing,Nothing} ) = nothing
459
- ss_color (color:: ColorType ) =
442
+ styledstrings_color (:: Union{Missing,Nothing} ) = nothing
443
+ styledstrings_color (color:: ColorType ) =
460
444
if color ≡ INVALID_COLOR
461
445
nothing
462
446
elseif color < THRESHOLD # 24bit
@@ -465,33 +449,14 @@ ss_color(color::ColorType) =
465
449
StyledStrings. Legacy. legacy_color (Int (color - THRESHOLD))
466
450
end
467
451
468
- # function print_crayons(io, c, args...)
469
- # if CRAYONS_FAST[]
470
- # if Crayons.anyactive(c) # bypass crayons checks (_have_color, _force_color)
471
- # print(io, Crayons.CSI)
472
- # Crayons._print(io, c)
473
- # print(io, Crayons.END_ANSI, args..., CRAYONS_RESET)
474
- # else
475
- # print(io, args...)
476
- # end
477
- # else
478
- # print(io, c, args..., CRAYONS_RESET)
479
- # end
480
- # end
481
-
482
- # print_color(io::IO, color::Crayon, args...) = print_crayons(io, color, args...)
452
+ print_color (io:: IO , face:: StyledStrings.Face , args... ) = print (io, StyledStrings. face! (Base. annotatedstring (args... ), face))
483
453
print_color (io:: IO , color:: UserColorType , args... ) =
484
454
print_color (io, ansi_color (color), args... )
485
455
486
456
function print_color (io:: IO , color:: ColorType , args... ; bgcol = missing )
487
457
if get (io, :color , false )
488
- face = StyledStrings. Face (; foreground = ss_color (color), background = ss_color (bgcol))
489
- print (io, StyledStrings. face! (Base. annotatedstring (args... ), face))
490
- # print_crayons(
491
- # io,
492
- # Crayon(crayon_color(color), crayon_color(bgcol), CRAYONS_EMPTY_STYLES...),
493
- # args...,
494
- # )
458
+ face = StyledStrings. Face (foreground = styledstrings_color (color), background = styledstrings_color (bgcol))
459
+ print_color (io, face, args... )
495
460
else
496
461
print (io, args... )
497
462
end
@@ -540,34 +505,48 @@ c256(c::Integer) = c
540
505
# `ColorType` conversion - colormaps
541
506
ansi_color (rgb:: AbstractRGB ) = ansi_color ((c256 (rgb. r), c256 (rgb. g), c256 (rgb. b)))
542
507
ansi_color (rgb:: NTuple{3,AbstractFloat} ) = ansi_color (c256 .(rgb))
508
+ ansi_color (color:: NTuple{3,Integer} ):: ColorType = r32 (color[1 ]) + g32 (color[2 ]) + b32 (color[3 ])
543
509
544
510
ansi_color (color:: ColorType ):: ColorType = color # no-op
545
- ansi_color (crayon :: Crayon ) = ansi_color (crayon . fg ) # ignore bg & styles
546
- ansi_color (:: Missing ) = INVALID_COLOR # not a CrayonColorType
511
+ ansi_color (face :: StyledStrings.Face ) = ansi_color (face . foreground ) # ignore bg & styles
512
+ ansi_color (:: Union{Nothing, Missing} ) = INVALID_COLOR # not a StyledStringsColorType
547
513
548
- function ansi_color (color:: CrayonColorType ):: ColorType
514
+ function ansi_color (color:: StyledStringsColorType ):: ColorType
549
515
ignored_color (color) && return INVALID_COLOR
550
- ansi_color (Crayons . _parse_color (color))
516
+ ansi_color (StyledStrings . SimpleColor (color))
551
517
end
552
518
553
- ansi_color (c:: Crayons.ANSIColor ) = if COLORMODE[] ≡ Crayons. COLORS_24BIT
554
- if c. style ≡ Crayons. COLORS_24BIT
555
- r32 (c. r) + g32 (c. g) + b32 (c. b)
556
- elseif c. style ≡ Crayons. COLORS_256
557
- USE_LUT[] ? LUT_8BIT[c. r + 1 ] : THRESHOLD + c. r
558
- elseif c. style ≡ Crayons. COLORS_16
559
- c8 = ansi_4bit_to_8bit (c. r)
560
- USE_LUT[] ? LUT_8BIT[c8 + 1 ] : THRESHOLD + c8
519
+ function to_256_colors (color)
520
+ r, g, b = rgb = color. r, color. g, color. b
521
+ ansi = if r == g == b && r % 10 == 8
522
+ 232 + min ((r - 8 ) ÷ 10 , 23 ) # gray level
523
+ elseif all (map (c -> (c & 0x1 ) == 0 && (c > 0 ? c == 128 || c == 192 : true ), rgb))
524
+ (r >> 7 ) + 2 (g >> 7 ) + 4 (b >> 7 ) # primary color
525
+ else
526
+ r6, g6, b6 = map (c -> c < 48 ? 0 : (c < 114 ? 1 : trunc (Int, (c - 35 ) / 40 )), rgb)
527
+ 16 + 36 r6 + 6 g6 + b6 # cube 6x6x6
528
+ end
529
+ return UInt8 (ansi)
530
+ end
531
+
532
+ ansi_color (col:: StyledStrings.SimpleColor ) = let c = col. value
533
+ if COLORMODE[] ≡ COLORMODE_24BIT
534
+ if c isa Symbol
535
+ c4 = get (StyledStrings. ANSI_4BIT_COLORS, c, nothing )
536
+ c8 = ansi_4bit_to_8bit (UInt8 (c4))
537
+ return USE_LUT[] ? LUT_8BIT[c8 + 1 ] : THRESHOLD + c8
538
+ elseif c isa StyledStrings. RGBTuple
539
+ return r32 (c. r) + g32 (c. g) + b32 (c. b)
540
+ end :: ColorType
541
+ else # 0-255 ansi stored in a UInt32
542
+ return THRESHOLD + if c isa Symbol
543
+ c4 = get (StyledStrings. ANSI_4BIT_COLORS, c, nothing )
544
+ ansi_4bit_to_8bit (UInt8 (c4))
545
+ elseif c isa StyledStrings. RGBTuple
546
+ to_256_colors (c)
547
+ end :: UInt8
561
548
end :: ColorType
562
- else # 0-255 ansi stored in a UInt32
563
- THRESHOLD + if c. style ≡ Crayons. COLORS_24BIT
564
- Crayons. to_256_colors (c). r
565
- elseif c. style ≡ Crayons. COLORS_256
566
- c. r
567
- elseif c. style ≡ Crayons. COLORS_16
568
- ansi_4bit_to_8bit (c. r)
569
- end :: UInt8
570
- end :: ColorType
549
+ end
571
550
572
551
complement (color:: UserColorType ):: ColorType = complement (ansi_color (color))
573
552
complement (color:: ColorType ):: ColorType = if color ≡ INVALID_COLOR
0 commit comments