-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworking_space.py
187 lines (159 loc) · 5.48 KB
/
working_space.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""working_space
Generate the working space according to general inset outset principles.
__author__ = Troy James Sobotka
__copyright__ = Copyright 2023
__version__ = 1.0
__maintainer__ = Troy James Sobotka
__email__ = [email protected]
__status__ = Test
"""
import colour
import numpy
import shapely
import shapely.affinity
def create_workingspace(
primaries_rotate=[1.75, -0.5, -1.0],
primaries_scale=[0.15, 0.15, 0.10],
achromatic_rotate=0.0,
achromatic_outset=0.0,
colourspace_in=colour.RGB_COLOURSPACES["ITU-R BT.709"],
name="No name set",
):
#####
# Construct the Base Image Formation Colourspace
#####
arbitrary_scale = 4.0
point_red = shapely.Point(colourspace_in.primaries[0])
point_green = shapely.Point(colourspace_in.primaries[1])
point_blue = shapely.Point(colourspace_in.primaries[2])
point_achromatic = shapely.Point(colourspace_in.whitepoint)
# Scale the primaries outward by some safe and arbitrary amount to
# cover the totality of the target geometry.
scaled_red = shapely.affinity.scale(
point_red,
xfact=arbitrary_scale,
yfact=arbitrary_scale,
origin=point_achromatic,
)
scaled_green = shapely.affinity.scale(
point_green,
xfact=arbitrary_scale,
yfact=arbitrary_scale,
origin=point_achromatic,
)
scaled_blue = shapely.affinity.scale(
point_blue,
xfact=arbitrary_scale,
yfact=arbitrary_scale,
origin=point_achromatic,
)
scaled_achromatic = shapely.Point(
[
colourspace_in.whitepoint[0],
# Arbitrary distance from the white x coordinate up.
colourspace_in.whitepoint[1] * arbitrary_scale,
]
)
# Rotate the primaries. Positive values are counter clockwise.
rotate_red = primaries_rotate[0]
rotate_green = primaries_rotate[1]
rotate_blue = primaries_rotate[2]
rotate_achromatic = achromatic_rotate
rotated_out_red = shapely.affinity.rotate(
scaled_red, rotate_red, origin=point_achromatic
)
rotated_out_green = shapely.affinity.rotate(
scaled_green, rotate_green, origin=point_achromatic
)
rotated_out_blue = shapely.affinity.rotate(
scaled_blue, rotate_blue, origin=point_achromatic
)
rotated_out_achromatic = shapely.affinity.rotate(
scaled_achromatic, rotate_achromatic, origin=point_achromatic
)
# Generate bisecting lines.
rotated_out_lines = shapely.geometry.MultiLineString(
[
[rotated_out_red, point_achromatic],
[rotated_out_green, point_achromatic],
[rotated_out_blue, point_achromatic],
]
)
rotated_out_achromatic_line = shapely.geometry.LineString(
[rotated_out_achromatic, point_achromatic]
)
working_polygon = shapely.geometry.Polygon(colourspace_in.primaries)
# Calculate the intersections with the working space chosen.
intersections = working_polygon.intersection(rotated_out_lines)
intersection_achromatic = working_polygon.intersection(
rotated_out_achromatic_line
)
hull_rotated_points = []
for intersection in intersections.geoms:
hull_rotated_points.append(
[intersection.coords.xy[0][0], intersection.coords.xy[1][0]]
)
hull_rotated_achromatic = [
intersection_achromatic.coords.xy[0][0],
intersection_achromatic.coords.xy[1][0],
]
scale_red_in = primaries_scale[0]
scale_green_in = primaries_scale[1]
scale_blue_in = primaries_scale[2]
scale_achromatic_in = achromatic_outset
hull_red = shapely.geometry.Point(hull_rotated_points[0])
hull_green = shapely.geometry.Point(hull_rotated_points[1])
hull_blue = shapely.geometry.Point(hull_rotated_points[2])
hull_achromatic = shapely.geometry.Point(hull_rotated_achromatic)
# Inset according to the desired inset scales. Insetting controls the rate
# of attenuation.
rotated_inset_red = shapely.affinity.scale(
hull_red,
xfact=(1.0 - scale_red_in),
yfact=(1.0 - scale_red_in),
origin=point_achromatic,
)
rotated_inset_green = shapely.affinity.scale(
hull_green,
xfact=(1.0 - scale_green_in),
yfact=(1.0 - scale_green_in),
origin=point_achromatic,
)
rotated_inset_blue = shapely.affinity.scale(
hull_blue,
xfact=(1.0 - scale_blue_in),
yfact=(1.0 - scale_blue_in),
origin=point_achromatic,
)
rotated_outset_achromatic = shapely.affinity.scale(
point_achromatic,
xfact=(1.0 - scale_achromatic_in),
yfact=(1.0 - scale_achromatic_in),
origin=hull_achromatic,
)
primaries_inset = numpy.asarray(
[
rotated_inset_red.coords,
rotated_inset_green.coords,
rotated_inset_blue.coords,
]
)
achromatic_outset_coordinates = numpy.asarray(
[
rotated_outset_achromatic.coords.xy[0][0],
rotated_outset_achromatic.coords.xy[1][0],
]
)
colourspace = colour.RGB_Colourspace(
name=name,
primaries=primaries_inset,
whitepoint=achromatic_outset_coordinates,
whitepoint_name=colourspace_in.whitepoint_name,
cctf_encoding=colourspace_in.cctf_encoding,
cctf_decoding=colourspace_in.cctf_decoding,
use_derived_matrix_RGB_to_XYZ=True,
use_derived_matrix_XYZ_to_RGB=True,
)
return colourspace