diff options
Diffstat (limited to 'io_scene_gltf2/io/com/gltf2_io_color_management.py')
-rw-r--r-- | io_scene_gltf2/io/com/gltf2_io_color_management.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/io_scene_gltf2/io/com/gltf2_io_color_management.py b/io_scene_gltf2/io/com/gltf2_io_color_management.py index 56b3a246..58dc3a27 100644 --- a/io_scene_gltf2/io/com/gltf2_io_color_management.py +++ b/io_scene_gltf2/io/com/gltf2_io_color_management.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import numpy as np def color_srgb_to_scene_linear(c): """ @@ -29,8 +30,27 @@ def color_linear_to_srgb(c): Convert from linear to sRGB color space. Source: Cycles addon implementation, node_color.h. + c may be a single color value or an array. + + If c's last dimension is 4, it's assumed to be RGBA and the + alpha channel is not converted. """ - if c < 0.0031308: - return 0.0 if c < 0.0 else c * 12.92 + if type(c) in (list, np.ndarray): + colors = np.array(c, np.float32) if type(c) == list else c + if colors.ndim > 1 and colors.shape[-1] == 4: + colors_noa = colors[..., 0:3] # only process RGB for speed + else: + colors_noa = colors + not_small = colors_noa >= 0.0031308 + small_result = np.where(colors_noa < 0.0, 0.0, colors_noa * 12.92) + large_result = 1.055 * np.power(colors_noa, 1.0 / 2.4, where=not_small) - 0.055 + result = np.where(not_small, large_result, small_result) + if colors.ndim > 1 and colors.shape[-1] == 4: + # copy alpha from original + result = np.concatenate((result, colors[..., 3, np.newaxis]), axis=-1) + return result else: - return 1.055 * pow(c, 1.0 / 2.4) - 0.055 + if c < 0.0031308: + return 0.0 if c < 0.0 else c * 12.92 + else: + return 1.055 * pow(c, 1.0 / 2.4) - 0.055 |