Skip to content

Commit 0b47db0

Browse files
committed
Fix the truecolor display on windows
Close #42
1 parent b728d53 commit 0b47db0

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

cfonts/cli.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ def parse_args() -> argparse.Namespace:
6363
parser.add_argument(
6464
"-f",
6565
"--font",
66-
default=consts.FontFaces.block,
67-
choices=consts.FontFaces,
68-
type=consts.FontFaces,
66+
default=consts.FontFaces.block.value,
67+
choices=[f.value for f in consts.FontFaces],
6968
help="Use to define the font face",
7069
)
7170
parser.add_argument(
@@ -146,7 +145,7 @@ def main() -> None:
146145
else:
147146
gradient = None
148147
options = {
149-
"font": args.font.value,
148+
"font": args.font,
150149
"colors": colors,
151150
"background": args.background,
152151
"align": args.align,

cfonts/colors.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
22
Utility functions for handling terminal colors
33
"""
4-
import colorsys
54
import os
6-
from typing import Iterable, List, Mapping, NamedTuple, Tuple
5+
from typing import Iterable, List, Mapping, NamedTuple, Tuple, no_type_check
6+
7+
import colorama
78

89
from .consts import ANSI_COLORS, ANSI_RGB
910

@@ -17,6 +18,15 @@ class Style(NamedTuple):
1718
_Hsv = Tuple[float, float, float]
1819

1920

21+
def support_truecolor() -> bool:
22+
return os.name != "nt" or (
23+
os.getenv("ANSICON") is not None
24+
or os.getenv("WT_SESSION") is not None
25+
or "ON" == os.getenv("ConEmuANSI")
26+
or "xterm" == os.getenv("Term")
27+
)
28+
29+
2030
def hex_to_rgb(hex_string: str) -> _Rgb:
2131
"""Return a tuple of red, green and blue components for the color
2232
given as #rrggbb.
@@ -33,12 +43,56 @@ def rgb_to_hex(rgb: _Rgb) -> str:
3343
return "#" + "".join("%02x" % c for c in rgb)
3444

3545

46+
@no_type_check
3647
def rgb_to_hsv(rgb: _Rgb) -> _Hsv:
37-
return colorsys.rgb_to_hsv(*rgb)
48+
r, g, b = rgb
49+
r /= 255
50+
g /= 255
51+
b /= 255
52+
53+
max_value = max(r, g, b)
54+
min_value = min(r, g, b)
55+
diff = max_value - min_value
56+
57+
h, s, v = 0, diff / max_value if max_value > 0 else 0, max_value
58+
59+
if max_value == min_value:
60+
h = 0
61+
elif max_value == r:
62+
h = 60 * (g - b) / diff
63+
if g < b:
64+
h += 360
65+
elif max_value == g:
66+
h = 60 * (b - r) / diff + 120
67+
else:
68+
h = 60 * (r - g) / diff + 240
69+
70+
return h, (s * 100), (v * 100)
3871

3972

4073
def hsv_to_rgb(hsv: _Hsv) -> _Rgb:
41-
return tuple(int(c) for c in colorsys.hsv_to_rgb(*hsv)) # type: ignore
74+
h, s, v = hsv
75+
h /= 60
76+
s /= 100
77+
v /= 100
78+
hi = int(h) % 6
79+
80+
f = h - int(h)
81+
p = 255 * v * (1 - s)
82+
q = 255 * v * (1 - (s * f))
83+
t = 255 * v * (1 - (s * (1 - f)))
84+
v *= 255
85+
86+
result = {
87+
0: (v, t, p),
88+
1: (q, v, p),
89+
2: (p, v, t),
90+
3: (p, q, v),
91+
4: (t, p, v),
92+
5: (v, p, q),
93+
}[hi]
94+
r, g, b = result
95+
return int(r), int(g), int(b)
4296

4397

4498
def _color_distance(left: _Hsv, right: _Hsv) -> float:
@@ -147,10 +201,11 @@ def rgb_style(self, color: _Rgb, background: bool) -> Style:
147201
return Style("\x1b[{};2;{};{};{}m".format(open_bit, r, g, b), close)
148202

149203

150-
if (os.getenv("DISABLE_TRUECOLOR") or os.name == "nt") and not os.getenv(
204+
if (os.getenv("DISABLE_TRUECOLOR") or not support_truecolor()) and not os.getenv(
151205
"ENABLE_TRUECOLOR"
152206
):
153207
# Disable truecolor for windows
154208
pen = AnsiPen()
209+
colorama.init()
155210
else:
156211
pen = TrueColorPen()

cfonts/core.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313
import re
1414
from typing import List, Mapping, Optional, Tuple
1515

16-
import colorama
17-
1816
from .colors import pen
1917
from .consts import ALIGNMENT, CHARS, SIZE, BgColors, CandyColors, Colors, FontFaces
2018

21-
colorama.init()
22-
2319

2420
class Font:
2521
colors: int

0 commit comments

Comments
 (0)