Skip to content

Commit 96ade4e

Browse files
authored
Fixes to respect bright colors in palette (#126)
1 parent 0165811 commit 96ade4e

File tree

2 files changed

+94
-41
lines changed

2 files changed

+94
-41
lines changed

ansi2html/style.py

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def index2(grey: int) -> str:
114114
"#00ffff",
115115
"#ffffff",
116116
),
117+
# Based on the "osx" palette in previous versions of ansi2html.
117118
"osx": (
118119
"#000000",
119120
"#c23621",
@@ -123,8 +124,54 @@ def index2(grey: int) -> str:
123124
"#d338d3",
124125
"#33bbc8",
125126
"#cbcccd",
126-
)
127-
* 2,
127+
"#404040",
128+
"#ff7661",
129+
"#65fc64",
130+
"#eded67",
131+
"#896eff",
132+
"#ff78ff",
133+
"#73fbff",
134+
"#ffffff",
135+
),
136+
# Based on the "Basic" palette in macOS Terminal.
137+
"osx-basic": (
138+
"#000000",
139+
"#800000",
140+
"#008000",
141+
"#808000",
142+
"#000080",
143+
"#800080",
144+
"#008080",
145+
"#808080",
146+
"#666666",
147+
"#e60000",
148+
"#00d900",
149+
"#e6e600",
150+
"#0000ff",
151+
"#e600e6",
152+
"#00e6e6",
153+
"#e6e6e6",
154+
),
155+
# Based on the "Solid Colors" palette in macOS Terminal.
156+
# The colors are brighter than osx-basic.
157+
"osx-solid-colors": (
158+
"#000000",
159+
"#990000",
160+
"#00a600",
161+
"#999900",
162+
"#0000b3",
163+
"#b300b3",
164+
"#00a6b3",
165+
"#bfbfbf",
166+
"#666666",
167+
"#e60000",
168+
"#00d900",
169+
"#e6e600",
170+
"#0000ff",
171+
"#e600e6",
172+
"#00e6e6",
173+
"#e6e6e6",
174+
),
128175
# http://ethanschoonover.com/solarized
129176
"solarized": (
130177
"#262626",
@@ -194,7 +241,9 @@ def intensify(color: str, dark_bg: bool, amount: int = 64) -> str:
194241

195242

196243
def get_styles(
197-
dark_bg: bool = True, line_wrap: bool = True, scheme: str = "ansi2html"
244+
dark_bg: bool = True,
245+
line_wrap: bool = True,
246+
scheme: str = "ansi2html",
198247
) -> List[Rule]:
199248
css = [
200249
Rule(
@@ -224,11 +273,20 @@ def get_styles(
224273
Rule(".ansi9", text_decoration="line-through"),
225274
]
226275

276+
try:
277+
pal = SCHEME[scheme]
278+
except KeyError as e:
279+
raise ValueError(f"Unsupported color scheme {scheme!r}") from e
280+
281+
if len(pal) < 16:
282+
raise Exception(
283+
f"Color scheme {scheme!r} specifies fewer than 16 colors. 16 colors are required."
284+
)
285+
227286
# This is 8x2 palette of 3/4-bit color mode described at
228287
# https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
229288
# .ansi{30..37} is foreground
230289
# .ansi{40..47} is background
231-
pal = SCHEME[scheme]
232290
for _index in range(8):
233291
css.append(Rule(".ansi3%s" % _index, color=pal[_index]))
234292
css.append(Rule(".inv3%s" % _index, background_color=pal[_index]))
@@ -241,19 +299,14 @@ def get_styles(
241299
# .ansi{90..97} is foreground
242300
# .ansi{100..107} is background
243301
for _index in range(8):
244-
css.append(Rule(".ansi9%s" % _index, color=intensify(pal[_index], dark_bg)))
245-
css.append(
246-
Rule(".inv9%s" % _index, background_color=intensify(pal[_index], dark_bg))
247-
)
302+
css.append(Rule(".ansi9%s" % _index, color=pal[_index + 8]))
303+
css.append(Rule(".inv9%s" % _index, background_color=pal[_index + 8]))
248304
for _index in range(8):
249-
css.append(
250-
Rule(".ansi10%s" % _index, background_color=intensify(pal[_index], dark_bg))
251-
)
252-
css.append(Rule(".inv10%s" % _index, color=intensify(pal[_index], dark_bg)))
305+
css.append(Rule(".ansi10%s" % _index, background_color=pal[_index + 8]))
306+
css.append(Rule(".inv10%s" % _index, color=pal[_index + 8]))
253307

254308
# This is the first 16 palette slots of 8-bit color mode described at
255309
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
256-
pal = SCHEME[scheme]
257310
for _index in range(len(pal)):
258311
css.append(Rule(".ansi38-%s" % _index, color=pal[_index]))
259312
css.append(Rule(".inv38-%s" % _index, background_color=pal[_index]))

tests/produce_headers.txt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,36 @@
4545
.inv46 { color: #00aaaa; }
4646
.ansi47 { background-color: #F5F1DE; }
4747
.inv47 { color: #F5F1DE; }
48-
.ansi90 { color: #404356; }
49-
.inv90 { background-color: #404356; }
50-
.ansi91 { color: #ea4040; }
51-
.inv91 { background-color: #ea4040; }
52-
.ansi92 { color: #40ea40; }
53-
.inv92 { background-color: #40ea40; }
54-
.ansi93 { color: #ea9540; }
55-
.inv93 { background-color: #ea9540; }
56-
.ansi94 { color: #4040ea; }
57-
.inv94 { background-color: #4040ea; }
58-
.ansi95 { color: #ff90e8; }
59-
.inv95 { background-color: #ff90e8; }
60-
.ansi96 { color: #40eaea; }
61-
.inv96 { background-color: #40eaea; }
48+
.ansi90 { color: #7f7f7f; }
49+
.inv90 { background-color: #7f7f7f; }
50+
.ansi91 { color: #ff0000; }
51+
.inv91 { background-color: #ff0000; }
52+
.ansi92 { color: #00ff00; }
53+
.inv92 { background-color: #00ff00; }
54+
.ansi93 { color: #ffff00; }
55+
.inv93 { background-color: #ffff00; }
56+
.ansi94 { color: #5c5cff; }
57+
.inv94 { background-color: #5c5cff; }
58+
.ansi95 { color: #ff00ff; }
59+
.inv95 { background-color: #ff00ff; }
60+
.ansi96 { color: #00ffff; }
61+
.inv96 { background-color: #00ffff; }
6262
.ansi97 { color: #ffffff; }
6363
.inv97 { background-color: #ffffff; }
64-
.ansi100 { background-color: #404356; }
65-
.inv100 { color: #404356; }
66-
.ansi101 { background-color: #ea4040; }
67-
.inv101 { color: #ea4040; }
68-
.ansi102 { background-color: #40ea40; }
69-
.inv102 { color: #40ea40; }
70-
.ansi103 { background-color: #ea9540; }
71-
.inv103 { color: #ea9540; }
72-
.ansi104 { background-color: #4040ea; }
73-
.inv104 { color: #4040ea; }
74-
.ansi105 { background-color: #ff90e8; }
75-
.inv105 { color: #ff90e8; }
76-
.ansi106 { background-color: #40eaea; }
77-
.inv106 { color: #40eaea; }
64+
.ansi100 { background-color: #7f7f7f; }
65+
.inv100 { color: #7f7f7f; }
66+
.ansi101 { background-color: #ff0000; }
67+
.inv101 { color: #ff0000; }
68+
.ansi102 { background-color: #00ff00; }
69+
.inv102 { color: #00ff00; }
70+
.ansi103 { background-color: #ffff00; }
71+
.inv103 { color: #ffff00; }
72+
.ansi104 { background-color: #5c5cff; }
73+
.inv104 { color: #5c5cff; }
74+
.ansi105 { background-color: #ff00ff; }
75+
.inv105 { color: #ff00ff; }
76+
.ansi106 { background-color: #00ffff; }
77+
.inv106 { color: #00ffff; }
7878
.ansi107 { background-color: #ffffff; }
7979
.inv107 { color: #ffffff; }
8080
.ansi38-0 { color: #000316; }

0 commit comments

Comments
 (0)