File size: 661 Bytes
41ff959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from typing import Literal

import torch
from torch import Tensor


ColorSpace = Literal["sRGB", "linearRGB"]

_SRGB_THRESHOLD = 0.04045
_LINEAR_THRESHOLD = 0.0031308


def encode_color_space(color_space: ColorSpace) -> int:
    return 0 if color_space == "sRGB" else 1


def sRGB2linearRGB(srgb: Tensor) -> Tensor:
    return torch.where(
        srgb <= _SRGB_THRESHOLD,
        srgb / 12.92,
        ((srgb + 0.055) / 1.055).pow(2.4),
    )


def linearRGB2sRGB(linear_rgb: Tensor) -> Tensor:
    return torch.where(
        linear_rgb <= _LINEAR_THRESHOLD,
        linear_rgb * 12.92,
        1.055 * linear_rgb.clamp(min=0.0).pow(1.0 / 2.4) - 0.055,
    )