Skip to content

Commit

Permalink
feat: precompute LIN_RGB_MATRIX_INV
Browse files Browse the repository at this point in the history
closes: #1
  • Loading branch information
senyai committed Oct 25, 2024
1 parent 49aef0b commit d74206c
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions vsl_ial/cs/linrgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

LIN_RGB_MATRIX = np.asarray(
(
(3.2404542, -1.5371385, -0.4985314),
(-0.9692660, 1.8760108, 0.0415560),
(0.0556434, -0.2040259, 1.0572252),
(3.2404542, -0.9692660, 0.0556434),
(-1.5371385, 1.8760108, -0.2040259),
(-0.4985314, 0.0415560, 1.0572252),
),
dtype=np.float64,
).T
)

LIN_RGB_MATRIX_INV = np.asarray(
(
(0.4124564, 0.2126729, 0.0193339),
(0.3575761, 0.7151522, 0.1191920),
(0.1804375, 0.0721750, 0.9503041),
),
dtype=np.float64,
)


class linRGB(CS):
Expand All @@ -25,8 +34,10 @@ def _from_XYZ(self, src: CS, color: FArray) -> FArray:

def _from_sRGB(self, src: CS, color: FArray) -> FArray:
if color.min() < 0 or color.max() > 1:
# this warning is usually harmless
warnings.warn(
f"sRGB range should be in [0, 1] not [{color.min()}, {color.max()}]"
f"sRGB range should be in [0, 1] not "
f"[{color.min()}, {color.max()}]"
)

thres = 12.92 * 0.0031308
Expand All @@ -50,4 +61,4 @@ def _from_sRGB(self, src: CS, color: FArray) -> FArray:
return color

def _to_XYZ(self, src: CS, color: FArray) -> FArray:
return color @ np.linalg.inv(LIN_RGB_MATRIX)
return color @ LIN_RGB_MATRIX_INV

0 comments on commit d74206c

Please sign in to comment.