Skip to content

Commit

Permalink
Merge f8c9032 into 9fe1fdf
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdcs committed Jun 20, 2024
2 parents 9fe1fdf + f8c9032 commit d254342
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 72 deletions.
86 changes: 23 additions & 63 deletions colour/algebra/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,42 +1127,14 @@ def y(self, value: ArrayLike):

self._y = value

yp1 = np.ravel(
(
np.dot(
self.SPRAGUE_C_COEFFICIENTS[0],
np.reshape(np.array(value[0:6]), (6, 1)),
)
)
/ 209
)[0]
yp2 = np.ravel(
(
np.dot(
self.SPRAGUE_C_COEFFICIENTS[1],
np.reshape(np.array(value[0:6]), (6, 1)),
)
yp1, yp2, yp3, yp4 = (
np.sum(
self.SPRAGUE_C_COEFFICIENTS
* np.asarray((value[0:6], value[0:6], value[-6:], value[-6:])),
axis=1,
)
/ 209
)[0]
yp3 = np.ravel(
(
np.dot(
self.SPRAGUE_C_COEFFICIENTS[2],
np.reshape(np.array(value[-6:]), (6, 1)),
)
)
/ 209
)[0]
yp4 = np.ravel(
(
np.dot(
self.SPRAGUE_C_COEFFICIENTS[3],
np.reshape(np.array(value[-6:]), (6, 1)),
)
)
/ 209
)[0]
)

self._yp = np.concatenate(
[
Expand Down Expand Up @@ -1217,36 +1189,24 @@ def _evaluate(self, x: NDArrayFloat) -> NDArrayFloat:

r = self._yp

a0p = r[i]
a1p = (2 * r[i - 2] - 16 * r[i - 1] + 16 * r[i + 1] - 2 * r[i + 2]) / 24
a2p = (-r[i - 2] + 16 * r[i - 1] - 30 * r[i] + 16 * r[i + 1] - r[i + 2]) / 24
a3p = (
-9 * r[i - 2]
+ 39 * r[i - 1]
- 70 * r[i]
+ 66 * r[i + 1]
- 33 * r[i + 2]
+ 7 * r[i + 3]
) / 24
a4p = (
13 * r[i - 2]
- 64 * r[i - 1]
+ 126 * r[i]
- 124 * r[i + 1]
+ 61 * r[i + 2]
- 12 * r[i + 3]
) / 24
a5p = (
-5 * r[i - 2]
+ 25 * r[i - 1]
- 50 * r[i]
+ 50 * r[i + 1]
- 25 * r[i + 2]
+ 5 * r[i + 3]
) / 24

y = a0p + a1p * X + a2p * X**2 + a3p * X**3 + a4p * X**4 + a5p * X**5
r_s = np.asarray((r[i - 2], r[i - 1], r[i], r[i + 1], r[i + 2], r[i + 3]))
w_s = np.asarray(
(
(2, -16, 0, 16, -2, 0),
(-1, 16, -30, 16, -1, 0),
(-9, 39, -70, 66, -33, 7),
(13, -64, 126, -124, 61, -12),
(-5, 25, -50, 50, -25, 5),
)
)
a = np.dot(w_s, r_s) / 24

# Fancy vector code here... use underlying numpy structures to accelerate
# parts of the linear algebra.

y = r[i] + (a.reshape(5, -1) * X ** np.arange(1, 6).reshape(-1, 1)).sum(axis=0)
if y.size == 1:
return y[0]
return y

def _validate_dimensions(self):
Expand Down
8 changes: 4 additions & 4 deletions colour/colorimetry/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ def sd_gaussian_fwhm(
SpectralShape(360.0, 780.0, 1.0)
>>> sd[555] # doctest: +SKIP
1.0
>>> sd[530]
0.0625
>>> sd[530] # doctest: +ELLIPSIS
0.062...
"""

settings = {"name": f"{peak_wavelength}nm - {fwhm} FWHM - Gaussian"}
Expand Down Expand Up @@ -543,8 +543,8 @@ def sd_gaussian(
SpectralShape(360.0, 780.0, 1.0)
>>> sd[555] # doctest: +SKIP
1.0
>>> sd[530]
0.0625
>>> sd[530] # doctest: +ELLIPSIS
0.062...
"""

method = validate_method(method, tuple(SD_GAUSSIAN_METHODS))
Expand Down
2 changes: 1 addition & 1 deletion colour/colorimetry/lefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def mesopic_weighting_function(
Examples
--------
>>> mesopic_weighting_function(500, 0.2) # doctest: +ELLIPSIS
0.7052200...
0.7052...
"""

photopic_lef = optional(
Expand Down
78 changes: 75 additions & 3 deletions colour/models/rgb/transfer_functions/gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,87 @@

__all__ = [
"gamma_function",
"GammaFunction",
]

NegativeNumberHandlingType = (
Literal["Clamp", "Indeterminate", "Mirror", "Preserve"] | str
)


class GammaFunction:
"""Provides an object oriented interface to contain optional parameters for
an underlying :func:gamma_function call. Useful for providing both a simpler
and constructed api for gamma_function as well as allowing for control flow.
"""

def __init__(
self,
exponent: float = 1,
negative_number_handling: NegativeNumberHandlingType = "Indeterminate",
):
"""
Construct an object oriented interface to contain optional parameters for
an underlying :func:gamma_function call. Useful for providing both a simpler
and constructed api for gamma_function as well as allowing for control flow.
Parameters
----------
exponent : float, optional
The exponent value in a^b, by default 1
negative_number_handling : NegativeNumberHandlingType, optional
Defines the behavior for negative number handling, by default
"Indeterminate"
See Also
--------
:func:gamma_function
"""
self._exponent = exponent
self._negative_number_handling = negative_number_handling

@property
def exponent(self) -> float:
"""The exponent, b, in the function a^b
Returns
-------
float
"""
return self._exponent

@property
def negative_number_handling(self) -> NegativeNumberHandlingType:
"""How to treat negative numbers. See also :func:gamma_function
Returns
-------
NegativeNumberHandlingType
See also :func:gamma_function
"""
return self._negative_number_handling

def __call__(self, a: ArrayLike):
"""Calculate a typical encoding / decoding function on `a`. Representative
of the function a ^ b where b is determined by the instance value of
`exponent` and negative handling behavior is defined by the instance
value `negative_number_handling`. See also :func:gamma_function
Parameters
----------
a : ArrayLike
"""
return gamma_function(
a,
exponent=self.exponent,
negative_number_handling=self.negative_number_handling,
)


def gamma_function(
a: ArrayLike,
exponent: ArrayLike = 1,
negative_number_handling: (
Literal["Clamp", "Indeterminate", "Mirror", "Preserve"] | str
) = "Indeterminate",
negative_number_handling: NegativeNumberHandlingType = "Indeterminate",
) -> NDArrayFloat:
"""
Define a typical gamma encoding / decoding function.
Expand Down
Loading

0 comments on commit d254342

Please sign in to comment.