Skip to content

Commit 4e673f7

Browse files
authored
Change ANSIColor rgb fields to UInt8 (#41)
* 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.
1 parent 8a92a6f commit 4e673f7

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/crayon.jl

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,16 @@ COLORS_256,
4343
COLORS_24BIT)
4444

4545
struct ANSIColor
46-
r::Int # [0-9, 60-69], for 16 colors, 0-255 for 256 colors and 24 bit
47-
g::Int # [0-255] Only used for 24 bit colors
48-
b::Int # [0-255] Only used for 24 bit colors
46+
r::UInt8 # [0-9, 60-69] for 16 colors, 0-255 for 256 colors
47+
g::UInt8
48+
b::UInt8
4949
style::ColorMode
5050
active::Bool
51-
function ANSIColor(r::Int, g::Int = 0, b::Int = 0, style::ColorMode = COLORS_16, active = true)
52-
for v in (r, g, b)
53-
!(0 <= v <= 255) && throw(ArgumentError("RGB color component has to be between 0 and 255"))
54-
end
55-
return new(r, g, b, style, active)
56-
end
5751
end
5852

59-
60-
ANSIColor() = ANSIColor(0, 0, 0, COLORS_16, false)
61-
ANSIColor(val::Int, style::ColorMode, active::Bool = true) = ANSIColor(val, 0, 0, style, active)
53+
ANSIColor(r, g, b, style::ColorMode=COLORS_16, active=true) = ANSIColor(UInt8(r), UInt8(g), UInt8(b), style, active)
54+
ANSIColor() = ANSIColor(0x0, 0x0, 0x0, COLORS_16, false)
55+
ANSIColor(val::Integer, style::ColorMode, active::Bool = true) = ANSIColor(UInt8(val), 0, 0, style, active)
6256

6357
red(x::ANSIColor) = x.r
6458
green(x::ANSIColor) = x.g
@@ -67,7 +61,7 @@ val(x::ANSIColor) = x.r
6761

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

7266
struct ANSIStyle
7367
on::Bool
@@ -137,7 +131,10 @@ function Base.show(io::IO, x::Crayon)
137131
end
138132

139133
_ishex(c::Char) = isdigit(c) || ('a' <= c <= 'f') || ('A' <= c <= 'F')
140-
_torgb(hex::UInt32) = Int(hex << 8 >> 24), Int(hex << 16 >> 24), Int(hex << 24 >> 24)
134+
135+
function _torgb(hex::UInt32)::NTuple{3, UInt8}
136+
(hex << 8 >> 24, hex << 16 >> 24, hex << 24 >> 24)
137+
end
141138

142139
function _parse_color(c::Union{Integer,Symbol,NTuple{3,Integer},UInt32})
143140
ansicol = ANSIColor()

src/crayon_stack.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ end
77
Base.print(io::IO, cs::CrayonStack) = print(io, cs.crayons[end])
88

99
function CrayonStack(; incremental::Bool = false)
10-
CrayonStack(incremental, [Crayon(ANSIColor(9, COLORS_16, !incremental),
11-
ANSIColor(9, COLORS_16, !incremental),
10+
CrayonStack(incremental, [Crayon(ANSIColor(0x9, COLORS_16, !incremental),
11+
ANSIColor(0x9, COLORS_16, !incremental),
1212
ANSIStyle(false, !incremental),
1313
ANSIStyle(false, !incremental),
1414
ANSIStyle(false, !incremental),
@@ -70,8 +70,8 @@ function Base.pop!(cs::CrayonStack)
7070
c = pop!(cs.crayons)
7171
pc = cs.crayons[end]
7272
if length(cs.crayons) == 1
73-
pc = Crayon(ANSIColor(9, COLORS_16, true),
74-
ANSIColor(9, COLORS_16, true),
73+
pc = Crayon(ANSIColor(0x9, COLORS_16, true),
74+
ANSIColor(0x9, COLORS_16, true),
7575
ANSIStyle(false, true),
7676
ANSIStyle(false, true),
7777
ANSIStyle(false, true),

src/downcasts.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ function to_256_colors(color::ANSIColor)
2323
r, g, b = color.r, color.g, color.b
2424
r24, g24, b24 = map(c->round(Int, c * 23 / 256), (r, g, b))
2525
if r24 == g24 == b24
26-
return ANSIColor(232 + r24, COLORS_256, color.active)
26+
return ANSIColor(UInt8(232 + r24), COLORS_256, color.active)
2727
else
2828
r6, g6, b6 = map(c->round(Int, c * 5 / 256), (r, g, b))
29-
return ANSIColor(16 + 36 * r6 + 6 * g6 + b6, COLORS_256, color.active)
29+
return ANSIColor(UInt8(16 + 36 * r6 + 6 * g6 + b6), COLORS_256, color.active)
3030
end
3131
end
3232

@@ -91,5 +91,5 @@ function to_system_colors(color::ANSIColor)
9191
round(Int, r / 255))
9292
value == 2 && (ansi += 60)
9393
end
94-
return ANSIColor(ansi, COLORS_16, color.active)
95-
end
94+
return ANSIColor(UInt8(ansi), COLORS_16, color.active)
95+
end

0 commit comments

Comments
 (0)