Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor json interface #1168

Merged
merged 72 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
ab80778
introduce first draft of __json_dump__ and __json_load__
tomvanmele Jul 13, 2023
55ffe01
test of data data
tomvanmele Jul 17, 2023
aa3a9fd
Merge branch 'main' into json-interface
tomvanmele Aug 4, 2023
fb1ed5c
color data and data schema
tomvanmele Aug 9, 2023
14620fd
vector
tomvanmele Aug 9, 2023
8f216d9
update changes in log
tomvanmele Aug 9, 2023
6ba222b
updated base data class
tomvanmele Aug 9, 2023
35978cc
remove validate_data from validators
tomvanmele Aug 9, 2023
4bfe59b
removed validate_json
tomvanmele Aug 9, 2023
8d692bb
use jsondump and jsonload
tomvanmele Aug 10, 2023
5193683
add schema module
tomvanmele Aug 10, 2023
c9f87a1
fix data interface assembly
tomvanmele Aug 10, 2023
eed498e
fix data interface part
tomvanmele Aug 10, 2023
0160621
log
tomvanmele Aug 10, 2023
44e0a0f
assembly needs a lot of work
tomvanmele Aug 10, 2023
8dc5082
remove typo
tomvanmele Aug 10, 2023
95ed45f
add name property to base data structure class
tomvanmele Aug 10, 2023
fea785f
Merge branch 'main' into json-interface
tomvanmele Aug 10, 2023
2673858
type stuff
tomvanmele Aug 11, 2023
9c90898
type stuff
tomvanmele Aug 11, 2023
49a2904
JSON compatible data in datastructures
tomvanmele Aug 11, 2023
6a7eb03
changed NotImplmentedError to PluginNotInstalledError
tomvanmele Aug 11, 2023
ea7af77
simplify log
tomvanmele Aug 11, 2023
b2c23d2
data interface update
tomvanmele Aug 11, 2023
be85f58
fix tests
tomvanmele Aug 12, 2023
f19cc81
rename to compas_dataclasses
tomvanmele Aug 14, 2023
776cec1
stub for tests
tomvanmele Aug 14, 2023
64bfa45
formatting
tomvanmele Aug 14, 2023
4547500
remove precision from repr
tomvanmele Aug 14, 2023
7930696
add data tests
tomvanmele Aug 14, 2023
3087e0b
ipy repr is fucked
tomvanmele Aug 14, 2023
d75a823
data tests
tomvanmele Aug 14, 2023
b6571ef
plane data tests
tomvanmele Aug 14, 2023
00bd027
test closeness not equality
tomvanmele Aug 14, 2023
1a04f3b
add other planes
tomvanmele Aug 14, 2023
65437d4
normalisation can result in minor differences between vector components
tomvanmele Aug 14, 2023
a668667
Merge branch 'main' into json-interface
tomvanmele Aug 14, 2023
33ac168
frame tests
tomvanmele Aug 14, 2023
a303d75
cleanup
tomvanmele Aug 14, 2023
77facd3
schema and dataschema tests
tomvanmele Aug 14, 2023
5efdd8e
polygon and pointcloud tests
tomvanmele Aug 14, 2023
c6991f2
curve data tests
tomvanmele Aug 14, 2023
954ef9e
basic color tests
tomvanmele Aug 14, 2023
be62def
adjacency is not a required attribute
tomvanmele Aug 14, 2023
b009b68
closeness not equality
tomvanmele Aug 14, 2023
fcadae1
adjacency is not required
tomvanmele Aug 14, 2023
d70781f
type stuff
tomvanmele Aug 14, 2023
1bae6e5
base datastructure data tests
tomvanmele Aug 14, 2023
9a9a383
fix color repr ipy?
tomvanmele Aug 15, 2023
d8574d4
use allclose with random numbers
tomvanmele Aug 15, 2023
dd371e1
halfface data test
tomvanmele Aug 15, 2023
7e7e82f
conical surface data test
tomvanmele Aug 15, 2023
942fdd8
cylindrical surface data test
tomvanmele Aug 15, 2023
5f2615a
consistency with other surfaces
tomvanmele Aug 15, 2023
0a3cfa8
planar surface data tests
tomvanmele Aug 15, 2023
fda3a0e
consistency
tomvanmele Aug 15, 2023
b5536ad
spherical surface data tests
tomvanmele Aug 15, 2023
c21d36c
torus data schema
tomvanmele Aug 15, 2023
ec98118
toroidal surface data tests
tomvanmele Aug 15, 2023
0cc6465
reorganize
tomvanmele Aug 15, 2023
3d89d5f
quaternion data tests
tomvanmele Aug 15, 2023
ac57aab
remove space for ipy
tomvanmele Aug 15, 2023
7fb2649
add param tests
tomvanmele Aug 15, 2023
b8f3b84
fixed bug in insert vertex
tomvanmele Aug 16, 2023
f670d55
simplified edgedata recovery
tomvanmele Aug 16, 2023
85b8ebc
data test mesh
tomvanmele Aug 16, 2023
ff904c2
request doesn't work in ipy
tomvanmele Aug 16, 2023
4c587ad
network data tests
tomvanmele Aug 16, 2023
94fd7cb
use randint for random count
tomvanmele Aug 16, 2023
6f2d7c0
basic volmesh data test
tomvanmele Aug 16, 2023
5a9a22e
add features to part data
tomvanmele Aug 16, 2023
39c8b5e
Merge branch 'main' into json-interface
tomvanmele Aug 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 81 additions & 61 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,46 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Added `create_id` to `compas_ghpython.utilities`. (moved from `compas_fab`)
* Added representation for features in `compas.datastructures.Part`.
* Added `split` and `split_by_length` to `compas.geometry.Polyline`.
* Added `split` and `split_by_length` to `compas.geometry.Polyline`.
* Added `compas.rpc.XFunc`.
* Added `compas.data.Data.validate_jsonstring`.
* Added `compas.data.Data.validate_jsondata`.
* Added `compas.data.Data.JSONSCHEMA`.
* Added `compas.data.json_validate`.
* Added `compas.datastructures.Graph.JSONSCHEMA`.
* Added attribute `compas.color.Color.DATASCHEMA`.
* Added attribute `compas.data.Data.DATASCHEMA`.
* Added attribute `compas.datastructures.Graph.DATASCHEMA`.
* Added attribute `compas.datastructures.Halfedge.DATASCHEMA`.
* Added attribute `compas.datastructures.Halfface.DATASCHEMA`.
* Added attribute `compas.geometry.Arc.DATASCHEMA`.
* Added attribute `compas.geometry.Bezier.DATASCHEMA`.
* Added attribute `compas.geometry.Box.DATASCHEMA`.
* Added attribute `compas.geometry.Capsule.DATASCHEMA`.
* Added attribute `compas.geometry.Circle.DATASCHEMA`.
* Added attribute `compas.geometry.Cone.DATASCHEMA`.
* Added attribute `compas.geometry.Cylinder.DATASCHEMA`.
* Added attribute `compas.geometry.Ellipse.DATASCHEMA`.
* Added attribute `compas.geometry.Frame.DATASCHEMA`.
* Added attribute `compas.geometry.Line.DATASCHEMA`.
* Added attribute `compas.geometry.NurbsCurve.DATASCHEMA`.
* Added attribute `compas.geometry.NurbsSurface.DATASCHEMA`.
* Added attribute `compas.geometry.Plane.DATASCHEMA`.
* Added attribute `compas.geometry.Point.DATASCHEMA`.
* Added attribute `compas.geometry.Pointcloud.DATASCHEMA`.
* Added attribute `compas.geometry.Polygon.DATASCHEMA`.
* Added attribute `compas.geometry.Polyhedron.DATASCHEMA`.
* Added attribute `compas.geometry.Polyline.DATASCHEMA`.
* Added attribute `compas.geometry.Sphere.DATASCHEMA`.
* Added attribute `compas.geometry.Torus.DATASCHEMA`.
* Added attribute `compas.geometry.Quaternion.DATASCHEMA`.
* Added attribute `compas.geometry.Vector.DATASCHEMA`.
* Added implementation of property `compas.color.Color.data`.
* Added `compas.data.Data.validate_data`.
* Added `compas.data.Data.__jsondump__`.
* Added `compas.data.Data.__jsonload__`.
* Added `compas.data.schema.dataclass_dataschema`.
* Added `compas.data.schema.dataclass_typeschema`.
* Added `compas.data.schema.dataclass_jsonschema`.
* Added `compas.data.schema.compas_jsonschema`.
* Added `compas.data.schema.compas_dataclasses`.
* Added `compas.datastructures.Graph.to_jsondata`.
* Added `compas.datastructures.Graph.from_jsondata`.
* Added `compas.datastructures.Halfedge.JSONSCHEMA`.
* Added `compas.datastructures.Halfface.JSONSCHEMA`.
* Added `compas.geometry.Arc.JSONSCHEMA`.
* Added `compas.geometry.Bezier.JSONSCHEMA`.
* Added `compas.geometry.Box.JSONSCHEMA`.
* Added `compas.geometry.Capsule.JSONSCHEMA`.
* Added `compas.geometry.Circle.JSONSCHEMA`.
* Added `compas.geometry.Cone.JSONSCHEMA`.
* Added `compas.geometry.Cylinder.JSONSCHEMA`.
* Added `compas.geometry.Ellipse.JSONSCHEMA`.
* Added `compas.geometry.Frame.JSONSCHEMA`.
* Added `compas.geometry.Line.JSONSCHEMA`.
* Added `compas.geometry.NurbsCurve.JSONSCHEMA`.
* Added `compas.geometry.NurbsSurface.JSONSCHEMA`.
* Added `compas.geometry.Plane.JSONSCHEMA`.
* Added `compas.geometry.Point.JSONSCHEMA`.
* Added `compas.geometry.Pointcloud.JSONSCHEMA`.
* Added `compas.geometry.Polygon.JSONSCHEMA`.
* Added `compas.geometry.Polyhedron.JSONSCHEMA`.
* Added `compas.geometry.Polyline.JSONSCHEMA`.
* Added `compas.geometry.Sphere.JSONSCHEMA`.
* Added `compas.geometry.Torus.JSONSCHEMA`.
* Added `compas.geometry.Quaternion.JSONSCHEMA`.
* Added `compas.geometry.Vector.JSONSCHEMA`.
* Added `compas.datastructures.Halfedge.halfedge_loop_vertices`.
* Added `compas.datastructures.Halfedge.halfedge_strip_faces`.
* Added `compas.datastructures.Mesh.vertex_point`.
Expand All @@ -59,16 +66,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `compas.datastructures.Graph.node_index` and `compas.datastructures.Graph.index_node`.
* Added `compas.datastructures.Graph.edge_index` and `compas.datastructures.Graph.index_edge`.
* Added `compas.datastructures.Halfedge.vertex_index` and `compas.datastructures.Halfedge.index_vertex`.
* Added `compas.geometry.trimesh_descent_numpy`.
* Added `compas.geometry.trimesh_gradient_numpy`.
* Added a deprecation warning when using `Artist` for `Plotter`.
* Added `compas.geometry.Hyperbola`.
* Added `compas.geometry.Parabola`.
* Added `compas.geometry.PlanarSurface`.
* Added `compas.geometry.CylindricalSurface`.
* Added `compas.geometry.SphericalSurface`.
* Added `compas.geometry.ConicalSurface`.
* Added `compas.geometry.ToroidalSurface`.
* Added `compas.geometry.trimesh_descent_numpy`.
* Added `compas.geometry.trimesh_gradient_numpy`.
* Added `compas.geometry.boolean_union_polygon_polygon` pluggable.
* Added `compas.geometry.boolean_intersection_polygon_polygon` pluggable.
* Added `compas.geometry.boolean_difference_polygon_polygon` pluggable.
Expand All @@ -95,6 +101,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `vertices_and_faces_to_rhino` to `compas_rhino.conversions`.
* Added `polyhedron_to_rhino` to `compas_rhino.conversions`.
* Added `from_mesh` plugin to `compas_rhino.geometry.RhinoBrep`.
* Added `compas.geometry.Plane.worldYZ` and `compas.geometry.Plane.worldZX`.

### Changed

Expand Down Expand Up @@ -159,7 +166,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed base class of `compas.geometry.Line` to `compas.geometry.Curve.`
* Changed base class of `compas.geometry.Polyline` to `compas.geometry.Curve.`
* Changed `compas.geometry.oriented_bounding_box_numpy` to minimize volume.
* Fixed data interface `compas.datastructures.Assembly` and `compas.datastructures.Part`.
* Changed data property of `compas.datastructures.Graph` to contain only JSON compatible data.
* Changed data property of `compas.datastructures.Halfedge` to contain only JSON compatible data.
* Changed data property of `compas.datastructures.Halfface` to contain only JSON compatible data.
* Changed `__repr__` of `compas.geometry.Point` and `compas.geometry.Vector` to not use limited precision (`compas.PRECISION`) to ensure proper object reconstruction through `eval(repr(point))`.
* Changed `compas.datastructures.Graph.delete_edge` to delete invalid (u, u) edges and not delete edges in opposite directions (v, u)
* Fixed bug in `compas.datastructures.Mesh.insert_vertex`.
* Fixed bug in `compas.geometry.angle_vectors_signed`.

### Removed
Expand All @@ -172,36 +185,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Removed `compas.datastructures.Halfedge.get_any_vertices`.
* Removed `compas.datastructures.Halfedge.get_any_face`.
* Removed "schemas" folder and all contained `.json` files from `compas.data`.
* Removed `compas.data.Data.DATASCHEMA`.
* Removed `compas.data.Data.JSONSCHEMANAME`.
* Removed `compas.data.Data.jsondefinititions`.
* Removed `compas.data.Data.jsonvalidator`.
* Removed `compas.data.Data.validate_data`.
* Removed `compas.datastructures.Graph.DATASCHEMA` and `compas.datastructures.Graph.JSONSCHEMANAME`.
* Removed `compas.datastructures.Halfedge.DATASCHEMA` and `compas.datastructures.Halfedge.JSONSCHEMANAME`.
* Removed `compas.datastructures.Halfface.DATASCHEMA` and `compas.datastructures.Halfface.JSONSCHEMANAME`.
* Removed `compas.geometry.Arc.DATASCHEMA` and `compas.geometry.Arc.JSONSCHEMANAME`.
* Removed `compas.geometry.Bezier.DATASCHEMA` and `compas.geometry.Bezier.JSONSCHEMANAME`.
* Removed `compas.geometry.Box.DATASCHEMA` and `compas.geometry.Box.JSONSCHEMANAME`.
* Removed `compas.geometry.Capsule.DATASCHEMA` and `compas.geometry.Capsule.JSONSCHEMANAME`.
* Removed `compas.geometry.Circle.DATASCHEMA` and `compas.geometry.Circle.JSONSCHEMANAME`.
* Removed `compas.geometry.Cone.DATASCHEMA` and `compas.geometry.Cone.JSONSCHEMANAME`.
* Removed `compas.geometry.Cylinder.DATASCHEMA` and `compas.geometry.Cylinder.JSONSCHEMANAME`.
* Removed `compas.geometry.Ellipse.DATASCHEMA` and `compas.geometry.Ellipse.JSONSCHEMANAME`.
* Removed `compas.geometry.Frame.DATASCHEMA` and `compas.geometry.Frame.JSONSCHEMANAME`.
* Removed `compas.geometry.Line.DATASCHEMA` and `compas.geometry.Line.JSONSCHEMANAME`.
* Removed `compas.geometry.NurbsCurve.DATASCHEMA` and `compas.geometry.NurbsCurve.JSONSCHEMANAME`.
* Removed `compas.geometry.NurbsSurface.DATASCHEMA` and `compas.geometry.NurbsSurface.JSONSCHEMANAME`.
* Removed `compas.geometry.Plane.DATASCHEMA` and `compas.geometry.Plane.JSONSCHEMANAME`.
* Removed `compas.geometry.Point.DATASCHEMA` and `compas.geometry.Point.JSONSCHEMANAME`.
* Removed `compas.geometry.Pointcloud.DATASCHEMA` and `compas.geometry.Pointcloud.JSONSCHEMANAME`.
* Removed `compas.geometry.Polygon.DATASCHEMA` and `compas.geometry.Polygon.JSONSCHEMANAME`.
* Removed `compas.geometry.Polyhedron.DATASCHEMA` and `compas.geometry.Polyhedron.JSONSCHEMANAME`.
* Removed `compas.geometry.Polyline.DATASCHEMA` and `compas.geometry.Polyline.JSONSCHEMANAME`.
* Removed `compas.geometry.Sphere.DATASCHEMA` and `compas.geometry.Sphere.JSONSCHEMANAME`.
* Removed `compas.geometry.Torus.DATASCHEMA` and `compas.geometry.Torus.JSONSCHEMANAME`.
* Removed `compas.geometry.Quaternion.DATASCHEMA` and `compas.geometry.Quaternion.JSONSCHEMANAME`.
* Removed `compas.geometry.Vector.DATASCHEMA` and `compas.geometry.Vector.JSONSCHEMANAME`.
* Removed `compas.data.Data.validate_json`.
* Removed `compas.data.Data.validate_jsondata`.
* Removed `compas.data.Data.validate_jsonstring`.
* Removed `compas.data.Data.__getstate__`.
* Removed `compas.data.Data.__setstate__`.
* Removed setter of property `compas.data.Data.data` and similar setters in all data classes.
* Removed properties `compas.data.Data.DATASCHEMA` and `compas.data.Data.JSONSCHEMANAME`.
* Removed properties `compas.datastructures.Graph.DATASCHEMA` and `compas.datastructures.Graph.JSONSCHEMANAME`.
* Removed properties `compas.datastructures.Halfedge.DATASCHEMA` and `compas.datastructures.Halfedge.JSONSCHEMANAME`.
* Removed properties `compas.datastructures.Halfface.DATASCHEMA` and `compas.datastructures.Halfface.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Arc.DATASCHEMA` and `compas.geometry.Arc.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Bezier.DATASCHEMA` and `compas.geometry.Bezier.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Box.DATASCHEMA` and `compas.geometry.Box.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Capsule.DATASCHEMA` and `compas.geometry.Capsule.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Circle.DATASCHEMA` and `compas.geometry.Circle.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Cone.DATASCHEMA` and `compas.geometry.Cone.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Cylinder.DATASCHEMA` and `compas.geometry.Cylinder.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Ellipse.DATASCHEMA` and `compas.geometry.Ellipse.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Frame.DATASCHEMA` and `compas.geometry.Frame.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Line.DATASCHEMA` and `compas.geometry.Line.JSONSCHEMANAME`.
* Removed properties `compas.geometry.NurbsCurve.DATASCHEMA` and `compas.geometry.NurbsCurve.JSONSCHEMANAME`.
* Removed properties `compas.geometry.NurbsSurface.DATASCHEMA` and `compas.geometry.NurbsSurface.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Plane.DATASCHEMA` and `compas.geometry.Plane.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Point.DATASCHEMA` and `compas.geometry.Point.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Pointcloud.DATASCHEMA` and `compas.geometry.Pointcloud.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Polygon.DATASCHEMA` and `compas.geometry.Polygon.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Polyhedron.DATASCHEMA` and `compas.geometry.Polyhedron.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Polyline.DATASCHEMA` and `compas.geometry.Polyline.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Sphere.DATASCHEMA` and `compas.geometry.Sphere.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Torus.DATASCHEMA` and `compas.geometry.Torus.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Quaternion.DATASCHEMA` and `compas.geometry.Quaternion.JSONSCHEMANAME`.
* Removed properties `compas.geometry.Vector.DATASCHEMA` and `compas.geometry.Vector.JSONSCHEMANAME`.
* Removed `compas.datastructures.Graph.key_index`and `compas.datastructures.Graph.index_key`.
* Removed `compas.datastructures.Graph.uv_index`and `compas.datastructures.Graph.index_uv`.
* Removed `compas.datastructures.Halfedge.key_index` and `compas.datastructures.Halfedge.index_key`.
Expand All @@ -214,6 +231,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Removed class attribute `CONTEXT` from `compas.artists.Artist`.
* Removed class attribute `AVAILABLE_CONTEXTS` form `compas.artists.Artist`.
* Removed `compas.geometry.Primitive`.
* Removed classmethod `compas.color.Color.from_data`.
* Removed `validate_data` from `compas.data.validators`.
* Removed `json_validate` from `compas.data.json`.

## [1.17.5] 2023-02-16

Expand Down
136 changes: 66 additions & 70 deletions src/compas/colors/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import print_function

try:
basestring
basestring # type: ignore
except NameError:
basestring = str

Expand Down Expand Up @@ -104,6 +104,17 @@ class Color(Data):

"""

DATASCHEMA = {
"type": "object",
"properties": {
"red": {"type": "number", "minimum": 0.0, "maximum": 1.0},
"green": {"type": "number", "minimum": 0.0, "maximum": 1.0},
"blue": {"type": "number", "minimum": 0.0, "maximum": 1.0},
"alpha": {"type": "number", "minimum": 0.0, "maximum": 1.0},
},
"required": ["red", "green", "blue", "alpha"],
}

def __init__(self, red, green, blue, alpha=1.0, **kwargs):
super(Color, self).__init__(**kwargs)
self._r = 1.0
Expand All @@ -115,27 +126,64 @@ def __init__(self, red, green, blue, alpha=1.0, **kwargs):
self.b = blue
self.a = alpha

def __repr__(self):
return "{0}({1}, {2}, {3}, alpha={4})".format(type(self).__name__, self.r, self.g, self.b, self.a)

def __getitem__(self, key):
if key == 0:
return self.r
if key == 1:
return self.g
if key == 2:
return self.b
raise KeyError

def __len__(self):
return 3

def __iter__(self):
return iter(self.rgb)

def __eq__(self, other):
return all(a == b for a, b in zip(self, other))

# --------------------------------------------------------------------------
# Descriptor
# --------------------------------------------------------------------------

def __set_name__(self, owner, name):
self.public_name = name
self.private_name = "_" + name

def __get__(self, obj, otype=None):
return getattr(obj, self.private_name, None) or self

def __set__(self, obj, value):
if not obj:
return

if not value:
return

if Color.is_rgb255(value):
value = Color.from_rgb255(value[0], value[1], value[2])
elif Color.is_hex(value):
value = Color.from_hex(value)
else:
value = Color(value[0], value[1], value[2])

setattr(obj, self.private_name, value)

# --------------------------------------------------------------------------
# data
# Data
# --------------------------------------------------------------------------

@property
def data(self):
return {"red": self.r, "green": self.g, "blue": self.b, "alpha": self.a}

@data.setter
def data(self, data):
self.r = data["red"]
self.g = data["green"]
self.b = data["blue"]
self.a = data["alpha"]

@classmethod
def from_data(cls, data):
return cls(data["red"], data["green"], data["blue"], data["alpha"])

# --------------------------------------------------------------------------
# properties
# Properties
# --------------------------------------------------------------------------

@property
Expand Down Expand Up @@ -272,59 +320,7 @@ def saturation(self):
return (maxval - minval) / maxval

# --------------------------------------------------------------------------
# descriptor
# --------------------------------------------------------------------------

def __set_name__(self, owner, name):
self.public_name = name
self.private_name = "_" + name

def __get__(self, obj, otype=None):
return getattr(obj, self.private_name, None) or self

def __set__(self, obj, value):
if not obj:
return

if not value:
return

if Color.is_rgb255(value):
value = Color.from_rgb255(value[0], value[1], value[2])
elif Color.is_hex(value):
value = Color.from_hex(value)
else:
value = Color(value[0], value[1], value[2])

setattr(obj, self.private_name, value)

# --------------------------------------------------------------------------
# customization
# --------------------------------------------------------------------------

def __repr__(self):
return "Color({}, {}, {}, {})".format(self.r, self.g, self.b, self.a)

def __getitem__(self, key):
if key == 0:
return self.r
if key == 1:
return self.g
if key == 2:
return self.b
raise KeyError

def __len__(self):
return 3

def __iter__(self):
return iter(self.rgb)

def __eq__(self, other):
return all(a == b for a, b in zip(self, other))

# --------------------------------------------------------------------------
# constructors
# Constructors
# --------------------------------------------------------------------------

@classmethod
Expand Down Expand Up @@ -529,7 +525,7 @@ def from_name(cls, name):
return cls.from_rgb255(*rgb255)

# --------------------------------------------------------------------------
# presets
# Presets
# --------------------------------------------------------------------------

@classmethod
Expand Down Expand Up @@ -698,7 +694,7 @@ def pink(cls):
return cls(1.0, 0.0, 0.5)

# --------------------------------------------------------------------------
# other presets
# Other presets
# --------------------------------------------------------------------------

@classmethod
Expand Down Expand Up @@ -786,7 +782,7 @@ def silver(cls):
# salmon

# --------------------------------------------------------------------------
# methods
# Methods
# --------------------------------------------------------------------------

@staticmethod
Expand Down
Loading
Loading