| 1 |
"""Colorspace to VRML conversion for DisplayCAL.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import math |
| 6 |
import os |
| 7 |
from enum import Enum |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
|
| 10 |
from DisplayCAL import colormath, x3dom |
| 11 |
from DisplayCAL.log import safe_print |
| 12 |
from DisplayCAL.util_io import GzipFileProper |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from pathlib import Path |
| 16 |
|
| 17 |
from DisplayCAL.cgats import CGATS |
| 18 |
|
| 19 |
|
| 20 |
class CAT(Enum): |
| 21 |
"""Chromatic adaptation transform (CAT) enumeration.""" |
| 22 |
|
| 23 |
Bradford = "Bradford" |
| 24 |
BS = "BS" |
| 25 |
BS_PC = "BS-PC" |
| 26 |
CAT02 = "CAT02" |
| 27 |
CAT02BS = "CAT02BS" |
| 28 |
CAT97s = "CAT97s" |
| 29 |
CIE2012_2 = "CIE2012_2" |
| 30 |
CMCCAT2000 = "CMCCAT2000" |
| 31 |
HPE_D65 = "HPE D65" |
| 32 |
HPE_E = "HPE E" |
| 33 |
IPT = "IPT" |
| 34 |
Sharp = "Sharp" |
| 35 |
XYZ_Scaling = "XYZ scaling" |
| 36 |
|
| 37 |
def __str__(self) -> str: |
| 38 |
"""Get the string representation of the CAT.""" |
| 39 |
return self.value |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def to_cat(cls, cat: str | CAT) -> CAT: |
| 43 |
"""Convert the given cat value to a CAT enum. |
| 44 |
|
| 45 |
Args: |
| 46 |
cat (str | CAT): The value to convert to a CAT. |
| 47 |
|
| 48 |
Raises: |
| 49 |
TypeError: Input value type is invalid. |
| 50 |
ValueError: Input value is invalid. |
| 51 |
|
| 52 |
Returns: |
| 53 |
CAT: The enum. |
| 54 |
""" |
| 55 |
valid_values = sorted(set([c.name for c in cls] + [c.value for c in cls])) |
| 56 |
if not isinstance(cat, (str, CAT)): |
| 57 |
raise TypeError( |
| 58 |
f"cat should be a CAT enum value or one of {valid_values}, " |
| 59 |
f"not {cat.__class__.__name__}: '{cat}'" |
| 60 |
) |
| 61 |
if isinstance(cat, str): |
| 62 |
cat_name_lut = {c.name.lower(): c.name for c in cls} |
| 63 |
cat_name_lut.update({c.value.lower(): c.name for c in cls}) |
| 64 |
cat_lower_case = cat.lower() |
| 65 |
if cat_lower_case not in cat_name_lut: |
| 66 |
raise ValueError( |
| 67 |
f"cat should be a CAT enum value or one of {valid_values}, " |
| 68 |
f"not '{cat}'" |
| 69 |
) |
| 70 |
|
| 71 |
return cls.__members__[cat_name_lut[cat_lower_case]] |
| 72 |
|
| 73 |
return cat |
| 74 |
|
| 75 |
|
| 76 |
class ColorSpace(Enum): |
| 77 |
"""Color space enumeration.""" |
| 78 |
|
| 79 |
DIN99 = "DIN99" |
| 80 |
DIN99b = "DIN99b" |
| 81 |
DIN99c = "DIN99c" |
| 82 |
DIN99d = "DIN99d" |
| 83 |
HSI = "HSI" |
| 84 |
HSL = "HSL" |
| 85 |
HSV = "HSV" |
| 86 |
ICtCp = "ICtCp" |
| 87 |
IPT = "IPT" |
| 88 |
Lab = "Lab" |
| 89 |
LCHab = "LCH(ab)" |
| 90 |
LCHuv = "LCH(uv)" |
| 91 |
Lpt = "Lpt" |
| 92 |
LuvPrime = "Lu'v'" |
| 93 |
Luv = "Luv" |
| 94 |
RGB = "RGB" |
| 95 |
xyY = "xyY" # noqa: N815 |
| 96 |
|
| 97 |
def __str__(self) -> str: |
| 98 |
"""Get the string representation of the ColorSpace.""" |
| 99 |
return self.value |
| 100 |
|
| 101 |
@classmethod |
| 102 |
def to_colorspace(cls, colorspace: str | ColorSpace) -> ColorSpace: |
| 103 |
"""Convert the given colorspace value to a ColorSpace enum. |
| 104 |
|
| 105 |
Args: |
| 106 |
colorspace (str | ColorSpace): The value to convert to a ColorSpace. |
| 107 |
|
| 108 |
Raises: |
| 109 |
TypeError: Input value type is invalid. |
| 110 |
ValueError: Input value is invalid. |
| 111 |
|
| 112 |
Returns: |
| 113 |
ColorSpace: The enum. |
| 114 |
""" |
| 115 |
valid_values = sorted(set([c.name for c in cls] + [c.value for c in cls])) |
| 116 |
if not isinstance(colorspace, (str, ColorSpace)): |
| 117 |
raise TypeError( |
| 118 |
"colorspace should be a ColorSpace enum value or one of " |
| 119 |
f"{valid_values}, not {colorspace.__class__.__name__}: '{colorspace}'" |
| 120 |
) |
| 121 |
if isinstance(colorspace, str): |
| 122 |
colorspace_name_lut = {c.name.lower(): c.name for c in cls} |
| 123 |
colorspace_name_lut.update({c.value.lower(): c.name for c in cls}) |
| 124 |
colorspace_lower_case = colorspace.lower() |
| 125 |
if colorspace_lower_case not in colorspace_name_lut: |
| 126 |
raise ValueError( |
| 127 |
"colorspace should be a ColorSpace enum value or one of " |
| 128 |
f"{valid_values}, not '{colorspace}'" |
| 129 |
) |
| 130 |
|
| 131 |
return cls.__members__[colorspace_name_lut[colorspace_lower_case]] |
| 132 |
|
| 133 |
return colorspace |
| 134 |
|
| 135 |
def __eq__(self, other: object) -> bool: |
| 136 |
"""Check equality with another ColorSpace or string. |
| 137 |
|
| 138 |
Args: |
| 139 |
other (object): The object to compare with. |
| 140 |
|
| 141 |
Returns: |
| 142 |
bool: True if equal, False otherwise. |
| 143 |
""" |
| 144 |
if isinstance(other, str): |
| 145 |
return ( |
| 146 |
self.value.lower() == other.lower() |
| 147 |
or self.name.lower() == other.lower() |
| 148 |
) |
| 149 |
if isinstance(other, ColorSpace): |
| 150 |
return self.value == other.value |
| 151 |
return False |
| 152 |
|
| 153 |
|
| 154 |
class ColorSpaceToVRML: |
| 155 |
"""Color space class for easy VRML. |
| 156 |
|
| 157 |
Args: |
| 158 |
data (dict | CGATS): The color data to be processed, could be a |
| 159 |
dictionary or a CGATS instance containing color values ("DATA"). |
| 160 |
white_point (tuple[float, float, float]): The white point for the color |
| 161 |
space. |
| 162 |
cat (str): The chromatic adaptation transform to use. Defaults to |
| 163 |
"Bradford". |
| 164 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to |
| 165 |
40. |
| 166 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to |
| 167 |
False. |
| 168 |
""" |
| 169 |
|
| 170 |
sqrt3_100 = math.sqrt(3) * 100 |
| 171 |
sqrt3_50 = math.sqrt(3) * 50 |
| 172 |
|
| 173 |
VALID_COLOR_SPACE_NAMES = tuple(c.value for c in ColorSpace) |
| 174 |
|
| 175 |
VRML_TEMPLATE = """#VRML V2.0 utf8 |
| 176 |
|
| 177 |
Transform {{ |
| 178 |
children [ |
| 179 |
|
| 180 |
NavigationInfo {{ |
| 181 |
type "EXAMINE" |
| 182 |
}} |
| 183 |
|
| 184 |
DirectionalLight {{ |
| 185 |
direction 0 0 -1 |
| 186 |
direction 0 -1 0 |
| 187 |
}} |
| 188 |
|
| 189 |
Viewpoint {{ |
| 190 |
fieldOfView {fov} |
| 191 |
position 0 0 {offset_z} |
| 192 |
}} |
| 193 |
|
| 194 |
{axes} |
| 195 |
{children} |
| 196 |
] |
| 197 |
}} |
| 198 |
""" |
| 199 |
|
| 200 |
VRML_CHILD_TEMPLATE = """ # Sphere |
| 201 |
Transform {{ |
| 202 |
translation {offset_x:.6f} {offset_y:.6f} {offset_z:.6f} |
| 203 |
children [ |
| 204 |
Shape{{ |
| 205 |
geometry Sphere {{ radius {radius:.6f} }} |
| 206 |
appearance Appearance {{ material Material {{ diffuseColor {R:.6f} {G:.6f} {B:.6f} }} }} |
| 207 |
}} |
| 208 |
] |
| 209 |
}} |
| 210 |
""" # noqa: E501 |
| 211 |
|
| 212 |
VRML_AXES_TEMPLATE = """ Transform {{ |
| 213 |
translation {hundredth_scale:.1f} {hundredth_scale:.1f} -50.0 |
| 214 |
children [ |
| 215 |
Shape {{ |
| 216 |
geometry Text {{ |
| 217 |
string ["{colorspace}"] |
| 218 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {tenth_scale:.1f} }} |
| 219 |
}} |
| 220 |
appearance Appearance {{ |
| 221 |
material Material {{ diffuseColor 0.7 0.7 0.7 }} |
| 222 |
}} |
| 223 |
}} |
| 224 |
] |
| 225 |
}} |
| 226 |
""" # noqa: E501 |
| 227 |
|
| 228 |
VRML_AXES_TEMPLATE2 = """ # L* axis |
| 229 |
Transform {{ |
| 230 |
translation 0.0 0.0 0.0 |
| 231 |
children [ |
| 232 |
Shape {{ |
| 233 |
geometry Box {{ size {wh:.1f} {wh:.1f} 100.0 }} |
| 234 |
appearance Appearance {{ |
| 235 |
material Material {{ diffuseColor 0.7 0.7 0.7 }} |
| 236 |
}} |
| 237 |
}} |
| 238 |
] |
| 239 |
}} |
| 240 |
# L* axis label |
| 241 |
Transform {{ |
| 242 |
translation -{Ln:.1f} -{wh:.1f} 55.0 |
| 243 |
children [ |
| 244 |
Shape {{ |
| 245 |
geometry Text {{ |
| 246 |
string [{pllabel}] |
| 247 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {fontsize:.1f} }} |
| 248 |
}} |
| 249 |
appearance Appearance {{ |
| 250 |
material Material {{ diffuseColor 0.7 0.7 0.7}} |
| 251 |
}} |
| 252 |
}} |
| 253 |
] |
| 254 |
}} |
| 255 |
# +x axis |
| 256 |
Transform {{ |
| 257 |
translation {aboffset:.1f} 0.0 -50.0 |
| 258 |
children [ |
| 259 |
Shape {{ |
| 260 |
geometry Box {{ size {ab:.1f} {wh:.1f} {wh:.1f} }} |
| 261 |
appearance Appearance {{ |
| 262 |
material Material {{ diffuseColor {pxcolor} }} |
| 263 |
}} |
| 264 |
}} |
| 265 |
] |
| 266 |
}} |
| 267 |
# +x axis label |
| 268 |
Transform {{ |
| 269 |
translation {ap:.1f} -{wh:.1f} -50.0 |
| 270 |
children [ |
| 271 |
Shape {{ |
| 272 |
geometry Text {{ |
| 273 |
string [{pxlabel}] |
| 274 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {fontsize:.1f} }} |
| 275 |
}} |
| 276 |
appearance Appearance {{ |
| 277 |
material Material {{ diffuseColor {pxcolor} }} |
| 278 |
}} |
| 279 |
}} |
| 280 |
] |
| 281 |
}} |
| 282 |
# -x axis |
| 283 |
Transform {{ |
| 284 |
translation -{aboffset:.1f} 0.0 -50.0 |
| 285 |
children [ |
| 286 |
Shape {{ |
| 287 |
geometry Box {{ size {ab:.1f} {wh:.1f} {wh:.1f} }} |
| 288 |
appearance Appearance {{ |
| 289 |
material Material {{ diffuseColor {nxcolor} }} |
| 290 |
}} |
| 291 |
}} |
| 292 |
] |
| 293 |
}} |
| 294 |
# -x axis label |
| 295 |
Transform {{ |
| 296 |
translation -{an:.1f} -{wh:.1f} -50.0 |
| 297 |
children [ |
| 298 |
Shape {{ |
| 299 |
geometry Text {{ |
| 300 |
string [{nxlabel}] |
| 301 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {fontsize:.1f} }} |
| 302 |
}} |
| 303 |
appearance Appearance {{ |
| 304 |
material Material {{ diffuseColor {nxcolor} }} |
| 305 |
}} |
| 306 |
}} |
| 307 |
] |
| 308 |
}} |
| 309 |
# +y axis |
| 310 |
Transform {{ |
| 311 |
translation 0.0 {aboffset:.1f} -50.0 |
| 312 |
children [ |
| 313 |
Shape {{ |
| 314 |
geometry Box {{ size {wh:.1f} {ab:.1f} {wh:.1f} }} |
| 315 |
appearance Appearance {{ |
| 316 |
material Material {{ diffuseColor {pycolor} }} |
| 317 |
}} |
| 318 |
}} |
| 319 |
] |
| 320 |
}} |
| 321 |
# +y axis label |
| 322 |
Transform {{ |
| 323 |
translation -{bp0:.1f} {bp1:.1f} -50.0 |
| 324 |
children [ |
| 325 |
Shape {{ |
| 326 |
geometry Text {{ |
| 327 |
string [{pylabel}] |
| 328 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {fontsize:.1f} }} |
| 329 |
}} |
| 330 |
appearance Appearance {{ |
| 331 |
material Material {{ diffuseColor {pycolor} }} |
| 332 |
}} |
| 333 |
}} |
| 334 |
] |
| 335 |
}} |
| 336 |
# -y axis |
| 337 |
Transform {{ |
| 338 |
translation 0.0 -{aboffset:.1f} -50.0 |
| 339 |
children [ |
| 340 |
Shape {{ |
| 341 |
geometry Box {{ size {wh:.1f} {ab:.1f} {wh:.1f} }} |
| 342 |
appearance Appearance {{ |
| 343 |
material Material {{ diffuseColor {nycolor} }} |
| 344 |
}} |
| 345 |
}} |
| 346 |
] |
| 347 |
}} |
| 348 |
# -y axis label |
| 349 |
Transform {{ |
| 350 |
translation -{bn0:.1f} -{bn1:.1f} -50.0 |
| 351 |
children [ |
| 352 |
Shape {{ |
| 353 |
geometry Text {{ |
| 354 |
string [{nylabel}] |
| 355 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {fontsize:.1f} }} |
| 356 |
}} |
| 357 |
appearance Appearance {{ |
| 358 |
material Material {{ diffuseColor {nycolor} }} |
| 359 |
}} |
| 360 |
}} |
| 361 |
] |
| 362 |
}} |
| 363 |
# Zero |
| 364 |
Transform {{ |
| 365 |
translation -{Ln:.1f} -{wh:.1f} -55.0 |
| 366 |
children [ |
| 367 |
Shape {{ |
| 368 |
geometry Text {{ |
| 369 |
string ["0"] |
| 370 |
fontStyle FontStyle {{ family "SANS" style "BOLD" size {fontsize:.1f} }} |
| 371 |
}} |
| 372 |
appearance Appearance {{ |
| 373 |
material Material {{ diffuseColor 0.7 0.7 0.7}} |
| 374 |
}} |
| 375 |
}} |
| 376 |
] |
| 377 |
}} |
| 378 |
""" # noqa: E501 |
| 379 |
|
| 380 |
def __init__( |
| 381 |
self, |
| 382 |
data: dict | CGATS, |
| 383 |
white_point: tuple[float, float, float], |
| 384 |
cat: str | CAT = CAT.Bradford, |
| 385 |
rgb_black_offset: float = 40, |
| 386 |
normalize_rgb_white: bool = False, |
| 387 |
) -> None: |
| 388 |
self._data = None |
| 389 |
self._cat = None |
| 390 |
self.X = None |
| 391 |
self.Y = None |
| 392 |
self.Z = None |
| 393 |
self.L = None |
| 394 |
self.a = None |
| 395 |
self.b = None |
| 396 |
self.white_point = white_point |
| 397 |
self.cat = cat |
| 398 |
self.data = data |
| 399 |
self.extra_offset_x = 0.0 |
| 400 |
self.extra_offset_y = 0.0 |
| 401 |
self.scale = 1.0 |
| 402 |
self.radius = 15.0 / (len(self.data) ** (1.0 / 3.0)) |
| 403 |
self.max_z = 1.0 |
| 404 |
self.max_xy = 200 |
| 405 |
self.axes = "" |
| 406 |
self.vrml = "" |
| 407 |
self.children = [] |
| 408 |
self.rgb_black_offset = rgb_black_offset |
| 409 |
self.normalize_rgb_white = normalize_rgb_white |
| 410 |
self.fov = 45 |
| 411 |
self.offset_z = 340 |
| 412 |
|
| 413 |
@property |
| 414 |
def colorspace(self) -> str: |
| 415 |
"""Get the name of the color space. |
| 416 |
|
| 417 |
Returns: |
| 418 |
str: The name of the color space. |
| 419 |
""" |
| 420 |
return ColorSpace.to_colorspace(self.__class__.__name__.replace("ToVRML", "")) |
| 421 |
|
| 422 |
@property |
| 423 |
def data(self) -> list: |
| 424 |
"""Get the color data. |
| 425 |
|
| 426 |
Returns: |
| 427 |
list: The color data. |
| 428 |
""" |
| 429 |
return self._data |
| 430 |
|
| 431 |
@data.setter |
| 432 |
def data(self, data: list) -> None: |
| 433 |
"""Set the color data. |
| 434 |
|
| 435 |
Args: |
| 436 |
data (list): The color data to be set. |
| 437 |
""" |
| 438 |
from DisplayCAL.cgats import CGATS |
| 439 |
|
| 440 |
if not isinstance(data, (dict, CGATS)): |
| 441 |
raise TypeError( |
| 442 |
f"{self.__class__.__name__}.data must be a dictionary or a " |
| 443 |
f"CGATS instance, not {data.__class__.__name__}: {data}" |
| 444 |
) |
| 445 |
self._data = data |
| 446 |
|
| 447 |
@property |
| 448 |
def cat(self) -> CAT: |
| 449 |
"""Get the chromatic adaptation transform (CAT). |
| 450 |
|
| 451 |
Returns: |
| 452 |
CAT: The CAT enum. |
| 453 |
""" |
| 454 |
return self._cat |
| 455 |
|
| 456 |
@cat.setter |
| 457 |
def cat(self, cat: str | CAT) -> None: |
| 458 |
"""Set the chromatic adaptation transform (CAT). |
| 459 |
|
| 460 |
Args: |
| 461 |
cat (str | CAT): The CAT to be set. |
| 462 |
""" |
| 463 |
self._cat = CAT.to_cat(cat) |
| 464 |
|
| 465 |
def calculate_xyz_and_lab(self, entry: dict) -> None: |
| 466 |
"""Calculate XYZ and Lab values from the white point and CAT info. |
| 467 |
|
| 468 |
Args: |
| 469 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 470 |
""" |
| 471 |
self.X, self.Y, self.Z = colormath.adapt( |
| 472 |
entry["XYZ_X"], |
| 473 |
entry["XYZ_Y"], |
| 474 |
entry["XYZ_Z"], |
| 475 |
self.white_point, |
| 476 |
"D65" if isinstance(self, (ICtCpToVRML, IPTToVRML)) else "D50", |
| 477 |
cat=str(self.cat), |
| 478 |
) |
| 479 |
self.L, self.a, self.b = colormath.XYZ2Lab(self.X, self.Y, self.Z) |
| 480 |
|
| 481 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 482 |
"""Get the offset for the color space. |
| 483 |
|
| 484 |
Args: |
| 485 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 486 |
|
| 487 |
Returns: |
| 488 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 489 |
""" |
| 490 |
raise NotImplementedError("This method should be overridden in subclasses.") |
| 491 |
|
| 492 |
def generate_vrml_axes(self) -> None: |
| 493 |
"""Get the VRML axis representation for the color space.""" |
| 494 |
|
| 495 |
def generate_vrml_children(self) -> None: |
| 496 |
"""Generate VRML children for the color space.""" |
| 497 |
self.children = [] |
| 498 |
for entry in self.data.values(): |
| 499 |
self.calculate_xyz_and_lab(entry) |
| 500 |
offset_x, offset_y, offset_z = self.get_offsets(entry) |
| 501 |
|
| 502 |
h_ref = s_ref = 0.0 |
| 503 |
if self.rgb_black_offset != 40: |
| 504 |
# Keep reference hue and saturation |
| 505 |
# Lab to sRGB using reference black offset of 40 like Argyll CMS |
| 506 |
r, g, b = colormath.Lab2RGB( |
| 507 |
self.L * (100.0 - 40.0) / 100.0 + 40.0, |
| 508 |
self.a, |
| 509 |
self.b, |
| 510 |
scale=0.7, |
| 511 |
noadapt=not self.normalize_rgb_white, |
| 512 |
) |
| 513 |
h_ref, s_ref, _ = colormath.RGB2HSV(r, g, b) |
| 514 |
# Lab to sRGB using actual black offset |
| 515 |
r, g, b = colormath.Lab2RGB( |
| 516 |
self.L * (100.0 - self.rgb_black_offset) / 100.0 |
| 517 |
+ self.rgb_black_offset, |
| 518 |
self.a, |
| 519 |
self.b, |
| 520 |
scale=0.7, |
| 521 |
noadapt=not self.normalize_rgb_white, |
| 522 |
) |
| 523 |
if self.rgb_black_offset != 40: |
| 524 |
_, _, v = colormath.RGB2HSV(r, g, b) |
| 525 |
# Use reference H and S to go back to RGB |
| 526 |
r, g, b = colormath.HSV2RGB(h_ref, s_ref, v) |
| 527 |
|
| 528 |
child = self.VRML_CHILD_TEMPLATE.format( |
| 529 |
offset_x=offset_x, |
| 530 |
offset_y=offset_y, |
| 531 |
offset_z=offset_z, |
| 532 |
R=r + 0.05, |
| 533 |
G=g + 0.05, |
| 534 |
B=b + 0.05, |
| 535 |
radius=self.radius, |
| 536 |
) |
| 537 |
self.children.append(child) |
| 538 |
|
| 539 |
def generate_vrml(self) -> str: |
| 540 |
"""Generate the VRML representation for the color space. |
| 541 |
|
| 542 |
Returns: |
| 543 |
str: The VRML representation of the color space. |
| 544 |
""" |
| 545 |
self.generate_vrml_axes() |
| 546 |
self.generate_vrml_children() |
| 547 |
|
| 548 |
self.vrml = self.VRML_TEMPLATE.format( |
| 549 |
children="".join(self.children), |
| 550 |
axes=self.axes, |
| 551 |
fov=math.radians(self.fov), |
| 552 |
offset_z=self.offset_z, |
| 553 |
) |
| 554 |
|
| 555 |
def write_vrml( |
| 556 |
self, file_format: str, filename: str | Path, compress: bool = True |
| 557 |
) -> None: |
| 558 |
"""Write the VRML representation to a file. |
| 559 |
|
| 560 |
Args: |
| 561 |
file_format (str): The format of the output file, e.g., 'VRML'. |
| 562 |
filename (str): The name of the file to export to. |
| 563 |
compress (bool): Whether to compress the output file. |
| 564 |
""" |
| 565 |
if file_format != "VRML": |
| 566 |
print("Generating", file_format) |
| 567 |
x3d = x3dom.vrml2x3dom(self.vrml) |
| 568 |
if file_format == "HTML": |
| 569 |
out = x3d.html(title=os.path.basename(filename)) |
| 570 |
else: |
| 571 |
out = x3d.x3d() |
| 572 |
else: |
| 573 |
out = self.vrml |
| 574 |
writer = GzipFileProper if compress else open |
| 575 |
safe_print("Writing", filename) |
| 576 |
with writer(filename, "wb") as outfile: |
| 577 |
outfile.write(out.encode("utf-8")) |
| 578 |
|
| 579 |
|
| 580 |
class RGBToVRML(ColorSpaceToVRML): |
| 581 |
"""RGB color space class. |
| 582 |
|
| 583 |
Args: |
| 584 |
data (list): The color data to be processed. |
| 585 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 586 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 587 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 588 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 589 |
""" |
| 590 |
|
| 591 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 592 |
"""Get the offset for the color space. |
| 593 |
|
| 594 |
Args: |
| 595 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 596 |
|
| 597 |
Returns: |
| 598 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 599 |
""" |
| 600 |
return ( |
| 601 |
entry["RGB_G"] - 50, |
| 602 |
entry["RGB_B"] - 50, |
| 603 |
entry["RGB_R"] - 50, |
| 604 |
) |
| 605 |
|
| 606 |
|
| 607 |
class HSIToVRML(ColorSpaceToVRML): |
| 608 |
"""HSI color space class. |
| 609 |
|
| 610 |
Args: |
| 611 |
data (list): The color data to be processed. |
| 612 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 613 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 614 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 615 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 616 |
""" |
| 617 |
|
| 618 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 619 |
"""Get the offset for the color space. |
| 620 |
|
| 621 |
Args: |
| 622 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 623 |
|
| 624 |
Returns: |
| 625 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 626 |
""" |
| 627 |
h, s, offset_z = colormath.RGB2HSI( |
| 628 |
entry["RGB_R"] / 100.0, |
| 629 |
entry["RGB_G"] / 100.0, |
| 630 |
entry["RGB_B"] / 100.0, |
| 631 |
) |
| 632 |
radian = math.radians(h * 360) |
| 633 |
offset_x, offset_y = ( |
| 634 |
s * offset_z * math.cos(radian), |
| 635 |
s * offset_z * math.sin(radian), |
| 636 |
) |
| 637 |
# Fudge device locations into Lab space |
| 638 |
return ( |
| 639 |
offset_x * self.sqrt3_100, |
| 640 |
offset_y * self.sqrt3_100, |
| 641 |
offset_z * self.sqrt3_100 - self.sqrt3_50, |
| 642 |
) |
| 643 |
|
| 644 |
|
| 645 |
class HSLToVRML(ColorSpaceToVRML): |
| 646 |
"""HSL color space class. |
| 647 |
|
| 648 |
Args: |
| 649 |
data (list): The color data to be processed. |
| 650 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 651 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 652 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 653 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 654 |
""" |
| 655 |
|
| 656 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 657 |
"""Get the offset for the color space. |
| 658 |
|
| 659 |
Args: |
| 660 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 661 |
|
| 662 |
Returns: |
| 663 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 664 |
""" |
| 665 |
h, s, offset_z = colormath.RGB2HSL( |
| 666 |
entry["RGB_R"] / 100.0, |
| 667 |
entry["RGB_G"] / 100.0, |
| 668 |
entry["RGB_B"] / 100.0, |
| 669 |
) |
| 670 |
radian = math.radians(h * 360) |
| 671 |
s *= (1 - offset_z) if offset_z > 0.5 else (offset_z) |
| 672 |
offset_x, offset_y = s * math.cos(radian), s * math.sin(radian) |
| 673 |
# Fudge device locations into Lab space |
| 674 |
return ( |
| 675 |
offset_x * self.sqrt3_100, |
| 676 |
offset_y * self.sqrt3_100, |
| 677 |
offset_z * self.sqrt3_100 - self.sqrt3_50, |
| 678 |
) |
| 679 |
|
| 680 |
|
| 681 |
class HSVToVRML(ColorSpaceToVRML): |
| 682 |
"""HSV color space class. |
| 683 |
|
| 684 |
Args: |
| 685 |
data (list): The color data to be processed. |
| 686 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 687 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 688 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 689 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 690 |
""" |
| 691 |
|
| 692 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 693 |
"""Get the offset for the color space. |
| 694 |
|
| 695 |
Args: |
| 696 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 697 |
|
| 698 |
Returns: |
| 699 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 700 |
""" |
| 701 |
h, s, offset_z = colormath.RGB2HSV( |
| 702 |
entry["RGB_R"] / 100.0, |
| 703 |
entry["RGB_G"] / 100.0, |
| 704 |
entry["RGB_B"] / 100.0, |
| 705 |
) |
| 706 |
radian = math.radians(h * 360) |
| 707 |
offset_x, offset_y = ( |
| 708 |
s * offset_z * math.cos(radian), |
| 709 |
s * offset_z * math.sin(radian), |
| 710 |
) |
| 711 |
# Fudge device locations into Lab space |
| 712 |
return ( |
| 713 |
offset_x * self.sqrt3_50, |
| 714 |
offset_y * self.sqrt3_50, |
| 715 |
offset_z * self.sqrt3_100 - self.sqrt3_50, |
| 716 |
) |
| 717 |
|
| 718 |
|
| 719 |
class LabToVRML(ColorSpaceToVRML): |
| 720 |
"""Lab color space class. |
| 721 |
|
| 722 |
Args: |
| 723 |
data (list): The color data to be processed. |
| 724 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 725 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 726 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 727 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 728 |
""" |
| 729 |
|
| 730 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 731 |
"""Get the offset for the color space. |
| 732 |
|
| 733 |
Args: |
| 734 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 735 |
|
| 736 |
Returns: |
| 737 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 738 |
""" |
| 739 |
l, a, b = colormath.XYZ2Lab(self.X, self.Y, self.Z) |
| 740 |
# Fudge device locations into Lab space |
| 741 |
return a, b, l - 50 |
| 742 |
|
| 743 |
|
| 744 |
class DIN99ToVRML(ColorSpaceToVRML): |
| 745 |
"""DIN99 color space class. |
| 746 |
|
| 747 |
Args: |
| 748 |
data (list): The color data to be processed. |
| 749 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 750 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 751 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 752 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 753 |
""" |
| 754 |
|
| 755 |
def __init__( |
| 756 |
self, |
| 757 |
data: list, |
| 758 |
white_point: tuple[float, float, float], |
| 759 |
cat: str = "XYZ scaling", |
| 760 |
rgb_black_offset: float = 40, |
| 761 |
normalize_rgb_white: bool = False, |
| 762 |
) -> None: |
| 763 |
super().__init__(data, white_point, cat, rgb_black_offset, normalize_rgb_white) |
| 764 |
self.scale = 100.0 / 40 # Scale factor for DIN99 axes |
| 765 |
self.radius /= self.scale |
| 766 |
self.fov /= self.scale |
| 767 |
|
| 768 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 769 |
"""Get the offset for the color space. |
| 770 |
|
| 771 |
Args: |
| 772 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 773 |
|
| 774 |
Returns: |
| 775 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 776 |
""" |
| 777 |
l, a, b = colormath.XYZ2Lab(self.X, self.Y, self.Z) |
| 778 |
l99, a99, b99 = colormath.Lab2DIN99(l, a, b) |
| 779 |
return a99, b99, l99 - 50 |
| 780 |
|
| 781 |
def generate_vrml_axes(self) -> None: |
| 782 |
"""Get the VRML axis representation for the color space.""" |
| 783 |
self.axes = "" |
| 784 |
pxcolor = "1.0 0.0 0.0" |
| 785 |
nxcolor = "0.0 1.0 0.0" |
| 786 |
pycolor = "1.0 1.0 0.0" |
| 787 |
nycolor = "0.0 0.0 1.0" |
| 788 |
self.axes += self.VRML_AXES_TEMPLATE.format( |
| 789 |
hundredth_scale=100.0 / self.scale, |
| 790 |
tenth_scale=10.0 / self.scale, |
| 791 |
colorspace=str(self.colorspace), |
| 792 |
) |
| 793 |
(pxlabel, nxlabel, pylabel, nylabel, pllabel) = ( |
| 794 |
f'"a", "+{int(100 / self.scale)}"', |
| 795 |
f'"a", "-{int(100 / self.scale)}"', |
| 796 |
f'"b +{int(100 / self.scale)}"', |
| 797 |
f'"b -{int(100 / self.scale)}"', |
| 798 |
'"L", "+100"', |
| 799 |
) |
| 800 |
|
| 801 |
self.axes += self.VRML_AXES_TEMPLATE2.format( |
| 802 |
wh=2.0 / self.scale, |
| 803 |
ab=100.0 / self.scale, |
| 804 |
aboffset=50.0 / self.scale, |
| 805 |
fontsize=10.0 / self.scale, |
| 806 |
ap=102.0 / self.scale, |
| 807 |
an=108.0 / self.scale, |
| 808 |
Ln=3.0, |
| 809 |
bp0=3.0, |
| 810 |
bp1=103.0 / self.scale, |
| 811 |
bn0=3.0, |
| 812 |
bn1=107.0 / self.scale, |
| 813 |
pxlabel=pxlabel, |
| 814 |
nxlabel=nxlabel, |
| 815 |
pylabel=pylabel, |
| 816 |
nylabel=nylabel, |
| 817 |
pllabel=pllabel, |
| 818 |
pxcolor=pxcolor, |
| 819 |
nxcolor=nxcolor, |
| 820 |
pycolor=pycolor, |
| 821 |
nycolor=nycolor, |
| 822 |
) |
| 823 |
|
| 824 |
|
| 825 |
class DIN99bToVRML(DIN99ToVRML): |
| 826 |
"""DIN99b color space class. |
| 827 |
|
| 828 |
Args: |
| 829 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 830 |
extra_offset_x (float, optional): Extra offset in the X direction. |
| 831 |
Defaults to 0.0. |
| 832 |
extra_offset_y (float, optional): Extra offset in the Y direction. |
| 833 |
Defaults to 0.0. |
| 834 |
scale (float, optional): Scale factor for the offsets. Defaults to 1.0. |
| 835 |
max_z (float, optional): Maximum Z value for scaling. Defaults to 100.0. |
| 836 |
""" |
| 837 |
|
| 838 |
def __init__( |
| 839 |
self, |
| 840 |
data: list, |
| 841 |
white_point: tuple[float, float, float], |
| 842 |
cat: str = "XYZ scaling", |
| 843 |
rgb_black_offset: float = 40, |
| 844 |
normalize_rgb_white: bool = False, |
| 845 |
) -> None: |
| 846 |
super().__init__(data, white_point, cat, rgb_black_offset, normalize_rgb_white) |
| 847 |
self.scale = 100.0 / 50 # Scale factor for DIN99b axes |
| 848 |
self.radius /= self.scale |
| 849 |
|
| 850 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 851 |
"""Get the offset for the color space. |
| 852 |
|
| 853 |
Args: |
| 854 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 855 |
|
| 856 |
Returns: |
| 857 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 858 |
""" |
| 859 |
l, a, b = colormath.XYZ2Lab(self.X, self.Y, self.Z) |
| 860 |
l99, a99, b99 = colormath.Lab2DIN99b(l, a, b) |
| 861 |
return a99, b99, l99 - 50 |
| 862 |
|
| 863 |
|
| 864 |
class DIN99cToVRML(DIN99ToVRML): |
| 865 |
"""DIN99c color space class. |
| 866 |
|
| 867 |
Args: |
| 868 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 869 |
extra_offset_x (float, optional): Extra offset in the X direction. |
| 870 |
Defaults to 0.0. |
| 871 |
extra_offset_y (float, optional): Extra offset in the Y direction. |
| 872 |
Defaults to 0.0. |
| 873 |
scale (float, optional): Scale factor for the offsets. Defaults to 1.0. |
| 874 |
max_z (float, optional): Maximum Z value for scaling. Defaults to 100.0. |
| 875 |
""" |
| 876 |
|
| 877 |
def __init__( |
| 878 |
self, |
| 879 |
data: list, |
| 880 |
white_point: tuple[float, float, float], |
| 881 |
cat: str = "XYZ scaling", |
| 882 |
rgb_black_offset: float = 40, |
| 883 |
normalize_rgb_white: bool = False, |
| 884 |
) -> None: |
| 885 |
super().__init__(data, white_point, cat, rgb_black_offset, normalize_rgb_white) |
| 886 |
self.scale = 100.0 / 50 # Scale factor for DIN99c axes |
| 887 |
self.radius /= self.scale |
| 888 |
|
| 889 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 890 |
"""Get the offset for the color space. |
| 891 |
|
| 892 |
Args: |
| 893 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 894 |
|
| 895 |
Returns: |
| 896 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 897 |
""" |
| 898 |
l99, a99, b99 = colormath.XYZ2DIN99c(self.X, self.Y, self.Z) |
| 899 |
return a99, b99, l99 - 50 |
| 900 |
|
| 901 |
|
| 902 |
class DIN99dToVRML(DIN99ToVRML): |
| 903 |
"""DIN99d color space class. |
| 904 |
|
| 905 |
Args: |
| 906 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 907 |
extra_offset_x (float, optional): Extra offset in the X direction. |
| 908 |
Defaults to 0.0. |
| 909 |
extra_offset_y (float, optional): Extra offset in the Y direction. |
| 910 |
Defaults to 0.0. |
| 911 |
scale (float, optional): Scale factor for the offsets. Defaults to 1.0. |
| 912 |
max_z (float, optional): Maximum Z value for scaling. Defaults to 100.0. |
| 913 |
""" |
| 914 |
|
| 915 |
def __init__( |
| 916 |
self, |
| 917 |
data: list, |
| 918 |
white_point: tuple[float, float, float], |
| 919 |
cat: str = "XYZ scaling", |
| 920 |
rgb_black_offset: float = 40, |
| 921 |
normalize_rgb_white: bool = False, |
| 922 |
) -> None: |
| 923 |
super().__init__(data, white_point, cat, rgb_black_offset, normalize_rgb_white) |
| 924 |
self.scale = 100.0 / 50 # Scale factor for DIN99d axes |
| 925 |
self.radius /= self.scale |
| 926 |
|
| 927 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 928 |
"""Get the offset for the color space. |
| 929 |
|
| 930 |
Args: |
| 931 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 932 |
|
| 933 |
Returns: |
| 934 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 935 |
""" |
| 936 |
l99, a99, b99 = colormath.XYZ2DIN99d(self.X, self.Y, self.Z) |
| 937 |
return a99, b99, l99 - 50 |
| 938 |
|
| 939 |
|
| 940 |
class LCHabToVRML(ColorSpaceToVRML): |
| 941 |
"""LCH(ab) color space class. |
| 942 |
|
| 943 |
Args: |
| 944 |
data (list): The color data to be processed. |
| 945 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 946 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 947 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 948 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 949 |
""" |
| 950 |
|
| 951 |
def __init__( |
| 952 |
self, |
| 953 |
data: list, |
| 954 |
white_point: tuple[float, float, float], |
| 955 |
cat: str = "XYZ scaling", |
| 956 |
rgb_black_offset: float = 40, |
| 957 |
normalize_rgb_white: bool = False, |
| 958 |
) -> None: |
| 959 |
super().__init__(data, white_point, cat, rgb_black_offset, normalize_rgb_white) |
| 960 |
self.fov /= 16.0 |
| 961 |
self.offset_z *= 16 |
| 962 |
|
| 963 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 964 |
"""Get the offset for the color space. |
| 965 |
|
| 966 |
Args: |
| 967 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 968 |
|
| 969 |
Returns: |
| 970 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 971 |
""" |
| 972 |
l, c, h = colormath.Lab2LCHab(self.L, self.a, self.b) |
| 973 |
return h - 180, c - 100, l - 50 |
| 974 |
|
| 975 |
def generate_vrml_axes(self) -> None: |
| 976 |
"""Get the VRML axis representation for the color space.""" |
| 977 |
self.axes = "" |
| 978 |
self.max_z = 1.0 |
| 979 |
self.extra_offset_x = 0.0 |
| 980 |
self.extra_offset_y = 0.0 |
| 981 |
xlabel, ylabel, zlabel = "H(ab)", "C(ab)", "L*" |
| 982 |
self.axes = x3dom.get_vrml_axes( |
| 983 |
xlabel, ylabel, zlabel, -180, -100, 0, 360, 200, 100, False |
| 984 |
) |
| 985 |
|
| 986 |
|
| 987 |
class LCHuvToVRML(ColorSpaceToVRML): |
| 988 |
"""LCH(uv) color space class. |
| 989 |
|
| 990 |
Args: |
| 991 |
data (list): The color data to be processed. |
| 992 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 993 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 994 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 995 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 996 |
""" |
| 997 |
|
| 998 |
def __init__( |
| 999 |
self, |
| 1000 |
data: list, |
| 1001 |
white_point: tuple[float, float, float], |
| 1002 |
cat: str = "XYZ scaling", |
| 1003 |
rgb_black_offset: float = 40, |
| 1004 |
normalize_rgb_white: bool = False, |
| 1005 |
) -> None: |
| 1006 |
super().__init__(data, white_point, cat, rgb_black_offset, normalize_rgb_white) |
| 1007 |
self.fov /= 16.0 |
| 1008 |
self.offset_z *= 16 |
| 1009 |
|
| 1010 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1011 |
"""Get the offset for the color space. |
| 1012 |
|
| 1013 |
Args: |
| 1014 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1015 |
|
| 1016 |
Returns: |
| 1017 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1018 |
""" |
| 1019 |
l, c, h = colormath.Luv2LCHuv(*colormath.XYZ2Luv(self.X, self.Y, self.Z)) |
| 1020 |
return h - 180, c - 100, l - 50 |
| 1021 |
|
| 1022 |
def generate_vrml_axes(self) -> None: |
| 1023 |
"""Get the VRML axis representation for the color space.""" |
| 1024 |
self.axes = "" |
| 1025 |
self.max_z = 1.0 |
| 1026 |
self.extra_offset_x = 0.0 |
| 1027 |
self.extra_offset_y = 0.0 |
| 1028 |
xlabel, ylabel, zlabel = "H(uv)", "C(uv)", "L*" |
| 1029 |
self.axes = x3dom.get_vrml_axes( |
| 1030 |
xlabel, ylabel, zlabel, -180, -100, 0, 360, 200, 100, False |
| 1031 |
) |
| 1032 |
|
| 1033 |
|
| 1034 |
class LuvToVRML(ColorSpaceToVRML): |
| 1035 |
"""Luv color space class. |
| 1036 |
|
| 1037 |
Args: |
| 1038 |
data (list): The color data to be processed. |
| 1039 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 1040 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 1041 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 1042 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 1043 |
""" |
| 1044 |
|
| 1045 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1046 |
"""Get the offset for the color space. |
| 1047 |
|
| 1048 |
Args: |
| 1049 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1050 |
|
| 1051 |
Returns: |
| 1052 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1053 |
""" |
| 1054 |
l, u, v = colormath.XYZ2Luv(self.X, self.Y, self.Z) |
| 1055 |
return u, v, l - 50 |
| 1056 |
|
| 1057 |
def generate_vrml_axes(self) -> None: |
| 1058 |
"""Get the VRML axis representation for the color space.""" |
| 1059 |
self.axes = "" |
| 1060 |
pxcolor = "1.0 0.0 0.0" |
| 1061 |
nxcolor = "0.0 1.0 0.0" |
| 1062 |
pycolor = "1.0 1.0 0.0" |
| 1063 |
nycolor = "0.0 0.0 1.0" |
| 1064 |
x = "u" |
| 1065 |
y = "v" |
| 1066 |
(pxlabel, nxlabel, pylabel, nylabel, pllabel) = ( |
| 1067 |
f'"{x}*", "+100"', |
| 1068 |
f'"{x}*", "-100"', |
| 1069 |
f'"{y}* +100"', |
| 1070 |
f'"{y}* -100"', |
| 1071 |
'"L*", "+100"', |
| 1072 |
) |
| 1073 |
self.axes += self.VRML_AXES_TEMPLATE2.format( |
| 1074 |
wh=2.0 / self.scale, |
| 1075 |
ab=100.0 / self.scale, |
| 1076 |
aboffset=50.0 / self.scale, |
| 1077 |
fontsize=10.0 / self.scale, |
| 1078 |
ap=102.0 / self.scale, |
| 1079 |
an=108.0 / self.scale, |
| 1080 |
Ln=3.0, |
| 1081 |
bp0=3.0, |
| 1082 |
bp1=103.0 / self.scale, |
| 1083 |
bn0=3.0, |
| 1084 |
bn1=107.0 / self.scale, |
| 1085 |
pxlabel=pxlabel, |
| 1086 |
nxlabel=nxlabel, |
| 1087 |
pylabel=pylabel, |
| 1088 |
nylabel=nylabel, |
| 1089 |
pllabel=pllabel, |
| 1090 |
pxcolor=pxcolor, |
| 1091 |
nxcolor=nxcolor, |
| 1092 |
pycolor=pycolor, |
| 1093 |
nycolor=nycolor, |
| 1094 |
) |
| 1095 |
|
| 1096 |
|
| 1097 |
class LuvPrimeToVRML(ColorSpaceToVRML): |
| 1098 |
"""Lu'v' color space class. |
| 1099 |
|
| 1100 |
Args: |
| 1101 |
data (list): The color data to be processed. |
| 1102 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 1103 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 1104 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 1105 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 1106 |
""" |
| 1107 |
|
| 1108 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1109 |
"""Get the offset for the color space. |
| 1110 |
|
| 1111 |
Args: |
| 1112 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1113 |
|
| 1114 |
Returns: |
| 1115 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1116 |
""" |
| 1117 |
l, u_, v_ = colormath.XYZ2Lu_v_(self.X, self.Y, self.Z) |
| 1118 |
return ( |
| 1119 |
(u_ + self.extra_offset_x) * self.scale, |
| 1120 |
(v_ + self.extra_offset_y) * self.scale, |
| 1121 |
l / 100.0 * self.max_z - 50, |
| 1122 |
) |
| 1123 |
|
| 1124 |
def generate_vrml_axes(self) -> None: |
| 1125 |
"""Get the VRML axis representation for the color space.""" |
| 1126 |
self.axes = "" |
| 1127 |
self.max_z = self.scale = 100 |
| 1128 |
self.radius /= 2.0 |
| 1129 |
xlabel, ylabel, zlabel = "x 0.8", "y 0.8", "Y 100" |
| 1130 |
self.extra_offset_x, self.extra_offset_y = -0.4, -0.4 |
| 1131 |
self.scale = self.max_xy / 0.8 |
| 1132 |
xlabel, ylabel, zlabel = "u' 0.6", "v' 0.6", "L* 100" |
| 1133 |
self.extra_offset_x, self.extra_offset_y = -0.3, -0.3 |
| 1134 |
self.scale = self.max_xy / 0.6 |
| 1135 |
self.axes = x3dom.get_vrml_axes( |
| 1136 |
xlabel, |
| 1137 |
ylabel, |
| 1138 |
zlabel, |
| 1139 |
self.extra_offset_x * self.scale, |
| 1140 |
self.extra_offset_y * self.scale, |
| 1141 |
0, |
| 1142 |
self.max_xy, |
| 1143 |
self.max_xy, |
| 1144 |
self.max_z, |
| 1145 |
) |
| 1146 |
|
| 1147 |
|
| 1148 |
class xyYToVRML(ColorSpaceToVRML): # noqa: N801 |
| 1149 |
"""xyY color space class. |
| 1150 |
|
| 1151 |
Args: |
| 1152 |
data (list): The color data to be processed. |
| 1153 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 1154 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 1155 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 1156 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 1157 |
""" |
| 1158 |
|
| 1159 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1160 |
"""Get the offset for the color space. |
| 1161 |
|
| 1162 |
Args: |
| 1163 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1164 |
|
| 1165 |
Returns: |
| 1166 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1167 |
""" |
| 1168 |
offset_x, offset_y, self.Y = colormath.XYZ2xyY(self.X, self.Y, self.Z) |
| 1169 |
return ( |
| 1170 |
(offset_x + self.extra_offset_x) * self.scale, |
| 1171 |
(offset_y + self.extra_offset_y) * self.scale, |
| 1172 |
self.Y / 100.0 * self.max_z - 50, |
| 1173 |
) |
| 1174 |
|
| 1175 |
def generate_vrml_axes(self) -> None: |
| 1176 |
"""Get the VRML axis representation for the color space.""" |
| 1177 |
self.axes = "" |
| 1178 |
self.max_z = 1.0 |
| 1179 |
self.extra_offset_x = 0.0 |
| 1180 |
self.extra_offset_y = 0.0 |
| 1181 |
self.max_z = self.scale = 100 |
| 1182 |
self.radius /= 2.0 |
| 1183 |
xlabel, ylabel, zlabel = "x 0.8", "y 0.8", "Y 100" |
| 1184 |
self.extra_offset_x, self.extra_offset_y = -0.4, -0.4 |
| 1185 |
self.scale = self.max_xy / 0.8 |
| 1186 |
self.axes = x3dom.get_vrml_axes( |
| 1187 |
xlabel, |
| 1188 |
ylabel, |
| 1189 |
zlabel, |
| 1190 |
self.extra_offset_x * self.scale, |
| 1191 |
self.extra_offset_y * self.scale, |
| 1192 |
0, |
| 1193 |
self.max_xy, |
| 1194 |
self.max_xy, |
| 1195 |
self.max_z, |
| 1196 |
) |
| 1197 |
|
| 1198 |
|
| 1199 |
class ICtCpToVRML(ColorSpaceToVRML): |
| 1200 |
"""ICtCp color space class. |
| 1201 |
|
| 1202 |
Args: |
| 1203 |
data (list): The color data to be processed. |
| 1204 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 1205 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 1206 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 1207 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 1208 |
""" |
| 1209 |
|
| 1210 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1211 |
"""Get the offset for the color space. |
| 1212 |
|
| 1213 |
Args: |
| 1214 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1215 |
|
| 1216 |
Returns: |
| 1217 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1218 |
""" |
| 1219 |
i, ct, cp = colormath.XYZ2ICtCp( |
| 1220 |
self.X / 100.0, self.Y / 100.0, self.Z / 100.0, clamp=False |
| 1221 |
) |
| 1222 |
return ct * 100, cp * 100, i * 100 - 50 |
| 1223 |
|
| 1224 |
def generate_vrml_axes(self) -> None: |
| 1225 |
"""Get the VRML axis representation for the color space.""" |
| 1226 |
self.axes = "" |
| 1227 |
pxcolor = "1.0 0.0 0.0" |
| 1228 |
nxcolor = "0.0 1.0 0.0" |
| 1229 |
pycolor = "1.0 1.0 0.0" |
| 1230 |
nycolor = "0.0 0.0 1.0" |
| 1231 |
self.scale = 2.0 |
| 1232 |
self.radius /= 2.0 |
| 1233 |
(pxlabel, nxlabel, pylabel, nylabel, pllabel) = ( |
| 1234 |
'"Ct", "+0.5"', |
| 1235 |
'"Ct", "-0.5"', |
| 1236 |
'"Cp +0.5"', |
| 1237 |
'"Cp -0.5"', |
| 1238 |
'"I"', |
| 1239 |
) |
| 1240 |
pxcolor = "0.5 0.0 1.0" |
| 1241 |
nxcolor = "0.8 1.0 0.0" |
| 1242 |
pycolor = "1.0 0.0 0.25" |
| 1243 |
nycolor = "0.0 1.0 1.0" |
| 1244 |
self.axes += self.VRML_AXES_TEMPLATE2.format( |
| 1245 |
wh=2.0 / self.scale, |
| 1246 |
ab=100.0 / self.scale, |
| 1247 |
aboffset=50.0 / self.scale, |
| 1248 |
fontsize=10.0 / self.scale, |
| 1249 |
ap=102.0 / self.scale, |
| 1250 |
an=108.0 / self.scale, |
| 1251 |
Ln=3.0, |
| 1252 |
bp0=3.0, |
| 1253 |
bp1=103.0 / self.scale, |
| 1254 |
bn0=3.0, |
| 1255 |
bn1=107.0 / self.scale, |
| 1256 |
pxlabel=pxlabel, |
| 1257 |
nxlabel=nxlabel, |
| 1258 |
pylabel=pylabel, |
| 1259 |
nylabel=nylabel, |
| 1260 |
pllabel=pllabel, |
| 1261 |
pxcolor=pxcolor, |
| 1262 |
nxcolor=nxcolor, |
| 1263 |
pycolor=pycolor, |
| 1264 |
nycolor=nycolor, |
| 1265 |
) |
| 1266 |
|
| 1267 |
|
| 1268 |
class IPTToVRML(ColorSpaceToVRML): |
| 1269 |
"""IPT color space class. |
| 1270 |
|
| 1271 |
Args: |
| 1272 |
data (list): The color data to be processed. |
| 1273 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 1274 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 1275 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 1276 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 1277 |
""" |
| 1278 |
|
| 1279 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1280 |
"""Get the offset for the color space. |
| 1281 |
|
| 1282 |
Args: |
| 1283 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1284 |
|
| 1285 |
Returns: |
| 1286 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1287 |
""" |
| 1288 |
i, p, t = colormath.XYZ2IPT(self.X / 100.0, self.Y / 100.0, self.Z / 100.0) |
| 1289 |
return p * 100, t * 100, i * 100 - 50 |
| 1290 |
|
| 1291 |
def generate_vrml_axes(self) -> None: |
| 1292 |
"""Get the VRML axis representation for the color space.""" |
| 1293 |
self.axes = "" |
| 1294 |
pxcolor = "1.0 0.0 0.0" |
| 1295 |
nxcolor = "0.0 1.0 0.0" |
| 1296 |
pycolor = "1.0 1.0 0.0" |
| 1297 |
nycolor = "0.0 0.0 1.0" |
| 1298 |
(pxlabel, nxlabel, pylabel, nylabel, pllabel) = ( |
| 1299 |
'"P", "+1.0', |
| 1300 |
'"P", "-1.0"', |
| 1301 |
'"T +1.0"', |
| 1302 |
'"T -1.0"', |
| 1303 |
'"I"', |
| 1304 |
) |
| 1305 |
self.axes += self.VRML_AXES_TEMPLATE2.format( |
| 1306 |
wh=2.0 / self.scale, |
| 1307 |
ab=100.0 / self.scale, |
| 1308 |
aboffset=50.0 / self.scale, |
| 1309 |
fontsize=10.0 / self.scale, |
| 1310 |
ap=102.0 / self.scale, |
| 1311 |
an=108.0 / self.scale, |
| 1312 |
Ln=3.0, |
| 1313 |
bp0=3.0, |
| 1314 |
bp1=103.0 / self.scale, |
| 1315 |
bn0=3.0, |
| 1316 |
bn1=107.0 / self.scale, |
| 1317 |
pxlabel=pxlabel, |
| 1318 |
nxlabel=nxlabel, |
| 1319 |
pylabel=pylabel, |
| 1320 |
nylabel=nylabel, |
| 1321 |
pllabel=pllabel, |
| 1322 |
pxcolor=pxcolor, |
| 1323 |
nxcolor=nxcolor, |
| 1324 |
pycolor=pycolor, |
| 1325 |
nycolor=nycolor, |
| 1326 |
) |
| 1327 |
|
| 1328 |
|
| 1329 |
class LptToVRML(ColorSpaceToVRML): |
| 1330 |
"""Lpt color space class. |
| 1331 |
|
| 1332 |
Args: |
| 1333 |
data (list): The color data to be processed. |
| 1334 |
white_point (tuple[float, float, float]): The white point for the color space. |
| 1335 |
cat (str): The CAT for the color space. Defaults to "XYZ scaling". |
| 1336 |
rgb_black_offset (float, optional): Offset for RGB black. Defaults to 40. |
| 1337 |
normalize_rgb_white (bool, optional): Normalize RGB white. Defaults to False. |
| 1338 |
""" |
| 1339 |
|
| 1340 |
def get_offsets(self, entry: dict) -> tuple[float, float, float]: |
| 1341 |
"""Get the offset for the color space. |
| 1342 |
|
| 1343 |
Args: |
| 1344 |
entry (dict): The color data entry containing RGB and XYZ values. |
| 1345 |
|
| 1346 |
Returns: |
| 1347 |
tuple[float, float, float]: Offset values for X, Y, Z. |
| 1348 |
""" |
| 1349 |
l, p, t = colormath.XYZ2Lpt(self.X, self.Y, self.Z) |
| 1350 |
return p, t, l - 50 |
| 1351 |
|
| 1352 |
def generate_vrml_axes(self) -> None: |
| 1353 |
"""Get the VRML axis representation for the color space.""" |
| 1354 |
self.axes = "" |
| 1355 |
pxcolor = "1.0 0.0 0.0" |
| 1356 |
nxcolor = "0.0 1.0 0.0" |
| 1357 |
pycolor = "1.0 1.0 0.0" |
| 1358 |
nycolor = "0.0 0.0 1.0" |
| 1359 |
x = "p" |
| 1360 |
y = "t" |
| 1361 |
(pxlabel, nxlabel, pylabel, nylabel, pllabel) = ( |
| 1362 |
f'"{x}*", "+100"', |
| 1363 |
f'"{x}*", "-100"', |
| 1364 |
f'"{y}* +100"', |
| 1365 |
f'"{y}* -100"', |
| 1366 |
'"L*", "+100"', |
| 1367 |
) |
| 1368 |
self.axes += self.VRML_AXES_TEMPLATE2.format( |
| 1369 |
wh=2.0 / self.scale, |
| 1370 |
ab=100.0 / self.scale, |
| 1371 |
aboffset=50.0 / self.scale, |
| 1372 |
fontsize=10.0 / self.scale, |
| 1373 |
ap=102.0 / self.scale, |
| 1374 |
an=108.0 / self.scale, |
| 1375 |
Ln=3.0, |
| 1376 |
bp0=3.0, |
| 1377 |
bp1=103.0 / self.scale, |
| 1378 |
bn0=3.0, |
| 1379 |
bn1=107.0 / self.scale, |
| 1380 |
pxlabel=pxlabel, |
| 1381 |
nxlabel=nxlabel, |
| 1382 |
pylabel=pylabel, |
| 1383 |
nylabel=nylabel, |
| 1384 |
pllabel=pllabel, |
| 1385 |
pxcolor=pxcolor, |
| 1386 |
nxcolor=nxcolor, |
| 1387 |
pycolor=pycolor, |
| 1388 |
nycolor=nycolor, |
| 1389 |
) |
| 1390 |
|
| 1391 |
|
| 1392 |
COLORSPACE_NAME_TO_VRML_MAP = { |
| 1393 |
"DIN99": DIN99ToVRML, |
| 1394 |
"DIN99b": DIN99bToVRML, |
| 1395 |
"DIN99c": DIN99cToVRML, |
| 1396 |
"DIN99d": DIN99dToVRML, |
| 1397 |
"HSI": HSIToVRML, |
| 1398 |
"HSL": HSLToVRML, |
| 1399 |
"HSV": HSVToVRML, |
| 1400 |
"ICtCp": ICtCpToVRML, |
| 1401 |
"IPT": IPTToVRML, |
| 1402 |
"Lab": LabToVRML, |
| 1403 |
"LCH(ab)": LCHabToVRML, |
| 1404 |
"LCH(uv)": LCHuvToVRML, |
| 1405 |
"Lpt": LptToVRML, |
| 1406 |
"Lu'v'": LuvPrimeToVRML, |
| 1407 |
"Luv": LuvToVRML, |
| 1408 |
"RGB": RGBToVRML, |
| 1409 |
"xyY": xyYToVRML, |
| 1410 |
} |