Skip to content

Commit

Permalink
Add text annotation to theme display
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Aug 5, 2024
1 parent bd4b9ef commit aed9a0c
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 67 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"
system:
Expand All @@ -41,6 +40,10 @@ jobs:

- run: pip${{matrix.python-version}} install vmklib>=2.0.3

# Begin project-specific setup.
- run: mk python-editable
# End project-specific setup.

- run: mk python-sa-types

- name: lint and build
Expand Down Expand Up @@ -74,7 +77,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=svgen version=0.6.8
repo=svgen version=0.7.0
if: |
matrix.python-version == '3.12'
&& matrix.system == 'ubuntu-latest'
Expand Down
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[BASIC]
good-names=p1,p2,x1,x2,y1,y2,rx,ry,cx,cy,dx,dy,x,y

[MESSAGES CONTROL]
disable=too-few-public-methods

[DESIGN]
max-args=7
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.4
hash=81a7bf82ba5e1d93e03c2f2677035fae
hash=9520e8fc36ee88b167c27f532fc3c1e4
=====================================
-->

# svgen ([0.6.8](https://pypi.org/project/svgen/))
# svgen ([0.7.0](https://pypi.org/project/svgen/))

[![python](https://img.shields.io/pypi/pyversions/svgen.svg)](https://pypi.org/project/svgen/)
![Build Status](https://github.com/vkottler/svgen/workflows/Python%20Package/badge.svg)
Expand All @@ -29,7 +29,6 @@

This package is tested with the following Python minor versions:

* [`python3.10`](https://docs.python.org/3.10/)
* [`python3.11`](https://docs.python.org/3.11/)
* [`python3.12`](https://docs.python.org/3.12/)

Expand Down
2 changes: 1 addition & 1 deletion config
3 changes: 3 additions & 0 deletions local/configs/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ requirements:
dev_requirements:
- setuptools-wrapper
- pytest

ci_local:
- "- run: mk python-editable"
2 changes: 1 addition & 1 deletion local/configs/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author_info:
email: vaughnkottler@gmail.com
username: vkottler

versions: ["3.10", "3.11", "3.12"]
versions: ["3.11", "3.12"]

systems:
- macos-latest
Expand Down
4 changes: 2 additions & 2 deletions local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 0
minor: 6
patch: 8
minor: 7
patch: 0
entry: svgen
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "svgen"
version = "0.6.8"
version = "0.7.0"
description = "A tool for working with scalable vector graphics."
readme = "README.md"
requires-python = ">=3.10"
requires-python = ">=3.11"
authors = [
{name = "Vaughn Kottler", email = "vaughnkottler@gmail.com"}
]
maintainers = [
{name = "Vaughn Kottler", email = "vaughnkottler@gmail.com"}
]
classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Operating System :: Microsoft :: Windows",
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=fc5241a8b8252685d9df5e59db1c172d
# hash=6d153dc423cef1450e9b2cd28628b27c
# =====================================

"""
Expand All @@ -28,7 +28,6 @@
"version": VERSION,
"description": DESCRIPTION,
"versions": [
"3.10",
"3.11",
"3.12",
],
Expand Down
4 changes: 2 additions & 2 deletions svgen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=d5836c072a305ff3cc492ddda24e295b
# hash=9e67da4714539646e56663e5f514af0d
# =====================================

"""
Expand All @@ -10,4 +10,4 @@

DESCRIPTION = "A tool for working with scalable vector graphics."
PKG_NAME = "svgen"
VERSION = "0.6.8"
VERSION = "0.7.0"
2 changes: 1 addition & 1 deletion svgen/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def generate(
"""Generate a single SVG document."""

# Set a theme for this variant.
THEMES.theme = config["theme"]
THEMES.theme = config.data["theme"]

# Add the specified directory to the import path, so external scripts
# can load their own dependencies.
Expand Down
12 changes: 6 additions & 6 deletions svgen/attribute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# built-in
from abc import ABC, abstractmethod
from typing import Dict, List, Type, TypeVar, Union
from typing import TypeVar, Union

T = TypeVar("T", bound="Attribute")

Expand Down Expand Up @@ -45,7 +45,7 @@ def encode(self, quote: str = '"', force: bool = False) -> str:

@classmethod
@abstractmethod
def decode(cls: Type[T], key: str, value: str) -> T:
def decode(cls: type[T], key: str, value: str) -> T:
"""Create this attribute from a string."""


Expand All @@ -70,25 +70,25 @@ def value(self) -> str:

@classmethod
def decode(
cls: Type["SimpleAttribute"], key: str, value: str
cls: type["SimpleAttribute"], key: str, value: str
) -> "SimpleAttribute":
"""Create this attribute from a string."""
return cls(key, value)

@staticmethod
def from_dict(data: Dict[str, Union[str, int, float]]) -> List[Attribute]:
def from_dict(data: dict[str, Union[str, int, float]]) -> list[Attribute]:
"""Get a list of attributes from dictionary data."""
return [
SimpleAttribute(key, str(value)) for key, value in data.items()
]


PossibleAttributes = Union[
Dict[str, AttributeValue], List[Attribute], Attribute
dict[str, AttributeValue], list[Attribute], Attribute
]


def attributes(data: PossibleAttributes = None) -> List[Attribute]:
def attributes(data: PossibleAttributes = None) -> list[Attribute]:
"""
Get attributes from either an existing list of attributes, or dictionary
data.
Expand Down
35 changes: 35 additions & 0 deletions svgen/cartesian/angle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
A module implementing an interface for angles.
"""

# built-in
from abc import ABC, abstractmethod
from typing import TypeVar

T = TypeVar("T", bound="Rotatable")


class Rotatable(ABC):
"""A generic interface for rotatable instances."""

@abstractmethod
def arc(self: T, count: int = 1, divisor: int = 2) -> T:
"""Rotate this angle around the circle."""


class DegreePrimitive(int, Rotatable):
"""
A class to manage integer primitives for degrees within a 0 and 360 range.
"""

def __new__(cls, val: int) -> "DegreePrimitive":
"""Construct a new degree value."""
return super().__new__(cls, val % 360)

def rotate(self, val: int) -> "DegreePrimitive":
"""Rotate this degree instance."""
return DegreePrimitive(self + val)

def arc(self, count: int = 1, divisor: int = 2) -> "DegreePrimitive":
"""Rotate this angle around the circle."""
return self.rotate(round(count * (360 / divisor)))
4 changes: 4 additions & 0 deletions svgen/color/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ def from_hsl(cls, color: Hsl) -> "Color":
"""Create a color from an hsl object."""
return cls(hsl_to_rgb(color), color)

def hsl_arc(self, **kwargs) -> "Color":
"""Get a new color based on some rotation in HSL space."""
return self.from_hsl(self.hsl.arc(**kwargs))

@classmethod
def from_ctor(cls, value: str) -> "Color":
"""Create a color from an hsl or rgb constructor string."""
Expand Down
48 changes: 34 additions & 14 deletions svgen/color/hsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,17 @@
from typing import NamedTuple

# internal
from svgen.cartesian.angle import DegreePrimitive, Rotatable
from svgen.color.alpha import DEFAULT, Alpha
from svgen.color.numbers import parse_ctor


class DegreePrimitive(int):
"""
A class to manage integer primitives for degrees within a 0 and 360 range.
"""

def __new__(cls, val: int) -> "DegreePrimitive":
"""Construct a new degree value."""
return super().__new__(cls, val % 360)


class PercentPrimitive(int):
class PercentPrimitive(int, Rotatable):
"""A class for integer percentages."""

def __new__(cls, val: int) -> "PercentPrimitive":
"""Create a new percentage value."""
val = max(val, 0)
val = min(val, 100)
return super().__new__(cls, val)
return super().__new__(cls, min(max(val, 0), 100))

def __str__(self) -> str:
"""Get this percentage as a string."""
Expand All @@ -40,6 +29,15 @@ def ratio(self) -> float:
"""Get this percentage as a ratio between 0 and 1."""
return float(self) / 100.0

def arc(self, count: int = 1, divisor: int = 2) -> "PercentPrimitive":
"""Rotate this angle around the circle."""

new_val = self + round(count * (100 / divisor))
while new_val > 100:
new_val -= 100

return PercentPrimitive(new_val)


class Hsl(NamedTuple):
"""A definition of an hsl color."""
Expand All @@ -49,6 +47,28 @@ class Hsl(NamedTuple):
lightness: PercentPrimitive
alpha: Alpha = DEFAULT

def arc(
self,
hue_count: int = 1,
hue_divisor: int = 1,
saturation_count: int = 1,
saturation_divisor: int = 1,
lightness_count: int = 1,
lightness_divisor: int = 1,
) -> "Hsl":
"""Rotate this angle around the circle."""

return Hsl(
self.hue.arc(count=hue_count, divisor=hue_divisor),
self.saturation.arc(
count=saturation_count, divisor=saturation_divisor
),
self.lightness.arc(
count=lightness_count, divisor=lightness_divisor
),
self.alpha,
)

def __str__(self) -> str:
"""Convert this hsl color to a string."""

Expand Down
12 changes: 7 additions & 5 deletions svgen/color/theme/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def visualize_theme(
theme: Union[ColorTheme, str], rect: Rectangle
theme: Union[ColorTheme, str], rect: Rectangle, columns: bool = True
) -> Iterator[Rect]:
"""
Create filled rectangle elements for this theme inside another
Expand All @@ -27,16 +27,18 @@ def visualize_theme(

assert isinstance(theme, ColorTheme)
for box, color in zip(
RectangleGrid(rect, theme.size, 1).boxes,
RectangleGrid(
rect, theme.size if columns else 1, 1 if columns else theme.size
).boxes,
theme.data.values(),
):
curr = Rect(box)
curr.style.add_color(color, "fill")
curr.assign_fill_color(color)
yield curr


def visualize(
rect: Rectangle, manager: ColorThemeManager = None
rect: Rectangle, manager: ColorThemeManager = None, columns: bool = True
) -> Iterator[Rect]:
"""Visualize all managed themes within a provided rectangle."""

Expand All @@ -47,4 +49,4 @@ def visualize(
RectangleGrid(rect, 1, manager.size).boxes, manager.data.values()
):
if theme is not None:
yield from visualize_theme(theme, box)
yield from visualize_theme(theme, box, columns=columns)
Loading

0 comments on commit aed9a0c

Please sign in to comment.