Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Nov 10, 2024
1 parent 0313a66 commit 2182598
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
46 changes: 26 additions & 20 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,17 @@ styledstrings_color(color::ColorType) =
StyledStrings.Legacy.legacy_color(Int(color - THRESHOLD))
end

print_color(io::IO, face::StyledStrings.Face, args...) = print(io, StyledStrings.face!(Base.annotatedstring(args...), face))
print_color(io::IO, face::StyledStrings.Face, args...) =
print(io, StyledStrings.face!(Base.annotatedstring(args...), face))
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)
face = StyledStrings.Face(foreground = styledstrings_color(color), background = styledstrings_color(bgcol))
face = StyledStrings.Face(
foreground = styledstrings_color(color),
background = styledstrings_color(bgcol),
)
print_color(io, face, args...)
else
print(io, args...)
Expand Down Expand Up @@ -524,7 +528,8 @@ 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::NTuple{3,Integer})::ColorType = r32(color[1]) + g32(color[2]) + b32(color[3])
ansi_color(color::NTuple{3,Integer})::ColorType =
r32(color[1]) + g32(color[2]) + b32(color[3])

ansi_color(color::ColorType)::ColorType = color # no-op
ansi_color(face::StyledStrings.Face) = ansi_color(face.foreground) # ignore bg & styles
Expand All @@ -548,24 +553,25 @@ function to_256_colors(color)
UInt8(ansi)
end

ansi_color(color::StyledStrings.SimpleColor)::ColorType = let c = color.value
if COLORMODE[] COLORMODE_24BIT
if c isa Symbol
c4 = StyledStrings.ANSI_4BIT_COLORS[c]
c8 = ansi_4bit_to_8bit(UInt8(c4))
return USE_LUT[] ? LUT_8BIT[c8 + 1] : THRESHOLD + c8
elseif c isa StyledStrings.RGBTuple
return r32(c.r) + g32(c.g) + b32(c.b)
end::ColorType
else # 0-255 ansi stored in a UInt32
return THRESHOLD + if c isa Symbol
c4 = StyledStrings.ANSI_4BIT_COLORS[c]
ansi_4bit_to_8bit(UInt8(c4))
elseif c isa StyledStrings.RGBTuple
to_256_colors(c)
end::UInt8
ansi_color(color::StyledStrings.SimpleColor)::ColorType =
let c = color.value
if COLORMODE[] COLORMODE_24BIT
if c isa Symbol
c4 = StyledStrings.ANSI_4BIT_COLORS[c]
c8 = ansi_4bit_to_8bit(UInt8(c4))
return USE_LUT[] ? LUT_8BIT[c8 + 1] : THRESHOLD + c8
elseif c isa StyledStrings.RGBTuple
return r32(c.r) + g32(c.g) + b32(c.b)
end::ColorType
else # 0-255 ansi stored in a UInt32
return THRESHOLD + if c isa Symbol
c4 = StyledStrings.ANSI_4BIT_COLORS[c]
ansi_4bit_to_8bit(UInt8(c4))
elseif c isa StyledStrings.RGBTuple
to_256_colors(c)
end::UInt8
end
end
end

complement(color::UserColorType)::ColorType = complement(ansi_color(color))
complement(color::ColorType)::ColorType = if color INVALID_COLOR
Expand Down
3 changes: 2 additions & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
border_right_cbar_pad * '\n',
🗹;
p_width = p_width,
color = io_color ? StyledStrings.Face(foreground = :white, weight = :bold) : nothing,
color = io_color ? StyledStrings.Face(foreground = :white, weight = :bold) :
nothing,
)
h_lbl = print_labels(
io,
Expand Down

0 comments on commit 2182598

Please sign in to comment.