changeset 8:181a86533286

share Color with dimcurve
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 16:51:36 -0800
parents 05fb0319eb64
children 9f427d8073c3
files color.py pdm.lock pyproject.toml
diffstat 3 files changed, 114 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/color.py	Sun Jan 28 16:03:40 2024 -0800
+++ b/color.py	Sun Jan 28 16:51:36 2024 -0800
@@ -1,6 +1,39 @@
+import logging
+from dataclasses import dataclass
 
-class Color(str):
+from dataclasses_json import DataClassJsonMixin
+
+log = logging.getLogger('colr')
+
+
+def lerp(a, b, t):
+    return (1 - t) * a + (t) * b
+
+
+@dataclass(frozen=True)
+class Color(DataClassJsonMixin):
+    r: float
+    g: float
+    b: float
 
-    def to_js(self):
-        return self
+    def __post_init__(self):
+        super().__setattr__('r', max(0, min(1, self.r)))
+        super().__setattr__('g', max(0, min(1, self.g)))
+        super().__setattr__('b', max(0, min(1, self.b)))
+
+    def __repr__(self):
+        return f'(Color(r={self.r:.3f}, g={self.g:.3f}, b={self.b:.3f}))'
+
+    def avg(self):
+        return (self.r + self.g + self.b) / 3
 
+    def mix(self, other, x):
+        return Color(
+            lerp(self.r, other.r, x),
+            lerp(self.g, other.g, x),
+            lerp(self.b, other.b, x),
+        )
+
+    def hex(self):
+        r, g, b = int(self.r * 255), int(self.g * 255), int(self.b * 255)
+        return '#%02x%02x%02x' % (r, g, b)
--- a/pdm.lock	Sun Jan 28 16:03:40 2024 -0800
+++ b/pdm.lock	Sun Jan 28 16:51:36 2024 -0800
@@ -5,7 +5,7 @@
 groups = ["default"]
 strategy = ["cross_platform", "inherit_metadata"]
 lock_version = "4.4.1"
-content_hash = "sha256:a0e63f7479692d7acf7ccdd1794e368f6d5effbc17b0d913c369821c3a526318"
+content_hash = "sha256:238744667aa5bb7e379e16e2b9f424307a9a90b29eafd1734283956bf2b8fdec"
 
 [[package]]
 name = "aiohttp"
@@ -240,6 +240,21 @@
 ]
 
 [[package]]
+name = "dataclasses-json"
+version = "0.6.3"
+requires_python = ">=3.7,<4.0"
+summary = "Easily serialize dataclasses to and from JSON."
+groups = ["default"]
+dependencies = [
+    "marshmallow<4.0.0,>=3.18.0",
+    "typing-inspect<1,>=0.4.0",
+]
+files = [
+    {file = "dataclasses_json-0.6.3-py3-none-any.whl", hash = "sha256:4aeb343357997396f6bca1acae64e486c3a723d8f5c76301888abeccf0c45176"},
+    {file = "dataclasses_json-0.6.3.tar.gz", hash = "sha256:35cb40aae824736fdf959801356641836365219cfe14caeb115c39136f775d2a"},
+]
+
+[[package]]
 name = "frozenlist"
 version = "1.4.1"
 requires_python = ">=3.8"
@@ -354,6 +369,20 @@
 ]
 
 [[package]]
+name = "marshmallow"
+version = "3.20.2"
+requires_python = ">=3.8"
+summary = "A lightweight library for converting complex datatypes to and from native Python datatypes."
+groups = ["default"]
+dependencies = [
+    "packaging>=17.0",
+]
+files = [
+    {file = "marshmallow-3.20.2-py3-none-any.whl", hash = "sha256:c21d4b98fee747c130e6bc8f45c4b3199ea66bc00c12ee1f639f0aeca034d5e9"},
+    {file = "marshmallow-3.20.2.tar.gz", hash = "sha256:4c1daff273513dc5eb24b219a8035559dc573c8f322558ef85f5438ddd1236dd"},
+]
+
+[[package]]
 name = "multidict"
 version = "6.0.4"
 requires_python = ">=3.7"
@@ -379,6 +408,28 @@
 ]
 
 [[package]]
+name = "mypy-extensions"
+version = "1.0.0"
+requires_python = ">=3.5"
+summary = "Type system extensions for programs checked with the mypy type checker."
+groups = ["default"]
+files = [
+    {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"},
+    {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"},
+]
+
+[[package]]
+name = "packaging"
+version = "23.2"
+requires_python = ">=3.7"
+summary = "Core utilities for Python packages"
+groups = ["default"]
+files = [
+    {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"},
+    {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"},
+]
+
+[[package]]
 name = "patchablegraph"
 version = "1.5.0"
 requires_python = ">=3.9"
@@ -614,6 +665,31 @@
 ]
 
 [[package]]
+name = "typing-extensions"
+version = "4.9.0"
+requires_python = ">=3.8"
+summary = "Backported and Experimental Type Hints for Python 3.8+"
+groups = ["default"]
+files = [
+    {file = "typing_extensions-4.9.0-py3-none-any.whl", hash = "sha256:af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd"},
+    {file = "typing_extensions-4.9.0.tar.gz", hash = "sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783"},
+]
+
+[[package]]
+name = "typing-inspect"
+version = "0.9.0"
+summary = "Runtime inspection utilities for typing module."
+groups = ["default"]
+dependencies = [
+    "mypy-extensions>=0.3.0",
+    "typing-extensions>=3.7.4",
+]
+files = [
+    {file = "typing_inspect-0.9.0-py3-none-any.whl", hash = "sha256:9ee6fc59062311ef8547596ab6b955e1b8aa46242d854bfc78f4f6b0eff35f9f"},
+    {file = "typing_inspect-0.9.0.tar.gz", hash = "sha256:b23fc42ff6f6ef6954e4852c1fb512cdd18dbea03134f91f856a95ccc9461f78"},
+]
+
+[[package]]
 name = "uvicorn"
 version = "0.27.0"
 requires_python = ">=3.8"
--- a/pyproject.toml	Sun Jan 28 16:03:40 2024 -0800
+++ b/pyproject.toml	Sun Jan 28 16:51:36 2024 -0800
@@ -10,10 +10,10 @@
     "starlette-exporter>=0.13.0",
     "starlette>=0.20.4",
     "uvicorn[standard]>=0.18.2",
-
     "background-loop>=1.3.0",
     "patchablegraph>=1.5.0",
     "rdfdb==0.24.0",
+    "dataclasses-json>=0.6.3",
 ]
 requires-python = ">=3.11"
 license = {text = "MIT"}