Skip to content

Commit

Permalink
drop python 3.7, upgrade dependencies (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Feb 16, 2024
1 parent b7420ca commit 1759fd2
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 687 deletions.
20 changes: 8 additions & 12 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: abatilo/actions-poetry@v2
with:
poetry-version: "1.2.2"
- run: poetry config virtualenvs.create false
- run: poetry install -E pygments -E rich
- run: isort --check .
- run: black --check .
- run: pylint catppuccin
- run: mypy .
- uses: abatilo/actions-poetry@v3
- run: poetry install --all-extras
- run: poetry run ruff check
- run: poetry run ruff format --check --diff
- run: poetry run mypy .
12 changes: 4 additions & 8 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"
- uses: abatilo/actions-poetry@v2
with:
poetry-version: "1.2.2"
- run: poetry self add "poetry-dynamic-versioning[plugin]"
- run: poetry config virtualenvs.create false
- run: poetry config pypi-token.pypi ${{ secrets.PYPI_API_TOKEN }}
- run: poetry build
- run: poetry publish
- run: poetry publish --build
15 changes: 6 additions & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: abatilo/actions-poetry@v2
with:
poetry-version: "1.2.2"
- run: poetry config virtualenvs.create false
- run: poetry install -E pygments
- run: pytest --cov catppuccin
- uses: abatilo/actions-poetry@v3
- run: poetry install --all-extras
- run: poetry run pytest --cov catppuccin
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ instructions](https://python-poetry.org/docs/#installation).
Install the project's dependencies including extras:

```bash
poetry install -E pygments
poetry install --all-extras
```

#### Code Standards
Expand All @@ -114,9 +114,8 @@ Before committing changes, it is recommended to run the following tools to
ensure consistency in the codebase.

```bash
isort .
black .
pylint catppuccin
ruff format
ruff check
mypy .
pytest --cov catppuccin
```
Expand All @@ -128,7 +127,7 @@ path.

## 💝 Thanks to

- [backwardspy](https://github.com/backwardspy)
- [backwardspy](https://github.com/backwardspy)

 

Expand Down
6 changes: 4 additions & 2 deletions catppuccin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""🐍 Soothing pastel theme for Python."""
from catppuccin.colour import Colour as Colour
from catppuccin.flavour import Flavour as Flavour
from catppuccin.colour import Colour
from catppuccin.flavour import Flavour

__all__ = ["Colour", "Flavour"]
51 changes: 32 additions & 19 deletions catppuccin/colour.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""
Functionality relating to individual colours.
"""
"""Functionality relating to individual colours."""
from __future__ import annotations

import re
from dataclasses import dataclass
from typing import Any, Tuple

from catppuccin.hsl import rgb_to_hsl

HEXLEN_NO_ALPHA = 6
HEXLEN_ALPHA = 8
MAX_ALPHA = 255


@dataclass(frozen=True)
class Colour:
Expand All @@ -17,58 +18,70 @@ class Colour:
red: int
green: int
blue: int
alpha: int = 255
alpha: int = MAX_ALPHA

@property
def rgb(self) -> Tuple[int, int, int]:
def rgb(self) -> tuple[int, int, int]:
"""Get the colour as a 3-tuple of red, green, and blue."""
return self.red, self.green, self.blue

@property
def rgba(self) -> Tuple[int, int, int, int]:
def rgba(self) -> tuple[int, int, int, int]:
"""Get the colour as a 4-tuple of red, green, blue, and alpha."""
return self.red, self.green, self.blue, self.alpha

@property
def hsl(self) -> Tuple[float, float, float]:
def hsl(self) -> tuple[float, float, float]:
"""Get the colour as a 3-tuple of hue, saturation, and lightness."""
return rgb_to_hsl(*self.rgb)

@property
def hsla(self) -> Tuple[float, float, float, float]:
def hsla(self) -> tuple[float, float, float, float]:
"""Get the colour as a 4-tuple of hue, saturation, lightness, and alpha."""
return (*self.hsl, self.alpha)

@property
def hex(self) -> str:
"""Get the colour as a lowercase hex string."""
if self.alpha < 255:
if self.alpha < MAX_ALPHA:
return f"{self.red:02x}{self.green:02x}{self.blue:02x}{self.alpha:02x}"
return f"{self.red:02x}{self.green:02x}{self.blue:02x}"

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
"""Check equality against another colour."""
if not isinstance(other, Colour):
raise ValueError("Cannot check equality with non-colour types.")
e = "Cannot check equality with non-colour types."
raise TypeError(e)

return self.hex == other.hex

@classmethod
def from_hex(cls, hex_string: str) -> Colour:
"""Create a colour from hex string."""
if len(hex_string) not in (6, 8):
raise ValueError("Hex string must be 6 or 8 characters long.")

num_groups = 3 if len(hex_string) == 6 else 4
if len(hex_string) not in (HEXLEN_NO_ALPHA, HEXLEN_ALPHA):
e = (
f"Hex string must be {HEXLEN_NO_ALPHA} or "
f"{HEXLEN_ALPHA} characters long."
)
raise ValueError(e)

num_groups = (
HEXLEN_NO_ALPHA // 2
if len(hex_string) == HEXLEN_NO_ALPHA
else HEXLEN_ALPHA // 2
)
match = re.match(r"([\da-fA-F]{2})" * num_groups, hex_string)
if match is None:
raise ValueError("Hex string has an invalid format.")
e = "Hex string has an invalid format."
raise ValueError(e)

components = (int(col, 16) for col in match.groups())
return Colour(*components)

def opacity(self, opacity: float) -> Colour:
"""Return a new colour with the given opacity."""
if not 0 <= opacity <= 1:
raise ValueError("Opacity must be between 0 and 1.")
e = "Opacity must be between 0 and 1."
raise ValueError(e)

return Colour(self.red, self.green, self.blue, int(opacity * 255))
return Colour(self.red, self.green, self.blue, int(opacity * MAX_ALPHA))
1 change: 1 addition & 0 deletions catppuccin/extras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The extras submodule contains code for integration with other packages."""
16 changes: 7 additions & 9 deletions catppuccin/extras/pygments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""
Pygments styles for all Catppuccin flavours.
"""
from typing import Dict
"""Pygments styles for all Catppuccin flavours."""
from __future__ import annotations

from pygments.style import Style
from pygments.token import (
Expand All @@ -23,7 +21,7 @@
from catppuccin.flavour import Flavour


def _make_styles(flavour: Flavour) -> Dict[_TokenType, str]:
def _make_styles(flavour: Flavour) -> dict[_TokenType, str]:
return {
Token: f"#{flavour.text.hex}",
Text: f"#{flavour.text.hex}",
Expand Down Expand Up @@ -53,7 +51,7 @@ def _make_styles(flavour: Flavour) -> Dict[_TokenType, str]:
}


class LatteStyle(Style): # pylint: disable=too-few-public-methods
class LatteStyle(Style):
"""Catppuccin Latte pygments style."""

_flavour = Flavour.latte()
Expand All @@ -65,7 +63,7 @@ class LatteStyle(Style): # pylint: disable=too-few-public-methods
styles = _make_styles(_flavour)


class FrappeStyle(Style): # pylint: disable=too-few-public-methods
class FrappeStyle(Style):
"""Catppuccin Frappé pygments style."""

_flavour = Flavour.frappe()
Expand All @@ -77,7 +75,7 @@ class FrappeStyle(Style): # pylint: disable=too-few-public-methods
styles = _make_styles(_flavour)


class MacchiatoStyle(Style): # pylint: disable=too-few-public-methods
class MacchiatoStyle(Style):
"""Catppuccin Macchiato pygments style."""

_flavour = Flavour.macchiato()
Expand All @@ -89,7 +87,7 @@ class MacchiatoStyle(Style): # pylint: disable=too-few-public-methods
styles = _make_styles(_flavour)


class MochaStyle(Style): # pylint: disable=too-few-public-methods
class MochaStyle(Style):
"""Catppuccin Mocha pygments style."""

_flavour = Flavour.mocha()
Expand Down
6 changes: 2 additions & 4 deletions catppuccin/extras/rich_ctp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Rich themes for all Catppuccin flavours.
"""
"""Rich themes for all Catppuccin flavours."""
from rich.theme import Theme

from catppuccin.flavour import Flavour
Expand Down Expand Up @@ -35,7 +33,7 @@ def _make_theme(flavour: Flavour) -> Theme:
"base": f"#{flavour.base.hex}",
"mantle": f"#{flavour.mantle.hex}",
"crust": f"#{flavour.crust.hex}",
}
},
)


Expand Down
6 changes: 3 additions & 3 deletions catppuccin/flavour.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Functionality relating to Catppuccin flavours.
"""Functionality relating to Catppuccin flavours.
A flavour is a collection of colours.
"""
from dataclasses import dataclass
Expand All @@ -8,7 +8,7 @@


@dataclass(frozen=True)
class Flavour: # pylint: disable=too-many-instance-attributes
class Flavour:
"""All the colours in a flavour of Catppuccin."""

name: str
Expand Down
33 changes: 12 additions & 21 deletions catppuccin/hsl.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
"""
Utilities for working with HSL colours.
"""
"""Utilities for working with HSL colours."""
from __future__ import annotations

from typing import Tuple


# pylint: disable=invalid-name
def rgb_to_hsl(r: int, g: int, b: int) -> Tuple[int, float, float]:
def rgb_to_hsl(r: int, g: int, b: int) -> tuple[int, float, float]:
"""Convert RGB to HSL."""
r1 = r / 255
g1 = g / 255
Expand All @@ -18,23 +13,19 @@ def rgb_to_hsl(r: int, g: int, b: int) -> Tuple[int, float, float]:
delta = cmax - cmin

if delta == 0:
h = 0.0
hue = 0.0
elif cmax == r1:
h = ((g1 - b1) / delta) % 6
hue = ((g1 - b1) / delta) % 6
elif cmax == g1:
h = (b1 - r1) / delta + 2
hue = (b1 - r1) / delta + 2
else:
h = (r1 - g1) / delta + 4

h = round(h * 60)
if h < 0:
h += 360
hue = (r1 - g1) / delta + 4

l = (cmax + cmin) / 2
hue = round(hue * 60)
if hue < 0:
hue += 360

if delta == 0:
s = 0.0
else:
s = delta / (1 - abs(2 * l - 1))
lightness = (cmax + cmin) / 2
saturation = 0 if delta == 0 else delta / (1 - abs(2 * lightness - 1))

return h, round(s, 2), round(l, 2)
return hue, round(saturation, 2), round(lightness, 2)

0 comments on commit 1759fd2

Please sign in to comment.