Skip to content

Commit

Permalink
Change ANSIColor rgb fields to UInt8 (#41)
Browse files Browse the repository at this point in the history
* Change ANSIColor rgb fields to UInt8

* Change instances of the ANSIColor constructor

Follow up to issue #40 and PR #41

Write number literals using hexadecimal notation or call the
UInt8 constructor explicitly (Note that Julia won't implicitly cast
number literals).

* Add optional values ANSIColor constructor

* Modify 16/256 color constructor (integer values)

Allow the third ANSIColor constructor to take integers smaller than
64/32 bits, so that the values are passed (or casted) as UInt8 at a
higher level, without breaking code that still sends an Int32/64.
  • Loading branch information
savq authored Aug 31, 2020
1 parent 8a92a6f commit 4e673f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
25 changes: 11 additions & 14 deletions src/crayon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,16 @@ COLORS_256,
COLORS_24BIT)

struct ANSIColor
r::Int # [0-9, 60-69], for 16 colors, 0-255 for 256 colors and 24 bit
g::Int # [0-255] Only used for 24 bit colors
b::Int # [0-255] Only used for 24 bit colors
r::UInt8 # [0-9, 60-69] for 16 colors, 0-255 for 256 colors
g::UInt8
b::UInt8
style::ColorMode
active::Bool
function ANSIColor(r::Int, g::Int = 0, b::Int = 0, style::ColorMode = COLORS_16, active = true)
for v in (r, g, b)
!(0 <= v <= 255) && throw(ArgumentError("RGB color component has to be between 0 and 255"))
end
return new(r, g, b, style, active)
end
end


ANSIColor() = ANSIColor(0, 0, 0, COLORS_16, false)
ANSIColor(val::Int, style::ColorMode, active::Bool = true) = ANSIColor(val, 0, 0, style, active)
ANSIColor(r, g, b, style::ColorMode=COLORS_16, active=true) = ANSIColor(UInt8(r), UInt8(g), UInt8(b), style, active)
ANSIColor() = ANSIColor(0x0, 0x0, 0x0, COLORS_16, false)
ANSIColor(val::Integer, style::ColorMode, active::Bool = true) = ANSIColor(UInt8(val), 0, 0, style, active)

red(x::ANSIColor) = x.r
green(x::ANSIColor) = x.g
Expand All @@ -67,7 +61,7 @@ val(x::ANSIColor) = x.r

# The inverse sets the color to default.
# No point making active if color already is default
Base.inv(x::ANSIColor) = ANSIColor(9, 0, 0, COLORS_16, x.active && !(x.style == COLORS_16 && x.r == 9))
Base.inv(x::ANSIColor) = ANSIColor(0x9, 0x0, 0x0, COLORS_16, x.active && !(x.style == COLORS_16 && x.r == 9))

struct ANSIStyle
on::Bool
Expand Down Expand Up @@ -137,7 +131,10 @@ function Base.show(io::IO, x::Crayon)
end

_ishex(c::Char) = isdigit(c) || ('a' <= c <= 'f') || ('A' <= c <= 'F')
_torgb(hex::UInt32) = Int(hex << 8 >> 24), Int(hex << 16 >> 24), Int(hex << 24 >> 24)

function _torgb(hex::UInt32)::NTuple{3, UInt8}
(hex << 8 >> 24, hex << 16 >> 24, hex << 24 >> 24)
end

function _parse_color(c::Union{Integer,Symbol,NTuple{3,Integer},UInt32})
ansicol = ANSIColor()
Expand Down
8 changes: 4 additions & 4 deletions src/crayon_stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ end
Base.print(io::IO, cs::CrayonStack) = print(io, cs.crayons[end])

function CrayonStack(; incremental::Bool = false)
CrayonStack(incremental, [Crayon(ANSIColor(9, COLORS_16, !incremental),
ANSIColor(9, COLORS_16, !incremental),
CrayonStack(incremental, [Crayon(ANSIColor(0x9, COLORS_16, !incremental),
ANSIColor(0x9, COLORS_16, !incremental),
ANSIStyle(false, !incremental),
ANSIStyle(false, !incremental),
ANSIStyle(false, !incremental),
Expand Down Expand Up @@ -70,8 +70,8 @@ function Base.pop!(cs::CrayonStack)
c = pop!(cs.crayons)
pc = cs.crayons[end]
if length(cs.crayons) == 1
pc = Crayon(ANSIColor(9, COLORS_16, true),
ANSIColor(9, COLORS_16, true),
pc = Crayon(ANSIColor(0x9, COLORS_16, true),
ANSIColor(0x9, COLORS_16, true),
ANSIStyle(false, true),
ANSIStyle(false, true),
ANSIStyle(false, true),
Expand Down
8 changes: 4 additions & 4 deletions src/downcasts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ function to_256_colors(color::ANSIColor)
r, g, b = color.r, color.g, color.b
r24, g24, b24 = map(c->round(Int, c * 23 / 256), (r, g, b))
if r24 == g24 == b24
return ANSIColor(232 + r24, COLORS_256, color.active)
return ANSIColor(UInt8(232 + r24), COLORS_256, color.active)
else
r6, g6, b6 = map(c->round(Int, c * 5 / 256), (r, g, b))
return ANSIColor(16 + 36 * r6 + 6 * g6 + b6, COLORS_256, color.active)
return ANSIColor(UInt8(16 + 36 * r6 + 6 * g6 + b6), COLORS_256, color.active)
end
end

Expand Down Expand Up @@ -91,5 +91,5 @@ function to_system_colors(color::ANSIColor)
round(Int, r / 255))
value == 2 && (ansi += 60)
end
return ANSIColor(ansi, COLORS_16, color.active)
end
return ANSIColor(UInt8(ansi), COLORS_16, color.active)
end

0 comments on commit 4e673f7

Please sign in to comment.