Skip to content

Commit

Permalink
Merge 24d2c24 into 72e8c1d
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jan 28, 2022
2 parents 72e8c1d + 24d2c24 commit f6dfcf6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plumbum/cli/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if loc is None or loc.startswith("en"):

class NullTranslation:
def gettext(self, str1): # pylint: disable=no-self-use
def gettext(self, str1: str) -> str: # pylint: disable=no-self-use
return str1

def ngettext(self, str1, strN, n): # pylint: disable=no-self-use
Expand Down
10 changes: 6 additions & 4 deletions plumbum/cli/termsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import platform
import warnings
from struct import Struct
from typing import Optional, Tuple

from plumbum import local


def get_terminal_size(default=(80, 25)):
def get_terminal_size(default: Tuple[int, int] = (80, 25)) -> Tuple[int, int]:
"""
Get width and height of console; works on linux, os x, windows and cygwin
Expand Down Expand Up @@ -71,18 +72,19 @@ def _get_terminal_size_tput(): # pragma: no cover
return None


def _ioctl_GWINSZ(fd):
def _ioctl_GWINSZ(fd: int) -> Optional[Tuple[int, int]]:
yx = Struct("hh")
try:
import fcntl
import termios

return yx.unpack(fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
# TODO: Clean this up. Problems could be hidden by the broad except.
return yx.unpack(fcntl.ioctl(fd, termios.TIOCGWINSZ, b"1234")) # type: ignore[return-value]
except Exception:
return None


def _get_terminal_size_linux():
def _get_terminal_size_linux() -> Optional[Tuple[int, int]]:
cr = _ioctl_GWINSZ(0) or _ioctl_GWINSZ(1) or _ioctl_GWINSZ(2)
if not cr:
try:
Expand Down
20 changes: 10 additions & 10 deletions plumbum/colorlib/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
You can access the index of the colors with names.index(name). You can access the
rgb values with ``r=int(html[n][1:3],16)``, etc.
"""

from typing import Tuple

color_names = """\
black
Expand Down Expand Up @@ -345,7 +345,7 @@ class FindNearest:
"""This is a class for finding the nearest color given rgb values.
Different find methods are available."""

def __init__(self, r, g, b):
def __init__(self, r: int, g: int, b: int) -> None:
self.r = r
self.b = b
self.g = g
Expand All @@ -366,23 +366,23 @@ def only_basic(self):
+ (self.b >= midlevel) * 4
)

def all_slow(self, color_slice=EMPTY_SLICE):
def all_slow(self, color_slice: slice = EMPTY_SLICE) -> int:
"""This is a slow way to find the nearest color."""
distances = [
self._distance_to_color(color) for color in color_html[color_slice]
]
return min(range(len(distances)), key=distances.__getitem__)

def _distance_to_color(self, color):
def _distance_to_color(self, color: str) -> int:
"""This computes the distance to a color, should be minimized."""
rgb = (int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16))
return (self.r - rgb[0]) ** 2 + (self.g - rgb[1]) ** 2 + (self.b - rgb[2]) ** 2

def _distance_to_color_number(self, n):
def _distance_to_color_number(self, n: int) -> int:
color = color_html[n]
return self._distance_to_color(color)

def only_colorblock(self):
def only_colorblock(self) -> int:
"""This finds the nearest color based on block system, only works
for 17-232 color values."""
rint = min(
Expand All @@ -396,11 +396,11 @@ def only_colorblock(self):
)
return 16 + 36 * rint + 6 * gint + bint

def only_simple(self):
def only_simple(self) -> int:
"""Finds the simple color-block color."""
return self.all_slow(slice(0, 16, None))

def only_grey(self):
def only_grey(self) -> int:
"""Finds the greyscale color."""
rawval = (self.r + self.b + self.g) / 3
n = min(
Expand All @@ -409,14 +409,14 @@ def only_grey(self):
)
return n + 232

def all_fast(self):
def all_fast(self) -> int:
"""Runs roughly 8 times faster than the slow version."""
colors = [self.only_simple(), self.only_colorblock(), self.only_grey()]
distances = [self._distance_to_color_number(n) for n in colors]
return colors[min(range(len(distances)), key=distances.__getitem__)]


def from_html(color):
def from_html(color: str) -> Tuple[int, int, int]:
"""Convert html hex code to rgb."""
if len(color) != 7 or color[0] != "#":
raise ValueError("Invalid length of html code")
Expand Down

0 comments on commit f6dfcf6

Please sign in to comment.