1616#
1717# ##### END GPL LICENSE BLOCK #####
1818
19- # Copyright (C) 2013-2021 : SCS Software
19+ # Copyright (C) 2013-2022 : SCS Software
2020
2121import struct
2222import math
2323import re
24+ from numpy import vectorize
2425from mathutils import Matrix , Quaternion , Vector , Color
2526from io_scs_tools .consts import Colors as _COL_consts
2627from io_scs_tools .utils .printout import lprint
3233_BYTE_STRUCT = struct .Struct (">I" )
3334
3435
36+ def __linear_to_srgb (x ):
37+ """Converts value from linear to srgb
38+ NOTE: taken from game and blender code
39+
40+ :param x: value to convert
41+ :type x: float
42+ """
43+
44+ if x <= 0.0031308 :
45+ return 12.92 * x
46+ else :
47+ return (x ** (1.0 / 2.4 )) * 1.055 - 0.055
48+
49+
50+ def __srgb_to_linear (x ):
51+ """Converts value from linear to srgb
52+ NOTE: taken from game and blender code
53+
54+ :param x: value to convert
55+ :type x: float
56+ """
57+
58+ if x <= 0.04045 :
59+ return x / 12.92
60+ else :
61+ return ((x + 0.055 ) / 1.055 ) ** 2.4
62+
63+
64+ np_linear_to_srgb = vectorize (__linear_to_srgb )
65+ """Vectorizes linear to srgb function to be used with numpy arrays."""
66+
67+
68+ np_srgb_to_linear = vectorize (__srgb_to_linear )
69+ """Vectorizes srgb to linear function to be used with numpy arrays."""
70+
71+
3572def linear_to_srgb (value ):
3673 """Converts linear color to srgb colorspace. Function can convert single float or list of floats.
37- NOTE: taken from game code
3874
3975 :param value: list of floats or float
4076 :type value: float | collections.Iterable[float]
@@ -44,26 +80,13 @@ def linear_to_srgb(value):
4480
4581 is_float = isinstance (value , float )
4682 if is_float :
47- vals = [value ]
48- else :
49- vals = list (value )
50-
51- for i , v in enumerate (vals ):
52- if v <= 0.0031308 :
53- vals [i ] = 12.92 * v
54- else :
55- a = 0.055
56- vals [i ] = (v ** (1.0 / 2.4 ) * (1.0 + a )) - a
57-
58- if is_float :
59- return vals [0 ]
83+ return __linear_to_srgb (value )
6084 else :
61- return vals
85+ return [ __linear_to_srgb ( v ) for v in list ( value )]
6286
6387
6488def srgb_to_linear (value ):
6589 """Converts srgb color to linear colorspace. Function can convert single float or list of floats.
66- NOTE: taken from game code
6790
6891 :param value: list of floats or float
6992 :type value: float | collections.Iterable[float]
@@ -73,22 +96,9 @@ def srgb_to_linear(value):
7396
7497 is_float = isinstance (value , float )
7598 if is_float :
76- vals = [value ]
77- else :
78- vals = list (value )
79-
80- for i , v in enumerate (vals ):
81-
82- if v <= 0.04045 :
83- vals [i ] = v / 12.92
84- else :
85- a = 0.055
86- vals [i ] = ((v + a ) / (1.0 + a )) ** 2.4
87-
88- if is_float :
89- return vals [0 ]
99+ return __srgb_to_linear (value )
90100 else :
91- return vals
101+ return [ __srgb_to_linear ( v ) for v in list ( value )]
92102
93103
94104def pre_gamma_corrected_col (color ):
0 commit comments