Skip to content

Commit cf3055a

Browse files
authored
Fix downcast to 256 colors (#59)
1 parent 6a5a3d6 commit cf3055a

File tree

2 files changed

+387
-145
lines changed

2 files changed

+387
-145
lines changed

src/downcasts.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ end
2020

2121
function to_256_colors(color::ANSIColor)
2222
@assert color.style == COLORS_24BIT
23-
r, g, b = color.r, color.g, color.b
24-
r24, g24, b24 = map(c->round(Int, c * 23 / 256), (r, g, b))
25-
if r24 == g24 == b24
26-
return ANSIColor(UInt8(232 + r24), COLORS_256, color.active)
23+
r, g, b = rgb = color.r, color.g, color.b
24+
ansi = if r == g == b && r % 10 == 8
25+
232 + min((r - 8) ÷ 10, 23) # gray level
26+
elseif all(map(c -> (c & 0x1) == 0 && (c > 0 ? c == 128 || c == 192 : true), rgb))
27+
(r >> 7) + 2(g >> 7) + 4(b >> 7) # primary color
2728
else
28-
r6, g6, b6 = map(c->round(Int, c * 5 / 256), (r, g, b))
29-
return ANSIColor(UInt8(16 + 36 * r6 + 6 * g6 + b6), COLORS_256, color.active)
29+
r6, g6, b6 = map(c -> c < 48 ? 0 : (c < 114 ? 1 : trunc(Int, (c - 35) / 40)), rgb)
30+
16 + 36r6 + 6g6 + b6 # cube 6x6x6
3031
end
32+
return ANSIColor(UInt8(ansi), COLORS_256, color.active)
3133
end
3234

3335
# 24bit -> 16 system colors
@@ -54,7 +56,7 @@ end
5456
function compute_value(r, g, b)
5557
r′, g′, b′ = (r, g, b) ./ 255
5658
Cmax = max(r′, g′, b′)
57-
return Cmax * 100
59+
return 100Cmax
5860
#=
5961
# This is not needed
6062
Cmin = min(r′, g′, b′)
@@ -85,10 +87,11 @@ function to_system_colors(color::ANSIColor)
8587
if (value == 0)
8688
ansi = 0
8789
else
88-
ansi =
89-
((round(Int, b / 255) << 2) |
90-
(round(Int, g / 255) << 1) |
91-
round(Int, r / 255))
90+
ansi = (
91+
(round(Int, b / 255) << 2) |
92+
(round(Int, g / 255) << 1) |
93+
round(Int, r / 255)
94+
)
9295
value == 2 && (ansi += 60)
9396
end
9497
return ANSIColor(UInt8(ansi), COLORS_16, color.active)

0 commit comments

Comments
 (0)